synsbasen_api 1.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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 70f3463d8af67697bd559cb53ced9254cc04a6b217963e56b7fe89e6ebd19e33
4
+ data.tar.gz: f1670824ee49861fbc4ef6f50ae9e929552ce78687bf1105167338ddc45c5a3f
5
+ SHA512:
6
+ metadata.gz: d92c768c2ad3f9b159e66c388e7875a30cfc30e624886e3020005fb7c6b65ca773be241a61c5da715970afbbccd1d41c83d34e1cfe6d44e6b994a73f4ef4a840
7
+ data.tar.gz: 071c62aee5fc0c1de9261d14c63f6f56540025f65ec5dc55873bfe5e9a98a85a4c29d9138b462eb04736339b76ba32e24ffa9b52010dc54065b3d27f69a7c90c
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `ApiResponse` class represents the response structure from the Synsbasen API.
5
+ class ApiResponse
6
+ # @return [Hash] The data included in the API response.
7
+ attr_reader :data
8
+
9
+ # @return [Numeric] The cost associated with the API response.
10
+ attr_reader :cost
11
+
12
+ # @return [Boolean] Indicates whether there is more data available in the response.
13
+ attr_reader :has_more
14
+
15
+ # Initializes a new instance of `ApiResponse` with the provided response data.
16
+ #
17
+ # @param response [Hash] The response data from the API.
18
+ # @option response [Hash] :data The data included in the API response.
19
+ # @option response [Numeric] :cost The cost associated with the API response.
20
+ # @option response [Boolean] :has_more Indicates whether there is more data available in the response.
21
+ def initialize(response)
22
+ @data = response[:data]
23
+ @cost = response[:cost]
24
+ @has_more = response[:has_more] || false
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,83 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "synsbasen_api/api_response"
4
+ require "synsbasen_api/error"
5
+ require "faraday"
6
+ require "active_support/core_ext/hash/keys"
7
+
8
+ module SynsbasenApi
9
+ # The `Client` class serves as the base class for interacting with the Synsbasen API.
10
+ class Client
11
+ DEFAULT_BASE_URL = "https://api.synsbasen.dk".freeze
12
+
13
+ class << self
14
+ # Establishes and returns a connection to the Synsbasen API.
15
+ #
16
+ # @return [Faraday::Connection] A Faraday connection instance.
17
+ def connection
18
+ @_connection ||= Faraday.new(url: SynsbasenApi.config[:base_url] || DEFAULT_BASE_URL) do |conn|
19
+ conn.use Faraday::Response::RaiseError
20
+ conn.headers = {
21
+ 'Content-Type' => 'application/json',
22
+ 'Authorization' => 'Bearer ' + SynsbasenApi.config[:api_key],
23
+ }
24
+ end
25
+ end
26
+
27
+ # Sends a GET request to the Synsbasen API.
28
+ #
29
+ # @param path [String] The API endpoint path.
30
+ # @param params [Hash] Query parameters for the request.
31
+ # @param body [Hash] Request body.
32
+ # @return [ApiResponse] An instance of `ApiResponse` containing the API response.
33
+ # @raise [ClientError, ServerError] Raised for client or server errors.
34
+ def get(path, params: {}, body: {})
35
+ response = connection.get(path) do |req|
36
+ req.params = params
37
+ req.body = body unless body.empty?
38
+ end
39
+
40
+ ApiResponse.new(JSON.parse(response.body).deep_symbolize_keys)
41
+ rescue => e
42
+ rescue_and_raise_errors(e)
43
+ end
44
+
45
+ # Sends a POST request to the Synsbasen API.
46
+ #
47
+ # @param path [String] The API endpoint path.
48
+ # @param params [Hash] Query parameters for the request.
49
+ # @param body [Hash] Request body.
50
+ # @return [ApiResponse] An instance of `ApiResponse` containing the API response.
51
+ # @raise [ClientError, ServerError] Raised for client or server errors.
52
+ def post(path, params: {}, body: {})
53
+ response = connection.post(path) do |req|
54
+ req.params = params
55
+ req.body = body
56
+ end
57
+
58
+ ApiResponse.new(JSON.parse(response.body).deep_symbolize_keys)
59
+ rescue => e
60
+ rescue_and_raise_errors(e)
61
+ end
62
+
63
+ private
64
+
65
+ # Rescues and raises specific errors based on the type of Faraday error encountered.
66
+ #
67
+ # @param e [Exception] The exception to handle.
68
+ # @raise [ClientError, ServerError] Raised for client or server errors.
69
+ def rescue_and_raise_errors(e)
70
+ case e
71
+ when Faraday::UnauthorizedError
72
+ raise ClientError.new(e.message, e.response[:status], {})
73
+ when Faraday::ClientError
74
+ raise ClientError.new(e.message, e.response[:status], JSON.parse(e.response[:body]).deep_symbolize_keys)
75
+ when Faraday::ServerError
76
+ raise ServerError.new(e.message, e.response[:status], JSON.parse(e.response[:body]).deep_symbolize_keys)
77
+ else
78
+ raise e
79
+ end
80
+ end
81
+ end
82
+ end
83
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `Error` module provides custom error classes for handling API errors.
5
+ class Error < StandardError
6
+ # @return [String] The error message.
7
+ attr_accessor :message
8
+
9
+ # @return [String] The HTTP status code associated with the error.
10
+ attr_accessor :status
11
+
12
+ # @return [Hash] Additional data associated with the error.
13
+ attr_accessor :data
14
+
15
+ # Initializes a new instance of `Error`.
16
+ #
17
+ # @param msg [String] The error message.
18
+ # @param status [String] The HTTP status code associated with the error.
19
+ # @param data [Hash] Additional data associated with the error.
20
+ def initialize(msg = '', status = '', data = {})
21
+ @message = msg
22
+ @status = status
23
+ @data = data
24
+ end
25
+ end
26
+
27
+ # The `ClientError` class represents errors that occur on the client side (4xx status codes).
28
+ class ClientError < Error; end
29
+
30
+ # The `ServerError` class represents errors that occur on the server side (5xx status codes).
31
+ class ServerError < Error; end
32
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `Brand` class provides methods for interacting with brand-related
5
+ # endpoints in the Synsbasen API.
6
+ class Brand < Client
7
+ class << self
8
+ # Retrieves information about a specific brand based on its ID.
9
+ #
10
+ # @param id [String] The unique identifier of the brand.
11
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
12
+ # of the specified brand.
13
+ def find(id)
14
+ get("/v1/brands/#{id}")
15
+ end
16
+
17
+ # Retrieves information about all brands.
18
+ #
19
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
20
+ # of all brands.
21
+ def all
22
+ get("/v1/brands")
23
+ end
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `Inspection` class provides methods for interacting with inspection-related
5
+ # endpoints in the Synsbasen API.
6
+ class Inspection < Client
7
+ class << self
8
+ # Performs a search for inspections based on the provided criteria.
9
+ #
10
+ # @param args [Hash] Additional parameters to customize the search.
11
+ # @option args [String] :method The search method. Default is 'SELECT'.
12
+ # @return [ApiResponse] An instance of `ApiResponse` containing search results.
13
+ def search(args = {})
14
+ post(
15
+ "/v1/inspections/search",
16
+ body: {
17
+ method: 'SELECT',
18
+ }.merge(args).to_json
19
+ )
20
+ end
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `InspectionTestCenter` class provides methods for interacting with inspection test center-related
5
+ # endpoints in the Synsbasen API.
6
+ class InspectionTestCenter < Client
7
+ class << self
8
+ # Retrieves information about all inspection test centers.
9
+ #
10
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
11
+ # of all inspection test centers.
12
+ def all
13
+ get("/v1/inspection_test_centers")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `Model` class provides methods for interacting with model-related
5
+ # endpoints in the Synsbasen API.
6
+ class Model < Client
7
+ class << self
8
+ # Retrieves information about a specific model based on its ID.
9
+ #
10
+ # @param id [String] The unique identifier of the model.
11
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
12
+ # of the specified model.
13
+ def find(id)
14
+ get("/v1/models/#{id}")
15
+ end
16
+
17
+ # Retrieves information about all models associated with a given brand.
18
+ #
19
+ # @param brand_id [String] The unique identifier of the brand.
20
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
21
+ # of all models associated with the specified brand.
22
+ def all(brand_id)
23
+ get("/v1/brands/#{brand_id}/models")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `TestCenter` class provides methods for interacting with test center-related
5
+ # endpoints in the Synsbasen API.
6
+ class TestCenter < Client
7
+ class << self
8
+ # Retrieves information about all test centers.
9
+ #
10
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
11
+ # of all test centers.
12
+ def all
13
+ get("/v1/test_centers")
14
+ end
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `Variant` class provides methods for interacting with variant-related
5
+ # endpoints in the Synsbasen API.
6
+ class Variant < Client
7
+ class << self
8
+ # Retrieves information about a specific variant based on its ID.
9
+ #
10
+ # @param id [String] The unique identifier of the variant.
11
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
12
+ # of the specified variant.
13
+ def find(id)
14
+ get("/v1/variants/#{id}")
15
+ end
16
+
17
+ # Retrieves information about all variants associated with a given model.
18
+ #
19
+ # @param model_id [String] The unique identifier of the model.
20
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
21
+ # of all variants associated with the specified model.
22
+ def all(model_id)
23
+ get("/v1/models/#{model_id}/variants")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,41 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `Vehicle` class provides methods for interacting with vehicle-related
5
+ # endpoints in the Synsbasen API. It extends the `Client` class.
6
+ class Vehicle < Client
7
+ class << self
8
+ # Retrieves information about a specific vehicle based on its ID.
9
+ #
10
+ # @param id [String] The unique identifier of the vehicle.
11
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
12
+ # of the specified vehicle.
13
+ def find(id)
14
+ get("/v1/vehicles/#{id}")
15
+ end
16
+
17
+ # Retrieves information about a vehicle based on its registration number.
18
+ #
19
+ # @param registration [String] The registration number of the vehicle.
20
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
21
+ # of the specified vehicle.
22
+ def find_by_registration(registration)
23
+ get("/v1/vehicles/registration/#{registration}")
24
+ end
25
+
26
+ # Performs a search for vehicles based on the provided criteria.
27
+ #
28
+ # @param args [Hash] Additional parameters to customize the search.
29
+ # @option args [String] :method The search method. Default is 'SELECT'.
30
+ # @return [ApiResponse] An instance of `ApiResponse` containing search results.
31
+ def search(args = {})
32
+ post(
33
+ "/v1/vehicles/search",
34
+ body: {
35
+ method: 'SELECT',
36
+ }.merge(args).to_json
37
+ )
38
+ end
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `Version` class provides methods for interacting with version-related
5
+ # endpoints in the Synsbasen API.
6
+ class Version < Client
7
+ class << self
8
+ # Retrieves information about a specific version based on its ID.
9
+ #
10
+ # @param id [String] The unique identifier of the version.
11
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
12
+ # of the specified version.
13
+ def find(id)
14
+ get("/v1/versions/#{id}")
15
+ end
16
+
17
+ # Retrieves information about all versions associated with a given variant.
18
+ #
19
+ # @param variant_id [String] The unique identifier of the variant.
20
+ # @return [ApiResponse] An instance of `ApiResponse` containing details
21
+ # of all versions associated with the specified variant.
22
+ def all(variant_id)
23
+ get("/v1/variants/#{variant_id}/versions")
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module SynsbasenApi
4
+ # The `VERSION` module specifies the version of the SynsbasenApi gem.
5
+ VERSION = "1.0.1"
6
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "synsbasen_api/client"
4
+ require "synsbasen_api/resources/brand"
5
+ require "synsbasen_api/resources/inspection"
6
+ require "synsbasen_api/resources/inspection_test_center"
7
+ require "synsbasen_api/resources/model"
8
+ require "synsbasen_api/resources/test_center"
9
+ require "synsbasen_api/resources/vehicle"
10
+ require "synsbasen_api/resources/variant"
11
+ require "synsbasen_api/resources/version"
12
+
13
+ # The `SynsbasenApi` module provides a configuration mechanism and requires various
14
+ # classes and modules related to interacting with the Synsbasen API.
15
+ module SynsbasenApi
16
+ # An array of required configuration keys.
17
+ REQUIRED_CONFIGS = %i[api_key]
18
+
19
+ # Configures the Synsbasen API client with the specified options.
20
+ #
21
+ # @yieldparam config [OpenStruct] The configuration object.
22
+ def self.configure
23
+ @config ||= OpenStruct.new
24
+ yield(@config) if block_given?
25
+
26
+ raise "Missing configuration. Required configurations are #{REQUIRED_CONFIGS}" unless REQUIRED_CONFIGS.all? { |c| @config[c] }
27
+
28
+ @config
29
+ end
30
+
31
+ # Retrieves the current configuration or configures the Synsbasen API client with default options.
32
+ #
33
+ # @return [OpenStruct] The configuration object.
34
+ def self.config
35
+ @config || configure
36
+ end
37
+ end
metadata ADDED
@@ -0,0 +1,93 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: synsbasen_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Jimmy Poulsen
8
+ - Tobias Knudsen
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2023-11-12 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'
42
+ description: Synsbasen API is the easiest way to get access to the Danish vehicle
43
+ registry. See https://api.synsbasen.dk for details.
44
+ email:
45
+ - jimmypoulsen96@gmail.com
46
+ - tobias.knudsen@gmail.com
47
+ executables: []
48
+ extensions: []
49
+ extra_rdoc_files: []
50
+ files:
51
+ - lib/synsbasen_api.rb
52
+ - lib/synsbasen_api/api_response.rb
53
+ - lib/synsbasen_api/client.rb
54
+ - lib/synsbasen_api/error.rb
55
+ - lib/synsbasen_api/resources/brand.rb
56
+ - lib/synsbasen_api/resources/inspection.rb
57
+ - lib/synsbasen_api/resources/inspection_test_center.rb
58
+ - lib/synsbasen_api/resources/model.rb
59
+ - lib/synsbasen_api/resources/test_center.rb
60
+ - lib/synsbasen_api/resources/variant.rb
61
+ - lib/synsbasen_api/resources/vehicle.rb
62
+ - lib/synsbasen_api/resources/version.rb
63
+ - lib/synsbasen_api/version.rb
64
+ homepage: https://github.com/Synsbasen/synsbasen-api-ruby
65
+ licenses:
66
+ - MIT
67
+ metadata:
68
+ bug_tracker_uri: https://github.com/synsbasen/synsbasen-api-ruby/issues
69
+ changelog_uri: https://github.com/synsbasen/synsbasen-api-ruby/blob/master/CHANGELOG.md
70
+ documentation_uri: https://synsbasen.github.io/synsbasen-api-ruby
71
+ github_repo: ssh://github.com/synsbasen/synsbasen-api-ruby
72
+ homepage_uri: https://api.synsbasen.dk
73
+ source_code_uri: https://github.com/synsbasen/synsbasen-api-ruby
74
+ post_install_message:
75
+ rdoc_options: []
76
+ require_paths:
77
+ - lib
78
+ required_ruby_version: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ required_rubygems_version: !ruby/object:Gem::Requirement
84
+ requirements:
85
+ - - ">="
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubygems_version: 3.4.6
90
+ signing_key:
91
+ specification_version: 4
92
+ summary: Library that provides a simple way to interact with Synsbasen API
93
+ test_files: []