jbr 3.1.0 → 3.2.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 97dff097405bb45eb1ac6048e9ce131d6905f19221e1e910ccff1f533ede76ed
4
- data.tar.gz: 2cf46317599aa7146b73fa734441a0c995749692cea4565bcc6646b80fff478c
3
+ metadata.gz: d1028c7784920b94426d3c9d52a29232cdb4b8426baf5abe11cbcfe64d9f854f
4
+ data.tar.gz: d27d31e838ea1fd1c2c3035abf3e21635955039febc8a481bb514a8bd8e9fb9d
5
5
  SHA512:
6
- metadata.gz: 6fc99f2dbaa979073811af9382023b10acde8182e89ded303876876f92e54bcbce9f2ee732917003571823aa2b3127b8f960ad912f88ab53935e8041022b3f4a
7
- data.tar.gz: 656ca93656a1702f226d9f8aa4ed12e7e0d31f01a1ac4cda426096ec7eea48e30e5b2977ccbc113a5dfa21fdb6084009b400caae836f23335a4bd28a4626511d
6
+ metadata.gz: 74d582435245e0aebf5975748e2ba86e5e53f1190a4f4007d1b50e514df47816082f82a8cdf41b8bd9529e63fe25219035005335ed56708b857ba6aabfdbcb4e
7
+ data.tar.gz: 6977198a4d12a410b0c2b8ef2aa0cda829a43ec5fda670ce7d9dbcd13301ac2ef3da55b605d4a6ab7e3017839a115c9f757a6a4ae6d86522f4fa584151cb4f12
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## [3.2.0] - 2026-08-13
2
+
3
+ - [New] Wait rather than be refused. Jobber holds an app to two limits at once -- 2,500
4
+ requests every five minutes, and a bucket of query cost that drains as it is asked --
5
+ and a walk of many pages could reach either. Every request now spaces itself 0.12s from
6
+ the one before, which is the count spread evenly over the window, and reads the bucket
7
+ Jobber reports beside the data to wait longer where the next page cannot be paid for.
8
+ A request that follows no other waits for nothing, so a single lookup is as quick as it
9
+ was: only a walk long enough to be a problem is slowed, and only as much as it must be
10
+
1
11
  ## [3.1.0] - 2026-08-13
2
12
 
3
13
  - [Fix] An answer Jobber left empty now reads as no answer rather than as an empty string.
data/README.md CHANGED
@@ -76,7 +76,8 @@ job.completed_at # => 2026-05-18 11:36:13
76
76
 
77
77
  Or walk the account's jobs, oldest first. Jobber is asked for a page at a time, and only
78
78
  once the page before it runs out, so `first` costs one request where `to_a` costs as many
