Module: Sfctl::Toggl::Client

Defined in:
lib/sfctl/toggl/client.rb

Constant Summary collapse

DEFAULT_API_PATH =
'api/v8/'.freeze
REPORTS_API_PATH =
'reports/api/v2/'.freeze

Class Method Summary collapse

Class Method Details

.conn(token, api = 'default') ⇒ Object



10
11
12
13
14
15
16
17
18
19
20
# File 'lib/sfctl/toggl/client.rb', line 10

def self.conn(token, api = 'default')
  raise 'Please set toggl provider before continue.' if token.nil?

  api_path = api == 'reports' ? REPORTS_API_PATH : DEFAULT_API_PATH

  headers = { 'Content-Type' => 'application/json' }
  Faraday.new(url: "https://#{token}:[email protected]/#{api_path}", headers: headers) do |builder|
    builder.request :retry
    builder.adapter :net_http
  end
end

.parsed_response(response) ⇒ Object



22
23
24
# File 'lib/sfctl/toggl/client.rb', line 22

def self.parsed_response(response)
  [response.status == 200, JSON.parse(response.body)]
end

.project_tasks(token, project_id) ⇒ Object



36
37
38
39
40
41
42
# File 'lib/sfctl/toggl/client.rb', line 36

def self.project_tasks(token, project_id)
  response = conn(token).get("workspaces/#{project_id}/tasks")

  return [] if response.body.length.zero?

  parsed_response(response)
end

.time_entries(token, params) ⇒ Object



44
45
46
47
48
# File 'lib/sfctl/toggl/client.rb', line 44

def self.time_entries(token, params)
  params[:user_agent] = 'api_test'
  response = conn(token, 'reports').get('details', params)
  parsed_response(response)
end

.workspace_projects(token, workspace_id) ⇒ Object



31
32
33
34
# File 'lib/sfctl/toggl/client.rb', line 31

def self.workspace_projects(token, workspace_id)
  response = conn(token).get("workspaces/#{workspace_id}/projects")
  parsed_response(response)
end

.workspaces(token) ⇒ Object



26
27
28
29
# File 'lib/sfctl/toggl/client.rb', line 26

def self.workspaces(token)
  response = conn(token).get('workspaces')
  parsed_response(response)
end