From 28c3fd27bedd5daf71228e2c9913aa5e3e6317bc Mon Sep 17 00:00:00 2001 From: Joshua Coles Date: Sun, 4 Feb 2024 17:36:13 +0000 Subject: [PATCH] Move to local toggl-portal to work around bug, also improve error handling and add the two missing sigs --- app/lib/toggl.rb | 24 +++++++++++++++++++----- config/initializers/services.rb | 2 +- sig/toggl.rbs | 4 ++++ 3 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/lib/toggl.rb b/app/lib/toggl.rb index 27ce5f7..408b8be 100644 --- a/app/lib/toggl.rb +++ b/app/lib/toggl.rb @@ -1,13 +1,19 @@ module Toggl def self.current_time_entry - JSON.parse(HTTParty.get( + response = HTTParty.get( "#{TOGGL_PORTAL_URL}/current", headers: { 'Accept' => 'application/json' } - ).body) + ) + + 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:) - JSON.parse(HTTParty.post( + response = HTTParty.post( "#{TOGGL_PORTAL_URL}/report", body: { "start_date": start_time.to_date.to_fs(), @@ -15,11 +21,17 @@ module Toggl "project_ids": [toggl_project_id] }.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } - ).body) + ) + + 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:) - HTTParty.post( + response = HTTParty.post( "#{TOGGL_PORTAL_URL}/start_time_entry", body: { "created_with": "Attendance Tracker", @@ -30,5 +42,7 @@ module Toggl }.to_json, headers: { 'Content-Type' => 'application/json', 'Accept' => 'application/json' } ) + + raise "Error starting Toggl time entry: #{response.body}" unless response.success? end end diff --git a/config/initializers/services.rb b/config/initializers/services.rb index 88c264b..59fff4d 100644 --- a/config/initializers/services.rb +++ b/config/initializers/services.rb @@ -1,3 +1,3 @@ # TODO: Is there a better way to do this? -TOGGL_PORTAL_URL = ENV['TOGGL_PORTAL_URL'] || 'http://cosmos:7001' +TOGGL_PORTAL_URL = ENV['TOGGL_PORTAL_URL'] || 'http://localhost:3005' ||'http://cosmos:7001' PANOPTO_PORTAL_URL = ENV['PANOPTO_PORTAL_URL'] || 'https://lectures.joshuacoles.me' diff --git a/sig/toggl.rbs b/sig/toggl.rbs index 018f2c2..7cbc311 100644 --- a/sig/toggl.rbs +++ b/sig/toggl.rbs @@ -1,3 +1,7 @@ module Toggl def self.entries_for_project: -> Array[untyped] + + def self.current_time_entry: -> untyped + + def self.start_time_entry: (description: string, project_id: int) -> void end