Class: Perka::PerkaApi

Inherits:
ClientApi
  • Object
show all
Includes:
Flatpack::Core::MapInitialize
Defined in:
lib/perka/perka_api.rb

Defined Under Namespace

Classes: TokenRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from ClientApi

#annotation_put, #annotation_type_uuid_get, #asset_manifest_perks_get, #customer_points_get, #customer_reward_get, #customer_reward_put, #customer_visit_amend_put, #customer_visit_get, #customer_visit_post, #customer_visit_put, #customer_visit_validate_get, #describe_get, #describe_token_get, #describe_type_get, #describe_type_uuid_get, #integrator_customer_post, #integrator_customer_put, #integrator_destroy_delete, #integrator_managed_merchants_get, #integrator_managed_users_get, #integrator_merchant_post, #merchant_locations_get

Instance Attribute Details

#access_expirationObject

Returns the value of attribute access_expiration.



8
9
10
# File 'lib/perka/perka_api.rb', line 8

def access_expiration
  @access_expiration
end

#access_tokenObject

Returns the value of attribute access_token.



8
9
10
# File 'lib/perka/perka_api.rb', line 8

def access_token
  @access_token
end

#refresh_tokenObject

Returns the value of attribute refresh_token.



8
9
10
# File 'lib/perka/perka_api.rb', line 8

def refresh_token
  @refresh_token
end

#user_uuidObject

Returns the value of attribute user_uuid.



8
9
10
# File 'lib/perka/perka_api.rb', line 8

def user_uuid
  @user_uuid
end

Instance Method Details

#auth_headersObject

Concrete implementations that require authentication should implement this method if authenticated requests require custom request headers



22
23
24
# File 'lib/perka/perka_api.rb', line 22

def auth_headers
  @access_token ? {'Authorization' => "Bearer #{@access_token}"} : {}
end

#oauth_integrator_become(role, uuid) ⇒ Object

returns a new PerkaApi authorized as the given role and user



41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/perka/perka_api.rb', line 41

def oauth_integrator_become(role, uuid)
  role = role
  payload = "grant_type=client_credentials&scope=#{URI::encode(role)}:#{uuid}"
  new_api = PerkaApi.new({
    :server_base => @server_base,
    :flatpack => @flatpack,
    :access_token => @access_token,
    :access_expiration => @access_expiration,
    :verbose => @verbose,
  })
  execute_token_request(new_api, payload)
  new_api
end

#oauth_integrator_login(integrator_id, integrator_secret) ⇒ Object

Grants an integrator access token



27
28
29
30
31
32
33
34
35
36
37
38
# File 'lib/perka/perka_api.rb', line 27

def (integrator_id, integrator_secret)
  @integrator_id = integrator_id
  @integrator_secret = integrator_secret
  payload = 
    "grant_type=password"\
    "&username=#{integrator_id}"\
    "&client_id=#{integrator_id}"\
    "&password=#{URI::encode(integrator_secret)}"\
    "&scope=INTEGRATOR";
  
  execute_token_request(self, payload);
end

#oauth_refresh_tokenObject

Obtain a new access token using integrator credentials and a refresh token.



56
57
58
59
60
61
62
63
# File 'lib/perka/perka_api.rb', line 56

def oauth_refresh_token
  payload = "grant_type=refresh_token"\
    "&client_id=#{@integrator_id}"\
    "&client_secret=#{URI::encode(@integrator_secret)}"\
    "&refresh_token=#{@refresh_token}"
  
  execute_token_request(self, payload);
end

#refresh_sessionObject



16
17
18
# File 'lib/perka/perka_api.rb', line 16

def refresh_session
  oauth_refresh_token
end

#session_active?Boolean

our session is considered active if we’ve never received an access token, or our token expiration as after now

Returns:

  • (Boolean)


12
13
14
# File 'lib/perka/perka_api.rb', line 12

def session_active?
  !@access_token or @access_expiration > Time.now
end