Class: OAuth::RequestProxy::Base — Documentation by YARD 0.9.45

# Class: OAuth::RequestProxy::Base

Inherits:
  Object
  - Object
  - OAuth::RequestProxy::Base
  show all

Includes:
  Helper

Defined in:
  lib/oauth/request_proxy/base.rb

## Direct Known Subclasses

ActionControllerRequest, Curl::Easy, EventMachine::HttpRequest, JabberRequest, MockRequest, Net::HTTP::HTTPRequest, RackRequest, RestClient::Request, Typhoeus::Request

## Instance Attribute Summary collapse

- #options ⇒ Object

  Returns the value of attribute options.

- #request ⇒ Object

  Returns the value of attribute request.

- #unsigned_parameters ⇒ Object

  Returns the value of attribute unsigned_parameters.

## Class Method Summary collapse

- .proxies(klass) ⇒ Object

## Instance Method Summary collapse

- #initialize(request, options = {}) ⇒ Base constructor

  A new instance of Base.

- #non_oauth_parameters ⇒ Object
- #normalized_parameters ⇒ Object

  See 9.1.1.

- #normalized_uri ⇒ Object

  See 9.1.2 in specs.

- #oauth_callback ⇒ Object

  OAuth parameters.

- #oauth_consumer_key ⇒ Object (also: #consumer_key)
- #oauth_header(options = {}) ⇒ Object

  Authorization header for OAuth.

- #oauth_nonce ⇒ Object (also: #nonce)
- #oauth_parameters ⇒ Object
- #oauth_signature ⇒ Object (also: #signature)
- #oauth_signature_method ⇒ Object (also: #signature_method)
- #oauth_timestamp ⇒ Object (also: #timestamp)
- #oauth_token ⇒ Object (also: #token)
- #oauth_verifier ⇒ Object

  OAuth 1.0a only: value returned to the Consumer after user authorization and required when exchanging a Request Token for an Access Token.

- #oauth_version ⇒ Object
- #parameters ⇒ Object

  Parameter accessors.

- #parameters_for_signature ⇒ Object
- #query_string_blank? ⇒ Boolean
- #sign(options = {}) ⇒ Object
- #sign!(options = {}) ⇒ Object
- #signature_and_unsigned_parameters ⇒ Object
- #signature_base_string ⇒ Object

  See 9.1 in specs.

- #signed? ⇒ Boolean

  Has this request been signed yet?.

- #signed_uri(with_oauth: true) ⇒ Object

  URI, including OAuth parameters.

### Methods included from Helper

_escape, escape, generate_key, generate_timestamp, normalize, normalize_nested_query, parse_header, stringify_keys, unescape

## Constructor Details

### #initialize(request, options = {}) ⇒ `Base`

Returns a new instance of Base.

    19
    20
    21
    22
    23
    # File 'lib/oauth/request_proxy/base.rb', line 19

    def initialize(request, options = {})
      @request = request
      @unsigned_parameters = (options[:unsigned_parameters] || []).map(&:to_s)
      @options = options
    end

## Instance Attribute Details

### #options ⇒ `Object`

Returns the value of attribute options.

    17
    18
    19
    # File 'lib/oauth/request_proxy/base.rb', line 17

    def options
      @options
    end

### #request ⇒ `Object`

Returns the value of attribute request.

    17
    18
    19
    # File 'lib/oauth/request_proxy/base.rb', line 17

    def request
      @request
    end

### #unsigned_parameters ⇒ `Object`

Returns the value of attribute unsigned_parameters.

    17
    18
    19
    # File 'lib/oauth/request_proxy/base.rb', line 17

    def unsigned_parameters
      @unsigned_parameters
    end

## Class Method Details

### .proxies(klass) ⇒ `Object`

    12
    13
    14
    # File 'lib/oauth/request_proxy/base.rb', line 12

    def proxies(klass)
      OAuth::RequestProxy.available_proxies[klass] = self
    end

## Instance Method Details

### #non_oauth_parameters ⇒ `Object`

    89
    90
    91
    # File 'lib/oauth/request_proxy/base.rb', line 89

    def non_oauth_parameters
      parameters.select { |k, _v| !OAuth::PARAMETERS.include?(k) }
    end

### #normalized_parameters ⇒ `Object`

See 9.1.1. in specs Normalize Request Parameters

    104
    105
    106
    # File 'lib/oauth/request_proxy/base.rb', line 104

    def normalized_parameters
      normalize(parameters_for_signature)
    end

### #normalized_uri ⇒ `Object`

See 9.1.2 in specs

    98
    99
    100
    101
    # File 'lib/oauth/request_proxy/base.rb', line 98

    def normalized_uri
      u = URI.parse(uri)
      "#{u.scheme.downcase}://#{u.host.downcase}#{":#{u.port}" if (u.scheme.casecmp("http").zero? && u.port != 80) || (u.scheme.casecmp("https").zero? && u.port != 443)}#{(u.path && u.path != "") ? u.path : "/"}"
    end

### #oauth_callback ⇒ `Object`

OAuth parameters

    27
    28
    29
    # File 'lib/oauth/request_proxy/base.rb', line 27

    def oauth_callback
      [parameters["oauth_callback"]].flatten.first
    end

### #oauth_consumer_key ⇒ `Object` Also known as: consumer_key

    31
    32
    33
    # File 'lib/oauth/request_proxy/base.rb', line 31

    def oauth_consumer_key
      [parameters["oauth_consumer_key"]].flatten.first
    end

