overpass-api-ruby 0.1.3 → 0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c355abcbae641e6d2cc0e3294c805c356f8457fe
4
- data.tar.gz: a544c96a81dd680fdc95e686f791e866aee603ec
3
+ metadata.gz: 8ef9346aa556dd932264aaa04f016c2d86553a93
4
+ data.tar.gz: f6366d26fa31b74b910400e11f92376996dd0204
5
5
  SHA512:
6
- metadata.gz: 6a700aca42928b8c93c53925bc4f4388631842ebb3a36e18b73b4dc521b1c808c228b2d221bc30524fb39e62d13f7e3c540160d8ec39683b5c3892b1bbcadf60
7
- data.tar.gz: 9876dbd16aeebcd5eda9fed794ced6effd0f3e5266e2641f6fd2ae17051d94b8e2b1a059bc6a1a65207bd8da8599205c2600f194d18539adc7d3bb7861e64dbf
6
+ metadata.gz: 013cc225d04ad2f4fe501f2beb430ad837831ddc134e4630ac962d7f47518d34b9c9f796418d103862a2aca2a762f10be631a7db8a0b4abf252c9c16e5542f7a
7
+ data.tar.gz: 05eb5f15ed2b487300124889ca2ce7ecf5fedaa86cd3ec1c281af66c226083ca80bde55831b76ea525ecf66cf553053f20d98e25645ebca149b796bca1d37e35
data/.gitignore CHANGED
@@ -1 +1,2 @@
1
1
  .idea
2
+ *.gem
data/CHANGELOG.md ADDED
@@ -0,0 +1,7 @@
1
+ CHANGELOG
2
+ =========
3
+
4
+ v 0.2
5
+ -----
6
+ BREAKING CHANGES
7
+ The addition of XML and QL classes breaks the compatibility with previous versions. Also, the `element_limit` option was deprecated, so now it's named `maxsize`. Check the examples.
data/Gemfile.lock CHANGED
@@ -1,43 +1,39 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- overpass-api-ruby (0.1.3)
5
- diskcached
4
+ overpass-api-ruby (0.2)
6
5
  httpi (~> 2.4.0)
7
- nokogiri
8
6
 
9
7
  GEM
10
8
  remote: https://rubygems.org/
11
9
  specs:
12
- coderay (1.1.0)
13
- diff-lcs (1.2.5)
14
- diskcached (1.1.2)
15
- httpi (2.4.1)
10
+ coderay (1.1.1)
11
+ diff-lcs (1.3)
12
+ httpi (2.4.2)
16
13
  rack
14
+ socksify
17
15
  method_source (0.8.2)
18
- mini_portile (0.6.2)
19
- nokogiri (1.6.6.2)
20
- mini_portile (~> 0.6.0)
21
- pry (0.10.1)
16
+ pry (0.10.4)
22
17
  coderay (~> 1.1.0)
23
18
  method_source (~> 0.8.1)
24
19
  slop (~> 3.4)
25
- rack (1.6.4)
26
- rake (10.4.2)
27
- rspec (3.3.0)
28
- rspec-core (~> 3.3.0)
29
- rspec-expectations (~> 3.3.0)
30
- rspec-mocks (~> 3.3.0)
31
- rspec-core (3.3.2)
32
- rspec-support (~> 3.3.0)
33
- rspec-expectations (3.3.1)
20
+ rack (2.0.3)
21
+ rake (12.0.0)
22
+ rspec (3.6.0)
23
+ rspec-core (~> 3.6.0)
24
+ rspec-expectations (~> 3.6.0)
25
+ rspec-mocks (~> 3.6.0)
26
+ rspec-core (3.6.0)
27
+ rspec-support (~> 3.6.0)
28
+ rspec-expectations (3.6.0)
34
29
  diff-lcs (>= 1.2.0, < 2.0)
