Class: Jbr::Token

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

Overview

The endpoint that trades an authorization code or a refresh token for credentials.

Constant Summary collapse

URL =

Where a grant is exchanged.

'https://api.getjobber.com/api/oauth/token'
REFUSAL =

What Jobber calls a grant that is no good, in the OAuth 2 word for it.

'invalid_grant'

Class Method Summary collapse

Class Method Details

.post(params = {}) ⇒ Hash

Trade a grant for credentials.

Parameters:

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

    the grant, and the app making the exchange.

Returns:

  • (Hash)

    the tokens, and the moment the access one expires.

Raises:

  • (Refused)

    where Jobber says the grant itself is no good.

  • (Error)

    where Jobber could not answer about it.



15
16
17
18
19
20
21
22
23
24
# File 'lib/jbr/token.rb', line 15

def self.post(params = {})
  response = Net::HTTP.post_form URI(URL), params
  raise Refused, response.body if refused? response
  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