line-social 1.0.0 → 1.1.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: bddb24a7a0f87bbfb1c0da8cace65bc2fbeff831aeeed14302b36475d7b740ac
4
- data.tar.gz: 97cc163c482394a8dfb20742dcf712c289637f50e9c4a3489f03cf699c9da485
3
+ metadata.gz: 90050c8733508e2a727c8ee60b45cd7692f6a1e06e8659676f676a0308d22097
4
+ data.tar.gz: 6f27f570586db27ff6c7aa9f87269ae498ecbb29c4b27ab0ed21e0ff62f5595f
5
5
  SHA512:
6
- metadata.gz: d3a14f639e1731790ebcc7943132575e202aea638c57d938264bf364eb2191e0a35538963e994719909fe0ec230ccc932764eb345fa876059ab7574881ef9b66
7
- data.tar.gz: 7e9aaf92b9c30dcb75a041f176f52a6b6083f9980b00d212e07fd8364ca3e05a3a56ea8f9a6cd1e07479ad9b9bdf69072501fb5ca8cd5375016176d25c1d465e
6
+ metadata.gz: 39d6b3be949dc4e79a9eff9ebc129c1b3078dab85c9de30845fa108c3b373a2c9d051bc81e9893c07c2c524c29dbd58cc53cb96f4c9df7b786823288596cb055
7
+ data.tar.gz: c01a91cb3b706d5fabe75bf919c4de5505aad78a5f3156a06befb54465279e4dd52ef30ef57039b6f3f9fb25db4f2e484071bfc396c7bac2c13653d8162a44cd
@@ -1,6 +1,10 @@
1
1
  module Line
2
2
  module Social
3
3
  class Error < StandardError; end
4
- class NotImplementedError < Error; end
4
+ class BadRequestError < Error; end
5
+ class UnauthorizedError < Error; end
6
+ class ForbiddenError < Error; end
7
+ class TooManyRequestsError < Error; end
8
+ class InternalServerError < Error; end
5
9
  end
6
10
  end
@@ -2,12 +2,43 @@ module Line
2
2
  module Social
3
3
  module Request
4
4
  class Base
5
- def http_client
6
- Faraday.new do |connection|
5
+ def http_client(method, &block)
6
+ client = Faraday.new do |connection|
7
7
  connection.response :json, content_type: /\bjson$/
8
+ connection.response :raise_error
8
9
  connection.adapter Faraday.default_adapter
9
10
  end
11
+
12
+ client.run_request(method, nil, nil, nil, &block)
13
+ rescue Faraday::Error::ClientError => e
14
+ error_message = JSON.parse(e.response[:body])["message"]
15
+
16
+ case e.response[:status]
17
+ when 400
18
+ raise Line::Social::BadRequestError.new(error_message)
19
+ when 401
20
+ raise Line::Social::UnauthorizedError.new(error_message)
21
+ when 403
22
+ raise Line::Social::ForbiddenError.new(error_message)
23
+ when 429
24
+ raise Line::Social::TooManyRequestsError.new(error_message)
25
+ when 500
26
+ raise Line::Social::InternalServerError.new(error_message)
27
+ end
10
28
  end
29
+
30
+ # Will be uncommented below after releasing Faraday 1.0
31
+ # rescue Faraday::BadRequestError => e
32
+ # raise Line::Social::BadRequestError.new(error_message)
33
+ # rescue Faraday::UnauthorizedError => e
34
+ # raise Line::Social::UnauthorizedError.new(error_message)
35
+ # rescue Faraday::ForbiddenError => e
36
+ # raise Line::Social::ForbiddenError.new(error_message)
37
+ # rescue Faraday::ClientError => e
38
+ # raise Line::Social::TooManyRequestsError.new(error_message)
39
+ # rescue Faraday::ServerError => e
40
+ # raise Line::Social::InternalServerError.new(error_message)
41
+ # end
11
42
  end
12
43
  end
13
44
  end
