Class: FacebookAds::Base

Inherits:
Hashie::Mash
  • Object
show all
Defined in:
lib/facebook_ads/base.rb

Overview

The base class for all ads objects.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.delete(path, query: {}) ⇒ Object



33
34
35
36
37
38
39
40
41
42
43
# File 'lib/facebook_ads/base.rb', line 33

def delete(path, query: {})
  query = pack(query, objectify: false)
  uri = "#{FacebookAds.base_uri}#{path}?" + build_nested_query(query)
  FacebookAds.logger.debug "DELETE #{uri}"
  response = begin
    RestClient.delete(uri)
  rescue RestClient::Exception => e
    exception(:delete, path, e)
  end
  unpack(response, objectify: false)
end

.find(id) ⇒ Object



5
6
7
# File 'lib/facebook_ads/base.rb', line 5

def find(id)
  get("/#{id}", objectify: true)
end

.get(path, query: {}, objectify:) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/facebook_ads/base.rb', line 9

def get(path, query: {}, objectify:)
  query = pack(query, objectify: objectify) # Adds access token, fields, etc.
  uri = "#{FacebookAds.base_uri}#{path}?" + build_nested_query(query)
  FacebookAds.logger.debug "GET #{uri}"
  response = begin
    RestClient.get(uri, accept: :json, accept_encoding: :identity)
  rescue RestClient::Exception => e
    exception(:get, path, e)
  end
  unpack(response, objectify: objectify)
end

.paginate(path, query: {}) ⇒ Object



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/facebook_ads/base.rb', line 45

def paginate(path, query: {})
  query[:limit] ||= 100
  limit = query[:limit]
  response = get(path, query: query.merge(fields: self::FIELDS.join(',')), objectify: false)
  data = response['data'].nil? ? [] : response['data']

  if data.length == limit
    while !(paging = response['paging']).nil? && !(url = paging['next']).nil?
      FacebookAds.logger.debug "GET #{url}"
      response = begin
        RestClient.get(url)
      rescue RestClient::Exception => e
        exception(:get, url, e)
      end
      response = unpack(response, objectify: false)
      data += response['data'] unless response['data'].nil?
    end
  end

  if data.nil?
    []
  else
    data.map { |hash| instantiate(hash) }
  end
end

.post(path, query: {}) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
# File 'lib/facebook_ads/base.rb', line 21

def post(path, query: {})
  query = pack(query, objectify: false)
  uri = "#{FacebookAds.base_uri}#{path}"
  FacebookAds.logger.debug "POST #{uri} #{query}"
  response = begin
    RestClient.post(uri, query)
  rescue RestClient::Exception => e
    exception(:post, path, e)
  end
  unpack(response, objectify: false)
end

Instance Method Details

#destroy(path: nil, query: {}) ⇒ Object



178
179
180
181
# File 'lib/facebook_ads/base.rb', line 178

def destroy(path: nil, query: {})
  response = self.class.delete(path || "/#{id}", query: query)
  response['success']
end

#saveObject



164
165
166
167
168
169
170
# File 'lib/facebook_ads/base.rb', line 164

def save
  return nil if changes.nil? || changes.length.zero?
  data = {}
  changes.keys.each { |key| data[key] = self[key] }
  return nil unless update(data)
  self.class.find(id)
end

#update(data) ⇒ Object



172
173
174
175
176
# File 'lib/facebook_ads/base.rb', line 172

def update(data)
  return false if data.nil?
  response = self.class.post("/#{id}", query: data)
  response['success']
end