action_network_rest 1.1.0 → 2.0.1

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: 1917a4b296e7224c3241742af15375a1c07f8ac6ec45131aaa9e6422b1dc0564
4
- data.tar.gz: ed2cc9463b6b0f5a9621ecf1dde0873fa475ab88e9b15de6630fa0640dabc0c9
3
+ metadata.gz: b1390448bae36f5af6554e8ff05b9e149c331cd59dc5eca35c2da1e300e80225
4
+ data.tar.gz: 8902d13dd62c3a801f07e599810d7969bb9bd4c1097d642c3c2a5e45b4b36414
5
5
  SHA512:
6
- metadata.gz: 3607a4d26767b60bc1615f7e7ce080b9f5b208461faf8c2c3707f8014fb41074eed92600340c8ff12a5204f64505a05cf3a99a395af74bb239d7f2e02ca6f374
7
- data.tar.gz: 7226aaf245a03715d265ed29ac04147ecbef67244bb905d63d667a90256c53d28b886475b3cf69c684b137f6e4f183864156896c26ef1127deed6e3392e4025d
6
+ metadata.gz: 3d74e20b22928d755fad1252d3626861d489ae2855866626e6f2769c4477107f213fde987bfb19132165f3ec469798a79790a4bab99e8790e434f78e93fa01f7
7
+ data.tar.gz: d5bf569a923e87ebe163496462bce1ba10c213f655cafe61b5b59344d60746be616413ddff1848b30a8b17bf420ba40c8e5761fd4b8bb42d39a0eaeae5cb396d
data/.ruby-version CHANGED
@@ -1 +1 @@
1
- 4.0.1
1
+ 4.0.10
data/CHANGELOG.md CHANGED
@@ -1,7 +1,20 @@
1
1
  # Changelog
2
2
 
3
+ ## [Unreleased]
4
+
5
+ ### Other Changes
6
+ - Added a new error class UnexpectedResponseFormat, as a direct subclass of StandardError (not ServerError, as the request may already have been processed and shouldn't be retried). A 2xx response whose body isn't JSON (for example an HTML error or maintenance page) now raises it instead of returning a raw String that callers fail to index with a confusing TypeError or NoMethodError.
7
+
8
+ ## [2.0.0] - 2026-06-30
9
+
10
+ ### Breaking Changes
11
+ - When a response has a 403 status, now raises AuthorizationError. (Previously, ResponseError was raised in this case.)
12
+
13
+ ### Other Changes
14
+ - Added a new error class ServerError, as a subclass of ResponseError. 5xx response statuses now raise this more specific type of error.
15
+
3
16
  ## [1.1.0] - 2026-02-11
4
17
 
5
18
  ### Breaking Changes
6
19
  - Now requires Faraday 2.x (previously supported Faraday 1.x)
7
- - Dropped support for Ruby versions older than 3.3. Officially supported versions are Ruby 3.3, 3.4, and 4.0.
20
+ - Dropped support for Ruby versions older than 3.3. Officially supported versions are Ruby 3.3, 3.4, and 4.0.
@@ -3,34 +3,58 @@
3
3
  module ActionNetworkRest
4
4
  module Response
5
5
  class RaiseError < Faraday::Middleware
6
- # rubocop:disable Style/GuardClause
7
6
  def on_complete(response)
8
7
  status_code = response[:status].to_i
9
-
10
- if (400...600).cover? status_code
11
- if status_code == 400 && response[:body].include?('error')
12
- error_hsh = JSON.parse(response[:body])
13
- error_message = error_hsh['error']
14
-
15
- if error_message == 'You must specify a valid person id'
16
- raise MustSpecifyValidPersonId, error_message(response)
17
- end
18
- elsif status_code == 404
19
- raise NotFoundError, error_message(response)
20
- elsif status_code == 429
21
- raise TooManyRequests, error_message(response)
22
- else
23
- raise ResponseError, error_message(response)
24
- end
25
- end
8
+ raise error_class_for(status_code, response), error_message(response) if (400...600).cover?(status_code)
9
+ raise UnexpectedResponseFormat, unexpected_format_message(response) if unparsed_body?(response)
26
10
  end
27
- # rubocop:enable Style/GuardClause
28
11
 
29
12
  def error_message(response)
30
13
  "#{response[:method].to_s.upcase} #{response[:url]}: #{response[:status]} \n\n #{response[:body]}"
31
14
  end
15
+
16
+ private
17
+
18
+ # The JSON middleware only parses JSON content types, so a success status
19
+ # carrying anything else (e.g. an HTML maintenance page) arrives as a raw
20
+ # String that callers would otherwise index as if it were a Hash.
21
+ def unparsed_body?(response)
22
+ body = response[:body]
23
+ body.is_a?(String) && !body.strip.empty? && !content_type(response).match?(/\bjson\b/)
24
+ end
25
+
26
+ def unexpected_format_message(response)
27
+ "#{response[:method].to_s.upcase} #{response[:url]}: #{response[:status]} with non-JSON " \
28
+ "content-type #{content_type(response).inspect} \n\n #{response[:body][0, 500]}"
29
+ end
30
+
31
+ def content_type(response)
32
+ (response[:response_headers] || {})['content-type'].to_s
33
+ end
34
+
35
+ def error_class_for(status_code, response)
36
+ case status_code
37
+ when 400 then bad_request_error_class(response)
38
+ when 403 then AuthorizationError
39
+ when 404 then NotFoundError
40
+ when 429 then TooManyRequests
41
+ when 500..599 then ServerError
42
+ else ResponseError
43
+ end
44
+ end
45
+
46
+ def bad_request_error_class(response)
47
+ return ResponseError unless response[:body].include?('error')
48
+
49
+ # A few specific 400 messages map to a dedicated exception; any other
50
+ # 400 still raises the generic ResponseError.
51
+ body_error = JSON.parse(response[:body])['error']
52
+ body_error == 'You must specify a valid person id' ? MustSpecifyValidPersonId : ResponseError
53
+ end
32
54
  end
33
55
 
56
+ class AuthorizationError < StandardError; end
57
+
34
58
  class MissingActionNetworkId < StandardError; end
35
59
 
36
60
  class MustSpecifyValidPersonId < StandardError; end
@@ -39,6 +63,17 @@ module ActionNetworkRest
39
63
 
40
64
  class ResponseError < StandardError; end
41
65
 
66
+ # Raised for 5xx responses. Subclasses ResponseError so existing rescues
67
+ # continue to catch server errors while allowing callers to distinguish
68
+ # transient server-side failures (worth retrying) from client errors.
69
+ class ServerError < ResponseError; end
70
+
71
+ # Raised when a success response has a body that isn't JSON, which happens
72
+ # when Action Network serves an error or maintenance page with a 2xx status.
73
+ # Deliberately not a ServerError: the request may already have been
74
+ # processed, so it shouldn't be treated as safe to retry.
75
+ class UnexpectedResponseFormat < StandardError; end
76
+
42
77
  class TooManyRequests < StandardError; end
43
78
 
44
79
  class UsedAllRequestTries < StandardError; end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module ActionNetworkRest
4
- VERSION = '1.1.0'
4
+ VERSION = '2.0.1'
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: action_network_rest
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 2.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Grey Moore
@@ -116,7 +116,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
116
116
  - !ruby/object:Gem::Version
117
117
  version: '0'
118
118
  requirements: []
119
- rubygems_version: 4.0.3
119
+ rubygems_version: 4.0.20
120
120
  specification_version: 4
121
121
  summary: Ruby client for interacting with the ActionNetwork REST API
122
122
  test_files: []