urbanesia 0.0.1.4 → 0.0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/urbanesia/agent.rb +69 -8
- data/lib/urbanesia.rb +3 -3
- metadata +1 -1
data/lib/urbanesia/agent.rb
CHANGED
@@ -13,14 +13,75 @@ module Urbanesia
|
|
13
13
|
@base_url = base_url || "http://api1.urbanesia.com/"
|
14
14
|
end
|
15
15
|
|
16
|
-
def
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
16
|
+
def request( endpoint = "get/super_search", post = "", get = "" )
|
17
|
+
|
18
|
+
start_time = Time.now # eg Time.parse('2012-12-08 09:00:03 UTC')
|
19
|
+
@time = start_time.to_f.to_s # eg 1354975291
|
20
|
+
@nonce = self.generate_nonce( @time )
|
21
|
+
|
22
|
+
post = self.generate_default_post(post)
|
23
|
+
|
24
|
+
requestify = self.requestify(post + "&" + get)
|
25
|
+
oauth_signature = self.generate_oauth_signature(endpoint, requestify)
|
26
|
+
|
27
|
+
#### Note:
|
28
|
+
#### get should have replaced "," with "%2C" but I didn't put it into a separate
|
29
|
+
#### string. This is the same process in the self.requestify method.
|
30
|
+
#### Check this line first if any errors.
|
31
|
+
final_url = base_url + endpoint + oauth_sig + "&" + get
|
32
|
+
|
33
|
+
agent = Mechanize.new
|
34
|
+
|
35
|
+
response = agent.post(final_url,
|
36
|
+
"oauth_consumer_key" => @consumer_key,
|
37
|
+
"oauth_nonce" => @nonce,
|
38
|
+
"oauth_signature_method" => "HMAC-SHA1",
|
39
|
+
"oauth_timestamp" => @time,
|
40
|
+
"oauth_version" => "1.0",
|
41
|
+
"safe_encode" => 1
|
42
|
+
).content
|
43
|
+
|
44
|
+
return response
|
45
|
+
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
# The "nonce" is a string based on a timestamp, eg 946eeff5e43578078c746bb1df62145d
|
51
|
+
# The API is limited to one request per nonce.
|
52
|
+
def generate_nonce(time = Time.now)
|
53
|
+
return Digest::MD5.hexdigest( time )
|
54
|
+
end
|
55
|
+
|
56
|
+
# Combine any user-inputted post variables with the default post variables required for the API call.
|
57
|
+
def generate_default_post(post)
|
58
|
+
return "oauth_consumer_key=" + @consumer_key + "&oauth_nonce=" + @nonce + "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" + @time + "&oauth_version=1.0&safe_encode=1&" + post
|
59
|
+
end
|
60
|
+
|
61
|
+
# Encode POST and GET for OAUTH
|
62
|
+
# Sort GET + POST into 1 string, sorted by param key
|
63
|
+
def requestify(vars)
|
64
|
+
arr = vars.split("&").sort!
|
65
|
+
requestify = ""
|
66
|
+
arr.each do |e|
|
67
|
+
requestify << "&" if !e.eql?(arr.first)
|
68
|
+
tmp = e.split("=")
|
69
|
+
requestify << CGI.escape(tmp[0]).gsub("+", "%20") + "=" + CGI.escape(tmp[1]).gsub("+", "%20")
|
70
|
+
end
|
71
|
+
return requestify
|
72
|
+
end
|
73
|
+
|
74
|
+
# The Oauth Signature is an encryption of the post/get variables and timestamp
|
75
|
+
# The API uses this to check that the API call is valid.
|
76
|
+
def generate_oauth_signature(endpoint, requestify)
|
77
|
+
base_sig = "POST&" + CGI.escape(@base_url + endpoint).gsub("+", "%20") + "&" + CGI.escape(requestify).gsub("+", "%20")
|
78
|
+
digest = OpenSSL::Digest::Digest.new('sha1')
|
79
|
+
oauth_sig = OpenSSL::HMAC.digest(digest, @consumer_secret + "&", base_sig )
|
80
|
+
oauth_sig = Base64.encode64( oauth_sig ).chomp.gsub(/\n/,'') # eg 2j16OUZpkwcj9oogIIPgIJhOI4Q=
|
81
|
+
oauth_sig = oauth_sig.gsub("=", "%3D")
|
82
|
+
oauth_sig = oauth_sig.gsub("+", "%2B") # eg OTg2N2I2YWIxZWFhOGNmNGYwNWM1Y2NkMTM1Mzc0YjFlMWE4MjE0Zg%3D%3D
|
83
|
+
oauth_sig = "?oauth_signature=" + oauth_sig
|
84
|
+
return oauth_sig
|
24
85
|
end
|
25
86
|
|
26
87
|
end
|
data/lib/urbanesia.rb
CHANGED
@@ -12,11 +12,11 @@ module Urbanesia
|
|
12
12
|
# @param [String] post Any required post variables, eg "year=2011"
|
13
13
|
# @param [String] get Any required get variables, eg "what=culinary&where=jakarta&row=1&offset=1000"
|
14
14
|
# @return [String] response The output of a Mechanize agent.post to the API endpoint
|
15
|
-
def request( endpoint = "get/super_search", post = "", get = "" )
|
15
|
+
def self.request( endpoint = "get/super_search", post = "", get = "" )
|
16
16
|
|
17
|
-
|
17
|
+
@agent = Agent.new(@@consumer_key, @@consumer_secret, @@base_url) if @agent.nil?
|
18
18
|
|
19
|
-
|
19
|
+
return @agent.request(endpoint, post, get)
|
20
20
|
|
21
21
|
end
|
22
22
|
|