michaeldwan-urban-mapping-api 0.9.2 → 0.9.3

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.
@@ -25,13 +25,31 @@ Before you do anything, create an instance of UrbanMapping::Interface
25
25
 
26
26
  For premium API access, include the premium API key
27
27
 
28
- interface = UrbanMapping::Interface.new('my-api-key', 'my-shared-secred')
28
+ interface = UrbanMapping::Interface.new('my-api-key', :shared_secret => 'my-shared-secred')
29
29
  interface.premium_api?
30
30
  # => true
31
+
32
+ The default return value of all methods is an OpenStruct or array of OpenStructs.
33
+
34
+ interface = UrbanMapping::Interface.new('my-api-key')
35
+ hood = interface.get_neighborhood_detail(3094847)
36
+ hood
37
+ # => #<OpenStruct city="Chicago", name="The Loop", state="IL", ...>
38
+ hood.city
39
+ # => "Chicago"
40
+
41
+ If you want the raw hash output, pass :raw => true to the constructor
42
+
43
+ interface = UrbanMapping::Interface.new('my-api-key', :shared_secret => 'my-shared-secred', :raw => true)
44
+ hood = interface.get_neighborhood_detail(3094847)
45
+ hood
46
+ # => {"wkt_centroid"=>"POINT(-87.6260772332496 41.8782770670931)", "name"=>"The Loop", "city"=>"Chicago"...
47
+ hood.city
48
+ # => "Chicago"
31
49
 
