Class: OAuth::Client::Helper — Documentation by YARD 0.9.45

# Class: OAuth::Client::Helper

Inherits:
  Object
  - Object
  - OAuth::Client::Helper
  show all

Includes:
  Helper

Defined in:
  lib/oauth/client/helper.rb

## Instance Attribute Summary collapse

- #options ⇒ Object readonly

  Returns the value of attribute options.

## Instance Method Summary collapse

- #amend_user_agent_header(headers) ⇒ Object
- #hash_body ⇒ Object
- #header ⇒ Object
- #initialize(request, options = {}) ⇒ Helper constructor

  A new instance of Helper.

- #nonce ⇒ Object
- #oauth_parameters ⇒ Object
- #parameters ⇒ Object
- #parameters_with_oauth ⇒ Object
- #signature(extra_options = {}) ⇒ Object
- #signature_base_string(extra_options = {}) ⇒ Object
- #timestamp ⇒ Object
- #token_request? ⇒ Boolean

### Methods included from Helper

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

## Constructor Details

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

Returns a new instance of Helper.

    14
    15
    16
    17
    18
    # File 'lib/oauth/client/helper.rb', line 14

    def initialize(request, options = {})
      @request = request
      @options = options
      @options[:signature_method] ||= "HMAC-SHA1"
    end

## Instance Attribute Details

### #options ⇒ `Object` (readonly)

Returns the value of attribute options.

    20
    21
    22
    # File 'lib/oauth/client/helper.rb', line 20

    def options
      @options
    end

## Instance Method Details

### #amend_user_agent_header(headers) ⇒ `Object`

    77
    78
    79
    80
    81
    82
    83
    84
    85
    # File 'lib/oauth/client/helper.rb', line 77

    def amend_user_agent_header(headers)
      @oauth_ua_string ||= "OAuth gem v#{OAuth::Version::VERSION}"
      # Net::HTTP in 1.9 appends Ruby
      if headers["User-Agent"] && headers["User-Agent"] != "Ruby"
        headers["User-Agent"] += " (#{@oauth_ua_string})"
      else
        headers["User-Agent"] = @oauth_ua_string
      end
    end

### #hash_body ⇒ `Object`

    73
    74
    75
    # File 'lib/oauth/client/helper.rb', line 73

    def hash_body
      @options[:body_hash] = OAuth::Signature.body_hash(@request, parameters: oauth_parameters)
    end

### #header ⇒ `Object`

    87
    88
    89
    90
    91
    92
    93
    94
    95
    # File 'lib/oauth/client/helper.rb', line 87

    def header
      parameters = oauth_parameters
      parameters["oauth_signature"] = signature(options.merge(parameters: parameters))

      header_params_str = parameters.sort.map { |k, v| "#{k}=\"#{escape(v)}\"" }.join(", ")

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

### #nonce ⇒ `Object`

    22
    23
    24
    # File 'lib/oauth/client/helper.rb', line 22

    def nonce
      options[:nonce] ||= generate_key
    end

### #oauth_parameters ⇒ `Object`

    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    # File 'lib/oauth/client/helper.rb', line 30

    def oauth_parameters
      out = {
        "oauth_body_hash" => options[:body_hash],
        "oauth_callback" => options[:oauth_callback],
        "oauth_consumer_key" => options[:consumer].key,
        "oauth_token" => options[:token] ? options[:token].token : "",
        "oauth_signature_method" => options[:signature_method],
        "oauth_timestamp" => timestamp,
        "oauth_nonce" => nonce,
        "oauth_verifier" => options[:oauth_verifier],
        "oauth_version" => options[:oauth_version] || "1.0",
        "oauth_session_handle" => options[:oauth_session_handle]
      }
      allowed_empty_params = options[:allow_empty_params]
      if allowed_empty_params != true && !allowed_empty_params.is_a?(Array)
        allowed_empty_params = (allowed_empty_params == false) ? [] : [allowed_empty_params]
      end
      out.select! { |k, v| v.to_s != "" || allowed_empty_params == true || allowed_empty_params.include?(k) }
      out
    end

### #parameters ⇒ `Object`

    97
    98
    99
    # File 'lib/oauth/client/helper.rb', line 97

    def parameters
      OAuth::RequestProxy.proxy(@request).parameters
    end

### #parameters_with_oauth ⇒ `Object`

    101
    102
    103
    # File 'lib/oauth/client/helper.rb', line 101

    def parameters_with_oauth
      oauth_parameters.merge(parameters)
    end

### #signature(extra_options = {}) ⇒ `Object`

    51
    52
    53
    54
    55
    56
    57
    58
    # File 'lib/oauth/client/helper.rb', line 51

    def signature(extra_options = {})
      OAuth::Signature.sign(@request, {
        uri: options[:request_uri],
        consumer: options[:consumer],
        token: options[:token],
        unsigned_parameters: options[:unsigned_parameters]
      }.merge(extra_options))
    end

### #signature_base_string(extra_options = {}) ⇒ `Object`

    60
    61
    62
    63
    64
    65
    66
    67
    # File 'lib/oauth/client/helper.rb', line 60

    def signature_base_string(extra_options = {})
      OAuth::Signature.signature_base_string(@request, {
        uri: options[:request_uri],
        consumer: options[:consumer],
        token: options[:token],
        parameters: oauth_parameters
      }.merge(extra_options))
    end

### #timestamp ⇒ `Object`

    26
    27
    28
    # File 'lib/oauth/client/helper.rb', line 26

    def timestamp
      options[:timestamp] ||= generate_timestamp
    end

### #token_request? ⇒ `Boolean`

Returns:

- (`Boolean`)

    69
    70
    71
    # File 'lib/oauth/client/helper.rb', line 69

    def token_request?
      @options[:token_request].eql?(true)
    end