79
- as the account has pages:
79
+ as the account has pages. A walk paces itself, so a long one is never refused: see
80
+ [Rate limits](#rate-limits).
80
81
 
81
82
  ```ruby
82
83
  jobs = oauth.jobs # => an Enumerable of every job, nothing fetched yet
@@ -154,6 +155,20 @@ visit.property.client.name # => whoever the place sits on the file of
154
155
  Ask for nothing and nothing arrives: `oauth.visits.first.client.name` is nil where the
155
156
  query never named a client.
156
157
 
158
+ ### Rate limits
159
+
160
+ Jobber holds an app to two limits at once: 2,500 requests every five minutes, and a bucket
161
+ of query cost that drains as it is asked and refills at a rate it reports. Nothing has to be
162
+ done about either — every request waits for itself:
163
+
164
+ - It spaces itself 0.12 seconds from the request before, which is 2,500 spread evenly over
165
+ five minutes.
166
+ - It reads `extensions.cost` off each answer, and where the bucket can no longer pay for a
167
+ page like the last one, it waits for the shortfall to refill at Jobber's own restore rate.
168
+
169
+ A request that follows no other waits for nothing, so a single `find` is as quick as it ever
170
+ was. Only a walk long enough to be a problem is slowed, and only by as much as it must be.
171
+
157
172
  ### Events
158
173
 
159
174
  Parse the payload of a Jobber event webhook:
@@ -16,6 +16,7 @@ module GraphQL
16
16
 
17
17
  # @param query [String] the GraphQL query string.
18
18
  # @param variables [Hash] the variables to interpolate into the query.
19
+ # @yield [Hash] the `extensions` the endpoint answered beside the data, where it did.
19
20
  # @return [Hash] the `data` portion of the GraphQL response.
20
21
  def query(query, variables: {})
21
22
  response = Net::HTTP.post @endpoint, { query:, variables: }.to_json, request_headers
@@ -24,6 +25,7 @@ module GraphQL
24
25
  body = JSON.parse(response.body)
25
26
  errors = body['errors']
26
27
  raise Error, errors.map { |error| error['message'] }.join('; ') if errors.present?
28
+ yield body['extensions'] if block_given?
27
29
  body.fetch('data')
28
30
  end
29
31
 
data/lib/jbr/oauth.rb CHANGED
@@ -34,12 +34,11 @@ module Jbr
34
34
  def requests = Request.new oauth: self
35
35
  def visits = Visits.new oauth: self
36
36
 
37
- # Run a statement, refreshing the access token once if Jobber says it expired.
38
- # @param statement [String] the query or mutation to run.
39
- # @param variables [Hash] the variables it interpolates.
37
+ # Run a statement, waiting for what Jobber will still answer and refreshing a stale token.
40
38
  # @return [Hash] the data Jobber answered, or empty when the credentials are dead.
41
39
  def query(statement, variables: {})
42
- client.query statement, variables: variables
40
+ throttle.wait
41
+ client.query(statement, variables: variables) { |extensions| throttle.read extensions }
43
42
  rescue GraphQL::Unauthorized
44
43
  refresh ? retry : {}
45
44
  end
@@ -65,8 +64,7 @@ module Jbr
65
64
  # @return [String, nil] The client secret to interact with the API.
66
65
  def self.client_secret = ENV['JOBBER_CLIENT_SECRET']
67
66
 
68
- # Exchange a code or a refresh token for credentials. Public because #refresh
69
- # reaches it through self.class, which a private class method forbids.
67
+ # Exchange a code or a refresh token for credentials.
70
68
  def self.post(params = {})
71
69
  uri = URI 'https://api.getjobber.com/api/oauth/token'
72
70
  response = Net::HTTP.post_form uri,
@@ -80,6 +78,8 @@ module Jbr
80
78
 
81
79
  private
82
80
 
81
+ def throttle = @throttle ||= Throttle.new
82
+
83
83
  def refresh
84
84
  output = self.class.post refresh_token: @refresh_token, grant_type: 'refresh_token'
85
85
  @access_token = output[:access_token]
@@ -0,0 +1,42 @@
1
+ module Jbr
2
+ # How much Jobber will still answer, and how long to wait before asking again. Jobber holds
3
+ # an app to two limits at once, and this keeps both: a count of requests over a window, and
4
+ # a bucket of query cost that drains as it is asked and refills at a rate it reports.
5
+ class Throttle
6
+ # Jobber answers 2,500 requests every 5 minutes, which is one every 0.12 seconds. Spacing
7
+ # them is what keeps a walk of many pages under the count, whatever each page costs.
8
+ SPACING = 300.0 / 2_500
9
+
10
+ # @return [Float] the seconds waited, which is zero where nothing was owed.
11
+ def wait
12
+ owed = [ spacing_owed, restore_owed ].max
13
+ sleep owed if owed.positive?
14
+ @asked_at = Time.now
15
+ owed
16
+ end
17
+
18
+ # Takes in what the answer said it had left. Jobber reports the bucket beside the data,
19
+ # so what the next caller may ask for is known before they ask for it.
20
+ # @param extensions [Hash, nil] the +extensions+ Jobber answered beside the data.
21
+ def read(extensions)
22
+ cost = extensions.to_h['cost'].to_h
23
+ status = cost['throttleStatus'].to_h
24
+ @cost = cost['actualQueryCost'].to_f
25
+ @available = status['currentlyAvailable'].to_f
26
+ @restore_rate = status['restoreRate'].to_f
27
+ end
28
+
29
+ private
30
+
31
+ # Nothing is owed to the first caller: a request that follows no other is not too soon.
32
+ def spacing_owed = (@asked_at ? [ SPACING - (Time.now - @asked_at), 0.0 ].max : 0.0)
33
+
34
+ # What the last answer cost is what the next one is taken to cost, since a walk asks the
35
+ # same query of every page. Where the bucket cannot pay for it, wait for it to refill.
36
+ def restore_owed
37
+ return 0.0 unless @restore_rate.to_f.positive? && @available.to_f < @cost.to_f
38
+
39
+ (@cost - @available) / @restore_rate
40
+ end
41
+ end
42
+ end
data/lib/jbr/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # A Ruby client for the Jobber API.
2
2
  module Jbr
3
3
  # The version of this gem.
4
- VERSION = '3.1.0'
4
+ VERSION = '3.2.0'
5
5
  end
data/lib/jbr.rb CHANGED
@@ -21,6 +21,7 @@ require 'jbr/phone'
21
21
  require 'jbr/cliental'
22
22
  require 'jbr/named'
23
23
  require 'jbr/resource'
24
+ require 'jbr/throttle'
24
25
  require 'jbr/request'
25
26
  require 'jbr/oauth'
26
27
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: jbr
3
3
  version: !ruby/object:Gem::Version
4
- version: 3.1.0
4
+ version: 3.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Claudio Baccigalupo
@@ -139,6 +139,7 @@ files:
139
139
  - lib/jbr/quote.rb
140
140
  - lib/jbr/request.rb
141
141
  - lib/jbr/resource.rb
142
+ - lib/jbr/throttle.rb
142
143
  - lib/jbr/url.rb
143
144
  - lib/jbr/version.rb
144
145
  - lib/jbr/visit.rb