geokit 1.10.0 → 1.11.0
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.
- checksums.yaml +4 -4
- data/.gitignore +2 -0
- data/.hound.yml +2 -0
- data/.rubocop.yml +358 -0
- data/.travis.yml +4 -4
- data/CHANGELOG.md +16 -0
- data/Gemfile +3 -3
- data/README.markdown +19 -4
- data/Rakefile +11 -16
- data/fixtures/keys.yml +9 -0
- data/fixtures/vcr_cassettes/google_postal_town.yml +117 -0
- data/fixtures/vcr_cassettes/mapbox_forward_geocode.yml +30 -30
- data/fixtures/vcr_cassettes/mapbox_forward_geocode_city_only.yml +25 -25
- data/fixtures/vcr_cassettes/mapbox_forward_geocode_state_only.yml +71 -0
- data/fixtures/vcr_cassettes/mapbox_reverse_geocode.yml +25 -25
- data/fixtures/vcr_cassettes/test_component_filtering_off.yml +390 -0
- data/fixtures/vcr_cassettes/test_component_filtering_on.yml +164 -0
- data/fixtures/vcr_cassettes/test_component_filtering_on_without_filter.yml +404 -0
- data/geokit.gemspec +24 -23
- data/lib/geokit.rb +7 -7
- data/lib/geokit/core_ext.rb +1 -1
- data/lib/geokit/geo_loc.rb +25 -19
- data/lib/geokit/geocoders.rb +21 -30
- data/lib/geokit/geocoders/bing.rb +5 -4
- data/lib/geokit/geocoders/ca_geocoder.rb +10 -11
- data/lib/geokit/geocoders/fcc.rb +9 -9
- data/lib/geokit/geocoders/free_geo_ip.rb +8 -8
- data/lib/geokit/geocoders/geo_plugin.rb +7 -7
- data/lib/geokit/geocoders/geobytes.rb +10 -10
- data/lib/geokit/geocoders/geocodio.rb +14 -14
- data/lib/geokit/geocoders/geonames.rb +12 -12
- data/lib/geokit/geocoders/google.rb +89 -61
- data/lib/geokit/geocoders/ip.rb +9 -14
- data/lib/geokit/geocoders/mapbox.rb +30 -30
- data/lib/geokit/geocoders/mapquest.rb +12 -12
- data/lib/geokit/geocoders/maxmind.rb +1 -1
- data/lib/geokit/geocoders/opencage.rb +19 -19
- data/lib/geokit/geocoders/openstreetmap.rb +21 -19
- data/lib/geokit/geocoders/ripe.rb +7 -7
- data/lib/geokit/geocoders/us_geocoder.rb +5 -5
- data/lib/geokit/geocoders/yahoo.rb +46 -46
- data/lib/geokit/geocoders/yandex.rb +18 -17
- data/lib/geokit/inflectors.rb +5 -5
- data/lib/geokit/lat_lng.rb +5 -4
- data/lib/geokit/multi_geocoder.rb +4 -2
- data/lib/geokit/net_adapter/net_http.rb +3 -2
- data/lib/geokit/net_adapter/typhoeus.rb +2 -1
- data/lib/geokit/version.rb +1 -1
- data/test/coverage_loader.rb +25 -0
- data/test/helper.rb +18 -87
- data/test/test_base_geocoder.rb +44 -11
- data/test/test_bing_geocoder.rb +40 -48
- data/test/test_bounds.rb +1 -1
- data/test/test_ca_geocoder.rb +15 -15
- data/test/test_fcc_geocoder.rb +8 -9
- data/test/test_free_geo_ip_geocoder.rb +8 -10
- data/test/test_geo_plugin_geocoder.rb +21 -22
- data/test/test_geobytes_geocoder.rb +9 -11
- data/test/test_geocodio_geocoder.rb +12 -14
- data/test/test_geoloc.rb +48 -49
- data/test/test_geonames_geocoder.rb +19 -23
- data/test/test_google_geocoder.rb +197 -189
- data/test/test_inflector.rb +7 -7
- data/test/test_ipgeocoder.rb +32 -31
- data/test/test_latlng.rb +28 -28
- data/test/test_map_quest.rb +23 -27
- data/test/test_mapbox_geocoder.rb +38 -28
- data/test/test_mappable.rb +2 -2
- data/test/test_maxmind_geocoder.rb +16 -16
- data/test/test_multi_geocoder.rb +5 -5
- data/test/test_multi_ip_geocoder.rb +3 -3
- data/test/test_net_adapter.rb +4 -4
- data/test/test_opencage_geocoder.rb +58 -67
- data/test/test_openstreetmap_geocoder.rb +67 -65
- data/test/test_polygon.rb +4 -22
- data/test/test_ripe_geocoder.rb +21 -26
- data/test/test_us_geocoder.rb +21 -21
- data/test/test_useragent.rb +46 -0
- data/test/test_yahoo_geocoder.rb +35 -47
- data/test/test_yandex_geocoder.rb +29 -27
- data/test/vcr_loader.rb +18 -0
- metadata +20 -6
@@ -16,17 +16,17 @@ module Geokit
|
|
16
16
|
end
|
17
17
|
|
18
18
|
# Template method which does the geocode lookup.
|
19
|
-
def self.do_geocode(address)
|
19
|
+
def self.do_geocode(address, _=nil)
|
20
20
|
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
|
21
21
|
url = "#{protocol}://www.mapquestapi.com/geocoding/v1/address?key=#{key}&location=#{Geokit::Inflector.url_escape(address_str)}"
|
22
22
|
process :json, url
|
23
23
|
end
|
24
24
|
|
25
25
|
def self.parse_json(results)
|
26
|
-
return GeoLoc.new unless results[
|
26
|
+
return GeoLoc.new unless results['info']['statuscode'] == 0
|
27
27
|
loc = nil
|
28
|
-
results[
|
29
|
-
result[
|
28
|
+
results['results'].each do |result|
|
29
|
+
result['locations'].each do |location|
|
30
30
|
extracted_geoloc = extract_geoloc(location)
|
31
31
|
if loc.nil?
|
32
32
|
loc = extracted_geoloc
|
@@ -40,8 +40,8 @@ module Geokit
|
|
40
40
|
|
41
41
|
def self.extract_geoloc(result_json)
|
42
42
|
loc = new_loc
|
43
|
-
loc.lat = result_json[
|
44
|
-
loc.lng = result_json[
|
43
|
+
loc.lat = result_json['latLng']['lat']
|
44
|
+
loc.lng = result_json['latLng']['lng']
|
45
45
|
set_address_components(result_json, loc)
|
46
46
|
set_precision(result_json, loc)
|
47
47
|
loc.success = true
|
@@ -49,15 +49,15 @@ module Geokit
|
|
49
49
|
end
|
50
50
|
|
51
51
|
def self.set_address_components(result_json, loc)
|
52
|
-
loc.country_code = result_json[
|
53
|
-
loc.street_address = result_json[
|
54
|
-
loc.city = result_json[
|
55
|
-
loc.state = result_json[
|
56
|
-
loc.zip = result_json[
|
52
|
+
loc.country_code = result_json['adminArea1']
|
53
|
+
loc.street_address = result_json['street'].to_s.empty? ? nil : result_json['street']
|
54
|
+
loc.city = result_json['adminArea5']
|
55
|
+
loc.state = result_json['adminArea3']
|
56
|
+
loc.zip = result_json['postalCode']
|
57
57
|
end
|
58
58
|
|
59
59
|
def self.set_precision(result_json, loc)
|
60
|
-
loc.precision = result_json[
|
60
|
+
loc.precision = result_json['geocodeQuality']
|
61
61
|
loc.accuracy = %w(unknown country state state city zip zip+4 street address building).index(loc.precision)
|
62
62
|
end
|
63
63
|
end
|
@@ -18,7 +18,7 @@ module Geokit
|
|
18
18
|
url = "#{protocol}://api.opencagedata.com/geocode/v1/json?"
|
19
19
|
url += "key=#{key}#{options_str}&"
|
20
20
|
url += "query=#{Geokit::Inflector.url_escape(address_str)}&"
|
21
|
-
url +=
|
21
|
+
url += 'no_annotations=1'
|
22
22
|
process :json, url
|
23
23
|
end
|
24
24
|
# Template method which does the reverse-geocode lookup.
|
@@ -34,7 +34,7 @@ module Geokit
|
|
34
34
|
end
|
35
35
|
|
36
36
|
def self.generate_param_for_option(param, options)
|
37
|
-
options[param] ? "&#{param}=#{Geokit::Inflector.url_escape(options[param])}" :
|
37
|
+
options[param] ? "&#{param}=#{Geokit::Inflector.url_escape(options[param])}" : ''
|
38
38
|
end
|
39
39
|
|
40
40
|
def self.generate_bool_param_for_option(param, options)
|
@@ -44,11 +44,11 @@ module Geokit
|
|
44
44
|
def self.parse_json(results)
|
45
45
|
return GeoLoc.new if results.empty?
|
46
46
|
if results.is_a?(Hash)
|
47
|
-
return GeoLoc.new unless results[
|
47
|
+
return GeoLoc.new unless results['status']['message'] == 'OK'
|
48
48
|
end
|
49
49
|
|
50
50
|
loc = nil
|
51
|
-
results[
|
51
|
+
results['results'].each do |result|
|
52
52
|
extracted_geoloc = extract_geoloc(result)
|
53
53
|
if loc.nil?
|
54
54
|
loc = extracted_geoloc
|
@@ -61,9 +61,9 @@ module Geokit
|
|
61
61
|
|
62
62
|
def self.extract_geoloc(result_json)
|
63
63
|
loc = new_loc
|
64
|
-
loc.lat = result_json[
|
65
|
-
loc.lng = result_json[
|
66
|
-
set_address_components(result_json[
|
64
|
+
loc.lat = result_json['geometry']['lat']
|
65
|
+
loc.lng = result_json['geometry']['lng']
|
66
|
+
set_address_components(result_json['components'], loc)
|
67
67
|
set_precision(result_json, loc)
|
68
68
|
loc.success = true
|
69
69
|
loc
|
@@ -71,22 +71,22 @@ module Geokit
|
|
71
71
|
|
72
72
|
def self.set_address_components(address_data, loc)
|
73
73
|
return unless address_data
|
74
|
-
loc.country = address_data[
|
75
|
-
loc.country_code = address_data[
|
76
|
-
loc.state_name = address_data[
|
77
|
-
loc.city = address_data[
|
78
|
-
loc.city = address_data[
|
79
|
-
loc.zip = address_data[
|
80
|
-
loc.district = address_data[
|
81
|
-
loc.district = address_data[
|
82
|
-
loc.street_address = "#{address_data['road']} #{address_data['house_number']}".strip if address_data[
|
83
|
-
loc.street_name = address_data[
|
84
|
-
loc.street_number = address_data[
|
74
|
+
loc.country = address_data['country']
|
75
|
+
loc.country_code = address_data['country_code'].upcase if address_data['country_code']
|
76
|
+
loc.state_name = address_data['state']
|
77
|
+
loc.city = address_data['city']
|
78
|
+
loc.city = address_data['county'] if loc.city.nil? && address_data['county']
|
79
|
+
loc.zip = address_data['postcode']
|
80
|
+
loc.district = address_data['city_district']
|
81
|
+
loc.district = address_data['state_district'] if loc.district.nil? && address_data['state_district']
|
82
|
+
loc.street_address = "#{address_data['road']} #{address_data['house_number']}".strip if address_data['road']
|
83
|
+
loc.street_name = address_data['road']
|
84
|
+
loc.street_number = address_data['house_number']
|
85
85
|
end
|
86
86
|
|
87
87
|
def self.set_precision(result_json, loc)
|
88
88
|
# a value between 1 (worse) and 10 (best). 0 means unknown
|
89
|
-
loc.precision = result_json[
|
89
|
+
loc.precision = result_json['confidence']
|
90
90
|
end
|
91
91
|
end
|
92
92
|
end
|
@@ -38,7 +38,7 @@ module Geokit
|
|
38
38
|
end
|
39
39
|
|
40
40
|
def self.generate_param_for_option(param, options)
|
41
|
-
options[param] ? "&#{param}=#{Geokit::Inflector.url_escape(options[param])}" :
|
41
|
+
options[param] ? "&#{param}=#{Geokit::Inflector.url_escape(options[param])}" : ''
|
42
42
|
end
|
43
43
|
|
44
44
|
def self.generate_bool_param_for_option(param, options)
|
@@ -47,7 +47,7 @@ module Geokit
|
|
47
47
|
|
48
48
|
def self.parse_json(results)
|
49
49
|
if results.is_a?(Hash)
|
50
|
-
return GeoLoc.new if results[
|
50
|
+
return GeoLoc.new if results['error']
|
51
51
|
results = [results]
|
52
52
|
end
|
53
53
|
return GeoLoc.new if results.empty?
|
@@ -68,12 +68,12 @@ module Geokit
|
|
68
68
|
loc = new_loc
|
69
69
|
|
70
70
|
# basic
|
71
|
-
loc.lat = result_json[
|
72
|
-
loc.lng = result_json[
|
71
|
+
loc.lat = result_json['lat']
|
72
|
+
loc.lng = result_json['lon']
|
73
73
|
|
74
|
-
set_address_components(result_json[
|
74
|
+
set_address_components(result_json['address'], loc)
|
75
75
|
set_precision(result_json, loc)
|
76
|
-
set_bounds(result_json[
|
76
|
+
set_bounds(result_json['boundingbox'], loc)
|
77
77
|
loc.success = true
|
78
78
|
|
79
79
|
loc
|
@@ -81,24 +81,24 @@ module Geokit
|
|
81
81
|
|
82
82
|
def self.set_address_components(address_data, loc)
|
83
83
|
return unless address_data
|
84
|
-
loc.country = address_data[
|
85
|
-
loc.country_code = address_data[
|
86
|
-
loc.state_name = address_data[
|
87
|
-
loc.city = address_data[
|
88
|
-
loc.city = address_data[
|
89
|
-
loc.zip = address_data[
|
90
|
-
loc.district = address_data[
|
91
|
-
loc.district = address_data[
|
92
|
-
loc.street_address = "#{address_data['road']} #{address_data['house_number']}".strip if address_data[
|
93
|
-
loc.street_name = address_data[
|
94
|
-
loc.street_number = address_data[
|
84
|
+
loc.country = address_data['country']
|
85
|
+
loc.country_code = address_data['country_code'].upcase if address_data['country_code']
|
86
|
+
loc.state_name = address_data['state']
|
87
|
+
loc.city = address_data['city']
|
88
|
+
loc.city = address_data['county'] if loc.city.nil? && address_data['county']
|
89
|
+
loc.zip = address_data['postcode']
|
90
|
+
loc.district = address_data['city_district']
|
91
|
+
loc.district = address_data['state_district'] if loc.district.nil? && address_data['state_district']
|
92
|
+
loc.street_address = "#{address_data['road']} #{address_data['house_number']}".strip if address_data['road']
|
93
|
+
loc.street_name = address_data['road']
|
94
|
+
loc.street_number = address_data['house_number']
|
95
95
|
end
|
96
96
|
|
97
97
|
def self.set_precision(result_json, loc)
|
98
98
|
# Todo accuracy does not work as Yahoo and Google maps on OSM
|
99
99
|
# loc.accuracy = %w{unknown amenity building highway historic landuse leisure natural place railway shop tourism waterway man_made}.index(loc.precision)
|
100
|
-
loc.precision = result_json[
|
101
|
-
loc.accuracy = result_json[
|
100
|
+
loc.precision = result_json['class']
|
101
|
+
loc.accuracy = result_json['type']
|
102
102
|
end
|
103
103
|
|
104
104
|
def self.set_bounds(result_json, loc)
|
@@ -110,4 +110,6 @@ module Geokit
|
|
110
110
|
end
|
111
111
|
end
|
112
112
|
end
|
113
|
+
|
114
|
+
Geokit::Geocoders::OsmGeocoder = Geokit::Geocoders::OSMGeocoder
|
113
115
|
end
|
@@ -6,7 +6,7 @@ module Geokit
|
|
6
6
|
|
7
7
|
private
|
8
8
|
|
9
|
-
def self.do_geocode(ip)
|
9
|
+
def self.do_geocode(ip, _=nil)
|
10
10
|
process :json, ip
|
11
11
|
end
|
12
12
|
|
@@ -16,19 +16,19 @@ module Geokit
|
|
16
16
|
|
17
17
|
def self.parse_json(json)
|
18
18
|
loc = new_loc
|
19
|
-
data = json[
|
19
|
+
data = json['data']['locations'][0]
|
20
20
|
|
21
21
|
if data
|
22
|
-
loc.lat = data[
|
23
|
-
loc.lng = data[
|
22
|
+
loc.lat = data['latitude']
|
23
|
+
loc.lng = data['longitude']
|
24
24
|
set_address_components(data, loc)
|
25
|
-
loc.success = data && (data[
|
25
|
+
loc.success = data && (data['status_code'] == 200)
|
26
26
|
end
|
27
27
|
loc
|
28
28
|
end
|
29
29
|
|
30
30
|
def self.set_address_components(data, loc)
|
31
|
-
match = data[
|
31
|
+
match = data['country'].match(/([A-Z]+)(\(([A-Z]+)\))?/)
|
32
32
|
if match[3]
|
33
33
|
loc.state_code = match[1]
|
34
34
|
loc.country_code = match[3]
|
@@ -36,7 +36,7 @@ module Geokit
|
|
36
36
|
loc.country_code = match[1]
|
37
37
|
end
|
38
38
|
|
39
|
-
loc.city = data[
|
39
|
+
loc.city = data['city']
|
40
40
|
end
|
41
41
|
end
|
42
42
|
end
|
@@ -8,14 +8,14 @@ module Geokit
|
|
8
8
|
|
9
9
|
private
|
10
10
|
|
11
|
-
def self.do_geocode(address)
|
11
|
+
def self.do_geocode(address, _=nil)
|
12
12
|
process :csv, submit_url(address)
|
13
13
|
end
|
14
14
|
|
15
15
|
def self.submit_url(address)
|
16
16
|
address_str = address.is_a?(GeoLoc) ? address.to_geocodeable_s : address
|
17
|
-
query = (address_str =~ /^\d{5}(?:-\d{4})?$/ ?
|
18
|
-
base = key ? "http://#{key}@geocoder.us/member" :
|
17
|
+
query = (address_str =~ /^\d{5}(?:-\d{4})?$/ ? 'zip' : 'address') + "=#{Geokit::Inflector.url_escape(address_str)}"
|
18
|
+
base = key ? "http://#{key}@geocoder.us/member" : 'http://geocoder.us'
|
19
19
|
"#{base}/service/csv/geocode?#{query}"
|
20
20
|
end
|
21
21
|
|
@@ -23,11 +23,11 @@ module Geokit
|
|
23
23
|
loc = GeoLoc.new
|
24
24
|
if array.length == 5
|
25
25
|
loc.lat, loc.lng, loc.city, loc.state, loc.zip = array
|
26
|
-
loc.country_code =
|
26
|
+
loc.country_code = 'US'
|
27
27
|
loc.success = true
|
28
28
|
elsif array.length == 6
|
29
29
|
loc.lat, loc.lng, loc.street_address, loc.city, loc.state, loc.zip = array
|
30
|
-
loc.country_code =
|
30
|
+
loc.country_code = 'US'
|
31
31
|
loc.success = true
|
32
32
|
end
|
33
33
|
loc
|
@@ -21,12 +21,12 @@ module Geokit
|
|
21
21
|
end
|
22
22
|
|
23
23
|
# Template method which does the geocode lookup.
|
24
|
-
def self.do_geocode(address)
|
24
|
+
def self.do_geocode(address, _=nil)
|
25
25
|
process :json, submit_url(address)
|
26
26
|
end
|
27
27
|
|
28
28
|
def self.parse_json(results)
|
29
|
-
boss_results = results && results[
|
29
|
+
boss_results = results && results['bossresponse'] && results['bossresponse']['placefinder'] && results['bossresponse']['placefinder']['results']
|
30
30
|
return GeoLoc.new unless boss_results && boss_results.first
|
31
31
|
loc = nil
|
32
32
|
boss_results.each do |result|
|
@@ -42,8 +42,8 @@ module Geokit
|
|
42
42
|
|
43
43
|
def self.extract_geoloc(result_json)
|
44
44
|
loc = new_loc
|
45
|
-
loc.lat = result_json[
|
46
|
-
loc.lng = result_json[
|
45
|
+
loc.lat = result_json['latitude']
|
46
|
+
loc.lng = result_json['longitude']
|
47
47
|
set_address_components(result_json, loc)
|
48
48
|
set_precision(result_json, loc)
|
49
49
|
loc.success = true
|
@@ -51,25 +51,25 @@ module Geokit
|
|
51
51
|
end
|
52
52
|
|
53
53
|
def self.set_address_components(result_json, loc)
|
54
|
-
loc.country_code = result_json[
|
55
|
-
loc.street_address = result_json[
|
56
|
-
loc.city = result_json[
|
57
|
-
loc.state = loc.is_us? ? result_json[
|
58
|
-
loc.zip = result_json[
|
54
|
+
loc.country_code = result_json['countrycode']
|
55
|
+
loc.street_address = result_json['line1'].to_s.empty? ? nil : result_json['line1']
|
56
|
+
loc.city = result_json['city']
|
57
|
+
loc.state = loc.is_us? ? result_json['statecode'] : result_json['state']
|
58
|
+
loc.zip = result_json['postal']
|
59
59
|
end
|
60
60
|
|
61
61
|
def self.set_precision(result_json, loc)
|
62
|
-
loc.precision = case result_json[
|
63
|
-
when 9, 10 then
|
64
|
-
when 19..30 then
|
65
|
-
when 39, 40 then
|
66
|
-
when 49, 50 then
|
67
|
-
when 59, 60, 64 then
|
68
|
-
when 74, 75 then
|
69
|
-
when 70..72 then
|
70
|
-
when 80..87 then
|
71
|
-
when 62, 63, 90, 99 then
|
72
|
-
else
|
62
|
+
loc.precision = case result_json['quality'].to_i
|
63
|
+
when 9, 10 then 'country'
|
64
|
+
when 19..30 then 'state'
|
65
|
+
when 39, 40 then 'city'
|
66
|
+
when 49, 50 then 'neighborhood'
|
67
|
+
when 59, 60, 64 then 'zip'
|
68
|
+
when 74, 75 then 'zip+4'
|
69
|
+
when 70..72 then 'street'
|
70
|
+
when 80..87 then 'address'
|
71
|
+
when 62, 63, 90, 99 then 'building'
|
72
|
+
else 'unknown'
|
73
73
|
end
|
74
74
|
|
75
75
|
loc.accuracy = %w(unknown country state state city zip zip+4 street address building).index(loc.precision)
|
@@ -85,47 +85,47 @@ end
|
|
85
85
|
# License: http://gist.github.com/375593
|
86
86
|
# Usage: see example.rb below
|
87
87
|
|
88
|
-
require
|
89
|
-
require
|
90
|
-
require
|
91
|
-
require
|
88
|
+
require 'uri'
|
89
|
+
require 'cgi'
|
90
|
+
require 'openssl'
|
91
|
+
require 'base64'
|
92
92
|
|
93
93
|
class OauthUtil
|
94
94
|
attr_accessor :consumer_key, :consumer_secret, :token, :token_secret, :req_method,
|
95
95
|
:sig_method, :oauth_version, :callback_url, :params, :req_url, :base_str
|
96
96
|
|
97
97
|
def initialize
|
98
|
-
@consumer_key =
|
99
|
-
@consumer_secret =
|
100
|
-
@token =
|
101
|
-
@token_secret =
|
102
|
-
@req_method =
|
103
|
-
@sig_method =
|
104
|
-
@oauth_version =
|
105
|
-
@callback_url =
|
98
|
+
@consumer_key = ''
|
99
|
+
@consumer_secret = ''
|
100
|
+
@token = ''
|
101
|
+
@token_secret = ''
|
102
|
+
@req_method = 'GET'
|
103
|
+
@sig_method = 'HMAC-SHA1'
|
104
|
+
@oauth_version = '1.0'
|
105
|
+
@callback_url = ''
|
106
106
|
end
|
107
107
|
|
108
108
|
# openssl::random_bytes returns non-word chars, which need to be removed. using alt method to get length
|
109
109
|
# ref http://snippets.dzone.com/posts/show/491
|
110
110
|
def nonce
|
111
|
-
Array.new(5) { rand(256) }.pack(
|
111
|
+
Array.new(5) { rand(256) }.pack('C*').unpack('H*').first
|
112
112
|
end
|
113
113
|
|
114
114
|
def percent_encode(string)
|
115
115
|
# ref http://snippets.dzone.com/posts/show/1260
|
116
|
-
URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).gsub(
|
116
|
+
URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")).gsub('*', '%2A')
|
117
117
|
end
|
118
118
|
|
119
119
|
# @ref http://oauth.net/core/1.0/#rfc.section.9.2
|
120
120
|
def signature
|
121
|
-
key = percent_encode(@consumer_secret) +
|
121
|
+
key = percent_encode(@consumer_secret) + '&' + percent_encode(@token_secret)
|
122
122
|
|
123
123
|
# ref: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks
|
124
|
-
digest = OpenSSL::Digest.new(
|
124
|
+
digest = OpenSSL::Digest.new('sha1')
|
125
125
|
hmac = OpenSSL::HMAC.digest(digest, key, @base_str)
|
126
126
|
|
127
127
|
# ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81
|
128
|
-
Base64.encode64(hmac).chomp.gsub(/\n/,
|
128
|
+
Base64.encode64(hmac).chomp.gsub(/\n/, '')
|
129
129
|
end
|
130
130
|
|
131
131
|
# sort (very important as it affects the signature), concat, and percent encode
|
@@ -137,7 +137,7 @@ class OauthUtil
|
|
137
137
|
@params.sort.each do |key, val|
|
138
138
|
pairs.push("#{ percent_encode(key) }=#{ percent_encode(val.to_s) }")
|
139
139
|
end
|
140
|
-
pairs.join
|
140
|
+
pairs.join '&'
|
141
141
|
end
|
142
142
|
|
143
143
|
def timestamp
|
@@ -147,11 +147,11 @@ class OauthUtil
|
|
147
147
|
# organize params & create signature
|
148
148
|
def sign(parsed_url)
|
149
149
|
@params = {
|
150
|
-
|
151
|
-
|
152
|
-
|
153
|
-
|
154
|
-
|
150
|
+
'oauth_consumer_key' => @consumer_key,
|
151
|
+
'oauth_nonce' => nonce,
|
152
|
+
'oauth_signature_method' => @sig_method,
|
153
|
+
'oauth_timestamp' => timestamp,
|
154
|
+
'oauth_version' => @oauth_version,
|
155
155
|
}
|
156
156
|
|
157
157
|
# if url has query, merge key/values into params obj overwriting defaults
|
@@ -166,7 +166,7 @@ class OauthUtil
|
|
166
166
|
end
|
167
167
|
|
168
168
|
# @ref http://oauth.net/core/1.0/#rfc.section.9.1.2
|
169
|
-
@req_url = parsed_url.scheme +
|
169
|
+
@req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path
|
170
170
|
|
171
171
|
# create base str. make it an object attr for ez debugging
|
172
172
|
# ref http://oauth.net/core/1.0/#anchor14
|
@@ -177,10 +177,10 @@ class OauthUtil
|
|
177
177
|
# normalization is just x-www-form-urlencoded
|
178
178
|
percent_encode(query_string),
|
179
179
|
|
180
|
-
].join(
|
180
|
+
].join('&')
|
181
181
|
|
182
182
|
# add signature
|
183
|
-
@params[
|
183
|
+
@params['oauth_signature'] = signature
|
184
184
|
|
185
185
|
self
|
186
186
|
end
|