Class: Chebyte::OauthUtil

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-yql.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeOauthUtil

Returns a new instance of OauthUtil.



14
15
16
17
18
19
20
21
22
23
24
# File 'lib/ruby-yql.rb', line 14

def initialize
  @consumer_key = ''
  @consumer_secret = ''
  @token = ''
  @token_secret = ''
  @req_method = 'GET'
  @sig_method = 'HMAC-SHA1'
  @oauth_version = '1.0'
  @callback_url = ''
  @params = []
end

Instance Attribute Details

#callback_urlObject

Returns the value of attribute callback_url.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def callback_url
  @callback_url
end

#consumer_keyObject

Returns the value of attribute consumer_key.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def consumer_key
  @consumer_key
end

#consumer_secretObject

Returns the value of attribute consumer_secret.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def consumer_secret
  @consumer_secret
end

#oauth_versionObject

Returns the value of attribute oauth_version.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def oauth_version
  @oauth_version
end

#paramsObject

Returns the value of attribute params.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def params
  @params
end

#req_methodObject

Returns the value of attribute req_method.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def req_method
  @req_method
end

#sig_methodObject

Returns the value of attribute sig_method.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def sig_method
  @sig_method
end

#tokenObject

Returns the value of attribute token.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def token
  @token
end

#token_secretObject

Returns the value of attribute token_secret.



12
13
14
# File 'lib/ruby-yql.rb', line 12

def token_secret
  @token_secret
end

Instance Method Details

#construct_req_url(url) ⇒ Object



38
39
40
41
# File 'lib/ruby-yql.rb', line 38

def construct_req_url( url)
  parsed_url = URI.parse( url )
  parsed_url.scheme + '://' + parsed_url.host + parsed_url.path
end

#generate_nonceObject



26
27
28
# File 'lib/ruby-yql.rb', line 26

def generate_nonce
  Array.new( 5 ) { rand(256) }.pack('C*').unpack('H*').first
end

#generate_sig(args) ⇒ Object



43
44
45
46
47
48
49
# File 'lib/ruby-yql.rb', line 43

def generate_sig( args )
  key     = percent_encode( args[:consumer_secret] ) + '&' + percent_encode( args[:token_secret] )
  text    = args[:base_str]
  digest  = OpenSSL::Digest::Digest.new( 'sha1' )
  raw_sig = OpenSSL::HMAC.digest( digest, key, text )
  Base64.encode64( raw_sig ).chomp.gsub( /\n/, '' )
end

#normalize_req_params(params) ⇒ Object



34
35
36
# File 'lib/ruby-yql.rb', line 34

def normalize_req_params( params )
  percent_encode( params.sort().join( '&' ) )
end

#percent_encode(string) ⇒ Object



30
31
32
# File 'lib/ruby-yql.rb', line 30

def percent_encode( string )
  URI.escape( string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]") )
end

#sign(url) ⇒ Object



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/ruby-yql.rb', line 55

def sign( url )

  parsed_url = URI.parse( url )

  @params.push( 'oauth_consumer_key=' + @consumer_key )
  @params.push( 'oauth_nonce=' + generate_nonce() )
  @params.push( 'oauth_signature_method=' + @sig_method )
  @params.push( 'oauth_timestamp=' + Time.now.to_i.to_s )
  @params.push( 'oauth_version=' + @oauth_version )

  if parsed_url.query
    @params = @params | parsed_url.query.split( '&' )
  end

  normal_req_params = normalize_req_params( params )

  req_url = parsed_url.scheme + '://' + parsed_url.host + parsed_url.path

  base_str = [ @req_method, percent_encode( req_url ), normal_req_params ].join( '&' )

  # sign
  signature = generate_sig({
    :base_str => base_str,
    :consumer_secret => @consumer_secret,
    :token_secret => @token_secret
  })

  @params.push( 'oauth_signature=' + percent_encode( signature ) )

  return self
end

#to_query_stringObject



51
52
53
# File 'lib/ruby-yql.rb', line 51

def to_query_string
  @params.join('&')
end