@@ -5,18 +5,9 @@ module Line
5
5
  API_URI = URI.parse("https://api.line.me/friendship/v1")
6
6
 
7
7
  def get(access_token)
8
- response = http_client.get do |request|
8
+ response = http_client(:get) do |request|
9
9
  request.url "#{API_URI}/status"
10
- request.headers["Authorization"] = "Bearer #{access_token}"
11
- end
12
-
13
- if response.body["error"]
14
- raise Line::Social::Error.new(response.body["error_description"])
15
- end
16
-
17
- # Case of "{"message"=>"There is no login bot linked to this channel."}"
18
- if response.body["message"]
19
- raise Line::Social::Error.new(response.body["message"])
10
+ request.headers["Authorization"] = "Bearer #{access_token}"
20
11
  end
21
12
 
22
13
  Line::Social::Friendship.new(response.body)
@@ -10,7 +10,7 @@ module Line
10
10
  end
11
11
 
12
12
  def issue(code:, redirect_uri:)
13
- response = http_client.post do |request|
13
+ response = http_client(:post) do |request|
14
14
  request.url "#{API_URI}/token"
15
15
  request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
16
16
  request.body = URI.encode_www_form(
@@ -22,25 +22,20 @@ module Line
22
22
  )
23
23
  end
24
24
 
25
- if response.body["error"]
26
- raise Line::Social::Error.new(response.body["error_description"])
27
- end
28
-
29
25
  Line::Social::Oauth.new(response.body.merge(client_id: @client_id, client_secret: @client_secret))
30
26
  end
31
27
 
32
28
  def verify(access_token)
33
- response = http_client.get("#{API_URI}/verify", access_token: access_token)
34
-
35
- if response.body["error"]
36
- raise Line::Social::Error.new(response.body["error_description"])
29
+ response = http_client(:get) do |request|
30
+ request.url "#{API_URI}/verify"
31
+ request.params["access_token"] = access_token
37
32
  end
38
33
 
39
34
  Line::Social::Oauth.new(response.body.merge(client_id: @client_id, client_secret: @client_secret))
40
35
  end
41
36
 
42
37
  def refresh(refresh_token)
43
- response = http_client.post do |request|
38
+ response = http_client(:post) do |request|
44
39
  request.url "#{API_URI}/token"
45
40
  request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
46
41
  request.body = URI.encode_www_form(
@@ -51,15 +46,11 @@ module Line
51
46
  )
52
47
  end
53
48
 
54
- if response.body["error"]
55
- raise Line::Social::Error.new(response.body["error_description"])
56
- end
57
-
58
49
  Line::Social::Oauth.new(response.body.merge(client_id: @client_id, client_secret: @client_secret))
59
50
  end
60
51
 
61
52
  def revoke(access_token)
62
- response = http_client.post do |request|
53
+ response = http_client(:post) do |request|
63
54
  request.url "#{API_URI}/revoke"
64
55
  request.headers['Content-Type'] = 'application/x-www-form-urlencoded'
65
56
  request.body = URI.encode_www_form(
@@ -5,15 +5,11 @@ module Line
5
5
  API_URI = URI.parse("https://api.line.me/v2")
6
6
 
7
7
  def get(access_token)
8
- response = http_client.get do |request|
8
+ response = http_client(:get) do |request|
9
9
  request.url "#{API_URI}/profile"
10
10
  request.headers["Authorization"] = "Bearer #{access_token}"
11
11
  end
12
12
 
13
- if response.body["error"]
14
- raise Line::Social::Error.new(response.body["error_description"])
15
- end
16
-
17
13
  Line::Social::Profile.new(response.body)
18
14
  end
19
15
  end
@@ -1,5 +1,5 @@
1
1
  module Line
2
2
  module Social
3
- VERSION = "1.0.0"
3
+ VERSION = "1.1.0"
4
4
  end
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: line-social
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.1.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - camelmasa
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-13 00:00:00.000000000 Z
11
+ date: 2019-04-25 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday