rosemary 0.4.2 → 0.4.3

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -2,6 +2,12 @@ rosemary CHANGELOG
2
2
  ===================================
3
3
  This file is used to list changes made in each version.
4
4
 
5
+ v0.4.2 (2014 Aug 7)
6
+ ------
7
+
8
+ * Allow arbitrary tags to be attached to changeset
9
+ * Upgrade all specs to 'expect' syntax instead of 'should'
10
+
5
11
 
6
12
  v0.4.1 (2014 May 27)
7
13
  ------
data/lib/rosemary/api.rb CHANGED
@@ -93,6 +93,19 @@ module Rosemary
93
93
  resp
94
94
  end
95
95
 
96
+ # Get the bounding box which is represented by the Rosemary::BoundingBox
97
+ #
98
+ # @param [Numeric] left is the longitude of the left (westernmost) side of the bounding box.
99
+ # @param [Numeric] bottom is the latitude of the bottom (southernmost) side of the bounding box.
100
+ # @param [Numeric] right is the longitude of the right (easternmost) side of the bounding box.
101
+ # @param [Numeric] top is the latitude of the top (northernmost) side of the bounding box.
102
+ # @return [Rosemary::BoundingBox] the bounding box containing all ways, nodes and relations inside the given coordinates
103
+ #
104
+ def find_bounding_box(left,bottom,right,top)
105
+ do_request(:get, "/map?bbox=#{left},#{bottom},#{right},#{top}", {} )
106
+ end
107
+
108
+
96
109
  def permissions
97
110
  if client.nil?
98
111
  get("/permissions")
@@ -282,4 +295,4 @@ module Rosemary
282
295
  end
283
296
 
284
297
  end
285
- end
298
+ end
@@ -0,0 +1,46 @@
1
+ require 'builder'
2
+
3
+ module Rosemary
4
+ # OpenStreetMap Boundary Box.
5
+ #
6
+ class BoundingBox < Element
7
+
8
+ attr_accessor :nodes, :ways, :relations, :minlat, :minlon, :maxlat, :maxlon
9
+
10
+ # Create new Node object.
11
+ #
12
+ # If +id+ is +nil+ a new unique negative ID will be allocated.
13
+ def initialize(attrs = {})
14
+ attrs = attrs.dup.stringify_keys!
15
+ @minlat = attrs['minlat'].to_f
16
+ @minlon = attrs['minlon'].to_f
17
+ @maxlat = attrs['maxlat'].to_f
18
+ @maxlon = attrs['maxlon'].to_f
19
+
20
+ @nodes = []
21
+ @ways = []
22
+ @relations = []
23
+ end
24
+
25
+
26
+ def type
27
+ 'BoundingBoxBox'
28
+ end
29
+
30
+ # List of attributes for a bounds element
31
+ def attribute_list
32
+ [:minlat, :minlon, :maxlat, :maxlon]
33
+ end
34
+
35
+ def to_xml(options = {})
36
+ xml = options[:builder] ||= Builder::XmlMarkup.new
37
+ xml.instruct! unless options[:skip_instruct]
38
+ xml.osm(:generator => "rosemary v#{Rosemary::VERSION}", :version => Rosemary::Api::API_VERSION) do
39
+ xml.bounds(attributes)
40
+ [ nodes, ways, relations].each do |elements|
41
+ elements.each { |e| e.to_xml(:builder => xml, :skip_instruct => true) }
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -26,7 +26,12 @@ class Rosemary::Parser < HTTParty::Parser
26
26
 
27
27
  @parser.callbacks = self
28
28
  @parser.parse
29
- @collection.empty? ? @context : @collection
29
+
30
+ if @bounding_box
31
+ @bounding_box
32
+ else
33
+ @collection.empty? ? @context : @collection
34
+ end
30
35
  end
31
36
 
32
37
  def plain
@@ -70,6 +75,7 @@ class Rosemary::Parser < HTTParty::Parser
70
75
  when 'permissions' then _start_permissions(attr_hash)
71
76
  when 'permission' then _start_permission(attr_hash)
72
77
  when 'note' then _start_note(attr_hash)
78
+ when 'bounds' then _start_bounds(attr_hash)
73
79
  end
74
80
  end
75
81
  end
@@ -101,15 +107,21 @@ class Rosemary::Parser < HTTParty::Parser
101
107
 
102
108
  private
103
109
  def _start_node(attr_hash)
104
- @context = Rosemary::Node.new(attr_hash)
110
+ node = Rosemary::Node.new(attr_hash)
111
+ @bounding_box.nodes << node if @bounding_box
112
+ @context = node
105
113
  end
106
114
 
107
115
  def _start_way(attr_hash)
