Class: Signet::OAuth1::Credential
- Inherits:
-
Object
- Object
- Signet::OAuth1::Credential
- Defined in:
- lib/signet/oauth_1/credential.rb
Instance Attribute Summary collapse
-
#key ⇒ Object
Returns the value of attribute key.
-
#secret ⇒ Object
Returns the value of attribute secret.
Instance Method Summary collapse
- #==(other) ⇒ Object
-
#initialize(*args) ⇒ Credential
constructor
Creates a token object from a key and secret.
- #to_hash ⇒ Object (also: #to_h)
Constructor Details
#initialize(*args) ⇒ Credential
Creates a token object from a key and secret.
37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 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 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/signet/oauth_1/credential.rb', line 37 def initialize(*args) # We want to be particularly flexible in how we initialize a token # object for maximum interoperability. However, this flexibility # means we need to be careful about returning an unexpected value for # key or secret to avoid difficult-to-debug situations. Thus lots # of type-checking. # This is cheaper than coercing to some kind of Hash with # indifferent access. Also uglier. key_from_hash = lambda do |parameters| parameters["oauth_token"] || parameters[:oauth_token] || parameters["key"] || parameters[:key] end secret_from_hash = lambda do |parameters| parameters["oauth_token_secret"] || parameters[:oauth_token_secret] || parameters["secret"] || parameters[:secret] end if args.first.respond_to?(:to_hash) parameters = args.first.to_hash @key = key_from_hash.call(parameters) @secret = secret_from_hash.call(parameters) unless @key && @secret raise ArgumentError, "Could not find both key and secret in #{hash.inspect}." end else # Normalize to an Array if !args.first.kind_of?(String) && !args.first.respond_to?(:to_str) && args.first.kind_of?(Enumerable) # We need to special-case strings since they're technically # Enumerable objects. args = args.first.to_a elsif args.first.respond_to?(:to_ary) args = args.first.to_ary end if args.all? { |value| value.kind_of?(Array) } parameters = args.inject({}) { |h,(k,v)| h[k]=v; h } @key = key_from_hash.call(parameters) @secret = secret_from_hash.call(parameters) elsif args.size == 2 @key, @secret = args else raise ArgumentError, "wrong number of arguments (#{args.size} for 2)" end end if @key.respond_to?(:to_str) @key = @key.to_str else raise TypeError, "Expected String, got #{@key.class}." end if @secret.respond_to?(:to_str) @secret = @secret.to_str else raise TypeError, "Expected String, got #{@secret.class}." end end |
Instance Attribute Details
#key ⇒ Object
Returns the value of attribute key.
100 101 102 |
# File 'lib/signet/oauth_1/credential.rb', line 100 def key @key end |
#secret ⇒ Object
Returns the value of attribute secret.
100 101 102 |
# File 'lib/signet/oauth_1/credential.rb', line 100 def secret @secret end |
Instance Method Details
#==(other) ⇒ Object
110 111 112 113 114 115 116 |
# File 'lib/signet/oauth_1/credential.rb', line 110 def ==(other) if other.respond_to?(:key) && other.respond_to?(:secret) return self.key == other.key && self.secret == other.secret else return false end end |
#to_hash ⇒ Object Also known as: to_h
102 103 104 105 106 107 |
# File 'lib/signet/oauth_1/credential.rb', line 102 def to_hash return { "oauth_token" => self.key, "oauth_token_secret" => self.secret } end |