twitchrb 1.3.0 → 1.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 6774429fbfacd30d1308f5240f54d31bddfc9e5511ca11a5318317d8ecd426fe
4
- data.tar.gz: 8483a2e2a75269d40a6fcc56e1206dfc59a4033ef6d4d8bb679aaca6ab1e72dc
3
+ metadata.gz: 1a745f6e1fdd82794a6bc9b3aeb7e4f374efa7e50b8751f357fd23aa48ef7912
4
+ data.tar.gz: 7f08650f12bfb36269f359653ecfa23011f8d595ab6c84a2d284f87157ea5b17
5
5
  SHA512:
6
- metadata.gz: 36c6bea9d2c2e8e1ff5eab8d3daf2edc0b2edc1047bb65f5d38c77b78b9fcba7333dc628593c5d5e9aa6cc2a0543115738f7a437200867a4d7186c0edd5e01c6
7
- data.tar.gz: f775ef2e85b39651f8c4fcea07791badce94086b3129f52dab6d44bf08456a1083d2b805bb3b6ef2c3eb965ae1178190623af7f243109042dc7ae0b10b0dd1a9
6
+ metadata.gz: 566698cf2d321eadd523abf2777ff808fbce5f6d896b0a7e7db65af24f5643b38bc21be87a846f507b51d551f4f34f0ddfe2b85ab3d608585ac5914c7b4cecdf
7
+ data.tar.gz: d7ee4125e0cfb8f523a9b4bf3ada19f477fdd8d5865ac8801246864eb99ad5118481814ac8f5d44d1f8922bb578848d8033228156c91f2d83ce768e1171d7d37
@@ -1,5 +1,11 @@
1
1
  name: CI
2
- on: push
2
+ on:
3
+ push:
4
+ branches:
5
+ - main
6
+ pull_request:
7
+ branches:
8
+ - main
3
9
  jobs:
4
10
  lint:
5
11
  runs-on: ubuntu-latest
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- twitchrb (1.3.0)
4
+ twitchrb (1.4.0)
5
5
  faraday (~> 2.0)
6
6
 
7
7
  GEM
@@ -22,51 +22,53 @@ GEM
22
22
  bigdecimal (3.1.8)
23
23
  concurrent-ruby (1.3.3)
24
24
  connection_pool (2.4.1)
25
- dotenv (2.7.6)
25
+ dotenv (3.1.2)
26
26
  drb (2.2.1)
27
- faraday (2.9.2)
27
+ faraday (2.10.1)
28
28
  faraday-net_http (>= 2.0, < 3.2)
29
- faraday-net_http (3.1.0)
29
+ logger
30
+ faraday-net_http (3.1.1)
30
31
  net-http
31
32
  i18n (1.14.5)
32
33
  concurrent-ruby (~> 1.0)
33
34
  json (2.7.2)
34
35
  language_server-protocol (3.17.0.3)
35
- minitest (5.15.0)
36
+ logger (1.6.0)
37
+ minitest (5.24.1)
36
38
  mutex_m (0.2.0)
37
39
  net-http (0.4.1)
38
40
  uri
39
41
  parallel (1.25.1)
40
- parser (3.3.3.0)
42
+ parser (3.3.4.0)
41
43
  ast (~> 2.4.1)
42
44
  racc
43
- racc (1.8.0)
44
- rack (3.1.3)
45
+ racc (1.8.1)
46
+ rack (3.1.7)
45
47
  rainbow (3.1.1)
46
48
  rake (12.3.3)
47
49
  regexp_parser (2.9.2)
48
- rexml (3.3.0)
50
+ rexml (3.3.4)
49
51
  strscan
50
- rubocop (1.64.1)
52
+ rubocop (1.65.1)
51
53
  json (~> 2.3)
52
54
  language_server-protocol (>= 3.17.0)
53
55
  parallel (~> 1.10)
54
56
  parser (>= 3.3.0.2)
55
57
  rainbow (>= 2.2.2, < 4.0)
56
- regexp_parser (>= 1.8, < 3.0)
58
+ regexp_parser (>= 2.4, < 3.0)
57
59
  rexml (>= 3.2.5, < 4.0)
58
60
  rubocop-ast (>= 1.31.1, < 2.0)
59
61
  ruby-progressbar (~> 1.7)