35
- rspec-support (~> 3.3.0)
36
- rspec-mocks (3.3.2)
30
+ rspec-support (~> 3.6.0)
31
+ rspec-mocks (3.6.0)
37
32
  diff-lcs (>= 1.2.0, < 2.0)
38
- rspec-support (~> 3.3.0)
39
- rspec-support (3.3.0)
33
+ rspec-support (~> 3.6.0)
34
+ rspec-support (3.6.0)
40
35
  slop (3.6.0)
36
+ socksify (1.7.1)
41
37
 
42
38
  PLATFORMS
43
39
  ruby
@@ -50,4 +46,4 @@ DEPENDENCIES
50
46
  rspec (>= 3)
51
47
 
52
48
  BUNDLED WITH
53
- 1.10.6
49
+ 1.13.6
data/README.md CHANGED
@@ -1,7 +1,9 @@
1
1
  Overpass API Ruby
2
2
  =================
3
3
 
4
- A Ruby wrapper for OpenStreetMap Overpass API. Returns a Hash from a query.
4
+ A Ruby wrapper for OpenStreetMap Overpass API. Supports both QL and XML.
5
+
6
+ Note: Version 0.2 introduces breaking changes. Check the file CHANGELOG.md.
5
7
 
6
8
  Install
7
9
  -------
@@ -13,47 +15,65 @@ or add `gem 'overpass-api-ruby'` to your Gemfile
13
15
  Usage
14
16
  -----
15
17
 
18
+ Using XML:
19
+
16
20
  ```ruby
17
21
  require 'overpass_api_ruby'
18
22
 
19
- ba_query = "<union><query type='relation'><has-kv k='route' v='subway'/></query>" <<
20
- "</union><union><item/><recurse type='down'/></union>"
23
+ options={:bbox => {:s => -34.705448, :n => -34.526562,
24
+ :w => -58.531471, :e => -58.335159},
25
+ :timeout => 900,
26
+ :maxsize => 1073741824}
27
+
28
+ overpass = OverpassAPI::XML.new(options)
29
+
30
+ query = "<union><query type='relation'><has-kv k='route' v='subway'/></query>" <<
31
+ "</union><union><item/><recurse type='down'/></union>"
32
+
33
+ response = overpass.query(query)
34
+ ```
35
+
36
+ Using QL:
37
+
38
+ ```ruby
39
+ require 'overpass_api_ruby'
21
40
 
22
41
  options={:bbox => {:s => -34.705448, :n => -34.526562,
23
42
  :w => -58.531471, :e => -58.335159},
24
43
  :timeout => 900,
25
- :element_limit => 1073741824,
26
- :json => true}
44
+ :maxsize => 1073741824}
27
45
 
28
- overpass = OverpassAPI.new(options)
29
- result_hash = overpass.query(ba_query)
46
+ overpass = OverpassAPI::QL.new(options)
47
+
48
+ query = "rel['route'='subway'];(._;>;);out body;"
49
+
50
+ response = overpass.query(query)
30
51
  ```
31
52
 
32
53
  Options on instantiation
33
54
  ------------------------
34
55
  ```
35
56
  bbox Hash. Global bounding box.
36
- endpoint String.
57
+ endpoint String.
37
58
  Defaults to http://overpass-api.de/api/interpreter
38
59
  timeout Integer.
39
- element_limit Integer.
40
- json Boolean. API response is in JSON format, so parse to hash
41
- doesn't use the private parse_nokogiri method.
42
- Default: false.
43
- cache_expiration_time Integer. Default: 7200.
44
- dont_use_cache Boolean. Default: false.
60
+ maxsize Integer.
45
61
  ```
