Class: TheTradeDeskAds::Base

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

Overview

The base class for all ads objects.

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.auth(login: TheTradeDeskAds.login, password: TheTradeDeskAds.password, token_expiration_in_minutes: TheTradeDeskAds.token_expiration_in_minutes) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/the_trade_desk_ads/base.rb', line 6

def auth(login: TheTradeDeskAds., password: TheTradeDeskAds.password, token_expiration_in_minutes: TheTradeDeskAds.token_expiration_in_minutes)
  query = { "Login": ,
            "Password": password
          }
  if token_expiration_in_minutes && token_expiration_in_minutes.integer?
     query["TokenExpirationInMinutes"] = token_expiration_in_minutes
  end
  uri = "#{TheTradeDeskAds.base_uri}authentication"
  TheTradeDeskAds.logger.debug "POST #{uri}"
  response = begin
    RestClient.post(uri, query.to_json, content_type: :json)
  rescue RestClient::Exception => e
    exception(:post, uri, e)
  end
  unless e
    TheTradeDeskAds.access_token = JSON.parse(response)["Token"]
  end
end

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



55
56
57
58
59
60
61
62
63
64
65
# File 'lib/the_trade_desk_ads/base.rb', line 55

def delete(path, query: {})
  query = pack(query, objectify: false)
  uri = "#{TheTradeDeskAds.base_uri}#{path}?" + build_nested_query(query)
  TheTradeDeskAds.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



26
27
28
29
# File 'lib/the_trade_desk_ads/base.rb', line 26

def find(id)
  uri = "#{self.to_s.gsub("TheTradeDeskAds::Ad","").downcase}/#{id}"
  get(uri, objectify: true)
end

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



31
32
33
34
35
36
37
38
39
40
41
# File 'lib/the_trade_desk_ads/base.rb', line 31

def get(path, query: {}, objectify:)
  # query = pack(query, objectify: objectify) # Adds access token, fields, etc.
  uri = "#{TheTradeDeskAds.base_uri}#{path}?" + build_nested_query(query)
  TheTradeDeskAds.logger.debug "GET #{uri}"
  response = begin
    RestClient.get(uri, {"TTD-Auth": TheTradeDeskAds.access_token})
  rescue RestClient::Exception => e
    exception(:get, path, e)
  end
  unpack(response, objectify: objectify)
end

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



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

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?
      TheTradeDeskAds.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: {}, objectify:) ⇒ Object



43
44
45
46
47
48
49
50
51
52
53
# File 'lib/the_trade_desk_ads/base.rb', line 43

def post(path, query: {},objectify:)
  # query = pack(query, objectify: objectify)
  uri = "#{TheTradeDeskAds.base_uri}#{path}"
  TheTradeDeskAds.logger.debug "POST #{uri} #{query}"
  response = begin
    RestClient.post(uri, query.to_json,{"TTD-Auth": TheTradeDeskAds.access_token, content_type: :json})
  rescue RestClient::Exception => e
    exception(:post, path, e)
  end
  unpack(response, objectify: objectify)
end

Instance Method Details

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



216
217
218
219
# File 'lib/the_trade_desk_ads/base.rb', line 216

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

#saveObject



202
203
204
205
206
207
208
# File 'lib/the_trade_desk_ads/base.rb', line 202

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



210
211
212
213
214
# File 'lib/the_trade_desk_ads/base.rb', line 210

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