Skip to content

Commit

Permalink
Add Toggl Reports API
Browse files Browse the repository at this point in the history
Signed-off-by: Roy Mumaw <roy@mudbugmedia.com>
  • Loading branch information
Roy Mumaw committed Oct 9, 2015
1 parent 3e1fc2e commit 36113d2
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 0 deletions.
46 changes: 46 additions & 0 deletions lib/reportsv2.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
require 'faraday'
require 'oj'

require 'logger'
require 'awesome_print' # for debug output

require_relative 'reportsv2/summary'
require_relative 'reportsv2/details'
require_relative 'reportsv2/weekly'

require_relative 'toggl_base_api'


# mode: :compat will convert symbols to strings
Oj.default_options = { mode: :compat }

module ReportsV2
TOGGL_API_BASE_URL = 'https://www.toggl.com/reports/api/'

class API
attr_accessor :user_agent

# Because reports can contain information outside of the 'data' field
# We need to explicitly request the full response for reports
FULL_RESPONSE = true

def self.toggl_api_url
TOGGL_API_BASE_URL + 'v2/'
end

def user_agent
@user_agent || super
end

def get_report(report_type, workspace_id, params={})
params_string = params.map do |key, value|
if value.is_a? Array
value = value.join('%')
end
"#{key}=#{value}"
end.join('&')
get "#{report_type}?user_agent=#{user_agent}&workspace_id=#{workspace_id}&#{params_string}", FULL_RESPONSE
end
end

end
9 changes: 9 additions & 0 deletions lib/reportsv2/details.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ReportsV2
class API < TogglBaseApi

def details(workspace_id, params={})
get_report('details', workspace_id, params)
end

end
end
9 changes: 9 additions & 0 deletions lib/reportsv2/summary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ReportsV2
class API < TogglBaseApi

def summary(workspace_id, params={})
get_report('summary', workspace_id, params)
end

end
end
4 changes: 4 additions & 0 deletions lib/reportsv2/version.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
module TogglV8
# :section:
VERSION = "0.2.0"
end
9 changes: 9 additions & 0 deletions lib/reportsv2/weekly.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
module ReportsV2
class API < TogglBaseApi

def weekly(workspace_id, params={})
get_report('weekly', workspace_id, params)
end

end
end

0 comments on commit 36113d2

Please sign in to comment.