Class: MyInfo::V3::Token
- Inherits:
-
Api
- Object
- Api
- MyInfo::V3::Token
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
#code ⇒ Object
Returns the value of attribute code.
7
8
9
|
# File 'lib/myinfo/v3/token.rb', line 7
def code
@code
end
|
#state ⇒ Object
Returns the value of attribute state.
7
8
9
|
# File 'lib/myinfo/v3/token.rb', line 7
def state
@state
end
|
Instance Method Details
#call ⇒ Object
14
15
16
17
18
19
20
21
|
# File 'lib/myinfo/v3/token.rb', line 14
def call
super do
= (params: params).merge({ 'Content-Type' => 'application/x-www-form-urlencoded' })
response = http.request_post("/#{slug}", params.to_param, )
parse_response(response)
end
end
|
#errors ⇒ Object
44
45
46
|
# File 'lib/myinfo/v3/token.rb', line 44
def errors
%w[400 401]
end
|
#http_method ⇒ Object
23
24
25
|
# File 'lib/myinfo/v3/token.rb', line 23
def http_method
'POST'
end
|
#params ⇒ Object
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
|
#slug ⇒ Object
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
|