regio 0.1.0 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d23fef1e0c19ace88fc09034262df4f09ab9503ffad0d5052b34901e11012356
4
- data.tar.gz: 11ca77e05f124d409f53c987ea9bb47951661d48f2bd6a9a52ed9464019f85b6
3
+ metadata.gz: 58f1f77fb4ff83302a5591e138bface9f60913e2649727bee81b7460067d8d44
4
+ data.tar.gz: 7e10c3b4eb3826aaf0865888c98507f3cdf5a97e550f3100e8f91beb89bffe41
5
5
  SHA512:
6
- metadata.gz: f6987b0f7e02429e1bc4d035000dce128c1492f068a97f46e12594fbc90b8a89ceb1bd87d26d66ecf54222cf195cd63e8cdf051b9733afe93cd21b86068f51c0
7
- data.tar.gz: 58bad3e737e130eeba54d5e9fa14f99590e9eb0eef9f8d2b44cfe95e306830e5faeb8b0833db805904aa4ccf2f038b16ed54f3f8ada7ec49a796644bd5cecfc0
6
+ metadata.gz: 6702d492cb1b10ee6f6795ef16be9c1d7f8e36009d2e3667e513974b642ceed446c41a83e106b832176aa90f7eadc05d7185fb5c5f24b23f1a0e8676bb68975f
7
+ data.tar.gz: a9e6cf6a456cdcda8d5abb1adb718199df81295aca6bc1fe7d488276b5f6a7ba1aa6eb94f35435fb34e551b5c6bc3da9eabab1e26534918dd68bea080914f5e8
data/CHANGELOG.md CHANGED
@@ -1,6 +1,18 @@
1
1
  # Change log
2
2
 