60
62
  unicode-display_width (>= 2.4.0, < 3.0)
61
63
  rubocop-ast (1.31.3)
62
64
  parser (>= 3.3.1.0)
63
- rubocop-minitest (0.35.0)
65
+ rubocop-minitest (0.35.1)
64
66
  rubocop (>= 1.61, < 2.0)
65
67
  rubocop-ast (>= 1.31.1, < 2.0)
66
- rubocop-performance (1.21.0)
68
+ rubocop-performance (1.21.1)
67
69
  rubocop (>= 1.48.1, < 2.0)
68
70
  rubocop-ast (>= 1.31.1, < 2.0)
69
- rubocop-rails (2.25.0)
71
+ rubocop-rails (2.25.1)
70
72
  activesupport (>= 4.2.0)
71
73
  rack (>= 1.1)
72
74
  rubocop (>= 1.33.0, < 2.0)
@@ -96,4 +98,4 @@ DEPENDENCIES
96
98
  vcr
97
99
 
98
100
  BUNDLED WITH
99
- 2.3.21
101
+ 2.5.17
@@ -0,0 +1,128 @@
1
+ module Twitch
2
+ class ErrorGenerator < StandardError
3
+ attr_reader :http_status_code
4
+ attr_reader :twitch_error_code
5
+ attr_reader :twitch_error_message
6
+
7
+ def initialize(response_body, http_status_code)
8
+ @response_body = response_body
9
+ @http_status_code = http_status_code
10
+ set_twitch_error_values
11
+ super(build_message)
12
+ end
13
+
14
+ private
15
+
16
+ def set_twitch_error_values
17
+ @twitch_error_code = @response_body.dig("error")
18
+ @twitch_error_message = @response_body.dig("message")
19
+ end
20
+
21
+ def error_message
22
+ @twitch_error_message || @response_body.dig("error")
23
+ rescue NoMethodError
24
+ "An unknown error occurred."
25
+ end
26
+
27
+ def build_message
28
+ if twitch_error_code.nil?
29
+ return "Error #{@http_status_code}: #{error_message}"
30
+ end
31
+ "Error #{@http_status_code}: #{error_message} '#{twitch_error_message}'"
32
+ end
33
+ end
34
+
35
+ module Errors
36
+ class BadRequestError < ErrorGenerator
37
+ private
38
+
39
+ def error_message
40
+ "Your request was malformed."
41
+ end
42
+ end
43
+
44
+ class AuthenticationMissingError < ErrorGenerator
45
+ private
46
+
47
+ def error_message
48
+ "You did not supply valid authentication credentials."
49
+ end
50
+ end
51
+
52
+ class ForbiddenError < ErrorGenerator
53
+ private
54
+
55
+ def error_message
56
+ "You are not allowed to perform that action."
57
+ end
58
+ end
59
+
60
+ class EntityNotFoundError < ErrorGenerator
61
+ private
62
+
63
+ def error_message
64
+ "No results were found for your request."
65
+ end
66
+ end
67
+
68
+ class ConflictError < ErrorGenerator
69
+ private
70
+
71
+ def error_message
72
+ "Your request was a conflict."
73
+ end
74
+ end
75
+
76
+ class TooManyRequestsError < ErrorGenerator
77
+ private
78
+
79
+ def error_message
80
+ "Your request exceeded the API rate limit."
81
+ end
82
+ end
83
+
84
+ class InternalError < ErrorGenerator
85
+ private
86
+
87
+ def error_message
88
+ "We were unable to perform the request due to server-side problems."
89
+ end
90
+ end
91
+
92
+ class ServiceUnavailableError < ErrorGenerator
93
+ private
94
+
95
+ def error_message
96
+ "You have been rate limited for sending more than 20 requests per second."
97
+ end
98
+ end
99
+
100
+ class NotImplementedError < ErrorGenerator
101
+ private
102
+
103
+ def error_message
104
+ "This resource has not been implemented."
105
+ end
106
+ end
107
+ end
108
+
109
+ class ErrorFactory
110
+ HTTP_ERROR_MAP = {
111
+ 400 => Errors::BadRequestError,
112
+ 401 => Errors::AuthenticationMissingError,
113
+ 403 => Errors::ForbiddenError,
114
+ 404 => Errors::EntityNotFoundError,
115
+ 409 => Errors::ConflictError,
116
+ 429 => Errors::TooManyRequestsError,
117
+ 500 => Errors::InternalError,
118
+ 503 => Errors::ServiceUnavailableError,
119
+ 501 => Errors::NotImplementedError
120
+ }.freeze
121
+
122
+ def self.create(response_body, http_status_code)
123
+ status = http_status_code
124
+ error_class = HTTP_ERROR_MAP[status] || ErrorGenerator
125
+ error_class.new(response_body, http_status_code) if error_class
126
+ end
127
+ end
128
+ end
@@ -29,31 +29,20 @@ module Twitch
29
29
  end
