Class: SimpleOAuth::Header
- Inherits:
-
Object
- Object
- SimpleOAuth::Header
- Defined in:
- lib/simple_oauth/header.rb
Constant Summary collapse
- ATTRIBUTE_KEYS =
[:callback, :consumer_key, :nonce, :signature_method, :timestamp, :token, :verifier, :version]
- IGNORED_KEYS =
[:consumer_secret, :token_secret, :signature]
Instance Attribute Summary collapse
-
#method ⇒ Object
readonly
Returns the value of attribute method.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
-
#params ⇒ Object
readonly
Returns the value of attribute params.
Class Method Summary collapse
- .default_options ⇒ Object
- .escape(value) ⇒ Object (also: encode)
- .parse(header) ⇒ Object
- .unescape(value) ⇒ Object (also: decode)
Instance Method Summary collapse
-
#initialize(method, url, params, oauth = {}) ⇒ Header
constructor
A new instance of Header.
- #signed_attributes ⇒ Object
- #to_s ⇒ Object
- #url ⇒ Object
- #valid?(secrets = {}) ⇒ Boolean
Constructor Details
#initialize(method, url, params, oauth = {}) ⇒ Header
Returns a new instance of Header.
48 49 50 51 52 53 54 55 56 |
# File 'lib/simple_oauth/header.rb', line 48 def initialize(method, url, params, oauth = {}) @method = method.to_s.upcase @uri = URI.parse(url.to_s) @uri.scheme = @uri.scheme.downcase @uri.normalize! @uri.fragment = nil @params = params @options = oauth.is_a?(Hash) ? self.class..merge(oauth) : self.class.parse(oauth) end |
Instance Attribute Details
#method ⇒ Object (readonly)
Returns the value of attribute method.
12 13 14 |
# File 'lib/simple_oauth/header.rb', line 12 def method @method end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
12 13 14 |
# File 'lib/simple_oauth/header.rb', line 12 def @options end |
#params ⇒ Object (readonly)
Returns the value of attribute params.
12 13 14 |
# File 'lib/simple_oauth/header.rb', line 12 def params @params end |
Class Method Details
.default_options ⇒ Object
15 16 17 18 19 20 21 22 |
# File 'lib/simple_oauth/header.rb', line 15 def { :nonce => OpenSSL::Random.random_bytes(16).unpack('H*')[0], :signature_method => 'HMAC-SHA1', :timestamp => Time.now.to_i.to_s, :version => '1.0', } end |
.escape(value) ⇒ Object Also known as: encode
31 32 33 |
# File 'lib/simple_oauth/header.rb', line 31 def escape(value) uri_parser.escape(value.to_s, /[^a-z0-9\-\.\_\~]/i) end |
.parse(header) ⇒ Object
24 25 26 27 28 29 |
# File 'lib/simple_oauth/header.rb', line 24 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 => unescape(match[2])) end end |
.unescape(value) ⇒ Object Also known as: decode
36 37 38 |
# File 'lib/simple_oauth/header.rb', line 36 def unescape(value) uri_parser.unescape(value.to_s) end |
Instance Method Details
#signed_attributes ⇒ Object
76 77 78 |
# File 'lib/simple_oauth/header.rb', line 76 def signed_attributes attributes.merge(:oauth_signature => signature) end |
#to_s ⇒ Object
64 65 66 |
# File 'lib/simple_oauth/header.rb', line 64 def to_s "OAuth #{normalized_attributes}" end |
#url ⇒ Object
58 59 60 61 62 |
# File 'lib/simple_oauth/header.rb', line 58 def url uri = @uri.dup uri.query = nil uri.to_s end |
#valid?(secrets = {}) ⇒ Boolean
68 69 70 71 72 73 74 |
# File 'lib/simple_oauth/header.rb', line 68 def valid?(secrets = {}) = .dup .merge!(secrets) valid = [:signature] == signature .replace() valid end |