Class: OauthUtil
Instance Attribute Summary collapse
-
#base_str ⇒ Object
Returns the value of attribute base_str.
-
#callback_url ⇒ Object
Returns the value of attribute callback_url.
-
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
-
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
-
#oauth_version ⇒ Object
Returns the value of attribute oauth_version.
-
#params ⇒ Object
Returns the value of attribute params.
-
#req_method ⇒ Object
Returns the value of attribute req_method.
-
#req_url ⇒ Object
Returns the value of attribute req_url.
-
#sig_method ⇒ Object
Returns the value of attribute sig_method.
-
#token ⇒ Object
Returns the value of attribute token.
-
#token_secret ⇒ Object
Returns the value of attribute token_secret.
Instance Method Summary collapse
-
#initialize ⇒ OauthUtil
constructor
A new instance of OauthUtil.
-
#nonce ⇒ Object
openssl::random_bytes returns non-word chars, which need to be removed.
- #percent_encode(string) ⇒ Object
-
#query_string ⇒ Object
sort (very important as it affects the signature), concat, and percent encode.
-
#sign(parsed_url) ⇒ Object
organize params & create signature.
- #signature ⇒ Object
- #timestamp ⇒ Object
Constructor Details
#initialize ⇒ OauthUtil
Returns a new instance of OauthUtil.
101 102 103 104 105 106 107 108 109 110 |
# File 'lib/geokit/geocoders/yahoo.rb', line 101 def initialize @consumer_key = '' @consumer_secret = '' @token = '' @token_secret = '' @req_method = 'GET' @sig_method = 'HMAC-SHA1' @oauth_version = '1.0' @callback_url = '' end |
Instance Attribute Details
#base_str ⇒ Object
Returns the value of attribute base_str.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def base_str @base_str end |
#callback_url ⇒ Object
Returns the value of attribute callback_url.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def callback_url @callback_url end |
#consumer_key ⇒ Object
Returns the value of attribute consumer_key.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def consumer_key @consumer_key end |
#consumer_secret ⇒ Object
Returns the value of attribute consumer_secret.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def consumer_secret @consumer_secret end |
#oauth_version ⇒ Object
Returns the value of attribute oauth_version.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def oauth_version @oauth_version end |
#params ⇒ Object
Returns the value of attribute params.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def params @params end |
#req_method ⇒ Object
Returns the value of attribute req_method.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def req_method @req_method end |
#req_url ⇒ Object
Returns the value of attribute req_url.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def req_url @req_url end |
#sig_method ⇒ Object
Returns the value of attribute sig_method.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def sig_method @sig_method end |
#token ⇒ Object
Returns the value of attribute token.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def token @token end |
#token_secret ⇒ Object
Returns the value of attribute token_secret.
98 99 100 |
# File 'lib/geokit/geocoders/yahoo.rb', line 98 def token_secret @token_secret end |
Instance Method Details
#nonce ⇒ Object
openssl::random_bytes returns non-word chars, which need to be removed. using alt method to get length ref snippets.dzone.com/posts/show/491
114 115 116 |
# File 'lib/geokit/geocoders/yahoo.rb', line 114 def nonce Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first end |
#percent_encode(string) ⇒ Object
118 119 120 121 |
# File 'lib/geokit/geocoders/yahoo.rb', line 118 def percent_encode( string ) # ref http://snippets.dzone.com/posts/show/1260 URI.escape( string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") ).gsub('*', '%2A') end |
#query_string ⇒ Object
sort (very important as it affects the signature), concat, and percent encode
139 140 141 142 143 144 145 |
# File 'lib/geokit/geocoders/yahoo.rb', line 139 def query_string pairs = [] @params.sort.each { | key, val | pairs.push( "#{ percent_encode( key ) }=#{ percent_encode( val.to_s ) }" ) } pairs.join '&' end |
#sign(parsed_url) ⇒ Object
organize params & create signature
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 |
# File 'lib/geokit/geocoders/yahoo.rb', line 152 def sign( parsed_url ) @params = { 'oauth_consumer_key' => @consumer_key, 'oauth_nonce' => nonce, 'oauth_signature_method' => @sig_method, 'oauth_timestamp' => , 'oauth_version' => @oauth_version } # if url has query, merge key/values into params obj overwriting defaults if parsed_url.query CGI.parse( parsed_url.query ).each do |k,v| if v.is_a?(Array) && v.count == 1 @params[k] = v.first else @params[k] = v end end end # @ref http://oauth.net/core/1.0/#rfc.section.9.1.2 @req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path # create base str. make it an object attr for ez debugging # ref http://oauth.net/core/1.0/#anchor14 @base_str = [ @req_method, percent_encode( req_url ), # normalization is just x-www-form-urlencoded percent_encode( query_string ) ].join( '&' ) # add signature @params[ 'oauth_signature' ] = signature self end |
#signature ⇒ Object
124 125 126 127 128 129 130 131 132 133 |
# File 'lib/geokit/geocoders/yahoo.rb', line 124 def signature key = percent_encode( @consumer_secret ) + '&' + percent_encode( @token_secret ) # ref: http://blog.nathanielbibler.com/post/63031273/openssl-hmac-vs-ruby-hmac-benchmarks digest = OpenSSL::Digest::Digest.new( 'sha1' ) hmac = OpenSSL::HMAC.digest( digest, key, @base_str ) # ref http://groups.google.com/group/oauth-ruby/browse_thread/thread/9110ed8c8f3cae81 Base64.encode64( hmac ).chomp.gsub( /\n/, '' ) end |
#timestamp ⇒ Object
147 148 149 |
# File 'lib/geokit/geocoders/yahoo.rb', line 147 def Time.now.to_i.to_s end |