30
30
 
31
31
  def handle_response(response)
32
- case response.status
33
- when 400
34
- raise Error, "Error 400: Your request was malformed. '#{response.body["message"]}'"
35
- when 401
36
- raise Error, "Error 401: You did not supply valid authentication credentials. '#{response.body["error"]}'"
37
- when 403
38
- raise Error, "Error 403: You are not allowed to perform that action. '#{response.body["error"]}'"
39
- when 404
40
- raise Error, "Error 404: No results were found for your request. '#{response.body["error"]}'"
41
- when 409
42
- raise Error, "Error 409: Your request was a conflict. '#{response.body["message"]}'"
43
- when 422
44
- raise Error, "Error 422: Unprocessable Entity. '#{response.body["message"]}"
45
- when 429
46
- raise Error, "Error 429: Your request exceeded the API rate limit. '#{response.body["error"]}'"
47
- when 500
48
- raise Error, "Error 500: We were unable to perform the request due to server-side problems. '#{response.body["error"]}'"
49
- when 503
50
- raise Error, "Error 503: You have been rate limited for sending more than 20 requests per second. '#{response.body["error"]}'"
51
- when 204
52
- # 204 is a response for success on Twitch's API
53
- return true
54
- end
55
-
56
- response
32
+ return true if response.status == 204
33
+ return response unless error?(response)
34
+
35
+ raise_error(response)
36
+ end
37
+
38
+ def error?(response)
39
+ [ 400, 401, 403, 404, 409, 429, 500, 501, 503 ].include?(response.status) ||
40
+ response.body&.key?("error")
41
+ end
42
+
43
+ def raise_error(response)
44
+ error = Twitch::ErrorFactory.create(response.body, response.status)
45
+ raise error if error
57
46
  end
58
47
  end
59
48
  end
@@ -1,3 +1,3 @@
1
1
  module Twitch
2
- VERSION = "1.3.0"
2
+ VERSION = "1.4.0"
3
3
  end
data/lib/twitch.rb CHANGED
@@ -1,11 +1,16 @@
1
1
  require "faraday"
2
2
  require "json"
3
+ require "ostruct"
3
4
  require "twitch/version"
4
5
 
5
6
  module Twitch
7
+ autoload :Error, "twitch/error"
8
+ autoload :Errors, "twitch/error_generator"
9
+ autoload :ErrorGenerator, "twitch/error_generator"
10
+ autoload :ErrorFactory, "twitch/error_generator"
11
+
6
12
  autoload :Client, "twitch/client"
7
13
  autoload :Collection, "twitch/collection"
8
- autoload :Error, "twitch/error"
9
14
  autoload :Resource, "twitch/resource"
10
15
  autoload :Object, "twitch/object"
11
16
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: twitchrb
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.3.0
4
+ version: 1.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Dean Perry
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2024-06-28 00:00:00.000000000 Z
11
+ date: 2024-08-02 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: faraday
@@ -47,6 +47,7 @@ files:
47
47
  - lib/twitch/client.rb
48
48
  - lib/twitch/collection.rb
49
49
  - lib/twitch/error.rb
50
+ - lib/twitch/error_generator.rb
50
51
  - lib/twitch/oauth.rb
51
52
  - lib/twitch/object.rb
52
53
  - lib/twitch/objects/automod_setting.rb
@@ -154,7 +155,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
154
155
  - !ruby/object:Gem::Version
155
156
  version: '0'
156
157
  requirements: []
157
- rubygems_version: 3.5.9
158
+ rubygems_version: 3.5.11
158
159
  signing_key:
159
160
  specification_version: 4
160
161
  summary: A Ruby library for interacting with the Twitch Helix API