Class: FreshBooks::CLI::Api
- Inherits:
-
Object
- Object
- FreshBooks::CLI::Api
- Defined in:
- lib/freshbooks/api.rb
Constant Summary collapse
- BASE =
"https://api.freshbooks.com"
Class Method Summary collapse
- .account_id ⇒ Object
-
.build_name_maps ⇒ Object
--- Name Resolution (for entries display) ---.
- .business_id ⇒ Object
-
.cache_fresh? ⇒ Boolean
--- Cache helpers ---.
- .cached_data(key) ⇒ Object
- .config ⇒ Object
- .create_time_entry(entry) ⇒ Object
- .delete_time_entry(entry_id) ⇒ Object
-
.fetch_all_pages(url, result_key, params: {}) ⇒ Object
--- Paginated fetch ---.
-
.fetch_clients(force: false) ⇒ Object
--- Clients ---.
-
.fetch_projects(force: false) ⇒ Object
--- Projects ---.
- .fetch_projects_for_client(client_id) ⇒ Object
-
.fetch_services(force: false) ⇒ Object
--- Services ---.
-
.fetch_time_entries(started_from: nil, started_to: nil) ⇒ Object
--- Time Entries ---.
- .fetch_time_entry(entry_id) ⇒ Object
- .headers ⇒ Object
- .update_cache(key, data) ⇒ Object
- .update_time_entry(entry_id, fields) ⇒ Object
Class Method Details
.account_id ⇒ Object
33 34 35 36 37 38 39 |
# File 'lib/freshbooks/api.rb', line 33 def account_id c = config unless c["account_id"] c = Auth.require_business(c) end c["account_id"] end |
.build_name_maps ⇒ Object
--- Name Resolution (for entries display) ---
246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 |
# File 'lib/freshbooks/api.rb', line 246 def build_name_maps cache = Auth.load_cache now = Time.now.to_i if cache["updated_at"] && (now - cache["updated_at"]) < 600 && cache["clients"] && !cache["clients"].empty? return { clients: (cache["clients"] || {}), projects: (cache["projects"] || {}), services: (cache["services"] || {}) } end clients = fetch_clients(force: true) projects = fetch_projects(force: true) services = fetch_services(force: true) client_map = {} clients.each do |c| name = c["organization"] name = "#{c["fname"]} #{c["lname"]}" if name.nil? || name.empty? client_map[c["id"].to_s] = name end project_map = {} projects.each do |p| project_map[p["id"].to_s] = p["title"] end service_map = {} services.each do |s| service_map[s["id"].to_s] = s["name"] end # Also collect services embedded in projects projects.each do |p| (p["services"] || []).each do |s| service_map[s["id"].to_s] ||= s["name"] end end cache = Auth.load_cache cache["updated_at"] = now cache["clients"] = client_map cache["projects"] = project_map cache["services"] = service_map Auth.save_cache(cache) { clients: client_map, projects: project_map, services: service_map } end |
.business_id ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/freshbooks/api.rb', line 25 def business_id c = config unless c["business_id"] c = Auth.require_business(c) end c["business_id"] end |
.cache_fresh? ⇒ Boolean
--- Cache helpers ---
77 78 79 80 |
# File 'lib/freshbooks/api.rb', line 77 def cache_fresh? cache = Auth.load_cache cache["updated_at"] && (Time.now.to_i - cache["updated_at"]) < 600 end |
.cached_data(key) ⇒ Object
82 83 84 85 86 87 88 |
# File 'lib/freshbooks/api.rb', line 82 def cached_data(key) return Auth.load_cache[key] if Thread.current[:fb_dry_run] cache = Auth.load_cache return nil unless cache["updated_at"] && (Time.now.to_i - cache["updated_at"]) < 600 cache[key] end |
.config ⇒ Object
20 21 22 23 |
# File 'lib/freshbooks/api.rb', line 20 def config @config = nil @config = Auth.require_config end |
.create_time_entry(entry) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/freshbooks/api.rb', line 178 def create_time_entry(entry) if Thread.current[:fb_dry_run] return { "_dry_run" => { "simulated" => true, "payload_sent" => entry }, "result" => { "time_entry" => entry.merge("id" => 0) } } end url = "#{BASE}/timetracking/business/#{business_id}/time_entries" body = { time_entry: entry } response = HTTParty.post(url, { headers: headers, body: body.to_json }) unless response.success? body = response.parsed_response msg = extract_error(body) || response.body abort("API error: #{msg}") end response.parsed_response end |
.delete_time_entry(entry_id) ⇒ Object
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 |
# File 'lib/freshbooks/api.rb', line 228 def delete_time_entry(entry_id) return true if Thread.current[:fb_dry_run] url = "#{BASE}/timetracking/business/#{business_id}/time_entries/#{entry_id}" response = HTTParty.delete(url, { headers: headers }) unless response.success? body = response.parsed_response msg = extract_error(body) || response.body abort("API error: #{msg}") end true end |
.fetch_all_pages(url, result_key, params: {}) ⇒ Object
--- Paginated fetch ---
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/freshbooks/api.rb', line 43 def fetch_all_pages(url, result_key, params: {}) page = 1 all_items = [] loop do response = HTTParty.get(url, { headers: headers, query: params.merge(page: page, per_page: 100) }) unless response.success? body = response.parsed_response msg = extract_error(body) || response.body abort("API error: #{msg}") end data = response.parsed_response items = dig_results(data, result_key) break if items.nil? || items.empty? all_items.concat(items) = (data) break if .nil? break if page >= ["pages"].to_i page += 1 end all_items end |
.fetch_clients(force: false) ⇒ Object
--- Clients ---
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/freshbooks/api.rb', line 99 def fetch_clients(force: false) unless force cached = cached_data("clients_data") return cached if cached end url = "#{BASE}/accounting/account/#{account_id}/users/clients" results = fetch_all_pages(url, "clients") update_cache("clients_data", results) results end |
.fetch_projects(force: false) ⇒ Object
--- Projects ---
113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/freshbooks/api.rb', line 113 def fetch_projects(force: false) unless force cached = cached_data("projects_data") return cached if cached end url = "#{BASE}/projects/business/#{business_id}/projects" results = fetch_all_pages(url, "projects") update_cache("projects_data", results) results end |
.fetch_projects_for_client(client_id) ⇒ Object
125 126 127 128 |
# File 'lib/freshbooks/api.rb', line 125 def fetch_projects_for_client(client_id) all = fetch_projects all.select { |p| p["client_id"].to_i == client_id.to_i } end |
.fetch_services(force: false) ⇒ Object
--- Services ---
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 |
# File 'lib/freshbooks/api.rb', line 132 def fetch_services(force: false) unless force cached = cached_data("services_data") return cached if cached end url = "#{BASE}/comments/business/#{business_id}/services" response = HTTParty.get(url, { headers: headers }) unless response.success? body = response.parsed_response msg = extract_error(body) || response.body abort("API error: #{msg}") end data = response.parsed_response services_hash = data.dig("result", "services") || {} results = services_hash.values update_cache("services_data", results) results end |
.fetch_time_entries(started_from: nil, started_to: nil) ⇒ Object
--- Time Entries ---
156 157 158 159 160 161 162 |
# File 'lib/freshbooks/api.rb', line 156 def fetch_time_entries(started_from: nil, started_to: nil) url = "#{BASE}/timetracking/business/#{business_id}/time_entries" params = {} params["started_from"] = "#{started_from}T00:00:00Z" if started_from params["started_to"] = "#{started_to}T23:59:59Z" if started_to fetch_all_pages(url, "time_entries", params: params) end |
.fetch_time_entry(entry_id) ⇒ Object
164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/freshbooks/api.rb', line 164 def fetch_time_entry(entry_id) url = "#{BASE}/timetracking/business/#{business_id}/time_entries/#{entry_id}" response = HTTParty.get(url, { headers: headers }) unless response.success? body = response.parsed_response msg = extract_error(body) || response.body abort("API error: #{msg}") end data = response.parsed_response data.dig("result", "time_entry") || data.dig("time_entry") end |
.headers ⇒ Object
12 13 14 15 16 17 18 |
# File 'lib/freshbooks/api.rb', line 12 def headers token = Auth.valid_access_token { "Authorization" => "Bearer #{token}", "Content-Type" => "application/json" } end |
.update_cache(key, data) ⇒ Object
90 91 92 93 94 95 |
# File 'lib/freshbooks/api.rb', line 90 def update_cache(key, data) cache = Auth.load_cache cache["updated_at"] = Time.now.to_i cache[key] = data Auth.save_cache(cache) end |
.update_time_entry(entry_id, fields) ⇒ Object
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 |
# File 'lib/freshbooks/api.rb', line 203 def update_time_entry(entry_id, fields) if Thread.current[:fb_dry_run] return { "_dry_run" => { "simulated" => true, "payload_sent" => fields }, "result" => { "time_entry" => fields.merge("id" => entry_id) } } end url = "#{BASE}/timetracking/business/#{business_id}/time_entries/#{entry_id}" body = { time_entry: fields } response = HTTParty.put(url, { headers: headers, body: body.to_json }) unless response.success? body = response.parsed_response msg = extract_error(body) || response.body abort("API error: #{msg}") end response.parsed_response end |