Module: Faraday::Utils

### Home Classes Methods

### In Files

- lib/faraday/utils.rb

### Namespace

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

### Methods

- #build_nested_query
- #build_query
- #deep_merge
- #deep_merge!
- #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::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::Builder
- Faraday::Builder::Handler
- Faraday::Builder::StackLocked
- Faraday::CompositeReadIO
- Faraday::Connection
- Faraday::Error
- Faraday::Error::ClientError
- Faraday::Error::ConnectionFailed
- Faraday::Error::MissingDependency
- Faraday::Error::ParsingError
- Faraday::Error::ResourceNotFound
- Faraday::Error::TimeoutError
- Faraday::Middleware
- Faraday::MiddlewareRegistry
- Faraday::Request
- Faraday::Request::Authorization
- Faraday::Request::BasicAuthentication
- Faraday::Request::Multipart
- Faraday::Request::Retry
- Faraday::Request::TokenAuthentication
- Faraday::Request::UrlEncoded
- Faraday::Response
- Faraday::Response::Logger
- Faraday::Response::Middleware
- Faraday::Response::RaiseError
- Faraday::Utils
- Faraday::Utils::Headers
- Faraday::Utils::ParamsHash
- Object

No matching classes.

# Faraday::Utils

### Constants

DEFAULT_SEP
ESCAPE_RE

### Public Instance Methods

build_nested_query(value, prefix = nil) click to toggle source

Rack's version modified to handle non-String values

    # File lib/faraday/utils.rb, line 157
    def build_nested_query(value, prefix = nil)
      case value
      when Array
        value.map { |v| build_nested_query(v, "#{prefix}%5B%5D") }.join("&")
      when Hash
        value.map { |k, v|
          build_nested_query(v, prefix ? "#{prefix}%5B#{escape(k)}%5D" : escape(k))
        }.join("&")
      when NilClass
        prefix
      else
        raise ArgumentError, "value must be a Hash" if prefix.nil?
        "#{prefix}=#{escape(value)}"
      end
    end

build_query(params) click to toggle source

Copied from Rack

    # File lib/faraday/utils.rb, line 146
    def build_query(params)
      params.map { |k, v|
        if v.class == Array
          build_query(v.map { |x| [k, x] })
        else
          v.nil? ? escape(k) : "#{escape(k)}=#{escape(v)}"
        end
      }.join("&")
    end

deep_merge(source, hash) click to toggle source

Recursive hash merge

    # File lib/faraday/utils.rb, line 264
    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 252
    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

escape(s) click to toggle source

    # File lib/faraday/utils.rb, line 175
    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 214
    def normalize_params(params, name, v = nil)
      name =~ %(\A[\[\]]*([^\[\]]+)\]*)
      k = $1 || ''
      after = $' || ''

      return if k.empty?

      if after == ""
        params[k] = v
      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 URL and returns just the path with the query string sorted.

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

parse_nested_query(qs) click to toggle source

    # File lib/faraday/utils.rb, line 203
    def parse_nested_query(qs)
      params = {}

      (qs || '').split(DEFAULT_SEP).each do |p|
        k, v = p.split('=', 2).map { |s| unescape(s) }
        normalize_params(params, k, v)
      end
      params
    end

parse_query(qs) click to toggle source

Adapted from Rack

    # File lib/faraday/utils.rb, line 186
    def parse_query(qs)
      params = {}

      (qs || '').split(DEFAULT_SEP).each do |p|
        k, v = p.split('=', 2).map { |x| unescape(x) }

        if cur = params[k]
          if cur.class == Array then params[k] << v
          else params[k] = [cur, v]
          end
        else
          params[k] = v
        end
      end
      params
    end

unescape(s) click to toggle source

    # File lib/faraday/utils.rb, line 181
    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 270
    def sort_query_params(query)
      query.split('&').sort.join('&')
    end

[Validate]

Generated with the Darkfish Rdoc Generator 2.