module Toggl def self.current_time_entry response = HTTParty.get( "#{TOGGL_PORTAL_URL}/current", headers: { 'Accept' => 'application/json' } ) if response.success? return JSON.parse(response.body) else raise "Error fetching current time entry: #{response.body}" end end def self.entries_for_project(toggl_project_id, start_time:, end_time:) response = HTTParty.post( "#{TOGGL_PORTAL_URL}/report", body: { # TODO: Replace these, rails keeps complaining about deprecations "start_date": start_time.to_date.to_fs(:db), "end_date": end_time.to_date.to_fs(:db), "project_ids": [toggl_project_id] }.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } ) if response.success? return JSON.parse(response.body) else raise "Error fetching Toggl entries for project: #{response.body}" end end def self.start_time_entry(description:, project_id:) response = HTTParty.post( "#{TOGGL_PORTAL_URL}/start_time_entry", body: { "created_with": "Attendance Tracker", "description": description, "start": Time.now.utc.iso8601, "duration": -1, "project_id": project_id, }.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } ) raise "Error starting Toggl time entry: #{response.body}" unless response.success? end end