Class: TimeDoctor::Token

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

Constant Summary collapse

ENTRY =
'https://webapi.timedoctor.com/oauth/v2/token'.freeze

Class Method Summary collapse

Class Method Details

.authorize(config) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
# File 'lib/timedoctor/token.rb', line 6

def authorize(config)
  response = Faraday.get ENTRY,
                         client_id:     config[:client_id],
                         client_secret: config[:client_secret],
                         code:          config[:code],
                         redirect_uri:  config[:redirect_uri],
                         grant_type:    :authorization_code,
                         _format:       :json

  case response.status
  when 200
    data = JSON.parse(response.body, symbolize_names: true)
    config[:access_token]  = data[:access_token]
    config[:refresh_token] = data[:refresh_token]
    config[:on_token_authorize].call(data, config)
  when 400
    data = JSON.parse(response.body, symbolize_names: true)
    config[:on_token_authorize_error].call(data, config)
  else
    raise UnknownError, response
  end
end

.refresh(config) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/timedoctor/token.rb', line 29

def refresh(config)
  response = Faraday.get ENTRY,
                         refresh_token: config[:refresh_token],
                         client_id:     config[:client_id],
                         client_secret: config[:client_secret],
                         grant_type:    :refresh_token,
                         _format:       :json

  case response.status
  when 200
    data = JSON.parse(response.body, symbolize_names: true)
    config[:access_token]  = data[:access_token]
    config[:refresh_token] = data[:refresh_token]
    config[:on_token_refresh].call(data, config)
  when 400
    data = JSON.parse(response.body, symbolize_names: true)
    config[:on_token_refresh_error].call(data, config)
  else
    raise UnknownError, response
  end
end