Class: LineRepository

Inherits:
Object
  • Object
show all
Defined in:
app/repositories/line_repository.rb

Constant Summary collapse

LINE_BASE_HEADERS =
{
  'Content-Type': "application/x-www-form-urlencoded"
}.freeze
LINE_TOKEN_URL =
"https://api.line.me/oauth2/v2.1/token"
LINE_INFORMATION_URL =
"https://api.line.me/v2/profile"

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.singletonObject



11
12
13
# File 'app/repositories/line_repository.rb', line 11

def self.singleton
  @singleton ||= new
end

Instance Method Details

#retrieve_access_token(redirect_uri:, code:) ⇒ Object



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'app/repositories/line_repository.rb', line 15

def retrieve_access_token(redirect_uri:, code:)
  response = HTTParty.post(
    LINE_TOKEN_URL,
    headers: LINE_BASE_HEADERS,
    body: URI.encode_www_form({
      grant_type: "authorization_code",
      code: code,
      redirect_uri: redirect_uri,
      client_id: Ibrain::Auth::Config.line_client_id,
      client_secret: Ibrain::Auth::Config.line_client_secret
    })
  )

  response.try(:fetch, "access_token", nil)
end

#retrieve_uid(redirect_uri:, code:) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/repositories/line_repository.rb', line 31

def retrieve_uid(redirect_uri:, code:)
  token = retrieve_access_token(
    redirect_uri: redirect_uri,
    code: code
  )

  response = HTTParty.get(
    LINE_INFORMATION_URL,
    headers: LINE_BASE_HEADERS.merge({
      'Authorization' => "Bearer #{token}"
    })
  )

  response.try(:fetch, 'userId', nil)
end

#retrieve_uid_by_access_token(access_token:) ⇒ Object



47
48
49
50
51
52
53
54
55
56
# File 'app/repositories/line_repository.rb', line 47

def retrieve_uid_by_access_token(access_token:)
  response = HTTParty.get(
    LINE_INFORMATION_URL,
    headers: LINE_BASE_HEADERS.merge({
      'Authorization' => "Bearer #{access_token}"
    })
  )

  response.try(:fetch, 'userId', nil)
end