class Timesheet::Toggl
Export reports from timesheet to toggle. @example
Timesheet::Toggl.new(config).sync_last_month Timesheet::Toggl.new(config).sync_last_month([14, 88]) # where 14, 88 -- user ids. Timesheet::Toggl.new(config).sync(from: (Date.today - 1.month), to: Date.today)
Constants
- BASE_URI
- VERSION
Attributes
config[RW]
Public Class Methods
new(hash)
click to toggle source
Example of config:
api_token: 1971800d4d82861d8f2c1651fea4d212 worspace_id: 123 source_name: 'toggl' redmine_time_entry_class: 'TimeEntryRedmine' # optional
# File lib/timesheet/toggl.rb, line 32 def initialize(hash) @config = hash name = hash[:source_name] src = DataSource.create_with(name: name). find_or_create_by(config_section_id: name, connector_type: 'toggl') @config[:source_id] = src.id end
Public Instance Methods
fetch(params, page)
click to toggle source
Get data from toggl. @param params [Hash] query params for api. @param page [Integer] number of page. @returns [Hash] parsed response from toggl.
# File lib/timesheet/toggl.rb, line 81 def fetch(params, page) print "page #{page}: " response = Curl.get(BASE_URI, params.merge(page: page)) do |request| request.http_auth_types = :basic request.username = config[:api_token] request.password = 'api_token' end parsed = JSON.parse(response.body, symbolize_names: true) fail "Request failed: #{parsed} with params #{params}" unless response.response_code == 200 parsed end
push(parsed_response)
click to toggle source
Push data to timesheet. @param parsed_response [Hash] resulf of fetch
# File lib/timesheet/toggl.rb, line 96 def push(parsed_response) TimeEntry.transaction do parsed_response[:data].map { |x| push_record x } end end
push_record(record)
click to toggle source
Push single record to database. Don't push time entry if no user set.
# File lib/timesheet/toggl.rb, line 105 def push_record(record) params = TogglRecord.new(record, config).push record[:id] end
sync(params, page)
click to toggle source
Since toggl has per-page api, we will follow them.
# File lib/timesheet/toggl.rb, line 71 def sync(params, page) data = fetch(params, page) push(data) end
sync_last_month(user_ids = [])
click to toggle source
# File lib/timesheet/toggl.rb, line 40 def sync_last_month(user_ids = []) from = (Date.today << 1).beginning_of_month to = Date.today synchronize(from, to, user_ids) end
synchronize(from, to, user_ids = [])
click to toggle source
curl -v -u 1971800d4d82861d8f2c1651fea4d212:api_token -X GET “toggl.com/reports/api/v2/details?
workspace_id=123& since=2013-05-19& until=2013-05-20& user_agent=api_test"
@return array of time entries' ids
# File lib/timesheet/toggl.rb, line 55 def synchronize(from, to, user_ids = []) params = { workspace_id: config[:workspace_id], since: from.to_s, until: to.to_s, user_agent: 'export_to_timesheet' } params[:user_ids] = user_ids.join(',') unless user_ids.empty? first_page = fetch(params, 1) te_ids = push(first_page) pages = first_page[:total_count] / first_page[:per_page] pages.times.map { |page| sync(params, page + 2) }.flatten + te_ids end