Class: Jbr::OAuth

Inherits:
Object
  • Object
show all
Defined in:
lib/jbr/oauth.rb

Overview

Credentials for one Jobber account, and the gateway to all they read or write.

Direct Known Subclasses

Mock::OAuth

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(credentials = {}) ⇒ OAuth

Returns a new instance of OAuth.

Parameters:

  • credentials (Hash) (defaults to: {})

    the tokens, their expiry, the account and when it went bad.



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_tokenObject (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_idString?

Returns the account these credentials reach.

Returns:

  • (String, nil)

    the account these credentials reach.



26
27
28
# File 'lib/jbr/oauth.rb', line 26

def 
  @account_id
end

#expires_atObject (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_atObject (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_tokenObject (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_idString?

Returns The client ID to interact with the API.

Returns:

  • (String, nil)

    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_secretString?

Returns The client secret to interact with the API.

Returns:

  • (String, nil)

    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.

Parameters:

  • code (String)

    the code Jobber sent to the redirect URI.

  • redirect_uri (String)

    the URI the code came back to.

Returns:

  • (OAuth)

    the new credentials.



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. = oauth..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.

Raises:



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

#accountObject

The resources these credentials read and write.



29
# File 'lib/jbr/oauth.rb', line 29

def  = Account.new oauth: self

#clientsObject



30
# File 'lib/jbr/oauth.rb', line 30

def clients = Client.new oauth: self

#deleteObject

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

#invoicesObject



31
# File 'lib/jbr/oauth.rb', line 31

def invoices = Invoice.new oauth: self

#jobsObject



32
# File 'lib/jbr/oauth.rb', line 32

def jobs = Job.new oauth: self

#query(statement, variables: {}) ⇒ Hash

Run a statement, refreshing the access token once if Jobber says it expired.

Parameters:

  • statement (String)

    the query or mutation to run.

  • variables (Hash) (defaults to: {})

    the variables it interpolates.

Returns:

  • (Hash)

    the data Jobber answered, or empty when the credentials are dead.



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

#quotesObject



33
# File 'lib/jbr/oauth.rb', line 33

def quotes = Quote.new oauth: self

#requestsObject



34
# File 'lib/jbr/oauth.rb', line 34

def requests = Request.new oauth: self

#visitsObject



35
# File 'lib/jbr/oauth.rb', line 35

def visits = Visit.new oauth: self