32
50
  (If you don't have an api key, go get one at http://developer.urbanmapping.com/accounts/register/)
33
51
 
34
- Now that you have an instance of the interface, you can make calls to the service
52
+ Now that you have an instance of the interface, you can make calls to the service. (The below examples are using raw output.)
35
53
 
36
54
  interface.get_neighborhoods_by_postal_code('60654')
37
55
  # => [{"name"=>"River North", "city"=>"Chicago", "country"=>"USA", "id"=>3320072, "state"=>"IL"}, ...]
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.9.2
1
+ 0.9.3
@@ -0,0 +1,19 @@
1
+ class Object
2
+ def to_openstruct
3
+ self
4
+ end
5
+ end
6
+
7
+ class Array
8
+ def to_openstruct
9
+ map{ |el| el.to_openstruct }
10
+ end
11
+ end
12
+
13
+ class Hash
14
+ def to_openstruct
15
+ mapped = {}
16
+ each{ |key,value| mapped[key] = value.to_openstruct }
17
+ OpenStruct.new(mapped)
18
+ end
19
+ end
@@ -1,6 +1,9 @@
1
1
  require 'curl'
2
2
  require 'json'
3
3
  require 'digest'
4
+ require 'ostruct'
5
+ require 'uri'
6
+ require "#{File.dirname(__FILE__)}/core_ext"
4
7
 
5
8
  module UrbanMapping
6
9
  class RequestError < StandardError
@@ -15,14 +18,18 @@ module UrbanMapping
15
18
  class Interface
16
19
  ENDPOINT = 'http://api1.urbanmapping.com/neighborhoods/rest'
17
20
 
18
- attr_reader :api_key, :shared_secret
21
+ attr_reader :api_key, :shared_secret, :options
19
22
 
20
23
  # Create a new instance.
21
24
  # Requeres an api_key. A shared key needs to be provided for
22
25
  # access to premium API methods.
23
- def initialize(api_key, shared_secret = nil)
26
+ def initialize(api_key, options = {})
27
+ options = {
28
+ :raw => false
29
+ }.merge(options)
24
30
  @api_key = api_key
25
- @shared_secret = shared_secret
31
+ @shared_secret = options.delete(:shared_secret)
32
+ @options = options
26
33
  end
27
34
 
28
35
  # Returns true if a shard_secret was provided to the constructor.
@@ -56,7 +63,7 @@ module UrbanMapping
56
63
  # and lists neighborhoods containing the point in a single response.
57
64
  # This is technically executed in a single request, but for the purposes
58
65
  # of account administration a single invocation is counted as two calls.
59
- def get_neighborhoods_by_address(street, city, state, country = nil)
66
+ def get_neighborhoods_by_address(street, city, state, country = 'USA')
60
67
  perform('getNeighborhoodsByAddress', :street => street,
61
68
  :city => city,
62
69
  :state => state,
@@ -64,7 +71,7 @@ module UrbanMapping
64
71
  end
65
72
 
66
73
  # Returns a list of neighborhood for the requested city.
67
- def get_neighborhoods_by_city_state_country(city, state, country = nil)
74
+ def get_neighborhoods_by_city_state_country(city, state, country = 'USA')
68
75
  perform('getNeighborhoodsByCityStateCountry', :city => city,
69
76
  :state => state,
70
77
  :country => country)
@@ -98,11 +105,11 @@ module UrbanMapping
98
105
  def perform(method, parameters)
99
106
  parameters.merge!({
100
107
  :format => 'json',
101
- :apikey => api_key,
108
+ :apikey => api_key
102
109
  })
103
110
  parameters.merge!(:sig => generate_signature) if premium_api?
104
-
105
- query_string = parameters.to_a.map{|x| x.join('=')}.join('&')
111
+
112
+ query_string = parameters.to_a.map{ |x| x.map{|val| URI.encode(val.to_s)}.join('=') }.join('&')
106
113
 
107
114
  url = "#{ENDPOINT}/#{method}?#{query_string}"
108
115
  response = Curl::Easy.perform(url)
@@ -110,8 +117,12 @@ module UrbanMapping
110
117
  if response.response_code != 200
111
118
  raise RequestError.new(response.response_code, url, response.body_str)
112
119
  end
120
+
121
+ output = JSON.parse(response.body_str)
122
+
123
+ return output if options[:raw]
113
124
 
114
- JSON.parse(response.body_str)
125
+ output.to_openstruct
115
126
  rescue StandardError => ex
116
127
  raise "An error occured while calling #{url}: #{ex.message}\n#{ex.backtrace}"
117
128
  end
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{urban-mapping-api}
8
- s.version = "0.9.2"
8
+ s.version = "0.9.3"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Michael Dwan"]
12
- s.date = %q{2009-09-02}
12
+ s.date = %q{2009-09-03}
13
13
  s.description = %q{A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API}
14
14
  s.email = %q{mpdwan@gmail.com}
15
15
  s.extra_rdoc_files = [
@@ -23,6 +23,7 @@ Gem::Specification.new do |s|
23
23
  "README.rdoc",
24
24
  "Rakefile",
25
25
  "VERSION",
26
+ "lib/core_ext.rb",
26
27
  "lib/urban-mapping-api.rb",
27
28
  "test/test_helper.rb",
28
29
  "test/urban-mapping-api_test.rb",
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: michaeldwan-urban-mapping-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.9.2
4
+ version: 0.9.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Michael Dwan
@@ -9,7 +9,7 @@ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
11
 
12
- date: 2009-09-02 00:00:00 -07:00
12
+ date: 2009-09-03 00:00:00 -07:00
13
13
  default_executable:
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
@@ -38,12 +38,14 @@ files:
38
38
  - README.rdoc
39
39
  - Rakefile
40
40
  - VERSION
41
+ - lib/core_ext.rb
41
42
  - lib/urban-mapping-api.rb
42
43
  - test/test_helper.rb
43
44
  - test/urban-mapping-api_test.rb
44
45
  - urban-mapping-api.gemspec
45
46
  has_rdoc: false
46
47
  homepage: http://github.com/michaeldwan/urban-mapping-api
48
+ licenses:
47
49
  post_install_message:
48
50
  rdoc_options:
49
51
  - --charset=UTF-8
@@ -64,7 +66,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
64
66
  requirements: []
65
67
 
66
68
  rubyforge_project:
67
- rubygems_version: 1.2.0
69
+ rubygems_version: 1.3.5
68
70
  signing_key:
69
71
  specification_version: 3
70
72
  summary: A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API