Class: Cookie::Client
- Inherits:
-
Object
- Object
- Cookie::Client
- Defined in:
- lib/cookie/client.rb
Overview
Client for interacting with the Digital Cookie API
Constant Summary collapse
- BASE_URL =
"https://apimobile.digitalcookie.girlscouts.org/mobileapp/api"
Instance Attribute Summary collapse
-
#adapter ⇒ Symbol
readonly
The Faraday adapter being used.
Class Method Summary collapse
-
.authenticate(username:, credential:) ⇒ Client
Authenticate with username and password to create a new client.
Instance Method Summary collapse
-
#connection ⇒ Faraday::Connection
The configured HTTP client.
-
#initialize(api_key: nil, adapter: Faraday.default_adapter) ⇒ Client
constructor
Initialize a new API client.
-
#login ⇒ Resources::Login
The login resource.
-
#orders ⇒ Resources::Orders
The orders resource.
-
#setup_authorization(token) ⇒ void
Set up authorization with a bearer token.
Constructor Details
#initialize(api_key: nil, adapter: Faraday.default_adapter) ⇒ Client
Initialize a new API client
42 43 44 45 |
# File 'lib/cookie/client.rb', line 42 def initialize(api_key: nil, adapter: Faraday.default_adapter) @api_key = api_key @adapter = adapter end |
Instance Attribute Details
#adapter ⇒ Symbol (readonly)
Returns The Faraday adapter being used.
21 22 23 |
# File 'lib/cookie/client.rb', line 21 def adapter @adapter end |
Class Method Details
.authenticate(username:, credential:) ⇒ Client
Authenticate with username and password to create a new client
30 31 32 33 34 35 |
# File 'lib/cookie/client.rb', line 30 def self.authenticate(username:, credential:) client = new response = client.login.authenticate(username: username, credential: credential) client.(response.token) client end |
Instance Method Details
#connection ⇒ Faraday::Connection
Returns The configured HTTP client.
59 60 61 62 63 64 65 66 67 |
# File 'lib/cookie/client.rb', line 59 def connection @connection ||= Faraday.new(url: BASE_URL) do |faraday| faraday.request :json faraday.response :json faraday.adapter adapter faraday.headers["Authorization"] = "Bearer #{@api_key}" if @api_key faraday.headers["Content-Type"] = "application/json" end end |
#login ⇒ Resources::Login
Returns The login resource.
70 71 72 |
# File 'lib/cookie/client.rb', line 70 def login @login ||= Resources::Login.new(self) end |
#orders ⇒ Resources::Orders
Returns The orders resource.
75 76 77 |
# File 'lib/cookie/client.rb', line 75 def orders @orders ||= Resources::Orders.new(self) end |
#setup_authorization(token) ⇒ void
This method returns an undefined value.
Set up authorization with a bearer token
51 52 53 54 55 56 |
# File 'lib/cookie/client.rb', line 51 def (token) @api_key = token # Reset the connection so it's rebuilt with the new token @connection = nil end |