Class: Cookie::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/cookie/client.rb

Overview

Client for interacting with the Digital Cookie API

Examples:

Authenticate and create a new client

client = Cookie::Client.authenticate(
  username: "[email protected]",
  credential: "password123"
)

Create a client with an existing token

client = Cookie::Client.new(api_key: "existing_token")

Constant Summary collapse

BASE_URL =
"https://apimobile.digitalcookie.girlscouts.org/mobileapp/api"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_key: nil, adapter: Faraday.default_adapter) ⇒ Client

Initialize a new API client

Parameters:

  • api_key (String, nil) (defaults to: nil)

    Bearer token for authentication

  • adapter (Symbol) (defaults to: Faraday.default_adapter)

    The Faraday adapter to use



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

#adapterSymbol (readonly)

Returns The Faraday adapter being used.

Returns:

  • (Symbol)

    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

Parameters:

  • username (String)

    Email address for authentication

  • credential (String)

    Password for authentication

Returns:

  • (Client)

    Authenticated client instance

Raises:



30
31
32
33
34
35
# File 'lib/cookie/client.rb', line 30

def self.authenticate(username:, credential:)
  client = new
  response = client..authenticate(username: username, credential: credential)
  client.setup_authorization(response.token)
  client
end

Instance Method Details

#connectionFaraday::Connection

Returns The configured HTTP client.

Returns:

  • (Faraday::Connection)

    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

#loginResources::Login

Returns The login resource.

Returns:



70
71
72
# File 'lib/cookie/client.rb', line 70

def 
  @login ||= Resources::Login.new(self)
end

#ordersResources::Orders

Returns The orders resource.

Returns:



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

Parameters:

  • token (String)

    Bearer token for authentication



51
52
53
54
55
56
# File 'lib/cookie/client.rb', line 51

def setup_authorization(token)
  @api_key = token

  # Reset the connection so it's rebuilt with the new token
  @connection = nil
end