mqproxy 1.0

Sign up to get free protection for your applications and to get access to all the features.
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2009 [name of plugin creator]
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.
data/README.rdoc ADDED
@@ -0,0 +1,75 @@
1
+ = MQProxy
2
+
3
+ Connect to Map Quest's API to:
4
+ * geocode address
5
+ * get route from one address to another
6
+
7
+ == Installation
8
+
9
+ === gem
10
+ gem install mqproxy
11
+
12
+ === Plugin
13
+
14
+ Rails <= 2.3.8
15
+ ./script/plugin install git://github.com/cbrady/MQ-Proxy.git
16
+
17
+ Rails >= 3.0
18
+ rails plugin install git://github.com/cbrady/MQ-Proxy.git
19
+
20
+ == Example
21
+
22
+ === Set up the addresses:
23
+
24
+ Addresses can be setup in a Hash:
25
+ address1 = {:postalCode => "<zip1>", :street => "<street1>", :adminArea1 => "<country1>", :adminArea3 => "<state1>", :adminArea4 => "<county1>",:adminArea5 => "<city1>"}
26
+ address2 = {:postalCode => "<zip2>", :street => "<street2>", :adminArea1 => "<country2>", :adminArea3 => "<state2>", :adminArea4 => "<county2>",:adminArea5 => "<city2>"}
27
+
28
+ Or as strings:
29
+ address1 = "<street1> <city1>, <state1> <zip1>"
30
+ address2 = "<street2> <city2>, <state2> <zip2>"
31
+
32
+ === To get route:
33
+ proxy = MQProxy.new
34
+ route = proxy.get_route(address1, address2)
35
+
36
+ This returns a MQProxyRoute object
37
+
38
+ MQProxyRoute gives you access to certain attributes from the MapQuest response:
39
+ time - total estimated travel time (seconds)
40
+ distance - total distance of trip (default: miles - can be overwritten in options sent to MapQuest)
41
+ directions - array of all steps of the trip
42
+ raw - Hash containing entire response from MapQuest
43
+
44
+ === To get geocode:
45
+
46
+ proxy = MQProxy.new
47
+ code = proxy.geocode_address(address1)
48
+
49
+ This returns a MQProxyGeocode object
50
+
51
+ MQProxyGeocode gives you access to certain attributes from the MapQuest response:
52
+ street - address' street
53
+ city - address' city
54
+ zip - address' zip code
55
+ county - address' county
56
+ state - address' state
57
+ country - address' country
58
+ lat - address' latitude
59
+ lgn - address' longitude
60
+ raw - Hash containing entire response from MapQuest
61
+
62
+
63
+ == Options:
64
+ To overwrite default MapQuest options pass mq_options Hash to method
65
+
66
+ proxy = MQProxy.new
67
+ route = MQProxy.get_route(address1, address2, :mq_proxy {:unit => 'k', :routeType => 'shortest'})
68
+
69
+ The this tells MapQuest that you would like the shortest route possible and all of the distances in the response to be in kilometers instead of miles.
70
+
71
+ For a full list of options see the MapQuest API documentation:
72
+
73
+ http://www.mapquestapi.com/
74
+
75
+ Copyright (c) 2011 Christopher Brady, released under the MIT license
data/Rakefile ADDED
@@ -0,0 +1,23 @@
1
+ require 'rake'
2
+ require 'rake/testtask'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run unit tests.'
6
+ task :default => :test
7
+
8
+ desc 'Test the mq_proxy plugin.'
9
+ Rake::TestTask.new(:test) do |t|
10
+ t.libs << 'lib'
11
+ t.libs << 'test'
12
+ t.pattern = 'test/**/*_test.rb'
13
+ t.verbose = true
14
+ end
15
+
16
+ desc 'Generate documentation for the mq_proxy plugin.'
17
+ Rake::RDocTask.new(:rdoc) do |rdoc|
18
+ rdoc.rdoc_dir = 'rdoc'
19
+ rdoc.title = 'MQProxy'
20
+ rdoc.options << '--line-numbers' << '--inline-source'
21
+ rdoc.rdoc_files.include('README')
22
+ rdoc.rdoc_files.include('lib/**/*.rb')
23
+ end
data/lib/mqproxy.rb ADDED
@@ -0,0 +1,108 @@
1
+ # MQProxy - Collection of classes used to access the MapQuest API.
2
+ # Copyright (c) 2011 Christopher Brady
3
+ # Licensed under the same terms as Ruby. No warranty is provided
4
+
5
+ require 'json'
6
+ require 'net/http'
7
+
8
+ # Main class for MapQuest API calls
9
+ class MQProxy
10
+ # Get directions from one address to the other
11
+ def get_route(source, destination, options = {})
12
+ addresses = generate_route_json(source,destination, options[:mq_options])
13
+ return MQProxyRoute.new(route_addresses(addresses,options))
14
+ end
15
+
16
+ # Get directions from one address to the other
17
+ def geocode_address(address, options = {})
18
+ doc = JSON.generate({:location => address, :options => options[:mq_options]})
19
+ xmlInputString = doc.to_s
20
+ return MQProxyGeocode.new(send_to_map_quest(xmlInputString, options))
21
+ end
22
+
23
+ private
24
+ # Format JSON for MapQuest route call
25
+ def generate_route_json(source, destination, options)
26
+ doc = {:locations => [source, destination], :options => options}
27
+ doc = JSON.generate(doc)
28
+ return doc
29
+ end
30
+
31
+ # Append proper path and send to MapQuest
32
+ def route_addresses(addresses, options)
33
+ xmlInputString = addresses.to_s
34
+ options[:path] ||= 'directions/v1/route'
35
+ return send_to_map_quest(xmlInputString, options)
36
+ end
37
+
38
+ # Generic function to send data to MapQuest and handle response
39
+ def send_to_map_quest(xmlInputString, options)
40
+ options[:method] ||= 'POST'
41
+ options[:port] ||= 80
42
+ options[:name] ||= 'www.mapquestapi.com'
43
+ options[:path] ||= 'geocoding/v1/address'
44
+ url = URI.parse("http://"+options[:name]+"/"+options[:path] +"?key=#{APP_KEY}")
45
+ headers = {"Content-Type" => "text/json; charset=utf-8"}
46
+ http = Net::HTTP.new(url.host, url.port)
47
+ json ||= begin
48
+ response = http.start {
49
+ http.post(url.request_uri ,xmlInputString,headers)
50
+ }
51
+ doc = JSON.parse(response.body)
52
+ if doc['info']['statuscode'] != 0
53
+ raise StandardError, "There was an error with your request: #{doc['info']['message']}"
54
+ end
55
+ doc
56
+ end
57
+ end
58
+ end
59
+
60
+ # Class to handle MapQuest directions request response
61
+ class MQProxyRoute
62
+ attr_accessor :time, :distance, :directions, :raw
63
+
64
+ # Initialize MQProxyRoute and set standard data
65
+ def initialize(route)
66
+ @raw = route
67
+ @time = extract_data('time')
68
+ @distance = extract_data('distance')
69
+ @directions = extract_data('legs')
70
+ end
71
+
72
+ private
73
+
74
+ # Extract information from repsonse
75
+ def extract_data(field)
76
+ return @raw['route'][field]
77
+ end
78
+ end
79
+
80
+ # Class to handle MapQuest geocode request response
81
+ class MQProxyGeocode
82
+ attr_accessor :street, :city, :zip, :county, :state, :country, :lat, :lng, :raw
83
+
84
+ # Initialize MQProxyGeocode and set standard data
85
+ def initialize(geocode)
86
+ @raw = geocode
87
+ @street = extract_data('street')
88
+ @city = extract_data('adminArea5')
89
+ @zip = extract_data('postalCode')
90
+ @county = extract_data('adminArea4')
91
+ @state = extract_data('adminArea3')
92
+ @country = extract_data('adminArea1')
93
+ @lng,@lat = extract_lat_lng
94
+ end
95
+
96
+ private
97
+
98
+ # Extract information from repsonse
99
+ def extract_data(field)
100
+ @raw['results'].first['locations'].first[field]
101
+ end
102
+
103
+ # Extract latitude and longitude information from response
104
+ def extract_lat_lng
105
+ latLng = @raw['results'].first['locations'].first['latLng']
106
+ return [latLng['lng'],latLng['lat']]
107
+ end
108
+ end
@@ -0,0 +1,35 @@
1
+ require './test_helper'
2
+
3
+ class MQProxyTest < Test::Unit::TestCase
4
+ def setup
5
+ @proxy = MQProxy.new
6
+ @data = {:adminArea1 => "US",:adminArea3 => "NY",:adminArea4 => "",:adminArea5 => "Brooklyn",:postalCode => "11222",:street => "527 Leonard St."}
7
+ end
8
+
9
+ def test_get_geocode
10
+ geocode = @proxy.geocode_address(@data)
11
+ assert_equal(geocode.street,"527 Leonard St")
12
+ assert_equal(geocode.city,"Brooklyn")
13
+ assert_equal(geocode.state,"NY")
14
+ assert_equal(geocode.county,"Kings County")
15
+ assert_equal(geocode.zip,"11222-3262")
16
+ assert_equal(geocode.country,"US")
17
+ assert_equal(geocode.lat,40.72307)
18
+ assert_equal(geocode.lng,-73.94939)
19
+ end
20
+
21
+ def test_get_route
22
+ data2 = {:adminArea1 => "US",:adminArea3 => "NY",:adminArea4 => "",:adminArea5 => "Hastings on Hudson",:postalCode => "10706",:street => "603 Warburton Ave."}
23
+ route = @proxy.get_route(@data,data2)
24
+ assert_equal(route.time,2461)
25
+ assert_equal(route.distance,24.2259998321533)
26
+ end
27
+
28
+ def test_route_with_string
29
+ address1 = '527 Leonard St. Brooklyn, NY 11222'
30
+ address2 = '603 Warburton Ave. Hastings on Hudson, NY 10706'
31
+ route = @proxy.get_route(address1,address2)
32
+ assert_equal(route.time,2461)
33
+ assert_equal(route.distance,24.2259998321533)
34
+ end
35
+ end
@@ -0,0 +1,4 @@
1
+ require 'rubygems'
2
+ require 'test/unit'
3
+ require File.dirname(__FILE__) + '/../lib/mqproxy'
4
+ APP_KEY = "GET YOUR OWN"
metadata ADDED
@@ -0,0 +1,71 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mqproxy
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: "1.0"
6
+ platform: ruby
7
+ authors:
8
+ - Christopher Brady
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-02-18 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ description: A wrapper for the MapQuest API, provides ability to get route and to geocode an address
28
+ email:
29
+ executables: []
30
+
31
+ extensions: []
32
+
33
+ extra_rdoc_files: []
34
+
35
+ files:
36
+ - lib/mqproxy.rb
37
+ - MIT-LICENSE
38
+ - Rakefile
39
+ - README.rdoc
40
+ - test/mqproxy_test.rb
41
+ - test/test_helper.rb
42
+ has_rdoc: true
43
+ homepage: http://github.com/cbrady/MQ-Proxy
44
+ licenses: []
45
+
46
+ post_install_message:
47
+ rdoc_options: []
48
+
49
+ require_paths:
50
+ - lib
51
+ required_ruby_version: !ruby/object:Gem::Requirement
52
+ none: false
53
+ requirements:
54
+ - - ">="
55
+ - !ruby/object:Gem::Version
56
+ version: "0"
57
+ required_rubygems_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ">="
61
+ - !ruby/object:Gem::Version
62
+ version: "0"
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.5.0
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: A wrapper for the MapQuest API, provides ability to get route and to geocode an address
70
+ test_files:
71
+ - test/mqproxy_test.rb