Class: TogglAPI::BaseClient

Inherits:
Object
  • Object
show all
Defined in:
lib/toggl_api/base_client.rb

Overview

Superclass for API clients.

Direct Known Subclasses

Client, ReportsClient

Constant Summary collapse

DEFAULT_USER_AGENT =
"TogglAPI"

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(api_token: default_api_token, user_agent: default_user_agent, logger: nil) ⇒ BaseClient

Returns a new instance of the client, configured with the specified parameters.

Parameters:

  • api_token (String) (defaults to: default_api_token)

    Toggl API token, for authentication

  • user_agent (String) (defaults to: default_user_agent)

    defaults to “TogglCache”

  • logger (Logger) (defaults to: nil)

    used to log messages (in particular fetch events). Defaults to nil.



20
21
22
23
24
25
# File 'lib/toggl_api/base_client.rb', line 20

def initialize(api_token: default_api_token, user_agent: default_user_agent, logger: nil)
  raise "Missing api_token" if api_token.nil? || api_token.empty?
  @api_token = api_token
  @user_agent = user_agent
  @logger = logger
end

Instance Attribute Details

#api_tokenObject (readonly)

Returns the value of attribute api_token.



10
11
12
# File 'lib/toggl_api/base_client.rb', line 10

def api_token
  @api_token
end

#loggerObject (readonly)

Returns the value of attribute logger.



10
11
12
# File 'lib/toggl_api/base_client.rb', line 10

def logger
  @logger
end

Instance Method Details

#credentialsObject



35
36
37
38
39
40
# File 'lib/toggl_api/base_client.rb', line 35

def credentials
  {
    username: @api_token,
    password: "api_token"
  }
end

#default_api_tokenObject



27
28
29
# File 'lib/toggl_api/base_client.rb', line 27

def default_api_token
  ENV["TOGGL_API_TOKEN"]
end

#default_headersObject



42
43
44
45
46
# File 'lib/toggl_api/base_client.rb', line 42

def default_headers
  {
    "Content-Type" => "application/json"
  }
end

#default_user_agentObject



31
32
33
# File 'lib/toggl_api/base_client.rb', line 31

def default_user_agent
  DEFAULT_USER_AGENT
end

#default_workspace_idObject



48
49
50
# File 'lib/toggl_api/base_client.rb', line 48

def default_workspace_id
  DEFAULT_WORKSPACE_ID
end