Class: Jbr::OAuth
- Inherits:
-
Object
- Object
- Jbr::OAuth
- Defined in:
- lib/jbr/oauth.rb
Constant Summary collapse
- DISCONNECT_MUTATION =
<<~GRAPHQL.freeze mutation Disconnect { appDisconnect { app { name author } userErrors { message } } } GRAPHQL
Instance Attribute Summary collapse
-
#access_token ⇒ Object
readonly
Returns the value of attribute access_token.
-
#account_id ⇒ Object
Returns the value of attribute account_id.
-
#expires_at ⇒ Object
readonly
Returns the value of attribute expires_at.
-
#invalid_at ⇒ Object
readonly
Returns the value of attribute invalid_at.
-
#refresh_token ⇒ Object
readonly
Returns the value of attribute refresh_token.
Class Method Summary collapse
- .client_id ⇒ Object
- .client_secret ⇒ Object
- .create(code:, redirect_uri:) ⇒ Object
- .post(params = {}) ⇒ Object
- .url_for(params = {}) ⇒ Object
Instance Method Summary collapse
- #account ⇒ Object
- #clients ⇒ Object
-
#delete ⇒ Object
Delete a token.
-
#initialize(credentials = {}) ⇒ OAuth
constructor
A new instance of OAuth.
- #invoices ⇒ Object
- #jobs ⇒ Object
- #query(statement, variables: {}) ⇒ Object
- #quotes ⇒ Object
- #requests ⇒ Object
Constructor Details
#initialize(credentials = {}) ⇒ OAuth
Returns a new instance of OAuth.
12 13 14 15 16 17 18 |
# File 'lib/jbr/oauth.rb', line 12 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)
Returns the value of attribute access_token.
20 21 22 |
# File 'lib/jbr/oauth.rb', line 20 def access_token @access_token end |
#account_id ⇒ Object
Returns the value of attribute account_id.
21 22 23 |
# File 'lib/jbr/oauth.rb', line 21 def account_id @account_id end |
#expires_at ⇒ Object (readonly)
Returns the value of attribute expires_at.
20 21 22 |
# File 'lib/jbr/oauth.rb', line 20 def expires_at @expires_at end |
#invalid_at ⇒ Object (readonly)
Returns the value of attribute invalid_at.
20 21 22 |
# File 'lib/jbr/oauth.rb', line 20 def invalid_at @invalid_at end |
#refresh_token ⇒ Object (readonly)
Returns the value of attribute refresh_token.
20 21 22 |
# File 'lib/jbr/oauth.rb', line 20 def refresh_token @refresh_token end |
Class Method Details
.client_id ⇒ Object
68 |
# File 'lib/jbr/oauth.rb', line 68 def self.client_id = ENV['JOBBER_CLIENT_ID'] |
.client_secret ⇒ Object
70 |
# File 'lib/jbr/oauth.rb', line 70 def self.client_secret = ENV['JOBBER_CLIENT_SECRET'] |
.create(code:, redirect_uri:) ⇒ Object
42 43 44 45 |
# File 'lib/jbr/oauth.rb', line 42 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
59 60 61 62 63 64 65 66 |
# File 'lib/jbr/oauth.rb', line 59 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.current + output.fetch('expires_in', 3600).to_i) } end |
.url_for(params = {}) ⇒ Object
78 79 80 81 82 |
# File 'lib/jbr/oauth.rb', line 78 def self.url_for(params = {}) uri = URI 'https://api.getjobber.com/api/oauth/authorize' uri.query ||= params.merge(response_type: 'code', client_id: client_id).to_query uri.to_s end |
Instance Method Details
#delete ⇒ Object
Delete a token. If the token is invalid, do nothing.
37 38 39 40 |
# File 'lib/jbr/oauth.rb', line 37 def delete client.query DISCONNECT_MUTATION rescue GraphQL::Unauthorized => e end |
#query(statement, variables: {}) ⇒ Object
30 31 32 33 34 |
# File 'lib/jbr/oauth.rb', line 30 def query(statement, variables: {}) client.query statement, variables: variables rescue GraphQL::Unauthorized => e refresh ? retry : {} end |