Class: MyInfo::V3::Token

Inherits:
Api
  • Object
show all
Defined in:
lib/myinfo/v3/token.rb

Overview

Called after authorise to obtain a token for API calls

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Api

#endpoint, #header, #support_gzip?

Constructor Details

#initialize(code:, state: nil) ⇒ Token

Returns a new instance of Token.



9
10
11
12
# File 'lib/myinfo/v3/token.rb', line 9

def initialize(code:, state: nil)
  @code = code
  @state = state
end

Instance Attribute Details

#codeObject

Returns the value of attribute code.



7
8
9
# File 'lib/myinfo/v3/token.rb', line 7

def code
  @code
end

#stateObject

Returns the value of attribute state.



7
8
9
# File 'lib/myinfo/v3/token.rb', line 7

def state
  @state
end

Instance Method Details

#callObject



14
15
16
17
18
19
20
21
# File 'lib/myinfo/v3/token.rb', line 14

def call
  super do
    headers = header(params: params).merge({ 'Content-Type' => 'application/x-www-form-urlencoded' })
    response = http.request_post("/#{slug}", params.to_param, headers)

    parse_response(response)
  end
end

#errorsObject



44
45
46
# File 'lib/myinfo/v3/token.rb', line 44

def errors
  %w[400 401]
end

#http_methodObject



23
24
25
# File 'lib/myinfo/v3/token.rb', line 23

def http_method
  'POST'
end

#paramsObject



33
34
35
36
37
38
39
40
41
42
# File 'lib/myinfo/v3/token.rb', line 33

def params
  {
    code: code,
    state: state,
    client_id: config.client_id,
    client_secret: config.client_secret,
    grant_type: 'authorization_code',
    redirect_uri: config.redirect_uri
  }.compact
end

#parse_response(response) ⇒ Object



48
49
50
51
52
53
54
55
# File 'lib/myinfo/v3/token.rb', line 48

def parse_response(response)
  super do
    json = JSON.parse(response.body)
    access_token = json['access_token']

    Response.new(success: true, data: access_token)
  end
end

#slugObject



27
28
29
30
31
# File 'lib/myinfo/v3/token.rb', line 27

def slug
  slug_prefix = config.public? ? 'com' : 'gov'

  "#{slug_prefix}/v3/token"
end