108
- @context = Rosemary::Way.new(attr_hash)
116
+ way = Rosemary::Way.new(attr_hash)
117
+ @bounding_box.ways << way if @bounding_box
118
+ @context = way
109
119
  end
110
120
 
111
121
  def _start_relation(attr_hash)
112
- @context = Rosemary::Relation.new(attr_hash)
122
+ relation = Rosemary::Relation.new(attr_hash)
123
+ @bounding_box.relations << relation if @bounding_box
124
+ @context = relation
113
125
  end
114
126
 
115
127
  def _start_changeset(attr_hash)
@@ -163,4 +175,8 @@ class Rosemary::Parser < HTTParty::Parser
163
175
  @context.lon = attr_hash['zoom'] if attr_hash['zoom']
164
176
  end
165
177
 
166
- end
178
+ def _start_bounds(attr_hash)
179
+ @bounding_box = Rosemary::BoundingBox.new(attr_hash)
180
+ end
181
+
182
+ end
@@ -1,4 +1,4 @@
1
1
  module Rosemary
2
2
  # The current version of this gem.
3
- VERSION = "0.4.2"
3
+ VERSION = "0.4.3"
4
4
  end
data/lib/rosemary.rb CHANGED
@@ -11,6 +11,7 @@ require 'rosemary/changeset'
11
11
  require 'rosemary/relation'
12
12
  require 'rosemary/member'
13
13
  require 'rosemary/user'
14
+ require 'rosemary/bounding_box'
14
15
  require 'rosemary/permissions'
15
16
  require 'rosemary/errors'
16
17
  require 'rosemary/client'
