class Amazon::Coral::QueryStringMap

A hash containing query string parameters that produces a query string via to_s. Also consumes hashes representing hierarchies of data to encode as query parameters.

Public Class Methods

new(hash = {}) click to toggle source

Instantiate a QueryStringMap with the contents of the specified hash. If no hash is provided, an empty map is created.

# File lib/amazon/coral/querystringmap.rb, line 16
def initialize(hash = {})
  add_flattened(hash)
end

Public Instance Methods

to_s() click to toggle source

Returns the query string representation of this map by collapsing its key-value pairs into URL parameters.

# File lib/amazon/coral/querystringmap.rb, line 22
def to_s
  qstr = ''
  isFirst = true
  each_pair { |k,v|
    if isFirst then
      isFirst = false
    else
      qstr << '&'
    end
    qstr << UrlEncoding.encode(k.to_s)
    unless(v.nil?) then
      qstr << '='
      qstr << UrlEncoding.encode(v.to_s)
    end
  }
  return qstr
end

Private Instance Methods

add_flattened(hash) click to toggle source
# File lib/amazon/coral/querystringmap.rb, line 41
def add_flattened(hash)
  stack = []
  add_flattened_helper(stack, hash)
end
add_flattened_helper(stack, obj) click to toggle source
# File lib/amazon/coral/querystringmap.rb, line 46
def add_flattened_helper(stack, obj)
  return if obj.nil?
  
  case obj
  when Hash then

      obj.each_pair { |k,v|
      stack.push(k)
      add_flattened_helper(stack, v)
      stack.pop
    }

  when Array then

      # Do artificial list member wrapping (Coral requires this
      # level of indirection, but doesn't validate the member name)
      stack.push("member")

    obj.each_index { |i|
      v = obj[i]
      stack.push(i + 1) # query string arrays are 1-based
      add_flattened_helper(stack, v)
      stack.pop
    }

    stack.pop
  else

    # this works for symbols also, because sym.id2name == sym.to_s
    self[get_key(stack)] = obj.to_s

  end
end
get_key(stack) click to toggle source
# File lib/amazon/coral/querystringmap.rb, line 80
def get_key(stack)
  key = ''
  stack.each_index { |i|
    key << '.' if(i > 0)
    key << stack[i].to_s
  }
  return key
end