Class: Adobe::Campaign::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/adobe/campaign/base.rb

Direct Known Subclasses

Profile, Service

Class Method Summary collapse

Class Method Details

.access_tokenObject



47
48
49
# File 'lib/adobe/campaign/base.rb', line 47

def access_token
  Adobe::Campaign::Base.load_access_token
end

.allObject



11
12
13
# File 'lib/adobe/campaign/base.rb', line 11

def all
  get_request(endpoint)
end

.auth_headersObject



40
41
42
43
44
45
# File 'lib/adobe/campaign/base.rb', line 40

def auth_headers
  {
    'Authorization' => "Bearer #{access_token}",
    'X-Api-Key' => Adobe::Campaign.configuration.api_key
  }
end

.endpointObject



7
8
9
# File 'lib/adobe/campaign/base.rb', line 7

def endpoint
  raise 'You can not call requests directly on Adobe::Campaign::Base'
end

.find(search_text) ⇒ Object



15
16
17
# File 'lib/adobe/campaign/base.rb', line 15

def find(search_text)
  get_request("#{endpoint}/byText?text=#{search_text}")
end

.get_request(url) ⇒ Object



23
24
25
26
27
28
29
30
31
# File 'lib/adobe/campaign/base.rb', line 23

def get_request(url)
  full_url = url_prefix(url)
  resp = RestClient::Request.execute(
    method: :get,
    url: full_url,
    headers: Adobe::Campaign::Base.auth_headers
  )
  JSON.parse(resp.body)
end

.load_access_tokenObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/adobe/campaign/base.rb', line 51

def load_access_token
  return @access_token if @access_token && @access_token_expires > Time.zone.now

  as_url = "https://#{Adobe::Campaign.configuration.ims_host}/ims/token/v3"
  as_payload = {
    client_id: Adobe::Campaign.configuration.api_key,
    client_secret: Adobe::Campaign.configuration.api_secret,
    grant_type: "client_credentials",
    scope: "campaign_sdk, openid, deliverability_service_general, campaign_config_server_general, " +
           "AdobeID, additional_info.projectedProductContext"
  }
  access_token_resp = RestClient.post(as_url, as_payload, {})
  json = JSON.parse(access_token_resp.body)
  @access_token_expires = 1439.minutes.from_now
  @access_token = json.dig('access_token')
end

.post(body) ⇒ Object



19
20
21
# File 'lib/adobe/campaign/base.rb', line 19

def post(body)
  post_request(endpoint, body)
end

.post_request(url, body) ⇒ Object



33
34
35
36
37
38
# File 'lib/adobe/campaign/base.rb', line 33

def post_request(url, body)
  full_url = url_prefix(url)
  json = body.is_a?(String) ? body : body.to_json
  resp = RestClient.post(full_url, json, auth_headers.merge('Content-Type' => 'application/json;charset=UTF-8'))
  JSON.parse(resp.body)
end