Class: Adparlor::Facebook::GraphApi::GraphObject

Inherits:
Object
  • Object
show all
Includes:
Api, Fields::FieldDecorator
Defined in:
lib/adparlor/facebook/graph_api/graph_object.rb

Constant Summary

Constants included from Fields::FieldDecorator

Fields::FieldDecorator::GLOBAL_FIELDS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Api

#base_uri, #conn, #conn_multi, #delete, #get, #post, #proxy_api_key

Methods included from Fields::FieldDecorator

included

Constructor Details

#initialize(attributes = {}) ⇒ GraphObject

Returns a new instance of GraphObject.



79
80
81
82
83
84
85
86
87
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 79

def initialize(attributes = {})
  self.class.validate_initialize_fields(*attributes.keys) if self.class.respond_to?(:validate_initialize_fields)
  if self.class != AdImage && attributes[:source]
    @files = {}
    attach_file(attributes)
  end
  attributes.each { |key, value| instance_variable_set("@#{key}".to_sym, value) }
  self
end

Instance Attribute Details

#access_tokenObject

Returns the value of attribute access_token.



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

def access_token
  @access_token
end

Class Method Details

.create(attributes = {}, options = {}) ⇒ Object



14
15
16
17
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 14

def create(attributes = {}, options = {})
  obj = new(attributes)
  obj.post(obj.path, options)
end

.data_pass_throughObject



74
75
76
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 74

def data_pass_through
  false
end

.destroy(attributes = {}, options = {}) ⇒ Object



24
25
26
27
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 24

def destroy(attributes = {}, options = {})
  obj = new(attributes)
  obj.delete(obj.update_path, options)
end

.get(path, options = {}) ⇒ Object

TODO: make each of the gets a read shift get into method on Adparlor::Facebook::Api

Raises:



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 30

def get(path, options = {})
  max_results = options.delete(:max_results)

  unless max_results.nil?
    options[:limit] ||= 100
    options[:limit] = [max_results, options[:limit]].min
  end

  response = new.get(path.to_s, options).body
  raise FbError.new(response['error'], 500) if response.key?('error')
  response['data'] = response.values if options[:ids]

  if response.key?('data')
    respond_for_data(response, options[:limit], max_results)
  else
    obj = new(response)
    obj.access_token ||= options[:access_token]
    obj
  end
end

.parse_data_for_collection(response) ⇒ Object



70
71
72
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 70

def parse_data_for_collection(response)
  response['data']
end

.read(attributes = {}, options = {}) ⇒ Object



10
11
12
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 10

def read(attributes = {}, options = {})
  self.get(new(attributes).path, options)
end

.respond_for_data(response, limit, max_results) ⇒ Object



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 51

def respond_for_data(response, limit, max_results)
  if data_pass_through
    new(response['data'])
  else
    return response['summary'] if response['summary']
    data = parse_data_for_collection(response)
    while response['paging'] && response['paging']['next']
      if max_results.nil? || data.size < max_results
        options = max_results.nil? ? {} : { limit: [(max_results - data.size), limit].min } # remaining limit
        response = new.get(response['paging']['next'].delete('\\'), options).body
        data += parse_data_for_collection(response)
      else
        break
      end
    end
    data.map { |object| new(object) } || []
  end
end

.update(attributes = {}, options = {}) ⇒ Object



19
20
21
22
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 19

def update(attributes = {}, options = {})
  obj = new(attributes)
  obj.post(obj.update_path, options, 'UPDATE')
end

Instance Method Details

#batchObject



89
90
91
92
93
94
95
96
97
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 89

def batch
  batch_api = BatchObject.new(self)
  if block_given?
    yield batch_api
    batch_api.execute
  else
    batch_api
  end
end

#create(path, options = {}) ⇒ Object



99
100
101
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 99

def create(path, options = {})
  post(path, options)
end

#destroy(path, options = {}) ⇒ Object



103
104
105
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 103

def destroy(path, options = {})
  delete(path, options)
end

#to_hashObject



111
112
113
114
115
116
117
118
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 111

def to_hash
  {}.tap do |h|
    self.instance_variables.each do |v|
      attribute = v.to_s.gsub('@','')
      h[attribute] = self.send(attribute)
    end
  end
end

#to_jsonObject



120
121
122
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 120

def to_json
  self.to_hash.to_json
end

#update(path, options = {}) ⇒ Object



107
108
109
# File 'lib/adparlor/facebook/graph_api/graph_object.rb', line 107

def update(path, options = {})
  post(path, options, 'UPDATE')
end