3
- ## v0.1.0 (8.10.2022)
3
+ ## v0.2.0 (15.10.2022)
4
+
5
+ * 1 major enhancement:
6
+ * Regio [reverse geocode](https://api.regio.ee/documentation/#docs/reverse_geocode)
7
+ * Minor refactoring
8
+
9
+ ## v0.1.1 (9.10.2022)
10
+
11
+ * 1 major enhancement:
12
+ * Regio [geocode](https://api.regio.ee/documentation/#docs/geocode)
13
+ * Minor improvements
14
+
15
+ ## v0.1.0 (9.10.2022)
4
16
 
5
17
  * 1 major enhancement:
6
18
  * Initial release
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- regio (0.1.0)
4
+ regio (0.2.0)
5
5
  httparty (~> 0.20.0)
6
6
 
7
7
  GEM
data/README.md CHANGED
@@ -1,5 +1,7 @@
1
1
  # Regio
2
2
 
3
+ [![Gem Version](https://badge.fury.io/rb/regio.svg)](https://badge.fury.io/rb/regio)
4
+
3
5
  [Regio geocoding API](https://api.regio.ee/documentation/#docs/geocode) lets you search for addresses from complete Estonian address database.
4
6
 
5
7
  [Regio](https://www.regio.ee/en/)
@@ -9,7 +11,7 @@
9
11
  Add this line to your application's Gemfile:
10
12
 
11
13
  ```ruby
12
- gem 'regio'
14
+ gem 'regio', '~> 0.2.0'
13
15
  ```
14
16
 
15
17
  And then execute:
@@ -22,7 +24,64 @@ Or install it yourself as:
22
24
 
23
25
  ## Usage
24
26
 
25
- TODO: Write usage instructions here
27
+ Get your own API key
28
+
29
+ ```shell
30
+ REGIO_API_KEY=SECRET
31
+ ```
32
+
33
+ ### Geocoding
34
+
35
+ Use `Geocode` class in your code
36
+
37
+ ```ruby
38
+ require 'regio'
39
+
40
+ class Geocoding
41
+
42
+ private
43
+
44
+ def results
45
+ @results ||= Regio::Geocode.new(options).results
46
+ end
47
+
48
+ def options
49
+ {
50
+ address: 'Tartu maantee 83',
51
+ country: 'ee'
52
+ }
53
+ end
54
+ end
55
+ ```
56
+
57
+ Check Regio [geocode](https://api.regio.ee/documentation/#docs/geocode) documentation
58
+
59
+
60
+ ### Reverse geocoding
61
+
62
+ Use `ReverseGeocode` class in your code
63
+
64
+ ```ruby
65
+ require 'regio'
66
+
67
+ class Geocoding
68
+
69
+ private
70
+
71
+ def results
72
+ @results ||= Regio::ReverseGeocode.new(options).results
73
+ end
74
+
75
+ def options
76
+ {
77
+ lat: 59.4276340999273,
78
+ lng: 24.7790924770962
79
+ }
80
+ end
81
+ end
82
+ ```
83
+
84
+ Check Regio [reverse geocode](https://api.regio.ee/documentation/#docs/reverse_geocode) documentation
26
85
 
27
86
  ## Development
28
87
 
data/lib/regio/core.rb ADDED
@@ -0,0 +1,58 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'httparty'
4
+ require 'json'
5
+
6
+ module Regio
7
+ class Core
8
+ include HTTParty
9
+ base_uri 'https://api.regio.ee'
10
+
11
+ attr_accessor :options
12
+
13
+ def initialize(options = {})
14
+ @options = default_options.merge(options)
15
+ end
16
+
17
+ COMPONENTS = [
18
+ { name: :country, type: 'A0' },
19
+ { name: :county, type: 'A1' },
20
+ { name: :municipality, type: 'A2' },
21
+ { name: :settlement, type: 'A3' },
22
+ { name: :small_place, type: 'A4' },
23
+ { name: :street, type: 'A5' },
24
+ { name: :farmstead, type: 'A6' },
25
+ { name: :house, type: 'A7' },
26
+ { name: :apartment, type: 'A8' }
27
+ ].freeze
28
+
29
+ def run(path, params = {})
30
+ JSON.parse(self.class.get(path, query: params).body, symbolize_names: true)
31
+ end
32
+
33
+ private
34
+
35
+ def transform(result)
36
+ hash = default_hash_for(result)
37
+
38
+ COMPONENTS.each do |component|
39
+ item = result[:components].detect { |o| o[:type] == component[:type] }
40
+ hash[component[:name]] = item.nil? ? nil : item[:name]
41
+ end
42
+
43
+ hash
44
+ end
45
+
46
+ def default_hash_for(result)
47
+ {
48
+ regio_id: result[:id],
49
+ title: result[:address],
50
+ valid: result[:is_valid],
51
+ complete: result[:is_complete],
52
+ zipcode: result[:postcode],
53
+ lat: result[:geometry].last,
54
+ lon: result[:geometry].first
55
+ }
56
+ end
57
+ end
58
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Regio
4
+ class Failed < StandardError
5
+ def initialize(msg = 'Failed')
6
+ super
7
+ end
8
+
9
+ def http_status_code
10
+ 400
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Regio
4
+ class Forbidden < StandardError
5
+ def initialize(msg = 'Forbidden')
6
+ super
7
+ end
8
+
9
+ def http_status_code
10
+ 403
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Regio
4
+ class NotFound < StandardError
5
+ def initialize(msg = 'Not found')
6
+ super
7
+ end
8
+
9
+ def http_status_code
10
+ 403
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Regio
4
+ class Unprocessable < StandardError
5
+ def initialize(msg = 'Unprocessable')
6
+ super
7
+ end
8
+
9
+ def http_status_code
10
+ 422
11
+ end
12
+ end
13
+ end
data/lib/regio/geocode.rb CHANGED
@@ -1,32 +1,10 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require 'httparty'
4
- require 'json'
5
-
6
3
  module Regio
7
- class Geocode
8
- include HTTParty
9
- base_uri 'https://api.regio.ee'
10
-
11
- COMPONENTS = [
12
- { name: :country, type: 'A0' },
13
- { name: :county, type: 'A1' },
14
- { name: :municipality, type: 'A2' },
15
- { name: :settlement, type: 'A3' },
16
- { name: :small_place, type: 'A4' },
17
- { name: :street, type: 'A5' },
18
- { name: :farmstead, type: 'A6' },
19
- { name: :house, type: 'A7' },
20
- { name: :apartment, type: 'A8' }
21
- ].freeze
22
-
23
- attr_accessor :options
24
-
25
- def initialize(options = {})
26
- @options = default_options.merge(options)
27
- end
28
-
4
+ class Geocode < Core
29
5
  def results
6
+ raise Unprocessable, response[:message] if response[:message]
7
+
30
8
  response.merge(
31
9
  collection: response[:data]&.map { |result| transform(result) } || []
32
10
  )
@@ -35,7 +13,7 @@ module Regio
35
13
  private
36
14
 
37
15
  def response
38
- JSON.parse(self.class.get('/geocode', query: options).body, symbolize_names: true)
16
+ run('/geocode', options)
39
17
  end
40
18
 
41
19
  # NOTE: all options described in the documentation
@@ -50,28 +28,5 @@ module Regio
50
28
  limit: 25
51
29
  }
52
30
  end
53
-
54
- def transform(result)
55
- hash = default_hash_for(result)
56
-
57
- COMPONENTS.each do |component|
58
- item = result[:components].detect { |o| o[:type] == component[:type] }
59
- hash[component[:name]] = item.nil? ? nil : item[:name]
60
- end
61
-
62
- hash
63
- end
64
-
65
- def default_hash_for(result)
66
- {
67
- regio_id: result[:id],
68
- title: result[:address],
69
- valid: result[:is_valid],
70
- complete: result[:is_complete],
71
- zipcode: result[:postcode],
72
- lat: result[:geometry].last,
73
- lon: result[:geometry].first
74
- }
75
- end
76
31
  end
77
32
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Regio
4
+ class ReverseGeocode < Core
5
+ def results
6
+ raise Unprocessable, response[:message] if response[:message]
7
+
8
+ response.merge(
9
+ collection: response[:data]&.map { |result| transform(result) } || []
10
+ )
11
+ end
12
+
13
+ private
14
+
15
+ def response
16
+ run('/revgeocode', options)
17
+ end
18
+
19
+ # NOTE: all options described in the documentation
20
+ # https://api.regio.ee/documentation/#docs/reverse_geocode
21
+ def default_options
22
+ {
23
+ apikey: Configuration.api_key,
24
+ address_format: 'long_address',
25
+ details: 'id,address,postcode,type,components,geometry,is_valid,is_complete',
26
+ output_format: 'json',
27
+ limit: 25
28
+ }
29
+ end
30
+ end
31
+ end
data/lib/regio/version.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Regio
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/regio.rb CHANGED
@@ -1,7 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'regio/configuration'
4
+ require 'regio/core'
4
5
  require 'regio/geocode'
6
+ require 'regio/reverse_geocode'
7
+ require 'regio/errors/failed'
8
+ require 'regio/errors/forbidden'
9
+ require 'regio/errors/not_found'
10
+ require 'regio/errors/unprocessable'
5
11
  require 'regio/version'
6
12
 
7
13
  module Regio
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: regio
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - tab
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2022-10-09 00:00:00.000000000 Z
11
+ date: 2022-10-15 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httparty
@@ -162,7 +162,13 @@ files:
162
162
  - bin/setup
163
163
  - lib/regio.rb
164
164
  - lib/regio/configuration.rb
165
+ - lib/regio/core.rb
166
+ - lib/regio/errors/failed.rb
167
+ - lib/regio/errors/forbidden.rb
168
+ - lib/regio/errors/not_found.rb
169
+ - lib/regio/errors/unprocessable.rb
165
170
  - lib/regio/geocode.rb
171
+ - lib/regio/reverse_geocode.rb
166
172
  - lib/regio/version.rb
167
173
  - regio.gemspec
168
174
  homepage: https://github.com/tab/regio