Class: RestClient::Payload::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/restclient/payload.rb

Direct Known Subclasses

Multipart, UrlEncoded

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



35
36
37
# File 'lib/restclient/payload.rb', line 35

def initialize(params)
  build_stream(params)
end

Instance Method Details

#build_stream(params) ⇒ Object



39
40
41
42
# File 'lib/restclient/payload.rb', line 39

def build_stream(params)
  @stream = StringIO.new(params)
  @stream.seek(0)
end

#closeObject



83
84
85
# File 'lib/restclient/payload.rb', line 83

def close
  @stream.close
end

#escape(v) ⇒ Object



50
51
52
# 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) ⇒ Object

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



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# 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).flatten
    elsif value.is_a? Array
      value.each do |elem|
        result << [calculated_key, elem]
      end
    else
      result << [calculated_key, value]
    end
  end
  result
end

#headersObject



73
74
75
# File 'lib/restclient/payload.rb', line 73

def headers
  { 'Content-Length' => size.to_s }
end

#inspectObject



87
88
89
90
91
# File 'lib/restclient/payload.rb', line 87

def inspect
  result = to_s.inspect
  @stream.seek(0)
  result
end

#read(bytes = nil) ⇒ Object Also known as: to_s



44
45
46
# File 'lib/restclient/payload.rb', line 44

def read(bytes=nil)
  @stream.read(bytes)
end

#short_inspectObject



93
94
95
# File 'lib/restclient/payload.rb', line 93

def short_inspect
  (size > 100 ? "#{size} byte(s) length" : inspect)
end

#sizeObject Also known as: length



77
78
79
# File 'lib/restclient/payload.rb', line 77

def size
  @stream.size
end