From 36113d21cba34e259af191301ae128f1462755f7 Mon Sep 17 00:00:00 2001 From: Roy Mumaw Date: Fri, 9 Oct 2015 12:08:25 -0500 Subject: [PATCH] Add Toggl Reports API Signed-off-by: Roy Mumaw --- lib/reportsv2.rb | 46 ++++++++++++++++++++++++++++++++++++++++ lib/reportsv2/details.rb | 9 ++++++++ lib/reportsv2/summary.rb | 9 ++++++++ lib/reportsv2/version.rb | 4 ++++ lib/reportsv2/weekly.rb | 9 ++++++++ 5 files changed, 77 insertions(+) create mode 100644 lib/reportsv2.rb create mode 100644 lib/reportsv2/details.rb create mode 100644 lib/reportsv2/summary.rb create mode 100644 lib/reportsv2/version.rb create mode 100644 lib/reportsv2/weekly.rb diff --git a/lib/reportsv2.rb b/lib/reportsv2.rb new file mode 100644 index 000000000..1792b7718 --- /dev/null +++ b/lib/reportsv2.rb @@ -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 \ No newline at end of file diff --git a/lib/reportsv2/details.rb b/lib/reportsv2/details.rb new file mode 100644 index 000000000..144fb7e21 --- /dev/null +++ b/lib/reportsv2/details.rb @@ -0,0 +1,9 @@ +module ReportsV2 + class API < TogglBaseApi + + def details(workspace_id, params={}) + get_report('details', workspace_id, params) + end + + end +end \ No newline at end of file diff --git a/lib/reportsv2/summary.rb b/lib/reportsv2/summary.rb new file mode 100644 index 000000000..f75e09852 --- /dev/null +++ b/lib/reportsv2/summary.rb @@ -0,0 +1,9 @@ +module ReportsV2 + class API < TogglBaseApi + + def summary(workspace_id, params={}) + get_report('summary', workspace_id, params) + end + + end +end \ No newline at end of file diff --git a/lib/reportsv2/version.rb b/lib/reportsv2/version.rb new file mode 100644 index 000000000..c7d56d15c --- /dev/null +++ b/lib/reportsv2/version.rb @@ -0,0 +1,4 @@ +module TogglV8 + # :section: + VERSION = "0.2.0" +end \ No newline at end of file diff --git a/lib/reportsv2/weekly.rb b/lib/reportsv2/weekly.rb new file mode 100644 index 000000000..d8dfa55f9 --- /dev/null +++ b/lib/reportsv2/weekly.rb @@ -0,0 +1,9 @@ +module ReportsV2 + class API < TogglBaseApi + + def weekly(workspace_id, params={}) + get_report('weekly', workspace_id, params) + end + + end +end \ No newline at end of file