class RestClient::Payload::Base

Public Class Methods

new(params) click to toggle source
# File lib/restclient/payload.rb, line 35
def initialize(params)
  build_stream(params)
end

Public Instance Methods

build_stream(params) click to toggle source
# File lib/restclient/payload.rb, line 39
def build_stream(params)
  @stream = StringIO.new(params)
  @stream.seek(0)
end
close() click to toggle source
# File lib/restclient/payload.rb, line 95
def close
  @stream.close
end
escape(v) click to toggle source
# File lib/restclient/payload.rb, line 50
def escape(v)
  URI.escape(v.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end
flatten_params(params, parent_key = nil) click to toggle source

Flatten parameters by converting hashes of hashes to flat hashes {keys1 => {keys2 => value}} will be transformed into [keys1, value]

# File lib/restclient/payload.rb, line 56
def flatten_params(params, parent_key = nil)
  result = []
  params.each do |key, value|
    calculated_key = parent_key ? "#{parent_key}[#{escape key}]" : escape(key)
    if value.is_a? Hash
      result += flatten_params(value, calculated_key)
    elsif value.is_a? Array
      result += flatten_params_array(value, calculated_key)
    else
      result << [calculated_key, value]
    end
  end
  result
end
flatten_params_array(value, calculated_key) click to toggle source
# File lib/restclient/payload.rb, line 71
def flatten_params_array value, calculated_key
  result = []
  value.each do |elem|
    if elem.is_a? Hash
      result +=  flatten_params(elem, calculated_key)
    elsif elem.is_a? Array
      result += flatten_params_array(elem, calculated_key)
    else
      result << ["#{calculated_key}[]", elem]
    end
  end
  result
end
headers() click to toggle source
# File lib/restclient/payload.rb, line 85
def headers
  { 'Content-Length' => size.to_s }
end
inspect() click to toggle source
# File lib/restclient/payload.rb, line 99
def inspect
  result = to_s.inspect
  @stream.seek(0)
  result
end
length() click to toggle source
Alias for: size
read(bytes=nil) click to toggle source
# File lib/restclient/payload.rb, line 44
def read(bytes=nil)
  @stream.read(bytes)
end
Also aliased as: to_s
short_inspect() click to toggle source
# File lib/restclient/payload.rb, line 105
def short_inspect
  (size > 500 ? "#{size} byte(s) length" : inspect)
end
size() click to toggle source
# File lib/restclient/payload.rb, line 89
def size
  @stream.size
end
Also aliased as: length
to_s(bytes=nil) click to toggle source
Alias for: read