Class: Jbr::OAuth

Inherits:
Object
  • Object
show all
Includes:
Asking
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

Constants included from Asking

Asking::ATTEMPTS

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Asking

#query

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.



17
18
19
20
21
22
23
# File 'lib/jbr/oauth.rb', line 17

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.



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

def access_token
  @access_token
end

#account_idString?

Returns the account these credentials reach.

Returns:

  • (String, nil)

    the account these credentials reach.



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

def 
  @account_id
end

#expires_atObject (readonly)

The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.



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

def expires_at
  @expires_at
end

#invalid_atObject (readonly)

The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.



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

def invalid_at
  @invalid_at
end

#refresh_tokenObject (readonly)

The credentials as Jobber last gave them, plus the moment a refusal to refresh landed.



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

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.



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

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.



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

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.



49
50
51
52
# File 'lib/jbr/oauth.rb', line 49

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.



61
62
63
# File 'lib/jbr/oauth.rb', line 61

def self.post(params = {})
  Token.post params.merge(client_id: client_id, client_secret: client_secret)
end

Instance Method Details

#accountObject

The resources these credentials read and write.



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

def  = Account.new oauth: self

#clientsObject



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

def clients = Client.new oauth: self

#deleteObject

Delete a token. If the token is invalid, do nothing.



40
41
42
43
# File 'lib/jbr/oauth.rb', line 40

def delete
  client.query DISCONNECT_MUTATION
rescue GraphQL::Unauthorized => e
end

#invoicesObject



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

def invoices = Invoice.new oauth: self

#jobsObject



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

def jobs = Jobs.new oauth: self

#quotesObject



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

def quotes = Quote.new oauth: self

#requestsObject



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

def requests = Request.new oauth: self

#visitsObject



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

def visits = Visits.new oauth: self