synsbasen_api 1.0.5 → 1.0.7

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: 488fa94b15f086f18590d5e13bda9455648fbdc8794b639760bf5b39708b74f5
4
- data.tar.gz: b6f821fc79f849a6a97927e65f39a68896b08307579b0bf62b09c3f606b22b29
3
+ metadata.gz: 6ba050bd5ad13d81687977a3963bb9ff3029508c76ff2390b96bb47808229b45
4
+ data.tar.gz: 1b00104a8517540a698866f3297cc0e451e5febc8e6afb0eea945a2d3585632c
5
5
  SHA512:
6
- metadata.gz: b4ab45d11acbfecd31e8f925952697f84f5a41bee63f941a5eb7d14600ce6e6c88b38f7b0c5d0d663bcba393f4ac89b1463f1f2ed1396663e767dfddb5712389
7
- data.tar.gz: eea99086d568db341f04f43e1e54048126a658c4dbd753e83b127b232f83121c8721a690bd503a86e0b206bc4d5ea7a9fd44c20f08341978d4b7704983579d3e
6
+ metadata.gz: 680d20a16b8e05837775a3ca5feb39703e09ed19a39f23f2b8c4031f89ccce241c871349c97fe5045e49e5c20a99d18df348dfb154ce1d6e147ce1b695720969
7
+ data.tar.gz: 410e80b9db9a317ca11a285452f5b1875b54556e17880354952bb4b01351952299a5bfd0d51de396af3456cd8343a73815e96e6fa7e16b6074565af1bb7e9dd8
@@ -15,17 +15,22 @@ module SynsbasenApi
15
15
  # @return [Numeric] The total number of pages available.
16
16
  attr_reader :total_pages
17
17
 
18
+ # @return [Numeric] The total count of items available.
19
+ attr_reader :total_count
20
+
18
21
  # Initializes a new instance of `ApiResponse` with the provided response data.
19
22
  #
20
23
  # @param response [Hash] The response data from the API.
21
24
  # @option response [Hash] :data The data included in the API response.
22
25
  # @option response [Numeric] :cost The cost associated with the API response.
23
26
  # @option response [Boolean] :has_more Indicates whether there is more data available in the response.
27
+ # @option response [Boolean] :total_pages The total number of pages available.
28
+ # @option response [Boolean] :total_count The total count of items available.
24
29
  def initialize(response)
25
30
  @data = response[:data]
26
31
  @cost = response[:cost]
27
32
 
28
- %i[has_more total_pages].each do |key|
33
+ %i[has_more total_pages total_count].each do |key|
29
34
  instance_variable_set("@#{key}", response[key]) if response.key?(key)
30
35
  end
31
36
  end
@@ -2,10 +2,9 @@
2
2
 
3
3
  require "synsbasen_api/api_response"
4
4
  require "synsbasen_api/error"
5
- require "faraday"
6
- require "active_support/core_ext/hash/keys"
7
- require "active_support/core_ext/object/blank"
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
- # Establishes and returns a connection to the Synsbasen API.
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 body [Hash] Request body.
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
- response = connection.get(path) do |req|
38
- req.params = { **params, expand: expand }.compact_blank
39
- end
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(JSON.parse(response.body).deep_symbolize_keys)
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 the Synsbasen API.
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
- response = connection.post(path) do |req|
57
- req.params = params
58
- req.body = { **body, expand: expand }.compact_blank.to_json
59
- end
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(JSON.parse(response.body).deep_symbolize_keys)
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
- # Rescues and raises specific errors based on the type of Faraday error encountered.
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 rescue_and_raise_errors(e)
75
- case e
76
- when Faraday::UnauthorizedError
77
- raise ClientError.new(e.message, e.response[:status], {})
78
- when Faraday::ClientError
79
- raise ClientError.new(e.message, e.response[:status], JSON.parse(e.response[:body]).deep_symbolize_keys)
80
- when Faraday::ServerError
81
- raise ServerError.new(e.message, e.response[:status], JSON.parse(e.response[:body]).deep_symbolize_keys)
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
- raise e
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 [Faraday::Response] The Faraday response object.
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
@@ -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.5"
5
+ VERSION = "1.0.7"
6
6
  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.5
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-01-16 00:00:00.000000000 Z
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: