open_street_map 1.1 → 1.1.1

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: 1ecb1e86901f5a99ee48c747fc4fda5c24e541f45faac9dd20541d112de3b2ab
4
- data.tar.gz: b35872595d45d4b7ff9aedb9c157b570ea515d69f7edf00c1d988d89778c4378
3
+ metadata.gz: cf89cc2a9860608487a2e39ae028912a15209f675d10ad70e4d09a5d1ca7903f
4
+ data.tar.gz: b08b3864ed1cb9e843a98faab1f07aefecbfea78eaa609148c78350e8364d126
5
5
  SHA512:
6
- metadata.gz: eae9c8162b19e8e508fedf2528091e13edc0e9701a2d4efc447eea465796ffb0200b9cad4bc7429ae490380b41812fcc0013542fcea4bd3d401d57cf82ec7d91
7
- data.tar.gz: 54dff490b60bd0e7414e2319faf8bacccbdb71f8129cbfe1350af78ca40298745c83e07099154371f756361f02cb811e9aaa73a690e59123e1c1cfef90a2624c
6
+ metadata.gz: e0747f589ef8b0870da627b6706c788d0fa74f5b63b1dcddcf4962b326a19f275c01871d3a1f08033426568273c231d4244bc135aded5bbe136fd1242a537781
7
+ data.tar.gz: c511bf49cd166de25a90c194618447c04a002a3930a86578a621c4b4e18a8ffaa16beb37d368fa9088dc9a959fb4200f5d7d387d72d9769c805bb535713018da
data/README.md CHANGED
@@ -34,7 +34,7 @@ Request for search objects is #search.
34
34
  client.search(q: '135 pilkington avenue, birmingham', format: 'json', addressdetails: '1', accept_language: 'ru')
35
35
  ```
36
36
  q - query
37
- format - one of the [xml|json|jsonv2]
37
+ format - one of the [xml|json|jsonv2], default - xml
38
38
  viewbox - The preferred area to find search results like <x1>,<y1>,<x2>,<y2>
39
39
  bounded - Restrict the results to only items contained with the viewbox, one of the [0|1]
40
40
  addressdetails - Include a breakdown of the address into elements, one of the [0|1]
@@ -86,7 +86,7 @@ Request for objects by coordinates is #reverse.
86
86
  ```ruby
87
87
  client.reverse(format: 'json', lat: '52.594417', lon: '39.493115', accept_language: 'ru')
88
88
  ```
89
- format - one of the [xml|json|jsonv2]
89
+ format - one of the [xml|json|jsonv2], default - xml
90
90
  zoom - Level of detail required where 0 is country and 18 is house/building, one of the [0-18]
91
91
  addressdetails - Include a breakdown of the address into elements, one of the [0|1]
92
92
  lat - Latitude, required
@@ -0,0 +1,88 @@
1
+ module OpenStreetMap
2
+ class Client
3
+ # methods for request
4
+ module Request
5
+ private
6
+
7
+ def call(type, args)
8
+ url = generate_url(type, args)
9
+ response = fetch(url, args)
10
+ sleep(REQUEST_TIMEOUT)
11
+ parse(response, args[:format])
12
+ rescue
13
+ { 'errors' => 'Bad request' }
14
+ end
15
+
16
+ # generate url from args
17
+ def generate_url(type, args)
18
+ valid_args_list = valid_args(type)
19
+ args = args.select { |key, _value| valid_args_list.include?(key) }
20
+ url = args.inject('') { |acc, (key, value)| acc + add_to_options(key, value, acc) }
21
+ type + url
22
+ end
23
+
24
+ # make request to API
25
+ def fetch(url, args)
26
+ url = hostname(args[:hostname]) + url
27
+ RestClient.get(url, user_agent: user_agent(args[:user_agent]))
28
+ end
29
+
30
+ # parse response
31
+ def parse(response, format)
32
+ if json_parse_valid?(format)
33
+ JSON.parse(response.body)
34
+ else
35
+ response.body
36
+ end
37
+ end
38
+
39
+ # get list of permitted args
40
+ def valid_args(type)
41
+ case type
42
+ when 'search' then %i[q format addressdetails extratags namedetails viewbox bounded exclude_place_ids limit accept_language email]
43
+ when 'reverse' then %i[format lat lon zoom addressdetails extratags namedetails accept_language email]
44
+ else []
45
+ end
46
+ end
47
+
48
+ # make combination of special symbol and param with value
49
+ def add_to_options(key, value, acc)
50
+ symbol_for_param(acc) + key_value_param(key, remove_spaces(value))
51
+ end
52
+
53
+ # select special symbol
54
+ def symbol_for_param(acc)
55
+ acc == '' ? '?' : '&'
56
+ end
57
+
58
+ # make combination of param with value
59
+ def key_value_param(key, value)
60
+ if key == :accept_language
61
+ "accept-language=#{value}"
62
+ else
63
+ "#{key}=#{value}"
64
+ end
65
+ end
66
+
67
+ # replace spaces with + for values
68
+ def remove_spaces(value)
69
+ value.gsub(/\s+/, '+')
70
+ end
71
+
72
+ # select hostname for request
73
+ def hostname(value)
74
+ value || BASE_URI
75
+ end
76
+
77
+ # select user_agent for request
78
+ def user_agent(value)
79
+ value || DEFAULT_USER_AGENT
80
+ end
81
+
82
+ # if format equal json then allow parsing
83
+ def json_parse_valid?(format)
84
+ format == 'json' || format == 'jsonv2'
85
+ end
86
+ end
87
+ end
88
+ end
@@ -1,18 +1,24 @@
1
1
  require 'rest-client'
2
2
  require 'json'
3
- require_relative 'client/reverse'
4
- require_relative 'client/search'
3
+ require_relative 'client/request'
5
4
 
6
5
  module OpenStreetMap
7
6
  # Client requests
8
7
  class Client
9
- include OpenStreetMap::Client::Reverse
10
- include OpenStreetMap::Client::Search
8
+ include OpenStreetMap::Client::Request
11
9
 
12
10
  BASE_URI = 'https://nominatim.openstreetmap.org/'.freeze
13
11
  DEFAULT_USER_AGENT = "webgents/open_street_map_gem_#{SecureRandom.urlsafe_base64}".freeze
14
12
  REQUEST_TIMEOUT = 1
15
13
 
16
- def initialize(args = {}); end
14
+ # Search objects by query
15
+ def search(args = {})
16
+ call('search', args)
17
+ end
18
+
19
+ # Reverse geocoding generates an address from a latitude and longitude
20
+ def reverse(args = {})
21
+ call('reverse', args)
22
+ end
17
23
  end
18
24
  end
@@ -1,3 +1,3 @@
1
1
  module OpenStreetMap
2
- VERSION = '1.1'.freeze
2
+ VERSION = '1.1.1'.freeze
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: open_street_map
3
3
  version: !ruby/object:Gem::Version
4
- version: '1.1'
4
+ version: 1.1.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - JungleCoders
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: exe
11
11
  cert_chain: []
12
- date: 2018-08-10 00:00:00.000000000 Z
12
+ date: 2018-10-31 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bundler
@@ -101,8 +101,7 @@ files:
101
101
  - bin/setup
102
102
  - lib/open_street_map.rb
103
103
  - lib/open_street_map/client.rb
104
- - lib/open_street_map/client/reverse.rb
105
- - lib/open_street_map/client/search.rb
104
+ - lib/open_street_map/client/request.rb
106
105
  - lib/open_street_map/version.rb
107
106
  - open_street_map.gemspec
108
107
  homepage: https://github.com/WebGents/open_street_map
@@ -1,28 +0,0 @@
1
- module OpenStreetMap
2
- class Client
3
- # Reverse geocoding generates an address from a latitude and longitude
4
- module Reverse
5
- def reverse(args = {})
6
- response = RestClient.get("#{args[:hostname] || BASE_URI}reverse?#{reverse_args_to_url(args)}", user_agent: args[:user_agent] || DEFAULT_USER_AGENT)
7
- sleep(REQUEST_TIMEOUT)
8
- args[:format] == 'xml' ? response.body : JSON.parse(response.body)
9
- rescue
10
- { 'errors' => 'Bad request' }
11
- end
12
-
13
- private
14
-
15
- def reverse_args_to_url(args, result = [])
16
- valid_reverse_args(args).each do |key, value|
17
- key = 'accept-language' if key == :accept_language
18
- result << "#{key}=#{value}"
19
- end
20
- result.join('&')
21
- end
22
-
23
- def valid_reverse_args(args)
24
- args.slice(:format, :lat, :lon, :zoom, :addressdetails, :extratags, :namedetails, :accept_language, :email)
25
- end
26
- end
27
- end
28
- end
@@ -1,32 +0,0 @@
1
- module OpenStreetMap
2
- class Client
3
- # Search objects by query
4
- module Search
5
- def search(args = {})
6
- response = RestClient.get("#{args[:hostname] || BASE_URI}search?#{search_arg(args[:q])}&#{search_args_to_url(args)}", user_agent: args[:user_agent] || DEFAULT_USER_AGENT)
7
- sleep(REQUEST_TIMEOUT)
8
- args[:format] == 'xml' ? response.body : JSON.parse(response.body)
9
- rescue
10
- { 'errors' => 'Bad request' }
11
- end
12
-
13
- private
14
-
15
- def search_arg(arg)
16
- "q=#{arg.gsub(/\s+/, '+')}"
17
- end
18
-
19
- def search_args_to_url(args, result = [])
20
- valid_search_args(args).each do |key, value|
21
- key = 'accept-language' if key == :accept_language
22
- result << "#{key}=#{value}"
23
- end
24
- result.join('&')
25
- end
26
-
27
- def valid_search_args(args)
28
- args.slice(:format, :addressdetails, :extratags, :namedetails, :viewbox, :bounded, :exclude_place_ids, :limit, :accept_language, :email)
29
- end
30
- end
31
- end
32
- end