46
62
  See [Overpass API](http://wiki.openstreetmap.org/wiki/Overpass_API/Language_Guide)
47
63
 
48
64
  Public methods
49
65
  --------------
50
66
 
67
+ Both `QL` and `XML` classes have the same public methods:
68
+
51
69
  ```ruby
52
- query (<String query>) Intended to pass only children tags of <osm-script>.
70
+ query (<String query>) Performs the query passed using the global values set on instantiation.
53
71
 
54
72
  raw_query (<String query>) The whole query must be passed.
55
73
 
56
- bbox (s,n,w,e) Defines global bounding box.
74
+ buid_query (<String query>) Returns a String containing the whole query.
75
+
76
+ bounding_box (s,n,w,e) Defines the global bounding box.
57
77
  ```
58
78
 
59
79
 
data/lib/base.rb ADDED
@@ -0,0 +1,40 @@
1
+ require 'httpi'
2
+ require 'open-uri'
3
+ require 'json'
4
+
5
+ module OverpassAPI
6
+ VERSION='0.2'
7
+
8
+ class Base
9
+ DEFAULT_ENDPOINT='http://overpass-api.de/api/interpreter?data='
10
+
11
+ def initialize(args={})
12
+ bbox = args[:bbox]
13
+ bounding_box(bbox[:s],bbox[:n],bbox[:w],bbox[:e]) if bbox
14
+
15
+ @endpoint = args[:endpoint] || DEFAULT_ENDPOINT
16
+ @timeout = args[:timeout]
17
+ @maxsize = args[:maxsize]
18
+ end
19
+
20
+ def bounding_box(s,n,w,e)
21
+ @bbox = "#{s},#{w},#{n},#{e}"
22
+ end
23
+
24
+ def query(q)
25
+ perform build_query(q)
26
+ end
27
+
28
+ def raw_query(q)
29
+ perform q
30
+ end
31
+
32
+ private
33
+
34
+ def perform(q)
35
+ url = URI::encode("#{@endpoint}#{q}")
36
+ r = HTTPI::Request.new(url)
37
+ JSON.parse(HTTPI.get(r).body, symbolize_names: true)
38
+ end
39
+ end
40
+ end
@@ -1,92 +1,2 @@
1
- require 'httpi'
2
- require 'diskcached'
3
- require 'open-uri'
4
- require 'json'
5
- require 'nokogiri'
6
-
7
- class OverpassAPI
8
- VERSION='0.1.3'
9
- DEFAULT_ENDPOINT='http://overpass-api.de/api/interpreter?data='
10
-
11
- def initialize(args={})
12
- bbox = args[:bbox]
13
- bbox(bbox[:s],bbox[:n],bbox[:w],bbox[:e]) if bbox
14
-
15
- cache_expiration_time = args[:cache_expiration_time] || 7200
16
- @cache = Diskcached.new('/tmp/cache',cache_expiration_time,true)
17
-
18
- @endpoint = args[:endpoint] || DEFAULT_ENDPOINT
19
- @json = args[:json] ? "output='json'" : ''
20
- @timeout = args[:timeout] ? "timeout='#{args[:timeout]}'" : ''
21
- @element_limit = args[:element_limit] ? "element-limit='#{args[:element_limit]}'" : ''
22
- @dont_use_cache = args[:dont_use_cache]
23
- end
24
-
25
- def bbox(s,n,w,e)
26
- @bbox = "bbox='#{s},#{w},#{n},#{e}'"
27
- end
28
-
29
- def query(query)
30
- return unless query
31
- perform "<osm-script #{@bbox} #{@timeout} #{@element_limit} #{@json}>" <<
32
- "#{query}<print/></osm-script>"
33
- end
34
-
35
- def raw_query(query)
36
- return unless query
37
- perform query
38
- end
39
-
40
- private
41
- def request(url)
42
- r = HTTPI::Request.new(url)
43
- HTTPI.get(r).body
44
- end
45
-
46
- def perform(query)
47
- url = URI::encode("#{@endpoint}#{query}")
48
- data = if @dont_use_cache
49
- request url
50
- else
51
- @cache.cache("overpass_api_ruby_#{query}") do
52
- request url
53
- end
54
- end
55
-
56
- return JSON.parse(data, :symbolize_names=> true)[:elements] unless @json.empty?
57
-
58
- doc = Nokogiri::XML(data) do |config|
59
- config.options = Nokogiri::XML::ParseOptions::NOBLANKS
60
- end
61
-
62
- raw = doc.children.first.children.select{|e| e.name != 'note' and e.name != 'meta'}
63
- parse_nokogiri raw
64
- end
65
-
66
- def parse_nokogiri(xml)
67
- xml.map {|e|
68
- element = {:type => e.name}
69
-
70
- e.attributes.each {|a|
71
- element[a[1].name.to_sym] = a[1].value
72
- }
73
-
74
- members = e.children.select{|n| n.name != 'tag'}.map {|child|
75
- key_values={:type => child.name }
76
- child.attributes.each {|a|
77
- key_values[a[1].name.to_sym] = a[1].value
78
- }
79
- key_values
80
- }
81
- element[:members] = members unless members.empty?
82
-
83
- tags={}
84
- e.children.select{|n| n.name == 'tag'}.each {|child|
85
- tags[child.values.first] = child.values[1]
86
- }
87
- element[:tags] = tags unless tags.empty?
88
-
89
- element
90
- }
91
- end
92
- end
1
+ require_relative "xml"
2
+ require_relative "ql"
data/lib/ql.rb ADDED
@@ -0,0 +1,16 @@
1
+ require_relative "base"
2
+
3
+ module OverpassAPI
4
+ class QL < Base
5
+ def build_query(q)
6
+ header = ""
7
+ header << "[bbox:#{@bbox}]" if @bbox
8
+ header << "[timeout:#{@timeout}]" if @timeout
9
+ header << "[maxsize:#{@maxsize}]" if @maxsize
10
+
11
+ header << "[out:json]"
12
+
13
+ "#{header};#{q}"
14
+ end
15
+ end
16
+ end
data/lib/xml.rb ADDED
@@ -0,0 +1,12 @@
1
+ require_relative 'base'
2
+
3
+ module OverpassAPI
4
+ class XML < Base
5
+ def build_query(q)
6
+ bbox = @bbox ? "bbox='#{@bbox}'" : ''
7
+ timeout = @timeout ? "timeout='#{@timeout}'" : ''
8
+ maxsize = @maxsize ? "maxsize='#{@maxsize}'" : ''
9
+ "<osm-script #{bbox} #{timeout} #{maxsize} output='json'>#{q}<print/></osm-script>"
10
+ end
11
+ end
12
+ end
@@ -9,7 +9,7 @@ Gem::Specification.new do |spec|
9
9
  spec.authors = ["Bruno Salerno"]
10
10
  spec.email = ["br.salerno@gmail.com"]
11
11
  spec.description = %q{A Ruby wrapper for OpenStreetMap Overpass API}
12
- spec.summary = %q{Returns a Hash from a query}
12
+ spec.summary = %q{This gem will allow you to perform queries to OSM Overpass API using QL or XML}
13
13
  spec.homepage = "https://github.com/BrunoSalerno/overpass-api-ruby"
14
14
  spec.license = "MIT"
15
15
 
@@ -19,8 +19,6 @@ Gem::Specification.new do |spec|
19
19
  spec.require_paths = ["lib"]
20
20
 
21
21
  spec.add_runtime_dependency "httpi", "~> 2.4.0"
22
- spec.add_runtime_dependency "diskcached"
23
- spec.add_runtime_dependency "nokogiri"
24
22
 
25
23
  spec.add_development_dependency "bundler", "~> 1.3"
26
24
  spec.add_development_dependency "rake"
@@ -2,18 +2,33 @@ require 'spec_helper'
2
2
  require 'overpass_api_ruby'
3
3
 
4
4
  describe OverpassAPI do
5
- it "'s example works" do
5
+ describe OverpassAPI::XML do
6
+ it "'s example works" do
7
+ options={:bbox => {:s => -34.705448, :n => -34.526562,
8
+ :w => -58.531471, :e => -58.335159},
9
+ :timeout => 900}
6
10
 
7
- ba_query = "<union><query type='relation'><has-kv k='route' v='subway'/></query>" <<
8
- "</union><union><item/><recurse type='down'/></union>"
11
+ overpass = OverpassAPI::XML.new(options)
9
12
 
10
- options={:bbox => {:s => -34.705448, :n => -34.526562,
11
- :w => -58.531471, :e => -58.335159},
12
- :timeout => 900,
13
- :element_limit => 1,
14
- :json => true}
13
+ ba_query = "<union><query type='relation'><has-kv k='route' v='subway'/></query>" <<
14
+ "</union><union><item/><recurse type='down'/></union>"
15
15
 
16
- overpass = OverpassAPI.new(options)
17
- expect(overpass.query(ba_query)).to be_a(Array)
16
+
17
+ expect(overpass.query(ba_query)[:elements]).to be_a(Array)
18
+ end
19
+ end
20
+
21
+ describe OverpassAPI::QL do
22
+ it "'s example works" do
23
+ options={:bbox => {:s => -34.705448, :n => -34.526562,
24
+ :w => -58.531471, :e => -58.335159},
25
+ :timeout => 900}
26
+
27
+ overpass = OverpassAPI::QL.new(options)
28
+
29
+ ba_query = "rel['route'='subway'];(._;>;);out body;"
30
+
31
+ expect(overpass.query(ba_query)[:elements]).to be_a(Array)
32
+ end
18
33
  end
19
34
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: overpass-api-ruby
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.3
4
+ version: '0.2'
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bruno Salerno
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-08-30 00:00:00.000000000 Z
11
+ date: 2017-05-20 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: httpi
@@ -24,34 +24,6 @@ dependencies:
24
24
  - - "~>"
25
25
  - !ruby/object:Gem::Version
26
26
  version: 2.4.0
27
- - !ruby/object:Gem::Dependency
28
- name: diskcached
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - ">="
32
- - !ruby/object:Gem::Version
33
- version: '0'
34
- type: :runtime
35
- prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - ">="
39
- - !ruby/object:Gem::Version
40
- version: '0'
41
- - !ruby/object:Gem::Dependency
42
- name: nokogiri
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - ">="
46
- - !ruby/object:Gem::Version
47
- version: '0'
48
- type: :runtime
49
- prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - ">="
53
- - !ruby/object:Gem::Version
54
- version: '0'
55
27
  - !ruby/object:Gem::Dependency
56
28
  name: bundler
57
29
  requirement: !ruby/object:Gem::Requirement
@@ -117,12 +89,16 @@ extra_rdoc_files: []
117
89
  files:
118
90
  - ".gitignore"
119
91
  - ".rspec"
92
+ - CHANGELOG.md
120
93
  - Gemfile
121
94
  - Gemfile.lock
122
95
  - LICENSE
123
96
  - README.md
124
97
  - Rakefile
98
+ - lib/base.rb
125
99
  - lib/overpass_api_ruby.rb
100
+ - lib/ql.rb
101
+ - lib/xml.rb
126
102
  - overpass-api-ruby.gemspec
127
103
  - spec/overpass_api_ruby_spec.rb
128
104
  - spec/spec_helper.rb
@@ -146,10 +122,11 @@ required_rubygems_version: !ruby/object:Gem::Requirement
146
122
  version: '0'
147
123
  requirements: []
148
124
  rubyforge_project:
149
- rubygems_version: 2.2.2
125
+ rubygems_version: 2.5.1
150
126
  signing_key:
151
127
  specification_version: 4
152
- summary: Returns a Hash from a query
128
+ summary: This gem will allow you to perform queries to OSM Overpass API using QL or
129
+ XML
153
130
  test_files:
154
131
  - spec/overpass_api_ruby_spec.rb
155
132
  - spec/spec_helper.rb