Class: UriSigner::QueryHashParser

Inherits:
Object
  • Object
show all
Defined in:
lib/uri_signer/query_hash_parser.rb

Overview

This object takes in a hash, most likely from Rack::Utils.parse_query, and transforms it into a query string that’s used for signing requests. It takes a hash, transforms the hash into a query string that has it’s parts ordered accordingly.

Examples:

parser = UriSigner::QueryHashParser.new({"order"=>["name:desc", "id:desc"], "where"=>["name:nate", "id:123"]})

parser.to_s
# => "order=name:desc&order=id:desc&where=name:nate&where=id:123"

Instance Method Summary collapse

Constructor Details

#initialize(query_hash) ⇒ void

Creates a new QueryHashParser instance

Parameters:

  • query_hash (Hash)

    A hash of key/values to turn into a query stringo

Raises:



18
19
20
21
22
# File 'lib/uri_signer/query_hash_parser.rb', line 18

def initialize(query_hash)
  @query_hash = query_hash

  raise UriSigner::Errors::MissingQueryHashError.new('Please provide a query string hash') unless query_hash?
end

Instance Method Details

#to_sString

Returns the hash (key/values) as an ordered query string. This joins the keys and values, and then joins it all with the ampersand. This is not escaped

Returns:



28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/uri_signer/query_hash_parser.rb', line 28

def to_s
  parts = @query_hash.sort.inject([]) do |arr, (key,value)|
    if value.kind_of?(Array)
      value.each do |nested|
        arr << "%s=%s" % [key, nested]
      end
    else
      arr << "%s=%s" % [key, value]
    end
    arr
  end
  parts.join('&')
end