Class: CardDB::Batch
- Inherits:
-
Object
- Object
- CardDB::Batch
- Defined in:
- lib/carddb/batch.rb
Overview
Batch query builder for combining multiple queries into a single request.
Instance Attribute Summary collapse
-
#operations ⇒ Array<Hash>
readonly
The queued operations.
Instance Method Summary collapse
-
#add_operation(resource:, method:, args: [], kwargs: {}) ⇒ Integer
Add an operation to the batch.
-
#datasets ⇒ BatchProxy
Access the Datasets resource for batching.
-
#decks ⇒ BatchProxy
Access the Decks resource for batching.
-
#execute ⇒ Array
Execute all batched operations.
-
#games ⇒ BatchProxy
Access the Games resource for batching.
-
#initialize(client) ⇒ Batch
constructor
A new instance of Batch.
-
#publishers ⇒ BatchProxy
Access the Publishers resource for batching.
-
#records ⇒ BatchProxy
Access the Records resource for batching.
Constructor Details
#initialize(client) ⇒ Batch
Returns a new instance of Batch.
21 22 23 24 25 |
# File 'lib/carddb/batch.rb', line 21 def initialize(client) @client = client @operations = [] @counter = 0 end |
Instance Attribute Details
#operations ⇒ Array<Hash> (readonly)
Returns The queued operations.
18 19 20 |
# File 'lib/carddb/batch.rb', line 18 def operations @operations end |
Instance Method Details
#add_operation(resource:, method:, args: [], kwargs: {}) ⇒ Integer
Add an operation to the batch
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/carddb/batch.rb', line 69 def add_operation(resource:, method:, args: [], kwargs: {}) @counter += 1 alias_name = "op#{@counter}" @operations << { alias: alias_name, resource: resource, method: method, args: args, kwargs: kwargs } @counter - 1 end |
#datasets ⇒ BatchProxy
Access the Datasets resource for batching
44 45 46 |
# File 'lib/carddb/batch.rb', line 44 def datasets BatchProxy.new(self, :datasets) end |
#decks ⇒ BatchProxy
Access the Decks resource for batching
58 59 60 |
# File 'lib/carddb/batch.rb', line 58 def decks BatchProxy.new(self, :decks) end |
#execute ⇒ Array
Execute all batched operations
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 |
# File 'lib/carddb/batch.rb', line 87 def execute return [] if @operations.empty? # Build combined GraphQL query query_parts = [] variables = {} @operations.each_with_index do |op, index| query_part, op_vars = build_operation_query(op, index) query_parts << query_part variables.merge!(op_vars) end # Combine into single query var_definitions = build_variable_definitions(variables) combined_query = <<~GRAPHQL query BatchQuery#{var_definitions} { #{query_parts.join("\n ")} } GRAPHQL # Execute the combined query data = @client.connection.execute(combined_query, variables) # Parse results @operations.map do |op| result = data[op[:alias]] wrap_result(result, op[:resource], op[:method]) end end |
#games ⇒ BatchProxy
Access the Games resource for batching
37 38 39 |
# File 'lib/carddb/batch.rb', line 37 def games BatchProxy.new(self, :games) end |
#publishers ⇒ BatchProxy
Access the Publishers resource for batching
30 31 32 |
# File 'lib/carddb/batch.rb', line 30 def publishers BatchProxy.new(self, :publishers) end |
#records ⇒ BatchProxy
Access the Records resource for batching
51 52 53 |
# File 'lib/carddb/batch.rb', line 51 def records BatchProxy.new(self, :records) end |