Class: Jbr::OAuth
- Inherits:
-
Object
- Object
- Jbr::OAuth
- Defined in:
- lib/jbr/oauth.rb
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 the access token once if Jobber says it expired.
- #quotes ⇒ Object
- #requests ⇒ Object
- #visits ⇒ Object
Constructor Details
#initialize(credentials = {}) ⇒ OAuth
Returns a new instance of OAuth.
15 16 17 18 19 20 21 |
# File 'lib/jbr/oauth.rb', line 15 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] end |
Instance Attribute Details
#access_token ⇒ Object (readonly)
The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.
24 25 26 |
# File 'lib/jbr/oauth.rb', line 24 def access_token @access_token end |
#account_id ⇒ String?
Returns the account these credentials reach.
26 27 28 |
# File 'lib/jbr/oauth.rb', line 26 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.
24 25 26 |
# File 'lib/jbr/oauth.rb', line 24 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.
24 25 26 |
# File 'lib/jbr/oauth.rb', line 24 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.
24 25 26 |
# File 'lib/jbr/oauth.rb', line 24 def refresh_token @refresh_token end |
Class Method Details
.client_id ⇒ String?
Returns The client ID to interact with the API.
63 |
# File 'lib/jbr/oauth.rb', line 63 def self.client_id = ENV['JOBBER_CLIENT_ID'] |
.client_secret ⇒ String?
Returns The client secret to interact with the API.
66 |
# File 'lib/jbr/oauth.rb', line 66 def self.client_secret = ENV['JOBBER_CLIENT_SECRET'] |
.create(code:, redirect_uri:) ⇒ OAuth
Exchange an authorization code for credentials, then learn their account.
57 58 59 60 |
# File 'lib/jbr/oauth.rb', line 57 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 |
.post(params = {}) ⇒ Object
Exchange a code or a refresh token for credentials. Public because #refresh reaches it through self.class, which a private class method forbids.
70 71 72 73 74 75 76 77 78 79 |
# File 'lib/jbr/oauth.rb', line 70 def self.post(params = {}) uri = URI 'https://api.getjobber.com/api/oauth/token' response = Net::HTTP.post_form uri, params.merge(client_id: client_id, client_secret: client_secret) raise Error, response.body unless response.is_a? Net::HTTPSuccess output = JSON.parse(response.body) { access_token: output['access_token'], refresh_token: output['refresh_token'], expires_at: (Time.now + output.fetch('expires_in', 3600).to_i), } end |
Instance Method Details
#account ⇒ Object
The resources these credentials read and write.
29 |
# File 'lib/jbr/oauth.rb', line 29 def account = Account.new oauth: self |
#delete ⇒ Object
Delete a token. If the token is invalid, do nothing.
48 49 50 51 |
# File 'lib/jbr/oauth.rb', line 48 def delete client.query DISCONNECT_MUTATION rescue GraphQL::Unauthorized => e end |
#query(statement, variables: {}) ⇒ Hash
Run a statement, refreshing the access token once if Jobber says it expired.
41 42 43 44 45 |
# File 'lib/jbr/oauth.rb', line 41 def query(statement, variables: {}) client.query statement, variables: variables rescue GraphQL::Unauthorized refresh ? retry : {} end |