@@ -0,0 +1,104 @@
1
+ require 'spec_helper'
2
+ include Rosemary
3
+ describe BoundingBox do
4
+
5
+ before do
6
+ WebMock.disable_net_connect!
7
+ end
8
+
9
+ let :osm do
10
+ Api.new
11
+ end
12
+
13
+ def valid_fake_boundary
14
+ boundary=<<-EOF
15
+ <osm>
16
+ <bounds minlat="37.3855400" minlon="-122.0359880" maxlat="37.4116770" maxlon="-122.0094800"/>
17
+ <node id="3147580094" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3872247" lon="-122.0216695"/>
18
+ <node id="3147580093" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3872110" lon="-122.0216157"/>
19
+ <node id="3147580084" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3870427" lon="-122.0216838"/>
20
+ <node id="3147580085" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3870564" lon="-122.0217376"/>
21
+ <node id="3147580103" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3873678" lon="-122.0218229"/>
22
+ <node id="3147580098" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3872486" lon="-122.0213856"/>
23
+ <node id="3147580089" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3871661" lon="-122.0214213"/>
24
+ <node id="3147580092" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3871896" lon="-122.0215072"/>
25
+ <node id="3147580077" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3869174" lon="-122.0216248"/>
26
+ <node id="3147580078" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3869465" lon="-122.0217314"/>
27
+ <node id="3147580081" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3870081" lon="-122.0217048"/>
28
+ <node id="3147580083" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3870225" lon="-122.0217578"/>
29
+ <node id="3147580079" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3869654" lon="-122.0217825"/>
30
+ <node id="3147580080" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3869935" lon="-122.0218854"/>
31
+ <node id="3147580099" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3872596" lon="-122.0217704"/>
32
+ <node id="3147580101" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:05Z" user="matthieun" uid="595221" lat="37.3872838" lon="-122.0218592"/>
33
+ <way id="309425057" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:06Z" user="matthieun" uid="595221">
34
+ <nd ref="3147580094"/>
35
+ <nd ref="3147580093"/>
36
+ <nd ref="3147580084"/>
37
+ <nd ref="3147580085"/>
38
+ <nd ref="3147580094"/>
39
+ </way>
40
+ <way id="309425054" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:06Z" user="matthieun" uid="595221">
41
+ <nd ref="3147580103"/>
42
+ <nd ref="3147580098"/>
43
+ <nd ref="3147580089"/>
44
+ <nd ref="3147580092"/>
45
+ <nd ref="3147580077"/>
46
+ <nd ref="3147580078"/>
47
+ <nd ref="3147580081"/>
48
+ <nd ref="3147580083"/>
49
+ <nd ref="3147580079"/>
50
+ <nd ref="3147580080"/>
51
+ <nd ref="3147580099"/>
52
+ <nd ref="3147580101"/>
53
+ <nd ref="3147580103"/>
54
+ </way>
55
+ <relation id="4133073" visible="true" version="1" changeset="26302066" timestamp="2014-10-24T15:12:07Z" user="matthieun" uid="595221">
56
+ <member type="way" ref="309425057" role="inner"/>
57
+ <member type="way" ref="309425054" role="outer"/>
58
+ <tag k="building" v="residential"/>
59
+ <tag k="type" v="multipolygon"/>
60
+ </relation>
61
+ </osm>
62
+ EOF
63
+ end
64
+
65
+ describe '#find:' do
66
+ it "should find an array of Ways, Nodes and Relations from the API response via find_boundary" do
67
+ stub_request(:get, "http://www.openstreetmap.org/api/0.6/map?bbox=-122.035988,37.38554,-122.00948,37.411677").to_return(:status => 200, :body => valid_fake_boundary, :headers => {'Content-Type' => 'application/xml'})
68
+ boundary = osm.find_bounding_box(-122.035988,37.38554,-122.00948,37.411677)
69
+
70
+ expect(boundary.class).to eql BoundingBox
71
+ expect(boundary.nodes).to include(Rosemary::Node.new({"id"=>"3147580094", "visible"=>"true", "version"=>"1", "changeset"=>"26302066", "timestamp"=>"2014-10-24T15:12:05Z", "user"=>"matthieun", "uid"=>"595221", "lat"=>"37.3872247", "lon"=>"-122.0216695"}))
72
+ expect(boundary.ways.map { |it| it.id }).to include(309425054)
73
+ expect(boundary.relations.map { |it| it.id }).to include(4133073)
74
+
75
+ parsed_relation = boundary.relations.first
76
+
77
+ expect(parsed_relation.members.length).to equal(2)
78
+ expect(parsed_relation.tags.length).to equal(2)
79
+ expect(boundary.minlat).to be_within(0.00001).of(37.3855400)
80
+ expect(boundary.minlon).to be_within(0.00001).of(-122.0359880)
81
+ expect(boundary.maxlat).to be_within(0.00001).of(37.4116770)
82
+ expect(boundary.maxlon).to be_within(0.00001).of(-122.0094800)
83
+ end
84
+ end
85
+
86
+ describe '#xml:' do
87
+ it "should produce an xml that is equivalent to the parsed one" do
88
+ stub_request(:get, "http://www.openstreetmap.org/api/0.6/map?bbox=-122.035988,37.38554,-122.00948,37.411677").to_return(:status => 200, :body => valid_fake_boundary, :headers => {'Content-Type' => 'application/xml'})
89
+ boundary = osm.find_bounding_box(-122.035988,37.38554,-122.00948,37.411677)
90
+
91
+ xml = boundary.to_xml
92
+ reparsed_boundary = Parser.call(xml, :xml)
93
+
94
+ expect(reparsed_boundary.minlat).to eql(boundary.minlat)
95
+ expect(reparsed_boundary.nodes.length).to eql(boundary.nodes.length)
96
+ expect(reparsed_boundary.nodes.first.lat).to eql(boundary.nodes.first.lat)
97
+ expect(reparsed_boundary.ways.length).to eql(boundary.ways.length)
98
+ expect(reparsed_boundary.ways.first.nodes.first).to eql(boundary.ways.first.nodes.first)
99
+ expect(reparsed_boundary.relations.length).to eql(boundary.relations.length)
100
+ expect(reparsed_boundary.relations.first.tags.first).to eql(boundary.relations.first.tags.first)
101
+ end
102
+
103
+ end
104
+ end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rosemary
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.2
4
+ version: 0.4.3
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-08-07 00:00:00.000000000 Z
12
+ date: 2015-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: httparty
@@ -212,6 +212,7 @@ files:
212
212
  - lib/rosemary.rb
213
213
  - lib/rosemary/api.rb
214
214
  - lib/rosemary/basic_auth_client.rb
215
+ - lib/rosemary/bounding_box.rb
215
216
  - lib/rosemary/changeset.rb
216
217
  - lib/rosemary/client.rb
217
218
  - lib/rosemary/element.rb
@@ -228,6 +229,7 @@ files:
228
229
  - lib/rosemary/version.rb
229
230
  - lib/rosemary/way.rb
230
231
  - rosemary.gemspec
232
+ - spec/integration/boundary_spec.rb
231
233
  - spec/integration/changeset_spec.rb
232
234
  - spec/integration/node_spec.rb
233
235
  - spec/integration/note_spec.rb
@@ -259,7 +261,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
259
261
  version: '0'
260
262
  segments:
261
263
  - 0
262
- hash: -1969309869700129610
264
+ hash: 3366646183016379761
263
265
  required_rubygems_version: !ruby/object:Gem::Requirement
264
266
  none: false
265
267
  requirements:
@@ -273,6 +275,7 @@ signing_key:
273
275
  specification_version: 3
274
276
  summary: OpenStreetMap API client for ruby
275
277
  test_files:
278
+ - spec/integration/boundary_spec.rb
276
279
  - spec/integration/changeset_spec.rb
277
280
  - spec/integration/node_spec.rb
278
281
  - spec/integration/note_spec.rb