lecture-attendance-manager/app/lib/toggl.rb

49 lines
1.4 KiB
Ruby

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: {
"start_date": start_time.to_date.to_fs(),
"end_date": end_time.to_date.to_fs(),
"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