Class: Bing::Content::Api::Batch

Inherits:
Object
  • Object
show all
Defined in:
lib/bing/content/api/batch.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBatch

Returns a new instance of Batch.



7
8
9
10
11
# File 'lib/bing/content/api/batch.rb', line 7

def initialize
  @operations = []

  @ids = (1..1000).to_a
end

Instance Attribute Details

#operationsObject (readonly)

Returns the value of attribute operations.



5
6
7
# File 'lib/bing/content/api/batch.rb', line 5

def operations
  @operations
end

Instance Method Details

#add_deletions(products) ⇒ Object



19
20
21
22
23
# File 'lib/bing/content/api/batch.rb', line 19

def add_deletions(products)
  products.each do |prod|
    @operations << BatchOperation.new(@ids.pop, prod, :delete)
  end
end

#add_insertions(products) ⇒ Object



13
14
15
16
17
# File 'lib/bing/content/api/batch.rb', line 13

def add_insertions(products)
  products.each do |prod|
    @operations << BatchOperation.new(@ids.pop, prod, :insert)
  end
end

#all_productsObject



25
26
27
# File 'lib/bing/content/api/batch.rb', line 25

def all_products
  @operations.map(&:product)
end

#product_by_batch_id(id) ⇒ Object



29
30
31
# File 'lib/bing/content/api/batch.rb', line 29

def product_by_batch_id(id)
  @operations.select { |op| op.batch_id == id }.first.product
end

#to_bodyObject



33
34
35
36
# File 'lib/bing/content/api/batch.rb', line 33

def to_body
  json_operations = @operations.map(&:bing_operation)
  { entries: json_operations }.to_json
end