Class: Jbr::OAuth
Overview
Credentials for one Jobber account, and the gateway to all they read or write.
Direct Known Subclasses
Constant Summary collapse
- DISCONNECT_MUTATION =
The mutation that revokes the app on the account.
<<~GRAPHQL mutation Disconnect { appDisconnect { app { name author } userErrors { message } } } GRAPHQL
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
-
#account_id ⇒ String?
The account these credentials reach.
-
#expires_at ⇒ Object
readonly
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
-
#invalid_at ⇒ Object
readonly
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
-
#refresh_token ⇒ Object
readonly
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
Class Method Summary collapse
-
.client_id ⇒ String?
The client ID to interact with the API.
-
.client_secret ⇒ String?
The client secret to interact with the API.
-
.create(code:, redirect_uri:) ⇒ OAuth
Exchange an authorization code for credentials, then learn their account.
-
.post(params = {}) ⇒ Object
Exchange a code or a refresh token for credentials.
Instance Method Summary collapse
-
#account ⇒ Object
The resources these credentials read and write.
- #clients ⇒ Object
-
#delete ⇒ Object
Delete a token.
-
#initialize(credentials = {}) ⇒ OAuth
constructor
A new instance of OAuth.
- #invoices ⇒ Object
- #jobs ⇒ Object
-
#query(statement, variables: {}) ⇒ Hash
Run a statement, refreshing a stale token.
- #quotes ⇒ Object
- #requests ⇒ Object
- #visits ⇒ Object
Constructor Details
#initialize(credentials = {}) ⇒ OAuth
Returns a new instance of OAuth.
18 19 20 21 22 23 24 25 |
# File 'lib/jbr/oauth.rb', line 18 def initialize(credentials = {}) @access_token = credentials[:access_token] @refresh_token = credentials[:refresh_token] @expires_at = credentials[:expires_at] @account_id = credentials[:account_id] @invalid_at = credentials[:invalid_at] @store = credentials[:store] end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
28 29 30 |
# File 'lib/jbr/oauth.rb', line 28 def access_token @access_token end |
#account_id ⇒ String?
Returns the account these credentials reach.
30 31 32 |
# File 'lib/jbr/oauth.rb', line 30 def account_id @account_id end |
#expires_at ⇒ Object (readonly)
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
28 29 30 |
# File 'lib/jbr/oauth.rb', line 28 def expires_at @expires_at end |
#invalid_at ⇒ Object (readonly)
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
28 29 30 |
# File 'lib/jbr/oauth.rb', line 28 def invalid_at @invalid_at end |
#refresh_token ⇒ Object (readonly)
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
28 29 30 |
# File 'lib/jbr/oauth.rb', line 28 def refresh_token @refresh_token end |
Class Method Details
.client_id ⇒ String?
Returns The client ID to interact with the API.
73 |
# File 'lib/jbr/oauth.rb', line 73 def self.client_id = ENV['JOBBER_CLIENT_ID'] |
.client_secret ⇒ String?
Returns The client secret to interact with the API.
76 |
# File 'lib/jbr/oauth.rb', line 76 def self.client_secret = ENV['JOBBER_CLIENT_SECRET'] |
.create(code:, redirect_uri:) ⇒ OAuth
Exchange an authorization code for credentials, then learn their account.
67 68 69 70 |
# File 'lib/jbr/oauth.rb', line 67 def self.create(code:, redirect_uri:) credentials = post code: code, redirect_uri: redirect_uri, grant_type: 'authorization_code' new(credentials).tap { |oauth| oauth.account_id = oauth.account.id } end |
Instance Method Details
#account ⇒ Object
The resources these credentials read and write.
33 |
# File 'lib/jbr/oauth.rb', line 33 def account = Account.new oauth: self |
#delete ⇒ Object
Delete a token. If the token is invalid, do nothing.
58 59 60 61 |
# File 'lib/jbr/oauth.rb', line 58 def delete client.query DISCONNECT_MUTATION rescue GraphQL::Unauthorized => e end |
#query(statement, variables: {}) ⇒ Hash
Run a statement, refreshing a stale token. Nothing here ever sleeps: where Jobber holds the app to a limit it says so, and a caller asking from a background job has a queue that will bring the whole job back later — which is worth more than a worker asleep holding a transaction open.
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/jbr/oauth.rb', line 46 def query(statement, variables: {}) client.query statement, variables: variables rescue GraphQL::Unauthorized refresh ? retry : {} rescue GraphQL::Error => error # The transport's own class never leaves the gem: a caller told to rescue `Jbr::Error` # was not catching a throttle, a 500 or an unreadable answer, and had its own job blow # up instead of hearing that Jobber would not answer. raise Error, error. end |