postal_codes_ruby_client 0.1.2

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: 6b837ce600dee1cbc4db227e2a1d98f712ce513d0aded2e8bec70423c8ed8758
4
+ data.tar.gz: a46fa5ad91f63a4f512dbf513337b97ed513b09b441c9e436598556c9c83ca62
5
+ SHA512:
6
+ metadata.gz: 8d56536ca511d429389f77faf4fa5dd37b7815e99fc38c6e92759366b5ae68fe245c86019752e6e9d481e5ee0ae4a8be693dad3fd80a00e35584def08418334c
7
+ data.tar.gz: f5aa387adb1fef7ae40e4c72f9ec929e92a7c84f0b996f70306bfe0b85d5b30b7454dcc817f14fadb34dfabd63f42b6c6030a3e6231cdab3aa8974a4c5169541
data/README.md ADDED
@@ -0,0 +1,83 @@
1
+ # PostalCodesRubyClient
2
+
3
+ Ruby-Client-Gem für die PLZ API – Postleitzahlen-Suche über 100+ Länder.
4
+
5
+ ## Installation
6
+
7
+ ```ruby
8
+ # Gemfile
9
+ gem "postal_codes_ruby_client", path: "../postal_codes_ruby_client"
10
+ ```
11
+
12
+ Oder als lokales Gem:
13
+
14
+ ```bash
15
+ cd postal_codes_ruby_client
16
+ gem build postal_codes_ruby_client.gemspec
17
+ gem install postal_codes_ruby_client-0.1.0.gem
18
+ ```
19
+
20
+ ## Konfiguration
21
+
22
+ ```ruby
23
+ require "postal_codes_ruby_client"
24
+
25
+ PostalCodesRubyClient.configure do |config|
26
+ config.base_url = "http://localhost:3000" # Standard
27
+ config.api_token = "dein_api_token"
28
+ config.timeout = 30 # Sekunden (Standard)
29
+ end
30
+ ```
31
+
32
+ ## Verwendung
33
+
34
+ ### Client erstellen
35
+
36
+ ```ruby
37
+ # Globale Konfiguration nutzen
38
+ client = PostalCodesRubyClient::Client.new
39
+
40
+ # Oder Token direkt übergeben
41
+ client = PostalCodesRubyClient::Client.new(
42
+ api_token: "dein_api_token",
43
+ base_url: "https://api.example.com"
44
+ )
45
+ ```
46
+
47
+ ```ruby
48
+ # PLZ-Suche (Teilsuche möglich)
49
+ results = client.postal_codes.search(q: "803", country: "DE")
50
+ results[:results].each do |pc|
51
+ puts "#{pc[:zipcode]} #{pc[:place]} (#{pc[:state]})"
52
+ end
53
+
54
+ # Suche ohne Länderfilter
55
+ results = client.postal_codes.search(q: "1010", limit: 10)
56
+
57
+ # Verfügbare Länder auflisten
58
+ countries = client.postal_codes.countries
59
+ puts countries[:countries].join(", ")
60
+ ```
61
+
62
+ ### Fehlerbehandlung
63
+
64
+ ```ruby
65
+ begin
66
+ client.postal_codes.search(q: "803")
67
+ rescue PostalCodesRubyClient::AuthenticationError => e
68
+ puts "Nicht autorisiert: #{e.message}"
69
+ rescue PostalCodesRubyClient::ValidationError => e
70
+ puts "Validierungsfehler: #{e.errors.join(', ')}"
71
+ rescue PostalCodesRubyClient::ApiError => e
72
+ puts "API-Fehler (#{e.status}): #{e.message}"
73
+ rescue PostalCodesRubyClient::Error => e
74
+ puts "Fehler: #{e.message}"
75
+ end
76
+ ```
77
+
78
+ ## API-Referenz
79
+
80
+ | Methode | Beschreibung |
81
+ |---------|-------------|
82
+ | `client.postal_codes.search(q:, country:, limit:)` | PLZ-Suche |
83
+ | `client.postal_codes.countries` | Verfügbare Länder |
@@ -0,0 +1,85 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "net/http"
4
+ require "uri"
5
+ require "json"
6
+
7
+ module PostalCodesRubyClient
8
+ class Client
9
+ attr_reader :postal_codes
10
+
11
+ def initialize(api_token: nil, base_url: nil)
12
+ @api_token = api_token || PostalCodesRubyClient.configuration.api_token
13
+ @base_url = base_url || PostalCodesRubyClient.configuration.base_url
14
+ @timeout = PostalCodesRubyClient.configuration.timeout
15
+
16
+ @postal_codes = Resources::PostalCodes.new(self)
17
+ end
18
+
19
+ # Update the API token (e.g. after login or token regeneration).
20
+ def api_token=(token)
21
+ @api_token = token
22
+ end
23
+
24
+ # Perform a GET request.
25
+ def get(path, params = {})
26
+ uri = build_uri(path, params)
27
+ request = Net::HTTP::Get.new(uri)
28
+ execute(uri, request)
29
+ end
30
+
31
+ # Perform a POST request with a JSON body.
32
+ def post(path, body = nil)
33
+ uri = build_uri(path)
34
+ request = Net::HTTP::Post.new(uri)
35
+ if body
36
+ request.body = JSON.generate(body)
37
+ request["Content-Type"] = "application/json"
38
+ end
39
+ execute(uri, request)
40
+ end
41
+
42
+ private
43
+
44
+ def build_uri(path, params = {})
45
+ uri = URI.join(@base_url, path)
46
+ uri.query = URI.encode_www_form(params) unless params.empty?
47
+ uri
48
+ end
49
+
50
+ def execute(uri, request)
51
+ request["Authorization"] = "Bearer #{@api_token}" if @api_token
52
+ request["Accept"] = "application/json"
53
+
54
+ http = Net::HTTP.new(uri.host, uri.port)
55
+ http.use_ssl = uri.scheme == "https"
56
+ http.open_timeout = @timeout
57
+ http.read_timeout = @timeout
58
+
59
+ response = http.request(request)
60
+ handle_response(response)
61
+ end
62
+
63
+ def handle_response(response)
64
+ body = response.body ? JSON.parse(response.body, symbolize_names: true) : nil
65
+
66
+ case response.code.to_i
67
+ when 200, 201
68
+ body
69
+ when 401
70
+ raise AuthenticationError, body&.dig(:error) || "Unauthorized"
71
+ when 422
72
+ raise ValidationError.new(
73
+ body&.dig(:error) || "Validation failed",
74
+ errors: body&.dig(:errors) || []
75
+ )
76
+ else
77
+ raise ApiError.new(
78
+ body&.dig(:error) || "API error (HTTP #{response.code})",
79
+ status: response.code.to_i,
80
+ body: body
81
+ )
82
+ end
83
+ end
84
+ end
85
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PostalCodesRubyClient
4
+ class Configuration
5
+ attr_accessor :base_url, :api_token, :timeout
6
+
7
+ def initialize
8
+ @base_url = "http://localhost:3000"
9
+ @api_token = nil
10
+ @timeout = 30
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,26 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PostalCodesRubyClient
4
+ class Error < StandardError; end
5
+
6
+ class AuthenticationError < Error; end
7
+
8
+ class ValidationError < Error
9
+ attr_reader :errors
10
+
11
+ def initialize(message, errors: [])
12
+ @errors = errors
13
+ super(message)
14
+ end
15
+ end
16
+
17
+ class ApiError < Error
18
+ attr_reader :status, :body
19
+
20
+ def initialize(message, status:, body: nil)
21
+ @status = status
22
+ @body = body
23
+ super(message)
24
+ end
25
+ end
26
+ end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PostalCodesRubyClient
4
+ module Resources
5
+ class PostalCodes
6
+ def initialize(client)
7
+ @client = client
8
+ end
9
+
10
+ # Search for postal codes.
11
+ #
12
+ # @param q [String] Full or partial postal code (required)
13
+ # @param country [String, nil] ISO country code, e.g. "DE", "AT", "CH"
14
+ # @param limit [Integer, nil] Max results (default 50, max 200)
15
+ # @return [Hash] { query:, country:, count:, results: [...] }
16
+ def search(q:, country: nil, limit: nil)
17
+ params = { q: q }
18
+ params[:country] = country if country
19
+ params[:limit] = limit if limit
20
+ @client.get("/api/v1/postal_codes", params)
21
+ end
22
+
23
+ # List all available country codes.
24
+ #
25
+ # @return [Hash] { countries: ["AT", "CH", "DE", ...] }
26
+ def countries
27
+ @client.get("/api/v1/postal_codes/countries")
28
+ end
29
+ end
30
+ end
31
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PostalCodesRubyClient
4
+ VERSION = "0.1.2"
5
+ end
@@ -0,0 +1,25 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "postal_codes_ruby_client/version"
4
+ require_relative "postal_codes_ruby_client/configuration"
5
+ require_relative "postal_codes_ruby_client/errors"
6
+ require_relative "postal_codes_ruby_client/resources/postal_codes"
7
+ require_relative "postal_codes_ruby_client/client"
8
+
9
+ module PostalCodesRubyClient
10
+ class << self
11
+ attr_writer :configuration
12
+
13
+ def configuration
14
+ @configuration ||= Configuration.new
15
+ end
16
+
17
+ def configure
18
+ yield(configuration)
19
+ end
20
+
21
+ def reset!
22
+ @configuration = Configuration.new
23
+ end
24
+ end
25
+ end
metadata ADDED
@@ -0,0 +1,106 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: postal_codes_ruby_client
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.2
5
+ platform: ruby
6
+ authors:
7
+ - zauberware technologies
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2026-04-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.0'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '13.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '13.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: webmock
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ description: A Ruby gem to search postal codes across 100+ countries via the PLZ API.
70
+ Supports authentication, postal code search, and country listing.
71
+ email:
72
+ executables: []
73
+ extensions: []
74
+ extra_rdoc_files: []
75
+ files:
76
+ - README.md
77
+ - lib/postal_codes_ruby_client.rb
78
+ - lib/postal_codes_ruby_client/client.rb
79
+ - lib/postal_codes_ruby_client/configuration.rb
80
+ - lib/postal_codes_ruby_client/errors.rb
81
+ - lib/postal_codes_ruby_client/resources/postal_codes.rb
82
+ - lib/postal_codes_ruby_client/version.rb
83
+ homepage: https://github.com/zauberware/postal-codes-ruby-client
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '3.1'
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubygems_version: 3.4.19
103
+ signing_key:
104
+ specification_version: 4
105
+ summary: Ruby client for the PLZ (Postleitzahlen) API
106
+ test_files: []