Class: CurrencyCloud::Session
- Inherits:
-
Object
- Object
- CurrencyCloud::Session
- Defined in:
- lib/currency_cloud/session.rb
Constant Summary collapse
- ENVIRONMENTS =
{ production: 'https://api.currencycloud.com', demonstration: 'https://devapi.currencycloud.com', uat: 'https://api-uat1.ccycloud.com' }.freeze
Instance Attribute Summary collapse
-
#api_key ⇒ Object
readonly
Returns the value of attribute api_key.
-
#environment ⇒ Object
readonly
Returns the value of attribute environment.
-
#login_id ⇒ Object
readonly
Returns the value of attribute login_id.
-
#token ⇒ Object
Returns the value of attribute token.
Class Method Summary collapse
Instance Method Summary collapse
- #authenticate ⇒ Object
- #close ⇒ Object
- #environment_url ⇒ Object
-
#initialize(environment, login_id, api_key, token) ⇒ Session
constructor
A new instance of Session.
-
#on_behalf_of ⇒ Object
Stored fiber-local so the shared session can't leak contact context across threads.
- #on_behalf_of=(value) ⇒ Object
- #reauthenticate ⇒ Object
Constructor Details
#initialize(environment, login_id, api_key, token) ⇒ Session
Returns a new instance of Session.
26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/currency_cloud/session.rb', line 26 def initialize(environment, login_id, api_key, token) @environment = environment @login_id = login_id @api_key = api_key if token self.class.validate_environment(environment) @token = token else authenticate end end |
Instance Attribute Details
#api_key ⇒ Object (readonly)
Returns the value of attribute api_key.
9 10 11 |
# File 'lib/currency_cloud/session.rb', line 9 def api_key @api_key end |
#environment ⇒ Object (readonly)
Returns the value of attribute environment.
9 10 11 |
# File 'lib/currency_cloud/session.rb', line 9 def environment @environment end |
#login_id ⇒ Object (readonly)
Returns the value of attribute login_id.
9 10 11 |
# File 'lib/currency_cloud/session.rb', line 9 def login_id @login_id end |
#token ⇒ Object
Returns the value of attribute token.
10 11 12 |
# File 'lib/currency_cloud/session.rb', line 10 def token @token end |
Class Method Details
.validate_environment(environment) ⇒ Object
21 22 23 24 |
# File 'lib/currency_cloud/session.rb', line 21 def self.validate_environment(environment) return if ENVIRONMENTS.key?(environment) raise "'#{environment}' is not a valid environment. Must be one of: #{ENVIRONMENTS.keys.join(', ')}" end |
Instance Method Details
#authenticate ⇒ Object
47 48 49 50 51 |
# File 'lib/currency_cloud/session.rb', line 47 def authenticate validate params = { login_id: login_id, api_key: api_key } CurrencyCloud.token = @token = request.post('authenticate/api', params, should_retry: false)['auth_token'] end |
#close ⇒ Object
43 44 45 |
# File 'lib/currency_cloud/session.rb', line 43 def close request.post('authenticate/close_session') end |
#environment_url ⇒ Object
39 40 41 |
# File 'lib/currency_cloud/session.rb', line 39 def environment_url ENVIRONMENTS[environment] end |
#on_behalf_of ⇒ Object
Stored fiber-local so the shared session can't leak contact context across threads.
13 14 15 |
# File 'lib/currency_cloud/session.rb', line 13 def on_behalf_of Thread.current[obo_key] end |
#on_behalf_of=(value) ⇒ Object
17 18 19 |
# File 'lib/currency_cloud/session.rb', line 17 def on_behalf_of=(value) Thread.current[obo_key] = value end |
#reauthenticate ⇒ Object
53 54 55 56 |
# File 'lib/currency_cloud/session.rb', line 53 def reauthenticate CurrencyCloud.token = @token = nil authenticate end |