bing-location 0.0.1b

Sign up to get free protection for your applications and to get access to all the features.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # bing-location rubygem
2
+ ## Important
3
+ This is nowhere near ready to be implemented. The code still includes debugging steps, please do not try to implement this without being willing to do some work to get it ready for your project
4
+ ## Ignored File 'config/api.yml'
5
+ The file listed in .gitignore will need to be recreated with the following syntax. This file is needed to run the tests as-is. Alternatively, you can edit the API key request in the files to a string.
6
+ production:
7
+ key: YOUR API KEY
8
+ development:
9
+ key: YOUR API KEY
@@ -0,0 +1,12 @@
1
+ require 'rubygems'
2
+
3
+ spec = Gem::Specification.new do |s|
4
+ s.name = 'bing-location'
5
+ s.version = '0.0.1b'
6
+ s.summary = 'Access the Bing Maps API by creating location objects with string based queries or geolocation data'
7
+ s.files = Dir.glob("**/**/**")
8
+ s.test_files = Dir.glob("test/*")
9
+ s.author = "Justin Reese"
10
+ s.email = "justin.x.reese@gmail.com"
11
+ s.has_rdoc = false
12
+ end
data/config/api.yml ADDED
@@ -0,0 +1,4 @@
1
+ production:
2
+ key: Aig7nAJS24mxOdd_jt--chlMU7NziVCGDjpElE4KXyJztr2kng_XRxiHnUFuf2cS
3
+ development:
4
+ key: Aig7nAJS24mxOdd_jt--chlMU7NziVCGDjpElE4KXyJztr2kng_XRxiHnUFuf2cS
data/lib/location.rb ADDED
@@ -0,0 +1,191 @@
1
+ require 'net/http'
2
+ require 'tempfile'
3
+ require 'fileutils'
4
+ require 'yaml'
5
+
6
+ # This class is a wrapper for the Bing Maps API ({API Documentation}[http://msdn.microsoft.com/en-us/library/ff701724.aspx])
7
+ # that makes it easy to get more information about a location or create maps.
8
+ #
9
+ # Currently the class is built for dealing with one object (point on a map/location).
10
+ #
11
+ # =Example Usage
12
+ # my_loc = BingLocator.new()
13
+ # my_loc.api_key = "YOU-BING-MAPS-API-KEY"
14
+ # my_loc.country_region = 'US'
15
+ # my_loc.admin_district = 'PA'
16
+ # my_loc.locality = 'Pittsburgh'
17
+ # my_loc.address_line = '430 Atwood St'
18
+ # my_loc.obj_type = 'json'
19
+ # my_loc.get_obj_by_address
20
+ # puts my_loc.object
21
+ #
22
+ #
23
+ # my_loc = BingLocator.new()
24
+ # my_loc.api_key = "YOU-BING-MAPS-API-KEY"
25
+ # my_loc.query = 'University of Pittsburgh'
26
+ # puts my_loc.get_img_url_by_query(800,600)
27
+ #
28
+ class BingLocator
29
+ attr_accessor :country_region, :admin_district, :postal_code, :locality,
30
+ :address_line, :api_key, :obj_type, :query, :map_type, :show_traffic,
31
+ :zoom_level, # All above are possible sent values in REST request
32
+ :name, :point, :latitude, :longitude, :admin_district_2, :formatted_address, :confidence, # Returned in REST object
33
+ :object, :image # Returned Objects
34
+ $img_base_url = "http://dev.virtualearth.net/REST/v1/Imagery/Map/"
35
+ $obj_base_url = "http://dev.virtualearth.net/REST/v1/Locations/"
36
+
37
+ # Makes assumptions for required fields that are not yet defined. Assumptions can be overridden by assigning values
38
+ # [country_region] Country or region, i.e. 'US'
39
+ # [admin_district] Administrative District. In the US this is State i.e. 'PA'
40
+ # [postal_code] Postal code or zip code. i.e. '15213'
41
+ # [locality] Locality The locality, such as the city or neighborhood, that corresponds to an address i.e. 'Pittsburgh'
42
+ # [address_line] The official street line of an address relative to the area i.e. '430 Atwood St'
43
+ # [obj_type] Type of object returned by REST request
44
+ # [map_type] Imagery to use for maps
45
+ # [zoom_level] Zoom level of a map
46
+ def set_defaults
47
+ self.country_region = '-' unless self.country_region
48
+ self.admin_district = '-' unless self.admin_district
49
+ self.postal_code = '-' unless self.postal_code
50
+ self.locality = '-' unless self.locality
51
+ self.address_line = '-' unless self.address_line
52
+ # Allowed values - json, xml
53
+ self.obj_type = 'json' unless self.obj_type
54
+ # Allowed values - Aerial, AerialWithLabels, Road
55
+ self.map_type = 'Road' unless self.map_type
56
+ # Allowed values - An integer between 1 and 22
57
+ self.zoom_level = '11' unless self.zoom_level
58
+ end
59
+
60
+ # Uses the object's address to get a Location object from the Bing API. The object contains a normalized address, extra
61
+ # information, and latitude, longitude information
62
+ def get_obj_by_address
63
+ self.set_defaults
64
+ return 'No address set for location' if !self.country_region && !self.admin_district &&
65
+ !self.postal_code && !self.locality && !self.address_line
66
+ # Reformat strings
67
+ address_line = self.address_line.gsub(' ','%20')
68
+ locality = self.locality.gsub(' ','%20')
69
+ # Build request URL
70
+ params = "#{self.country_region}/#{self.admin_district}/#{self.postal_code}/"
71
+ params += "#{locality}/#{address_line}?o=#{self.obj_type}"
72
+ request_url = $obj_base_url+params+"&key=#{self.api_key}"
73
+ # REST request !
74
+ begin
75
+ self.object = Net::HTTP.get_response(URI.parse(request_url)).body
76
+ return self.object
77
+ rescue
78
+ return FALSE
79
+ end
80
+ end
81
+
82
+ # Uses the object's latitude and longitude to get a Location object from the Bing API.
83
+ # The object contains a normalized address, extra information, and latitude, longitude information
84
+ def get_obj_by_point
85
+ self.set_defaults
86
+ return 'No latitude or longitude set for location' unless self.latitude && self.longitude
87
+ # Build request URL
88
+ params = +"#{self.latitude},#{self.longitude}?o=#{self.obj_type}"
89
+ request_url = $obj_base_url+params+"&key=#{self.api_key}"
90
+ # REST request !
91
+ begin
92
+ self.object = Net::HTTP.get_response(URI.parse(request_url)).body
93
+ return self.object
94
+ rescue
95
+ return FALSE
96
+ end
97
+ end
98
+
99
+
100
+ # Uses the object's query to get a Location object from the Bing API.
101
+ # The object contains a normalized address, extra information, and latitude, longitude information
102
+ def get_obj_by_query
103
+ self.set_defaults
104
+ return 'No query set for location' if !self.query
105
+ query = self.query.gsub(' ','%20')
106
+ # Build request URL
107
+ params = "#{query}?o=#{self.obj_type}"
108
+ request_url = $obj_base_url+params+"&key=#{self.api_key}"
109
+ # REST request !
110
+ begin
111
+ self.object = Net::HTTP.get_response(URI.parse(request_url)).body
112
+ return self.object
113
+ rescue
114
+ return FALSE
115
+ end
116
+ end
117
+
118
+ # Uses the object's query to get an image file from the Bing API.
119
+ #
120
+ # The file will be returned as a File object. To save the file use something like the following:
121
+ # File.rename(my_loc.get_img_by_query(800,600).path,'map.jpeg')
122
+ def get_img_by_query(width,height)
123
+ self.set_defaults
124
+ return 'No query set for location' unless self.query
125
+ query = self.query.gsub(' ','%20')
126
+ # Build request URL
127
+ params = "#{self.map_type}/#{query}?mapSize=#{width},#{height}"
128
+ params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE
129
+ request_url = $img_base_url+params+"&key=#{self.api_key}"
130
+ # REST request !
131
+ begin
132
+ tmp = Tempfile.new('map_image.jpeg')
133
+ tmp.write Net::HTTP.get_response(URI.parse(request_url)).body
134
+ return tmp.flush
135
+ rescue
136
+ return FALSE
137
+ end
138
+ end
139
+
140
+ # Uses the object's query to get an image url from the Bing API. Using the Bing URL is discouraged.
141
+ def get_img_url_by_query(width,height)
142
+ self.set_defaults
143
+ return 'No query set for location' unless self.query
144
+ query = self.query.gsub(' ','%20')
145
+ # Build request URL
146
+ params = "#{self.map_type}/#{query}?mapSize=#{width},#{height}"
147
+ params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE
148
+ request_url = $img_base_url+params+"&key=#{self.api_key}"
149
+ return request_url
150
+ end
151
+
152
+ # Uses the object's latitude and longitude to get an image url from the Bing API.
153
+ # Using the Bing URL is discouraged.
154
+ def get_img_url_by_point(width,height)
155
+ self.set_defaults
156
+ return 'No latitude or longitude set for location' unless self.latitude && self.longitude
157
+ # Build request URL
158
+ params = "#{self.map_type}/#{self.latitude},#{self.longitude}/#{self.zoom_level}"
159
+ params += "?mapSize=#{width},#{height}&pushpin=#{self.latitude},#{self.longitude}"
160
+ params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE
161
+ request_url = $img_base_url+params+"&key=#{self.api_key}"
162
+ return request_url
163
+ end
164
+
165
+ # Uses the object's latitude and longitude to get an image file from the Bing API.
166
+ #
167
+ # The file will be returned as a File object. To save the file use something like the following:
168
+ # File.rename(my_loc.get_img_by_point(800,600).path,'map.jpeg')
169
+ def get_img_by_point(width,height)
170
+ self.set_defaults
171
+ return 'No latitude or longitude set for location' unless self.latitude && self.longitude
172
+ # Build request URL
173
+ params = "#{self.map_type}/#{self.latitude},#{self.longitude}/#{self.zoom_level}"
174
+ params += "?mapSize=#{width},#{height}&pushpin=#{self.latitude},#{self.longitude}"
175
+ params += "&mapLayer=TrafficFlow" if self.show_traffic == TRUE
176
+ request_url = $img_base_url+params+"&key=#{self.api_key}"
177
+ # REST request !
178
+ begin
179
+ tmp = Tempfile.new('map_image.jpeg')
180
+ tmp.write Net::HTTP.get_response(URI.parse(request_url)).body
181
+ return tmp.flush
182
+ rescue
183
+ return FALSE
184
+ end
185
+ end
186
+
187
+ def parse_obj
188
+ # TODO - WRITE THIS
189
+ end
190
+
191
+ end
data/test/api_key.rb ADDED
@@ -0,0 +1,5 @@
1
+ require 'yaml'
2
+
3
+ config = YAML.load_file('../config/api.yml')
4
+ api_key = config['production']['key']
5
+ puts api_key
data/test/images.rb ADDED
@@ -0,0 +1,20 @@
1
+ require '../lib/location'
2
+
3
+ my_loc = BingLocator.new()
4
+ my_loc.api_key = YAML.load_file('../config/api.yml')['production']['key']
5
+
6
+ # my_loc.country_region = 'US'
7
+ # my_loc.admin_district = 'PA'
8
+ my_loc.locality = 'Pittsburgh'
9
+ # my_loc.address_line = '48 Shady Dr W'
10
+ # my_loc.obj_type = 'json'
11
+ # my_loc.get_obj_by_address
12
+ # puts my_loc.object
13
+
14
+ # my_loc.query = '48 Shady Dr W Pittsburgh PA 15228'
15
+ # my_loc.query = 'south side'
16
+ my_loc.query = 'eifel tower'
17
+ # my_loc.latitude = 40.383545
18
+ # my_loc.longitude = -80.046509
19
+
20
+ puts my_loc.get_img_url_by_query(800,600)
metadata ADDED
@@ -0,0 +1,72 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: bing-location
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: true
5
+ segments:
6
+ - 0
7
+ - 0
8
+ - 1b
9
+ version: 0.0.1b
10
+ platform: ruby
11
+ authors:
12
+ - Justin Reese
13
+ autorequire:
14
+ bindir: bin
15
+ cert_chain: []
16
+
17
+ date: 2011-01-07 00:00:00 -05:00
18
+ default_executable:
19
+ dependencies: []
20
+
21
+ description:
22
+ email: justin.x.reese@gmail.com
23
+ executables: []
24
+
25
+ extensions: []
26
+
27
+ extra_rdoc_files: []
28
+
29
+ files:
30
+ - bing-location.gemspec
31
+ - config/api.yml
32
+ - lib/location.rb
33
+ - README.md
34
+ - test/api_key.rb
35
+ - test/images.rb
36
+ has_rdoc: true
37
+ homepage:
38
+ licenses: []
39
+
40
+ post_install_message:
41
+ rdoc_options: []
42
+
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ none: false
47
+ requirements:
48
+ - - ">="
49
+ - !ruby/object:Gem::Version
50
+ segments:
51
+ - 0
52
+ version: "0"
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ none: false
55
+ requirements:
56
+ - - ">"
57
+ - !ruby/object:Gem::Version
58
+ segments:
59
+ - 1
60
+ - 3
61
+ - 1
62
+ version: 1.3.1
63
+ requirements: []
64
+
65
+ rubyforge_project:
66
+ rubygems_version: 1.3.7
67
+ signing_key:
68
+ specification_version: 3
69
+ summary: Access the Bing Maps API by creating location objects with string based queries or geolocation data
70
+ test_files:
71
+ - test/api_key.rb
72
+ - test/images.rb