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

.all(_query = {}) ⇒ Object

Common idioms.

Raises:

  • (StandardError)


53
54
55
# File 'lib/facebook_ads/base.rb', line 53

def all(_query = {})
  raise StandardError, 'Subclass must implement `all`.'
end

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



37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/facebook_ads/base.rb', line 37

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



57
58
59
# File 'lib/facebook_ads/base.rb', line 57

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

.find_by(conditions) ⇒ Object



61
62
63
64
65
66
67
# File 'lib/facebook_ads/base.rb', line 61

def find_by(conditions)
  all.detect do |object|
    conditions.all? do |key, value|
      object.send(key) == value
    end
  end
end

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

HTTP verbs.



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

def get(path, query: {}, objectify:)
  query = pack(query, objectify: objectify)
  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



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/facebook_ads/base.rb', line 69

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

  if data.length == query[: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



23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/facebook_ads/base.rb', line 23

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



204
205
206
207
# File 'lib/facebook_ads/base.rb', line 204

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

#saveObject



190
191
192
193
194
195
196
# File 'lib/facebook_ads/base.rb', line 190

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



198
199
200
201
202
# File 'lib/facebook_ads/base.rb', line 198

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