Class: Adparlor::Facebook::GraphApi::BatchObject

Inherits:
GraphObject
  • Object
show all
Defined in:
lib/adparlor/facebook/graph_api/batch_object.rb

Constant Summary

Constants included from Fields::FieldDecorator

Fields::FieldDecorator::GLOBAL_FIELDS

Instance Attribute Summary collapse

Attributes inherited from GraphObject

#access_token

Instance Method Summary collapse

Methods inherited from GraphObject

#batch, create, #create, data_pass_through, destroy, #destroy, get, parse_data_for_collection, read, respond_for_data, #to_hash, #to_json, update, #update

Methods included from Api

#base_uri, #conn, #conn_multi, #proxy_api_key

Methods included from Fields::FieldDecorator

included

Constructor Details

#initialize(graph_object) ⇒ BatchObject

Returns a new instance of BatchObject.



9
10
11
12
13
14
# File 'lib/adparlor/facebook/graph_api/batch_object.rb', line 9

def initialize(graph_object)
  @access_token = graph_object.access_token
  @batch_calls = { batch: [] }
  @batch_types = []
  @files = {}
end

Instance Attribute Details

#batch_callsObject

Returns the value of attribute batch_calls.



7
8
9
# File 'lib/adparlor/facebook/graph_api/batch_object.rb', line 7

def batch_calls
  @batch_calls
end

Instance Method Details

#delete(id, klass = nil, options = {}) ⇒ Object



16
17
18
19
20
21
22
# File 'lib/adparlor/facebook/graph_api/batch_object.rb', line 16

def delete(id, klass = nil, options = {})
  top_level_access_token(options)
  relative_url = relative_url(id, klass, options)
  @batch_types << klass
  @batch_calls[:batch] << { method: 'DELETE', relative_url: relative_url }
  nil
end

#executeObject



24
25
26
27
28
29
30
31
32
# File 'lib/adparlor/facebook/graph_api/batch_object.rb', line 24

def execute
  content_type_header = @files.any? ? 'multipart/form-data' : 'application/json'
  response = conn.post full_url('/', {}) do |request|
    request.headers['Content-Type'] = content_type_header
    request.params = { access_token: access_token }
    request.body = @files.any? ? @files.merge(batch: @batch_calls[:batch].to_json) : @batch_calls.to_json
  end
  initialize_response(response)
end

#get(id, klass = nil, options = {}) ⇒ Object



34
35
36
37
38
39
40
# File 'lib/adparlor/facebook/graph_api/batch_object.rb', line 34

def get(id, klass = nil, options = {})
  top_level_access_token(options)
  relative_url = relative_url(id, klass, options)
  @batch_types << klass
  @batch_calls[:batch] << { method: 'GET', relative_url: relative_url }
  nil
end

#post(id, klass, attributes = {}, options = {}) ⇒ Object



42
43
44
45
46
47
48
49
50
51
# File 'lib/adparlor/facebook/graph_api/batch_object.rb', line 42

def post(id, klass, attributes = {}, options = {})
  top_level_access_token(options)
  attached_file = attach_file(attributes)
  name = options.delete(:name)
  relative_url = relative_url(id, klass, options)
  body = query_string attributes
  @batch_types << klass
  @batch_calls[:batch] << { method: 'POST', relative_url: relative_url, attached_files: attached_file, name: name, body: body }.delete_if { |_, v| v.nil? }
  nil
end