lib/simple_oauth/header.rb in simple_oauth-0.1.9 vs lib/simple_oauth/header.rb in simple_oauth-0.2.0

- old
+ new

@@ -4,37 +4,47 @@ require 'cgi' module SimpleOAuth class Header ATTRIBUTE_KEYS = [:callback, :consumer_key, :nonce, :signature_method, :timestamp, :token, :verifier, :version] unless defined? ::SimpleOAuth::Header::ATTRIBUTE_KEYS + attr_reader :method, :params, :options - def self.default_options - { - :nonce => OpenSSL::Random.random_bytes(16).unpack('H*')[0], - :signature_method => 'HMAC-SHA1', - :timestamp => Time.now.to_i.to_s, - :version => '1.0' - } - end + class << self + def default_options + { + :nonce => OpenSSL::Random.random_bytes(16).unpack('H*')[0], + :signature_method => 'HMAC-SHA1', + :timestamp => Time.now.to_i.to_s, + :version => '1.0' + } + end - def self.encode(value) - URI.encode(value.to_s, /[^a-z0-9\-\.\_\~]/i) - end + def parse(header) + header.to_s.sub(/^OAuth\s/, '').split(/,\s*/).inject({}) do |attributes, pair| + match = pair.match(/^(\w+)\=\"([^\"]*)\"$/) + attributes.merge(match[1].sub(/^oauth_/, '').to_sym => decode(match[2])) + end + end - def self.decode(value) - URI.decode(value.to_s) - end + def escape(value) + uri_parser.escape(value.to_s, /[^a-z0-9\-\.\_\~]/i) + end + alias encode escape - def self.parse(header) - header.to_s.sub(/^OAuth\s/, '').split(/,\s*/).inject({}) do |attributes, pair| - match = pair.match(/^(\w+)\=\"([^\"]*)\"$/) - attributes.merge(match[1].sub(/^oauth_/, '').to_sym => decode(match[2])) + def unescape(value) + uri_parser.unescape(value.to_s) end - end + alias decode unescape - attr_reader :method, :params, :options + private + def uri_parser + @uri_parser ||= URI.const_defined?(:Parser) ? URI::Parser.new : URI + end + + end + def initialize(method, url, params, oauth = {}) @method = method.to_s.upcase @uri = URI.parse(url.to_s) @uri.scheme = @uri.scheme.downcase @uri.normalize! @@ -63,10 +73,10 @@ def signed_attributes attributes.merge(:oauth_signature => signature) end - private + private def normalized_attributes signed_attributes.sort_by{|k,v| k.to_s }.map{|k,v| %(#{k}="#{self.class.encode(v)}") }.join(', ') end