Class: Blurb::Account

Inherits:
BaseClass show all
Defined in:
lib/blurb/account.rb

Constant Summary collapse

API_URLS =
{
  "TEST" => "https://advertising-api-test.amazon.com",
  "NA" => "https://advertising-api.amazon.com",
  "EU" => "https://advertising-api-eu.amazon.com",
  "FE" => "https://advertising-api-fe.amazon.com"
}

Constants inherited from BaseClass

BaseClass::CAMPAIGN_TYPE_CODES

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(refresh_token:, region:, client:, profile_id: nil) ⇒ Account

Returns a new instance of Account.



16
17
18
19
20
21
22
23
# File 'lib/blurb/account.rb', line 16

def initialize(refresh_token:, region:, client:, profile_id: nil)
  @refresh_token = refresh_token
  @api_url = API_URLS[region]
  @client = client
  @token_refreshed_at = Process.clock_gettime(Process::CLOCK_MONOTONIC) # current time
  @authorization_token = retrieve_token
  initialize_profiles(profile_id)
end

Instance Attribute Details

#active_profileObject

Returns the value of attribute active_profile.



7
8
9
# File 'lib/blurb/account.rb', line 7

def active_profile
  @active_profile
end

#api_urlObject

Returns the value of attribute api_url.



7
8
9
# File 'lib/blurb/account.rb', line 7

def api_url
  @api_url
end

#clientObject

Returns the value of attribute client.



7
8
9
# File 'lib/blurb/account.rb', line 7

def client
  @client
end

#profilesObject

Returns the value of attribute profiles.



7
8
9
# File 'lib/blurb/account.rb', line 7

def profiles
  @profiles
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



7
8
9
# File 'lib/blurb/account.rb', line 7

def refresh_token
  @refresh_token
end

Instance Method Details

#get_profile(profile_id) ⇒ Object



48
49
50
# File 'lib/blurb/account.rb', line 48

def get_profile(profile_id)
  @profiles.find{ |p| p.profile_id == profile_id }
end

#initialize_profiles(profile_id = nil) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/blurb/account.rb', line 25

def initialize_profiles(profile_id=nil)
  @profiles = []
  if profile_id
    @profiles << Profile.new(
      profile_id: profile_id,
      account: self
    )
  else
    amazon_profiles = profile_list()
    amazon_profiles.each do |p|
      @profiles << Profile.new(
        profile_id: p[:profile_id],
        account: self
      )
    end
  end
  @active_profile = @profiles.first
end

#profile_listObject



52
53
54
# File 'lib/blurb/account.rb', line 52

def profile_list
  profile_request("/v2/profiles")
end

#retrieve_profile(profile_id) ⇒ Object



56
57
58
# File 'lib/blurb/account.rb', line 56

def retrieve_profile(profile_id)
  profile_request("/v2/profiles/#{profile_id}")
end

#retrieve_tokenObject



60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/blurb/account.rb', line 60

def retrieve_token
  current_time = Process.clock_gettime(Process::CLOCK_MONOTONIC)
  elapsed_time = current_time - @token_refreshed_at

  # refresh the token if it's been over an hour
  if @authorization_token.nil? || elapsed_time >= 3600 # 1 hour #Look at using amazons expires_inblurb/
    response = authorization_client.request(:post, "/auth/o2/token",
      {
        body: {
          grant_type: "refresh_token",
          client_id: @client.client_id,
          refresh_token: @refresh_token,
          client_secret: @client.client_secret
        }
      }
    )

    @authorization_token = JSON.parse(response.body)['access_token']
    @token_refreshed_at = current_time
  end

  return @authorization_token
end

#set_active_profile(profile_id) ⇒ Object



44
45
46
# File 'lib/blurb/account.rb', line 44

def set_active_profile(profile_id)
  @active_profile = get_profile(profile_id)
end