osmn 0.1.1 → 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.
@@ -1,8 +1,8 @@
1
+ require 'osmn/structs'
1
2
  require 'osmn/base'
2
3
  require 'osmn/search'
3
4
  require 'osmn/reverse'
4
5
  require 'osmn/version'
5
- require 'osmn/hash'
6
6
 
7
7
  module OSMN
8
8
 
@@ -5,7 +5,37 @@ require 'json'
5
5
  module OSMN
6
6
  class Base
7
7
 
8
+ def initialize(params)
9
+ @params = {}
10
+ self.params = params
11
+ end
12
+
13
+ def params=(value)
14
+ if value.is_a? Hash
15
+ value.each do |k,v|
16
+ @params[k.to_sym] = v
17
+ end
18
+ end
19
+
20
+ # We override the format because we expect a JSON response
21
+ @params[:format] = :json
22
+ end
23
+
24
+ def params
25
+ @params
26
+ end
27
+
8
28
  private
29
+ def request(action)
30
+ query = build_query(@params)
31
+
32
+ uri = URI("http://nominatim.openstreetmap.org/#{action.to_s}?#{query}")
33
+ request = Net::HTTP::Get.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
34
+ response = Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}
35
+
36
+ results = build_results(response.body) if response.code == '200'
37
+ end
38
+
9
39
  def build_query(params)
10
40
  query = params.map do |key, value|
11
41
  "#{key.to_s}=#{URI.escape(value.to_s)}" if value
@@ -23,9 +53,12 @@ module OSMN
23
53
 
24
54
  def build_place(place)
25
55
  if place.is_a? Hash
26
- result = place.to_struct('Result')
27
- if result.respond_to?('address')
28
- result.address = result.address.to_struct('Address')
56
+ result = Result.new
57
+ place.each {|k,v| result[k] = v }
58
+
59
+ if place["address"].is_a? Hash
60
+ result.address = Address.new
61
+ place["address"].each{|k,v| result.address[k] = v }
29
62
  end
30
63
 
31
64
  result
@@ -1,36 +1,8 @@
1
1
  module OSMN
2
2
  class Reverse < OSMN::Base
3
3
 
4
- def initialize(params)
5
- @params = {}
6
- self.params = params
7
- end
8
-
9
4
  def reverse_geocode
10
- if @params[:lat] && @params[:lon]
11
- query = build_query(@params)
12
-
13
- uri = URI("http://nominatim.openstreetmap.org/reverse?#{query}")
14
- request = Net::HTTP::Get.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
15
- response = Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}
16
-
17
- results = build_results(response.body) if response.code == '200'
18
- end
19
- end
20
-
21
- def params=(value)
22
- if value.is_a? Hash
23
- value.each do |k,v|
24
- @params[k.to_sym] = v
25
- end
26
- end
27
-
28
- # We override the format because we expect a JSON response
29
- @params[:format] = :json
30
- end
31
-
32
- def params
33
- @params
5
+ request(:reverse) if @params[:lat] && @params[:lon]
34
6
  end
35
7
  end
36
8
  end
@@ -1,36 +1,8 @@
1
1
  module OSMN
2
2
  class Search < OSMN::Base
3
3
 
4
- def initialize(params)
5
- @params = {}
6
- self.params = params
7
- end
8
-
9
4
  def search
10
- if @params[:q]
11
- query = build_query(@params)
12
-
13
- uri = URI("http://nominatim.openstreetmap.org/search?#{query}")
14
- request = Net::HTTP::Get.new(uri.request_uri, initheader = {'Content-Type' => 'application/json'})
15
- response = Net::HTTP.start(uri.host, uri.port) {|http| http.request(request)}
16
-
17
- results = build_results(response.body) if response.code == '200'
18
- end
19
- end
20
-
21
- def params=(value)
22
- if value.is_a? Hash
23
- value.each do |k,v|
24
- @params[k.to_sym] = v
25
- end
26
- end
27
-
28
- # We override the format because we expect a JSON response
29
- @params[:format] = :json
30
- end
31
-
32
- def params
33
- @params
5
+ request(:search) if @params[:q]
34
6
  end
35
7
  end
36
8
  end
@@ -0,0 +1,26 @@
1
+ module OSMN
2
+ Result = Struct.new(
3
+ :place_id,
4
+ :licence,
5
+ :osm_type,
6
+ :osm_id,
7
+ :boundingbox,
8
+ :lat,
9
+ :lon,
10
+ :display_name,
11
+ :class,
12
+ :type,
13
+ :address)
14
+
15
+ Address = Struct.new(
16
+ :house_number,
17
+ :road,
18
+ :suburb,
19
+ :city,
20
+ :county,
21
+ :state_district,
22
+ :state,
23
+ :postcode,
24
+ :country,
25
+ :country_code)
26
+ end
@@ -1,3 +1,3 @@
1
1
  module OSMN
2
- VERSION = '0.1.1'
2
+ VERSION = '0.1.2'
3
3
  end
@@ -42,7 +42,7 @@ class Tests < Test::Unit::TestCase
42
42
  assert_respond_to(response, :type)
43
43
 
44
44
  assert_raise NoMethodError do
45
- response.address
45
+ response.address.house_number
46
46
  end
47
47
  end
48
48
 
@@ -86,7 +86,7 @@ class Tests < Test::Unit::TestCase
86
86
  assert_respond_to(response, :display_name)
87
87
 
88
88
  assert_raise NoMethodError do
89
- response.address
89
+ response.address.house_number
90
90
  end
91
91
  end
92
92
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: osmn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
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: 2012-11-07 00:00:00.000000000 Z
12
+ date: 2013-01-28 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: json
@@ -53,10 +53,10 @@ files:
53
53
  - LICENSE
54
54
  - Rakefile
55
55
  - lib/osmn.rb
56
+ - lib/osmn/structs.rb
56
57
  - lib/osmn/base.rb
57
58
  - lib/osmn/search.rb
58
59
  - lib/osmn/reverse.rb
59
- - lib/osmn/hash.rb
60
60
  - lib/osmn/version.rb
61
61
  - test/test_osmn.rb
62
62
  homepage: http://github.com/kalmbach/osmn
@@ -1,5 +0,0 @@
1
- class Hash
2
- def to_struct(name)
3
- Struct.new(name, *keys).new(*values)
4
- end
5
- end