class Blurb::Account

Constants

API_URLS

Attributes

active_profile[RW]
api_url[RW]
client[RW]
profiles[RW]
refresh_token[RW]

Public Class Methods

new(refresh_token:, region:, client:, profile_id: nil) click to toggle source
# 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

Public Instance Methods

get_profile(profile_id) click to toggle source
# 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) click to toggle source
# 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_list() click to toggle source
# File lib/blurb/account.rb, line 52
def profile_list
  profile_request("/v2/profiles")
end
retrieve_profile(profile_id) click to toggle source
# File lib/blurb/account.rb, line 56
def retrieve_profile(profile_id)
  profile_request("/v2/profiles/#{profile_id}")
end
retrieve_token() click to toggle source
# 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) click to toggle source
# File lib/blurb/account.rb, line 44
def set_active_profile(profile_id)
  @active_profile = get_profile(profile_id)
end

Private Instance Methods

authorization_client() click to toggle source
# File lib/blurb/account.rb, line 100
def authorization_client
  OAuth2::Client.new(
    '',
    '',
    site: 'https://api.amazon.com',
    ssl: { version: :TLSv1 }
  )
end
profile_request(api_path) click to toggle source
# File lib/blurb/account.rb, line 86
def profile_request(api_path)
  request = Request.new(
    url: "#{@api_url}#{api_path}",
    request_type: :get,
    headers: {
      "Authorization" => "Bearer #{retrieve_token()}",
      "Content-Type" => "application/json",
      "Amazon-Advertising-API-ClientId" => @client.client_id
    }
  )

  request.make_request
end