Class: Line::Social::Request::Oauth

Inherits:
Base
  • Object
show all
Defined in:
lib/line/social/request/oauth.rb

Constant Summary collapse

API_URI =
URI.parse("https://api.line.me/oauth2/v2.1")

Instance Method Summary collapse

Methods inherited from Base

#http_client

Constructor Details

#initialize(client_id:, client_secret:) ⇒ Oauth

Returns a new instance of Oauth.



7
8
9
10
# File 'lib/line/social/request/oauth.rb', line 7

def initialize(client_id:, client_secret:)
  @client_id = client_id
  @client_secret = client_secret
end

Instance Method Details

#issue(code:, redirect_uri:) ⇒ Object



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
# File 'lib/line/social/request/oauth.rb', line 12

def issue(code:, redirect_uri:)
  response = http_client(:post) do |request|
    request.url "#{API_URI}/token"
    request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    request.body = URI.encode_www_form(
      client_id: @client_id,
      client_secret: @client_secret,
      code: code,
      grant_type: "authorization_code",
      redirect_uri: redirect_uri,
    )
  end

  Line::Social::Oauth.new(response.body.merge(client_id: @client_id, client_secret: @client_secret))
end

#refresh(refresh_token) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/line/social/request/oauth.rb', line 37

def refresh(refresh_token)
  response = http_client(:post) do |request|
    request.url "#{API_URI}/token"
    request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    request.body = URI.encode_www_form(
      client_id: @client_id,
      client_secret: @client_secret,
      grant_type: "refresh_token",
      refresh_token: refresh_token,
    )
  end

  Line::Social::Oauth.new(response.body.merge(client_id: @client_id, client_secret: @client_secret))
end

#revoke(access_token) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
# File 'lib/line/social/request/oauth.rb', line 52

def revoke(access_token)
  response = http_client(:post) do |request|
    request.url "#{API_URI}/revoke"
    request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
    request.body = URI.encode_www_form(
      access_token: access_token,
      client_id: @client_id,
      client_secret: @client_secret
    )
  end

  response.status == 200
end

#verify(access_token) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/line/social/request/oauth.rb', line 28

def verify(access_token)
  response = http_client(:get) do |request|
    request.url "#{API_URI}/verify"
    request.params["access_token"] = access_token
  end

  Line::Social::Oauth.new(response.body.merge(client_id: @client_id, client_secret: @client_secret))
end