class OauthUtil

Attributes

base_str[RW]
callback_url[RW]
consumer_key[RW]
consumer_secret[RW]
oauth_version[RW]
params[RW]
req_method[RW]
req_url[RW]
sig_method[RW]
token[RW]
token_secret[RW]

Public Class Methods

new() click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 97
def initialize
  @consumer_key = ''
  @consumer_secret = ''
  @token = ''
  @token_secret = ''
  @req_method = 'GET'
  @sig_method = 'HMAC-SHA1'
  @oauth_version = '1.0'
  @callback_url = ''
end

Public Instance Methods

nonce() click to toggle source

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

# File lib/geokit/geocoders/yahoo.rb, line 110
def nonce
  Array.new(5) { rand(256) }.pack('C*').unpack('H*').first
end
percent_encode(string) click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 114
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() click to toggle source

sort (very important as it affects the signature), concat, and percent encode @ref oauth.net/core/1.0/#rfc.section.9.1.1 @ref oauth.net/core/1.0/#9.2.1 @ref oauth.net/core/1.0/#rfc.section.A.5.1

# File lib/geokit/geocoders/yahoo.rb, line 135
def query_string
  pairs = []
  @params.sort.each do |key, val|
    pairs.push("#{ percent_encode(key) }=#{ percent_encode(val.to_s) }")
  end
  pairs.join '&'
end
sign(parsed_url) click to toggle source

organize params & create signature

# File lib/geokit/geocoders/yahoo.rb, line 148
def sign(parsed_url)
  @params = {
    'oauth_consumer_key' => @consumer_key,
    'oauth_nonce' => nonce,
    'oauth_signature_method' => @sig_method,
    'oauth_timestamp' => 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() click to toggle source

@ref oauth.net/core/1.0/#rfc.section.9.2

# File lib/geokit/geocoders/yahoo.rb, line 120
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.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() click to toggle source
# File lib/geokit/geocoders/yahoo.rb, line 143
def timestamp
  Time.now.to_i.to_s
end