Module: Faraday::Utils

### Home Classes Methods

### In Files

- lib/faraday/utils.rb

### Namespace

- CLASS Faraday::Utils::Headers
- CLASS Faraday::Utils::ParamsHash

### Methods

- #URI
- #build_nested_query
- #build_query
- #deep_merge
- #deep_merge!
- #default_params_encoder
- #default_uri_parser
- #default_uri_parser=
- #escape
- #normalize_params
- #normalize_path
- #parse_nested_query
- #parse_query
- #sort_query_params
- #unescape

### Class/Module Index

Quicksearch

- Faraday
- Faraday::Adapter
- Faraday::Adapter::EMHttp
- Faraday::Adapter::EMHttp::Manager
- Faraday::Adapter::EMHttp::Options
- Faraday::Adapter::EMSynchrony
- Faraday::Adapter::EMSynchrony::ParallelManager
- Faraday::Adapter::Excon
- Faraday::Adapter::HTTPClient
- Faraday::Adapter::NetHttp
- Faraday::Adapter::NetHttpPersistent
- Faraday::Adapter::Parallelism
- Faraday::Adapter::Patron
- Faraday::Adapter::Rack
- Faraday::Adapter::Test
- Faraday::Adapter::Test::Stub
- Faraday::Adapter::Test::Stubs
- Faraday::Adapter::Test::Stubs::NotFound
- Faraday::Adapter::Typhoeus
- Faraday::AutoloadHelper
- Faraday::ClientError
- Faraday::CompositeReadIO
- Faraday::Connection
- Faraday::ConnectionFailed
- Faraday::ConnectionOptions
- Faraday::Env
- Faraday::Error
- Faraday::FlatParamsEncoder
- Faraday::Middleware
- Faraday::MiddlewareRegistry
- Faraday::MissingDependency
- Faraday::NestedParamsEncoder
- Faraday::Options
- Faraday::ParsingError
- Faraday::ProxyOptions
- Faraday::RackBuilder
- Faraday::RackBuilder::Handler
- Faraday::RackBuilder::StackLocked
- Faraday::Request
- Faraday::Request::Authorization
- Faraday::Request::BasicAuthentication
- Faraday::Request::Instrumentation
- Faraday::Request::Instrumentation::Options
- Faraday::Request::Multipart
- Faraday::Request::Retry
- Faraday::Request::Retry::Options
- Faraday::Request::TokenAuthentication
- Faraday::Request::UrlEncoded
- Faraday::RequestOptions
- Faraday::ResourceNotFound
- Faraday::Response
- Faraday::Response::Logger
- Faraday::Response::Middleware
- Faraday::Response::RaiseError
- Faraday::SSLError
- Faraday::SSLOptions
- Faraday::TimeoutError
- Faraday::Utils
- Faraday::Utils::Headers
- Faraday::Utils::ParamsHash
- EmHttpSslPatch
- Object

No matching classes.

# Faraday::Utils

### Constants

DEFAULT_SEP
ESCAPE_RE

### Attributes

default_params_encoder[W]

### Public Instance Methods

URI(url) click to toggle source

Normalize URI() behavior across Ruby versions

url - A String or URI.

Returns a parsed URI.

    # File lib/faraday/utils.rb, line 242
    def URI(url)
      if url.respond_to?(:host)
        url
      elsif url.respond_to?(:to_str)
        default_uri_parser.call(url)
      else
        raise ArgumentError, "bad argument (expected URI object or URI string)"
      end
    end

build_nested_query(params) click to toggle source

    # File lib/faraday/utils.rb, line 167
    def build_nested_query(params)
      NestedParamsEncoder.encode(params)
    end

build_query(params) click to toggle source

    # File lib/faraday/utils.rb, line 163
    def build_query(params)
      FlatParamsEncoder.encode(params)
    end

deep_merge(source, hash) click to toggle source

Recursive hash merge

    # File lib/faraday/utils.rb, line 287
    def deep_merge(source, hash)
      deep_merge!(source.dup, hash)
    end

deep_merge!(target, hash) click to toggle source

Recursive hash update

    # File lib/faraday/utils.rb, line 275
    def deep_merge!(target, hash)
      hash.each do |key, value|
        if Hash === value and Hash === target[key]
          target[key] = deep_merge(target[key], value)
        else
          target[key] = value
        end
      end
      target
    end

default_params_encoder() click to toggle source

    # File lib/faraday/utils.rb, line 192
    def default_params_encoder
      @default_params_encoder ||= NestedParamsEncoder
    end

default_uri_parser() click to toggle source

    # File lib/faraday/utils.rb, line 252
    def default_uri_parser
      @default_uri_parser ||= begin
        require 'uri'
        Kernel.method(:URI)
      end
    end

default_uri_parser=(parser) click to toggle source

    # File lib/faraday/utils.rb, line 259
    def default_uri_parser=(parser)
      @default_uri_parser = if parser.respond_to?(:call) || parser.nil?
        parser
      else
        parser.method(:parse)
      end
    end

escape(s) click to toggle source

    # File lib/faraday/utils.rb, line 173
    def escape(s)
      s.to_s.gsub(ESCAPE_RE) {|match|
        '%' + match.unpack('H2' * match.bytesize).join('%').upcase
      }.tr(' ', '+')
    end

normalize_params(params, name, v = nil) click to toggle source

Stolen from Rack

    # File lib/faraday/utils.rb, line 201
    def normalize_params(params, name, v = nil)
      name =~ %(\A[\[\]]*([^\[\]]+)\]*)
      k = $1 || ''
      after = $' || ''

      return if k.empty?

      if after == ""
        if params[k]
          params[k] = Array[params[k]] unless params[k].kind_of?(Array)
          params[k] << v
        else
          params[k] = v
        end
      elsif after == "[]"
        params[k] ||= []
        raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
        params[k] << v
      elsif after =~ %(^\[\]\[([^\[\]]+)\]$) || after =~ %(^\[\](.+)$)
        child_key = $1
        params[k] ||= []
        raise TypeError, "expected Array (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Array)
        if params[k].last.is_a?(Hash) && !params[k].last.key?(child_key)
          normalize_params(params[k].last, child_key, v)
        else
          params[k] << normalize_params({}, child_key, v)
        end
      else
        params[k] ||= {}
        raise TypeError, "expected Hash (got #{params[k].class.name}) for param `#{k}'" unless params[k].is_a?(Hash)
        params[k] = normalize_params(params[k], after, v)
      end

      return params
    end

normalize_path(url) click to toggle source

Receives a String or URI and returns just the path with the query string sorted.

    # File lib/faraday/utils.rb, line 268
    def normalize_path(url)
      url = URI(url)
      (url.path.start_with?('/') ? url.path : '/' + url.path) +
      (url.query ? "?#{sort_query_params(url.query)}" : "")
    end

parse_nested_query(query) click to toggle source

    # File lib/faraday/utils.rb, line 188
    def parse_nested_query(query)
      NestedParamsEncoder.decode(query)
    end

parse_query(query) click to toggle source

Adapted from Rack

    # File lib/faraday/utils.rb, line 184
    def parse_query(query)
      FlatParamsEncoder.decode(query)
    end

unescape(s) click to toggle source

    # File lib/faraday/utils.rb, line 179
    def unescape(s) CGI.unescape s.to_s end

### Protected Instance Methods

sort_query_params(query) click to toggle source

    # File lib/faraday/utils.rb, line 293
    def sort_query_params(query)
      query.split('&').sort.join('&')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.