### #oauth_header(options = {}) ⇒ `Object`

Authorization header for OAuth

    145
    146
    147
    148
    149
    150
    # File 'lib/oauth/request_proxy/base.rb', line 145

    def oauth_header(options = {})
      header_params_str = oauth_parameters.map { |k, v| "#{k}=\"#{escape(v)}\"" }.join(", ")

      realm = "realm=\"#{options[:realm]}\", " if options[:realm]
      "OAuth #{realm}#{header_params_str}"
    end

### #oauth_nonce ⇒ `Object` Also known as: nonce

    35
    36
    37
    # File 'lib/oauth/request_proxy/base.rb', line 35

    def oauth_nonce
      [parameters["oauth_nonce"]].flatten.first
    end

### #oauth_parameters ⇒ `Object`

    85
    86
    87
    # File 'lib/oauth/request_proxy/base.rb', line 85

    def oauth_parameters
      parameters.select { |k, v| OAuth::PARAMETERS.include?(k) && !v.nil? && v != "" }
    end

### #oauth_signature ⇒ `Object` Also known as: signature

    39
    40
    41
    42
    # File 'lib/oauth/request_proxy/base.rb', line 39

    def oauth_signature
      # TODO: can this be nil?
      [parameters["oauth_signature"]].flatten.first || ""
    end

### #oauth_signature_method ⇒ `Object` Also known as: signature_method

    44
    45
    46
    # File 'lib/oauth/request_proxy/base.rb', line 44

    def oauth_signature_method
      [parameters["oauth_signature_method"]].flatten.first
    end

### #oauth_timestamp ⇒ `Object` Also known as: timestamp

    48
    49
    50
    # File 'lib/oauth/request_proxy/base.rb', line 48

    def oauth_timestamp
      [parameters["oauth_timestamp"]].flatten.first
    end

### #oauth_token ⇒ `Object` Also known as: token

    52
    53
    54
    # File 'lib/oauth/request_proxy/base.rb', line 52

    def oauth_token
      [parameters["oauth_token"]].flatten.first
    end

### #oauth_verifier ⇒ `Object`

OAuth 1.0a only: value returned to the Consumer after user authorization
and required when exchanging a Request Token for an Access Token.
Not present in OAuth 1.0 flows.

    59
    60
    61
    # File 'lib/oauth/request_proxy/base.rb', line 59

    def oauth_verifier
      [parameters["oauth_verifier"]].flatten.first
    end

### #oauth_version ⇒ `Object`

    63
    64
    65
    # File 'lib/oauth/request_proxy/base.rb', line 63

    def oauth_version
      [parameters["oauth_version"]].flatten.first
    end

### #parameters ⇒ `Object`

Parameter accessors

Raises:

- (`NotImplementedError`)

    77
    78
    79
    # File 'lib/oauth/request_proxy/base.rb', line 77

    def parameters
      raise NotImplementedError, "Must be implemented by subclasses"
    end

### #parameters_for_signature ⇒ `Object`

    81
    82
    83
    # File 'lib/oauth/request_proxy/base.rb', line 81

    def parameters_for_signature
      parameters.select { |k, _v| !signature_and_unsigned_parameters.include?(k) }
    end

### #query_string_blank? ⇒ `Boolean`

Returns:

- (`Boolean`)

    152
    153
    154
    155
    156
    157
    158
    # File 'lib/oauth/request_proxy/base.rb', line 152

    def query_string_blank?
      if (uri = request.env["REQUEST_URI"])
        uri.split("?", 2)[1].nil?
      else
        request.query_string.match(/\A\s*\z/)
      end
    end

### #sign(options = {}) ⇒ `Object`

    108
    109
    110
    # File 'lib/oauth/request_proxy/base.rb', line 108

    def sign(options = {})
      OAuth::Signature.sign(self, options)
    end

### #sign!(options = {}) ⇒ `Object`

    112
    113
    114
    115
    116
    # File 'lib/oauth/request_proxy/base.rb', line 112

    def sign!(options = {})
      parameters["oauth_signature"] = sign(options)
      @signed = true
      signature
    end

### #signature_and_unsigned_parameters ⇒ `Object`

    93
    94
    95
    # File 'lib/oauth/request_proxy/base.rb', line 93

    def signature_and_unsigned_parameters
      unsigned_parameters + ["oauth_signature"]
    end

### #signature_base_string ⇒ `Object`

See 9.1 in specs

    119
    120
    121
    122
    # File 'lib/oauth/request_proxy/base.rb', line 119

    def signature_base_string
      base = [method, normalized_uri, normalized_parameters]
      base.map { |v| escape(v) }.join("&")
    end

### #signed? ⇒ `Boolean`

Has this request been signed yet?

Returns:

- (`Boolean`)

    125
    126
    127
    # File 'lib/oauth/request_proxy/base.rb', line 125

    def signed?
      @signed
    end

### #signed_uri(with_oauth: true) ⇒ `Object`

URI, including OAuth parameters

    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    # File 'lib/oauth/request_proxy/base.rb', line 130

    def signed_uri(with_oauth: true)
      if signed?
        params = if with_oauth
          parameters
        else
          non_oauth_parameters
        end

        [uri, normalize(params)].join("?")
      else
        warn("This request has not yet been signed!")
      end
    end