synsbasen_api 1.0.13 → 1.0.14

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: 565b3bfae169b625688bcb1bb70d1fc3c68abb88d2226b0e796139a25adab9bc
4
- data.tar.gz: fdb44552e927ff505dca9d8a1778ee84fb99ea559d1362518176ebcdb913f1ed
3
+ metadata.gz: b3e082cca5c5eee71faf49856116fc9ec29c3413b3fe796804651f0647756686
4
+ data.tar.gz: a8a10e871246d73f00f901fcb1d98479391fb6a7d0a4ac802372a0a85e759c79
5
5
  SHA512:
6
- metadata.gz: 794d7cc6f66ddb9320e706810fd7aa6fcc262b81d1599ec7b92e8f265ea835e08568609ca23b5ae2be000a711a0ad07faadce0cdebd2b55ccf6289a526e48efb
7
- data.tar.gz: e701b46e2c20799a2fd53a4be1baadf650179b5ca5ee4d01377aebe8a39c049a287d688595100e4bca0653edafb0bcfef4d868a94b0643edc44a45e29eb14066
6
+ metadata.gz: a9194013eef9bffb68530e20495abdb22636f7114ab7b5ed47a72f27e36310b02dffb89c3034a33456966e436a451214a4f2201aaf077b5d302eee7718a9e557
7
+ data.tar.gz: 50f4f3e3c61636f9d3b51bb033c2199f946297db594980a7c29e3917509261dfef30eabb316a9d447453800395848d6358505afee5c36e4a7e1087f4acf3c8f0
@@ -22,7 +22,7 @@ module SynsbasenApi
22
22
  def get(path, params: {}, expand: [])
23
23
  request = build_request(path, method: Net::HTTP::Get, params: params, expand: expand)
24
24
 
25
- response = connection.request(request)
25
+ response = perform_request(request)
26
26
 
27
27
  raise_errors(response)
28
28
 
@@ -42,7 +42,7 @@ module SynsbasenApi
42
42
  def post(path, params: {}, body: {}, expand: [])
43
43
  request = build_request(path, method: Net::HTTP::Post, params: params, body: body, expand: expand)
44
44
 
45
- response = connection.request(request)
45
+ response = perform_request(request)
46
46
 
47
47
  raise_errors(response)
48
48
 
@@ -61,7 +61,7 @@ module SynsbasenApi
61
61
  def delete(path, params = {}, body = {})
62
62
  request = build_request(path, method: Net::HTTP::Delete, params: params, body: body)
63
63
 
64
- response = connection.request(request)
64
+ response = perform_request(request)
65
65
 
66
66
  raise_errors(response)
67
67
 
@@ -106,29 +106,37 @@ module SynsbasenApi
106
106
  request
107
107
  end
108
108
 
109
+ # Sends a request and translates transport-level timeouts into a public gem error.
110
+ #
111
+ # @param request [Net::HTTPRequest] The request to send.
112
+ # @return [Net::HTTPResponse] The response returned by the API.
113
+ # @raise [RequestTimeoutError] Raised when the request times out before a response is received.
114
+ def perform_request(request)
115
+ connection.request(request)
116
+ rescue Net::OpenTimeout, Net::ReadTimeout, Net::WriteTimeout => e
117
+ raise RequestTimeoutError.new(e.message)
118
+ end
119
+
109
120
  # Raises specific errors based on the type of Net::HTTP error encountered.
110
121
  #
111
- # @param e [Exception] The exception to handle.
112
- # @raise [ClientError, ServerError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, UnprocessableEntityError, InternalServerError] Raised for errors.
122
+ # @param response [Net::HTTPResponse] The response to handle.
123
+ # @raise [ClientError, ServerError, BadRequestError, UnauthorizedError, ForbiddenError, NotFoundError, ConflictError, UnprocessableEntityError, RequestTimeoutError, GatewayTimeoutError, InternalServerError] Raised for errors.
113
124
  def raise_errors(response)
114
125
  return unless response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError)
115
126
 
116
- case response
117
- when Net::HTTPUnauthorized
118
- raise ClientError.new(response.message, response.code, {})
119
- else
120
- error_class_name = "#{response.message.split.map(&:capitalize).join}Error"
121
-
122
- error_class =
123
- if SynsbasenApi.const_defined?(error_class_name, false) &&
124
- SynsbasenApi.const_get(error_class_name).is_a?(Class)
125
- SynsbasenApi.const_get(error_class_name)
126
- else
127
- response.is_a?(Net::HTTPClientError) ? ClientError : ServerError
128
- end
129
-
130
- raise error_class.new(response.message, response.code, parse_json(response.body))
131
- end
127
+ error_class = {
128
+ "400" => BadRequestError,
129
+ "401" => UnauthorizedError,
130
+ "403" => ForbiddenError,
131
+ "404" => NotFoundError,
132
+ "408" => RequestTimeoutError,
133
+ "409" => ConflictError,
134
+ "422" => UnprocessableEntityError,
135
+ "500" => InternalServerError,
136
+ "504" => GatewayTimeoutError
137
+ }[response.code] || (response.is_a?(Net::HTTPClientError) ? ClientError : ServerError)
138
+
139
+ raise error_class.new(response.message, response.code, parse_json(response.body))
132
140
  end
133
141
 
134
142
  # Calls the after_request callback if configured in the SynsbasenApi.
@@ -146,7 +154,7 @@ module SynsbasenApi
146
154
  # @param data [String] The JSON string to parse.
147
155
  # @return [Hash] The parsed JSON string as a hash with symbolized keys.
148
156
  def parse_json(data)
149
- data ||= "{}"
157
+ return {} if data.nil? || data.empty?
150
158
 
151
159
  deep_symbolize_keys(JSON.parse(data))
152
160
  end
@@ -30,6 +30,13 @@ module SynsbasenApi
30
30
  # The `ServerError` class represents errors that occur on the server side (5xx status codes).
31
31
  class ServerError < Error; end
32
32
 
33
+ # The `RequestTimeoutError` class represents requests that did not complete within the
34
+ # configured timeout, including HTTP 408 responses and transport-level timeouts.
35
+ class RequestTimeoutError < Error; end
36
+
37
+ # The `GatewayTimeoutError` class represents an HTTP 504 response from the API.
38
+ class GatewayTimeoutError < RequestTimeoutError; end
39
+
33
40
  %w[BadRequestError UnauthorizedError ForbiddenError NotFoundError
34
41
  ConflictError UnprocessableEntityError].each do |error|
35
42
  klass = Class.new(ClientError)
@@ -2,5 +2,5 @@
2
2
 
3
3
  module SynsbasenApi
4
4
  # The `VERSION` module specifies the version of the SynsbasenApi gem.
5
- VERSION = "1.0.13"
5
+ VERSION = "1.0.14"
6
6
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: synsbasen_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.13
4
+ version: 1.0.14
5
5
  platform: ruby
6
6
  authors:
7
7
  - Jimmy Poulsen
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2025-09-30 00:00:00.000000000 Z
12
+ date: 2026-08-03 00:00:00.000000000 Z
13
13
  dependencies: []
14
14
  description: Synsbasen API is the easiest way to get access to the Danish vehicle
15
15
  registry. See https://api.synsbasen.dk for details.