synsbasen_api 1.0.12 → 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: 16d6364975bbffedbffc512b5f710b9304bfc9df47e220ed3a33f957425114ba
4
- data.tar.gz: b580cfe2a5c4dbe8426803cea411936bc85b7444d97eb4f73b9dd176fc0c3172
3
+ metadata.gz: b3e082cca5c5eee71faf49856116fc9ec29c3413b3fe796804651f0647756686
4
+ data.tar.gz: a8a10e871246d73f00f901fcb1d98479391fb6a7d0a4ac802372a0a85e759c79
5
5
  SHA512:
6
- metadata.gz: 66a24651b84829ac1414f5f57a3df11e3d067cd7dab5d83cae46c8fdf683e65e74ff0be396fae3dc041f762cd77c72358c47f18cf0627446692a6f57c9a8b5d3
7
- data.tar.gz: 5c75347ce30bce0fe183efc8adff4b35f73de042c716bdf69dd6f3a68ca36d19c35985262c23cb7d34ecb82a570c91b06c3e43aad7d6f278f999523084b681f1
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,21 +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] Raised for client or server 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
- case response
115
- when Net::HTTPUnauthorized
116
- raise ClientError.new(response.message, response.code, {})
117
- when Net::HTTPClientError, Net::HTTPBadRequest, Net::HTTPForbidden, Net::HTTPNotFound
118
- raise ClientError.new(response.message, response.code, parse_json(response.body))
119
- when Net::HTTPServerError
120
- raise ServerError.new(response.message, response.code, {})
121
- else
122
- response
123
- end
125
+ return unless response.is_a?(Net::HTTPClientError) || response.is_a?(Net::HTTPServerError)
126
+
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))
124
140
  end
125
141
 
126
142
  # Calls the after_request callback if configured in the SynsbasenApi.
@@ -138,7 +154,7 @@ module SynsbasenApi
138
154
  # @param data [String] The JSON string to parse.
139
155
  # @return [Hash] The parsed JSON string as a hash with symbolized keys.
140
156
  def parse_json(data)
141
- data ||= "{}"
157
+ return {} if data.nil? || data.empty?
142
158
 
143
159
  deep_symbolize_keys(JSON.parse(data))
144
160
  end
@@ -30,6 +30,21 @@ 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
+
40
+ %w[BadRequestError UnauthorizedError ForbiddenError NotFoundError
41
+ ConflictError UnprocessableEntityError].each do |error|
42
+ klass = Class.new(ClientError)
43
+ const_set(error, klass)
44
+ end
45
+
46
+ class InternalServerError < ServerError; end
47
+
33
48
  # The `VehicleAlreadySubscribedError` class represents errors that occur when a vehicle is already being watched.
34
- class VehicleAlreadySubscribedError < ClientError; end
49
+ class VehicleAlreadySubscribedError < ConflictError; end
35
50
  end
@@ -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.12"
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.12
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-29 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.