michaeldwan-urban-mapping-api 0.9.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.
@@ -0,0 +1,5 @@
1
+ README.rdoc
2
+ lib/**/*.rb
3
+ bin/*
4
+ features/**/*.feature
5
+ LICENSE
@@ -0,0 +1,5 @@
1
+ *.sw?
2
+ .DS_Store
3
+ coverage
4
+ rdoc
5
+ pkg
data/LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 Michael Dwan
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,64 @@
1
+ = urban-mapping-api
2
+
3
+ A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API.
4
+
5
+ == Installation
6
+
7
+ sudo gem install michaeldwan-urban-mapping-api
8
+
9
+ Gem dependencies:
10
+
11
+ * curb
12
+ * json
13
+
14
+ == Examples
15
+
16
+ To get started you need to require 'urban-mapping-api':
17
+
18
+ % irb -rubygems
19
+ irb(main):001:0> require 'urban-mapping-api'
20
+ # => true
21
+
22
+ Before you do anything, create an instance of UrbanMapping::Interface
23
+
24
+ interface = UrbanMapping::Interface.new('my-api-key')
25
+
26
+ For premium API access, include the premium API key
27
+
28
+ interface = UrbanMapping::Interface.new('my-api-key', 'my-shared-secred')
29
+ interface.premium_api?
30
+ # => true
31
+
32
+ (If you don't have an api key, go get one at http://developer.urbanmapping.com/accounts/register/)
33
+
34
+ Now that you have an instance of the interface, you can make calls to the service
35
+
36
+ interface.get_neighborhoods_by_postal_code('60654')
37
+ # => [{"name"=>"River North", "city"=>"Chicago", "country"=>"USA", "id"=>3320072, "state"=>"IL"}, ...]
38
+
39
+ interface.get_neighborhoods_by_lat_lng(41.882088, -87.624454)
40
+ # => [{"name"=>"The Loop", "city"=>"Chicago", "country"=>"USA", "id"=>3094847, "state"=>"IL"}]
41
+
42
+ interface.get_neighborhood_detail(3094847)
43
+ # => {"wkt_centroid"=>"POINT(-87.6260772332496 41.8782770670931)", "name"=>"The Loop", "city"=>"Chicago"...
44
+
45
+ == Helpful Links
46
+
47
+ * urban-mapping-api Gem documentation http://rdoc.info/projects/michaeldwan/urban-mapping-api
48
+ * Urban Mapping http://www.urbanmapping.com/urbanware/neighborhood-database/index.html
49
+ * Api Documentation http://developer.urbanmapping.com/docs/neighborhoods
50
+ * Register for a free API key http://developer.urbanmapping.com/accounts/register
51
+
52
+ == Note on Patches/Pull Requests
53
+
54
+ * Fork the project.
55
+ * Make your feature addition or bug fix.
56
+ * Add tests for it. This is important so I don't break it in a
57
+ future version unintentionally.
58
+ * Commit, do not mess with rakefile, version, or history.
59
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
60
+ * Send me a pull request. Bonus points for topic branches.
61
+
62
+ == Copyright
63
+
64
+ Copyright (c) 2009 Michael Dwan. See LICENSE for details.
@@ -0,0 +1,56 @@
1
+ require 'rubygems'
2
+ require 'rake'
3
+
4
+ begin
5
+ require 'jeweler'
6
+ Jeweler::Tasks.new do |gem|
7
+ gem.name = "urban-mapping-api"
8
+ gem.summary = "A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API"
9
+ gem.description = "A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API"
10
+ gem.email = "mpdwan@gmail.com"
11
+ gem.homepage = "http://github.com/michaeldwan/urban-mapping-api"
12
+ gem.authors = ["Michael Dwan"]
13
+ gem.add_development_dependency "thoughtbot-shoulda"
14
+ # gem is a Gem::Specification... see http://www.rubygems.org/read/chapter/20 for additional settings
15
+ end
16
+ rescue LoadError
17
+ puts "Jeweler (or a dependency) not available. Install it with: sudo gem install jeweler"
18
+ end
19
+
20
+ require 'rake/testtask'
21
+ Rake::TestTask.new(:test) do |test|
22
+ test.libs << 'lib' << 'test'
23
+ test.pattern = 'test/**/*_test.rb'
24
+ test.verbose = true
25
+ end
26
+
27
+ begin
28
+ require 'rcov/rcovtask'
29
+ Rcov::RcovTask.new do |test|
30
+ test.libs << 'test'
31
+ test.pattern = 'test/**/*_test.rb'
32
+ test.verbose = true
33
+ end
34
+ rescue LoadError
35
+ task :rcov do
36
+ abort "RCov is not available. In order to run rcov, you must: sudo gem install spicycode-rcov"
37
+ end
38
+ end
39
+
40
+ task :test => :check_dependencies
41
+
42
+ task :default => :test
43
+
44
+ require 'rake/rdoctask'
45
+ Rake::RDocTask.new do |rdoc|
46
+ if File.exist?('VERSION')
47
+ version = File.read('VERSION')
48
+ else
49
+ version = ""
50
+ end
51
+
52
+ rdoc.rdoc_dir = 'rdoc'
53
+ rdoc.title = "urban-mapping-api #{version}"
54
+ rdoc.rdoc_files.include('README*')
55
+ rdoc.rdoc_files.include('lib/**/*.rb')
56
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.9.2
@@ -0,0 +1,119 @@
1
+ require 'curl'
2
+ require 'json'
3
+ require 'digest'
4
+
5
+ module UrbanMapping
6
+ class RequestError < StandardError
7
+ attr_reader :code, :url, :message
8
+ def initialize(code, url, message)
9
+ @code = code
10
+ @url = url
11
+ @message = message
12
+ end
13
+ end
14
+
15
+ class Interface
16
+ ENDPOINT = 'http://api1.urbanmapping.com/neighborhoods/rest'
17
+
18
+ attr_reader :api_key, :shared_secret
19
+
20
+ # Create a new instance.
21
+ # Requeres an api_key. A shared key needs to be provided for
22
+ # access to premium API methods.
23
+ def initialize(api_key, shared_secret = nil)
24
+ @api_key = api_key
25
+ @shared_secret = shared_secret
26
+ end
27
+
28
+ # Returns true if a shard_secret was provided to the constructor.
29
+ def premium_api?
30
+ !shared_secret.nil?
31
+ end
32
+
33
+ # Returns a list of neighborhoods whose boundaries contain the requested
34
+ # latitude and longitude.
35
+ def get_neighborhoods_by_lat_lng(latitude, longitude)
36
+ perform('getNeighborhoodsByLatLng', :lat => latitude, :lng => longitude)
37
+ end
38
+
39
+ # Returns the neighborhood whose centroid is nearest to the requested
40
+ # latitude and longitude within a 20 linear mile range.
41
+ def get_nearest_neighborhood(latitude, longitude)
42
+ perform('getNearestNeighborhood', :lat => latitude, :lng => longitude)
43
+ end
44
+
45
+ # Returns a list of neighborhoods within a bounding box extent defined by
46
+ # southwestern and northeastern corners. Note query extents covering more
47
+ # than 45 square miles will be rejected.
48
+ def get_neighborhoods_by_extent(southwest_latitude, southwest_longitude, northeast_latitude, northeast_longitude)
49
+ perform('getNeighborhoodsByExtent', :swlat => southwest_latitude,
50
+ :swlng => southwest_longitude,
51
+ :nelat => northeast_latitude,
52
+ :nelng => northeast_longitude)
53
+ end
54
+
55
+ # This method first geocodes the input address, then returns the geocode
56
+ # and lists neighborhoods containing the point in a single response.
57
+ # This is technically executed in a single request, but for the purposes
58
+ # of account administration a single invocation is counted as two calls.
59
+ def get_neighborhoods_by_address(street, city, state, country = nil)
60
+ perform('getNeighborhoodsByAddress', :street => street,
61
+ :city => city,
62
+ :state => state,
63
+ :country => country)
64
+ end
65
+
66
+ # Returns a list of neighborhood for the requested city.
67
+ def get_neighborhoods_by_city_state_country(city, state, country = nil)
68
+ perform('getNeighborhoodsByCityStateCountry', :city => city,
69
+ :state => state,
70
+ :country => country)
71
+ end
72
+
73
+ # Returns a list of neighborhoods whose areas intersect that of the requested postal code.
74
+ def get_neighborhoods_by_postal_code(postal_code)
75
+ perform('getNeighborhoodsByPostalCode', :postalCode => postal_code)
76
+ end
77
+
78
+ # Returns a list of neighborhoods for the requested neighborhood name.
79
+ def get_neighborhoods_by_name(name)
80
+ perform('getNeighborhoodsByName', :name => name)
81
+ end
82
+
83
+ # Returns neighborhood details for the requested neighborhood ID.
84
+ def get_neighborhood_detail(id)
85
+ perform('getNeighborhoodDetail', :neighborhoodId => id)
86
+ end
87
+
88
+ # Returns neighborhood relationship attributes for the requested neighborhood ID.
89
+ def get_neighborhood_relationships(id)
90
+ perform('getNeighborhoodRelationships', :neighborhoodId => id)
91
+ end
92
+
93
+ private
94
+ def generate_signature
95
+ Digest::MD5.hexdigest([api_key, shared_secret, Time.now.utc.to_i.to_s].join)
96
+ end
97
+
98
+ def perform(method, parameters)
99
+ parameters.merge!({
100
+ :format => 'json',
101
+ :apikey => api_key,
102
+ })
103
+ parameters.merge!(:sig => generate_signature) if premium_api?
104
+
105
+ query_string = parameters.to_a.map{|x| x.join('=')}.join('&')
106
+
107
+ url = "#{ENDPOINT}/#{method}?#{query_string}"
108
+ response = Curl::Easy.perform(url)
109
+
110
+ if response.response_code != 200
111
+ raise RequestError.new(response.response_code, url, response.body_str)
112
+ end
113
+
114
+ JSON.parse(response.body_str)
115
+ rescue StandardError => ex
116
+ raise "An error occured while calling #{url}: #{ex.message}\n#{ex.backtrace}"
117
+ end
118
+ end
119
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require 'shoulda'
4
+
5
+ $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
6
+ $LOAD_PATH.unshift(File.dirname(__FILE__))
7
+ require 'urban-mapping-api'
8
+
9
+ class Test::Unit::TestCase
10
+ end
@@ -0,0 +1,7 @@
1
+ require 'test_helper'
2
+
3
+ class UrbanMappingApiTest < Test::Unit::TestCase
4
+ should "probably rename this file and start testing for real" do
5
+ flunk "hey buddy, you should probably rename this file and start testing for real"
6
+ end
7
+ end
@@ -0,0 +1,53 @@
1
+ # Generated by jeweler
2
+ # DO NOT EDIT THIS FILE
3
+ # Instead, edit Jeweler::Tasks in Rakefile, and run `rake gemspec`
4
+ # -*- encoding: utf-8 -*-
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{urban-mapping-api}
8
+ s.version = "0.9.2"
9
+
10
+ s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
+ s.authors = ["Michael Dwan"]
12
+ s.date = %q{2009-09-02}
13
+ s.description = %q{A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API}
14
+ s.email = %q{mpdwan@gmail.com}
15
+ s.extra_rdoc_files = [
16
+ "LICENSE",
17
+ "README.rdoc"
18
+ ]
19
+ s.files = [
20
+ ".document",
21
+ ".gitignore",
22
+ "LICENSE",
23
+ "README.rdoc",
24
+ "Rakefile",
25
+ "VERSION",
26
+ "lib/urban-mapping-api.rb",
27
+ "test/test_helper.rb",
28
+ "test/urban-mapping-api_test.rb",
29
+ "urban-mapping-api.gemspec"
30
+ ]
31
+ s.homepage = %q{http://github.com/michaeldwan/urban-mapping-api}
32
+ s.rdoc_options = ["--charset=UTF-8"]
33
+ s.require_paths = ["lib"]
34
+ s.rubygems_version = %q{1.3.5}
35
+ s.summary = %q{A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API}
36
+ s.test_files = [
37
+ "test/test_helper.rb",
38
+ "test/urban-mapping-api_test.rb"
39
+ ]
40
+
41
+ if s.respond_to? :specification_version then
42
+ current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
43
+ s.specification_version = 3
44
+
45
+ if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
46
+ s.add_development_dependency(%q<thoughtbot-shoulda>, [">= 0"])
47
+ else
48
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
49
+ end
50
+ else
51
+ s.add_dependency(%q<thoughtbot-shoulda>, [">= 0"])
52
+ end
53
+ end
metadata ADDED
@@ -0,0 +1,73 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: michaeldwan-urban-mapping-api
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.9.2
5
+ platform: ruby
6
+ authors:
7
+ - Michael Dwan
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-09-02 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: thoughtbot-shoulda
17
+ type: :development
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: "0"
24
+ version:
25
+ description: A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API
26
+ email: mpdwan@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files:
32
+ - LICENSE
33
+ - README.rdoc
34
+ files:
35
+ - .document
36
+ - .gitignore
37
+ - LICENSE
38
+ - README.rdoc
39
+ - Rakefile
40
+ - VERSION
41
+ - lib/urban-mapping-api.rb
42
+ - test/test_helper.rb
43
+ - test/urban-mapping-api_test.rb
44
+ - urban-mapping-api.gemspec
45
+ has_rdoc: false
46
+ homepage: http://github.com/michaeldwan/urban-mapping-api
47
+ post_install_message:
48
+ rdoc_options:
49
+ - --charset=UTF-8
50
+ require_paths:
51
+ - lib
52
+ required_ruby_version: !ruby/object:Gem::Requirement
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ version:
58
+ required_rubygems_version: !ruby/object:Gem::Requirement
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ version:
64
+ requirements: []
65
+
66
+ rubyforge_project:
67
+ rubygems_version: 1.2.0
68
+ signing_key:
69
+ specification_version: 3
70
+ summary: A simple ruby interface to Urban Mapping's free and premium neighborhood lookup API
71
+ test_files:
72
+ - test/test_helper.rb
73
+ - test/urban-mapping-api_test.rb