Class: CardDB::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/carddb/batch.rb

Overview

Batch query builder for combining multiple queries into a single request.

Examples:

results = CardDB.batch do |b|
  b.games.fetch("uuid-1")
  b.games.fetch("uuid-2")
  b.publishers.fetch(slug: "pokemon-company")
end

results[0]  # => Game
results[1]  # => Game
results[2]  # => Publisher

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Batch

Returns a new instance of Batch.

Parameters:

  • client (Client)

    The client instance



21
22
23
24
25
# File 'lib/carddb/batch.rb', line 21

def initialize(client)
  @client = client
  @operations = []
  @counter = 0
end

Instance Attribute Details

#operationsArray<Hash> (readonly)

Returns The queued operations.

Returns:

  • (Array<Hash>)

    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

Parameters:

  • resource (Symbol)

    The resource type

  • method (Symbol)

    The method to call

  • args (Array) (defaults to: [])

    Method arguments

  • kwargs (Hash) (defaults to: {})

    Method keyword arguments

Returns:

  • (Integer)

    The index of this operation in the results



62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/carddb/batch.rb', line 62

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

#datasetsBatchProxy

Access the Datasets resource for batching

Returns:



44
45
46
# File 'lib/carddb/batch.rb', line 44

def datasets
  BatchProxy.new(self, :datasets)
end

#executeArray

Execute all batched operations

Returns:

  • (Array)

    Results in the same order as operations were added



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# File 'lib/carddb/batch.rb', line 80

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

#gamesBatchProxy

Access the Games resource for batching

Returns:



37
38
39
# File 'lib/carddb/batch.rb', line 37

def games
  BatchProxy.new(self, :games)
end

#publishersBatchProxy

Access the Publishers resource for batching

Returns:



30
31
32
# File 'lib/carddb/batch.rb', line 30

def publishers
  BatchProxy.new(self, :publishers)
end

#recordsBatchProxy

Access the Records resource for batching

Returns:



51
52
53
# File 'lib/carddb/batch.rb', line 51

def records
  BatchProxy.new(self, :records)
end