synsbasen_api 1.0.6 → 1.0.7
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/synsbasen_api/client.rb +96 -44
- data/lib/synsbasen_api/resources/inspection_test_center.rb +15 -0
- data/lib/synsbasen_api/version.rb +1 -1
- data/lib/synsbasen_api.rb +1 -0
- metadata +3 -31
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 6ba050bd5ad13d81687977a3963bb9ff3029508c76ff2390b96bb47808229b45
|
4
|
+
data.tar.gz: 1b00104a8517540a698866f3297cc0e451e5febc8e6afb0eea945a2d3585632c
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 680d20a16b8e05837775a3ca5feb39703e09ed19a39f23f2b8c4031f89ccce241c871349c97fe5045e49e5c20a99d18df348dfb154ce1d6e147ce1b695720969
|
7
|
+
data.tar.gz: 410e80b9db9a317ca11a285452f5b1875b54556e17880354952bb4b01351952299a5bfd0d51de396af3456cd8343a73815e96e6fa7e16b6074565af1bb7e9dd8
|
data/lib/synsbasen_api/client.rb
CHANGED
@@ -2,10 +2,9 @@
|
|
2
2
|
|
3
3
|
require "synsbasen_api/api_response"
|
4
4
|
require "synsbasen_api/error"
|
5
|
-
require "
|
6
|
-
require "
|
7
|
-
require "
|
8
|
-
require "active_support/core_ext/enumerable"
|
5
|
+
require "net/http"
|
6
|
+
require "uri"
|
7
|
+
require "json"
|
9
8
|
|
10
9
|
module SynsbasenApi
|
11
10
|
# The `Client` class serves as the base class for interacting with the Synsbasen API.
|
@@ -13,86 +12,139 @@ module SynsbasenApi
|
|
13
12
|
DEFAULT_BASE_URL = "https://api.synsbasen.dk".freeze
|
14
13
|
|
15
14
|
class << self
|
16
|
-
#
|
17
|
-
#
|
18
|
-
# @return [Faraday::Connection] A Faraday connection instance.
|
19
|
-
def connection
|
20
|
-
@_connection ||= Faraday.new(url: SynsbasenApi.config[:base_url] || DEFAULT_BASE_URL) do |conn|
|
21
|
-
conn.use Faraday::Response::RaiseError
|
22
|
-
conn.headers = {
|
23
|
-
'Content-Type' => 'application/json',
|
24
|
-
'Authorization' => 'Bearer ' + SynsbasenApi.config[:api_key],
|
25
|
-
}
|
26
|
-
end
|
27
|
-
end
|
28
|
-
|
29
|
-
# Sends a GET request to the Synsbasen API.
|
15
|
+
# Sends a GET request to Synsbasen API.
|
30
16
|
#
|
31
17
|
# @param path [String] The API endpoint path.
|
32
18
|
# @param params [Hash] Query parameters for the request.
|
33
|
-
# @param
|
19
|
+
# @param expand [Array] List of fields to expand in the response.
|
34
20
|
# @return [ApiResponse] An instance of `ApiResponse` containing the API response.
|
35
21
|
# @raise [ClientError, ServerError] Raised for client or server errors.
|
36
22
|
def get(path, params: {}, expand: [])
|
37
|
-
|
38
|
-
|
39
|
-
|
23
|
+
request = build_request(path, method: Net::HTTP::Get, params: params, expand: expand)
|
24
|
+
|
25
|
+
response = connection.request(request)
|
26
|
+
|
27
|
+
raise_errors(response)
|
40
28
|
|
41
29
|
handle_after_request_callback(response)
|
42
30
|
|
43
|
-
ApiResponse.new(
|
44
|
-
rescue => e
|
45
|
-
rescue_and_raise_errors(e)
|
31
|
+
ApiResponse.new(parse_json(response.body))
|
46
32
|
end
|
47
33
|
|
48
|
-
# Sends a POST request to
|
34
|
+
# Sends a POST request to Synsbasen API.
|
49
35
|
#
|
50
36
|
# @param path [String] The API endpoint path.
|
51
37
|
# @param params [Hash] Query parameters for the request.
|
52
38
|
# @param body [Hash] Request body.
|
39
|
+
# @param expand [Array] List of fields to expand in the response.
|
53
40
|
# @return [ApiResponse] An instance of `ApiResponse` containing the API response.
|
54
41
|
# @raise [ClientError, ServerError] Raised for client or server errors.
|
55
42
|
def post(path, params: {}, body: {}, expand: [])
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
43
|
+
request = build_request(path, method: Net::HTTP::Post, params: params, body: body, expand: expand)
|
44
|
+
|
45
|
+
response = connection.request(request)
|
46
|
+
|
47
|
+
raise_errors(response)
|
60
48
|
|
61
49
|
handle_after_request_callback(response)
|
62
50
|
|
63
|
-
ApiResponse.new(
|
64
|
-
rescue => e
|
65
|
-
rescue_and_raise_errors(e)
|
51
|
+
ApiResponse.new(parse_json(response.body))
|
66
52
|
end
|
67
53
|
|
68
54
|
private
|
69
55
|
|
70
|
-
#
|
56
|
+
# Establishes and returns a connection to Synsbasen API.
|
57
|
+
#
|
58
|
+
# @return [Net::HTTP] A Net::HTTP connection instance.
|
59
|
+
def connection
|
60
|
+
uri = URI.parse(SynsbasenApi.config[:base_url] || DEFAULT_BASE_URL)
|
61
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
62
|
+
http.use_ssl = uri.scheme == "https"
|
63
|
+
http
|
64
|
+
end
|
65
|
+
|
66
|
+
# Builds a Net::HTTP request object with the specified parameters.
|
67
|
+
#
|
68
|
+
# @param path [String] The API endpoint path.
|
69
|
+
# @param params [Hash] Query parameters for the request.
|
70
|
+
# @param body [Hash] Request body.
|
71
|
+
# @param expand [Array] List of fields to expand in the response.
|
72
|
+
# @param method [Net::HTTP::Get, Net::HTTP::Post] The HTTP method to use.
|
73
|
+
#
|
74
|
+
# @return [Net::HTTP::Get, Net::HTTP::Post] A Net::HTTP request object.
|
75
|
+
def build_request(path, method:, params: {}, body: {}, expand: [])
|
76
|
+
uri = URI.parse(SynsbasenApi.config[:base_url] || DEFAULT_BASE_URL)
|
77
|
+
uri.path = path
|
78
|
+
query = params
|
79
|
+
query.merge!('expand[]': expand) unless expand.nil? || expand.empty?
|
80
|
+
uri.query = URI.encode_www_form(query)
|
81
|
+
|
82
|
+
request = method.new(uri)
|
83
|
+
request.body = body.reject { |i| i.nil? || i.empty? }.to_json if method == Net::HTTP::Post
|
84
|
+
request["Content-Type"] = "application/json"
|
85
|
+
request["Authorization"] = "Bearer #{SynsbasenApi.config[:api_key]}"
|
86
|
+
|
87
|
+
request
|
88
|
+
end
|
89
|
+
|
90
|
+
# Raises specific errors based on the type of Net::HTTP error encountered.
|
71
91
|
#
|
72
92
|
# @param e [Exception] The exception to handle.
|
73
93
|
# @raise [ClientError, ServerError] Raised for client or server errors.
|
74
|
-
def
|
75
|
-
case
|
76
|
-
when
|
77
|
-
raise ClientError.new(
|
78
|
-
when
|
79
|
-
raise ClientError.new(
|
80
|
-
when
|
81
|
-
raise ServerError.new(
|
94
|
+
def raise_errors(response)
|
95
|
+
case response
|
96
|
+
when Net::HTTPUnauthorized
|
97
|
+
raise ClientError.new(response.message, response.code, {})
|
98
|
+
when Net::HTTPClientError, Net::HTTPBadRequest, Net::HTTPForbidden, Net::HTTPNotFound
|
99
|
+
raise ClientError.new(response.message, response.code, parse_json(response.body))
|
100
|
+
when Net::HTTPServerError
|
101
|
+
raise ServerError.new(response.message, response.code, {})
|
82
102
|
else
|
83
|
-
|
103
|
+
response
|
84
104
|
end
|
85
105
|
end
|
86
106
|
|
87
107
|
# Calls the after_request callback if configured in the SynsbasenApi.
|
88
108
|
#
|
89
|
-
# @param response [
|
109
|
+
# @param response [Net::HTTPResponse] The Net::HTTPResponse object.
|
90
110
|
# @return [void]
|
91
111
|
def handle_after_request_callback(response)
|
92
112
|
return unless SynsbasenApi.config[:after_request]
|
93
113
|
|
94
114
|
SynsbasenApi.config[:after_request].call(response)
|
95
115
|
end
|
116
|
+
|
117
|
+
# Parses a JSON string into a hash with symbolized keys.
|
118
|
+
#
|
119
|
+
# @param data [String] The JSON string to parse.
|
120
|
+
# @return [Hash] The parsed JSON string as a hash with symbolized keys.
|
121
|
+
def parse_json(data)
|
122
|
+
deep_symbolize_keys(JSON.parse(data))
|
123
|
+
end
|
124
|
+
|
125
|
+
# Recursively converts all keys in a hash to symbols.
|
126
|
+
#
|
127
|
+
# This method is used to convert all keys in the API response to symbols.
|
128
|
+
#
|
129
|
+
# @param hash [Hash] The hash to convert.
|
130
|
+
# @return [Hash] The hash with all keys converted to symbols.
|
131
|
+
# @example
|
132
|
+
# deep_symbolize_keys({ "key" => "value" }) #=> { key: "value" }
|
133
|
+
# deep_symbolize_keys({ "key" => { "nested_key" => "value" } }) #=> { key: { nested_key: "value" } }
|
134
|
+
def deep_symbolize_keys(obj)
|
135
|
+
case obj
|
136
|
+
when Hash
|
137
|
+
obj.each_with_object({}) do |(key, value), result|
|
138
|
+
new_key = key.is_a?(String) ? key.to_sym : key
|
139
|
+
new_value = deep_symbolize_keys(value)
|
140
|
+
result[new_key] = new_value
|
141
|
+
end
|
142
|
+
when Array
|
143
|
+
obj.map { |value| deep_symbolize_keys(value) }
|
144
|
+
else
|
145
|
+
obj
|
146
|
+
end
|
147
|
+
end
|
96
148
|
end
|
97
149
|
end
|
98
150
|
end
|
@@ -12,6 +12,21 @@ module SynsbasenApi
|
|
12
12
|
def all
|
13
13
|
get("/v1/inspection_test_centers")
|
14
14
|
end
|
15
|
+
|
16
|
+
# Performs a search for inspection test centers based on the provided criteria.
|
17
|
+
#
|
18
|
+
# @param args [Hash] Additional parameters to customize the search.
|
19
|
+
# @option args [String] :method The search method. Default is 'SELECT'.
|
20
|
+
# @return [ApiResponse] An instance of `ApiResponse` containing search results.
|
21
|
+
def search(args = {}, expand: [])
|
22
|
+
post(
|
23
|
+
"/v1/inspection_test_centers/search",
|
24
|
+
body: {
|
25
|
+
method: 'SELECT',
|
26
|
+
}.merge(args),
|
27
|
+
expand: expand
|
28
|
+
)
|
29
|
+
end
|
15
30
|
end
|
16
31
|
end
|
17
32
|
end
|
data/lib/synsbasen_api.rb
CHANGED
@@ -9,6 +9,7 @@ require "synsbasen_api/resources/test_center"
|
|
9
9
|
require "synsbasen_api/resources/vehicle"
|
10
10
|
require "synsbasen_api/resources/variant"
|
11
11
|
require "synsbasen_api/resources/version"
|
12
|
+
require "ostruct"
|
12
13
|
|
13
14
|
# The `SynsbasenApi` module provides a configuration mechanism and requires various
|
14
15
|
# classes and modules related to interacting with the Synsbasen API.
|
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.
|
4
|
+
version: 1.0.7
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jimmy Poulsen
|
@@ -9,36 +9,8 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2024-
|
13
|
-
dependencies:
|
14
|
-
- !ruby/object:Gem::Dependency
|
15
|
-
name: activesupport
|
16
|
-
requirement: !ruby/object:Gem::Requirement
|
17
|
-
requirements:
|
18
|
-
- - "~>"
|
19
|
-
- !ruby/object:Gem::Version
|
20
|
-
version: '7'
|
21
|
-
type: :runtime
|
22
|
-
prerelease: false
|
23
|
-
version_requirements: !ruby/object:Gem::Requirement
|
24
|
-
requirements:
|
25
|
-
- - "~>"
|
26
|
-
- !ruby/object:Gem::Version
|
27
|
-
version: '7'
|
28
|
-
- !ruby/object:Gem::Dependency
|
29
|
-
name: faraday
|
30
|
-
requirement: !ruby/object:Gem::Requirement
|
31
|
-
requirements:
|
32
|
-
- - "~>"
|
33
|
-
- !ruby/object:Gem::Version
|
34
|
-
version: '2.7'
|
35
|
-
type: :runtime
|
36
|
-
prerelease: false
|
37
|
-
version_requirements: !ruby/object:Gem::Requirement
|
38
|
-
requirements:
|
39
|
-
- - "~>"
|
40
|
-
- !ruby/object:Gem::Version
|
41
|
-
version: '2.7'
|
12
|
+
date: 2024-04-24 00:00:00.000000000 Z
|
13
|
+
dependencies: []
|
42
14
|
description: Synsbasen API is the easiest way to get access to the Danish vehicle
|
43
15
|
registry. See https://api.synsbasen.dk for details.
|
44
16
|
email:
|