Class: RestClient::Payload::Base
- Inherits:
-
Object
- Object
- RestClient::Payload::Base
- Defined in:
- lib/restclient/payload.rb
Direct Known Subclasses
Instance Method Summary (collapse)
- - (Object) build_stream(params)
- - (Object) close
-
- (Object) flatten_params(params, parent_key = nil)
Flatten parameters by converting hashes of hashes to flat hashes => {keys2 => value} will be transformed into [keys1[key2], value].
- - (Object) flatten_params_array(value, calculated_key)
- - (Object) headers
-
- (Base) initialize(params)
constructor
A new instance of Base.
- - (Object) inspect
- - (Object) read(bytes = nil) (also: #to_s)
- - (Object) short_inspect
- - (Object) size (also: #length)
Constructor Details
- (Base) initialize(params)
A new instance of Base
52 53 54 |
# File 'lib/restclient/payload.rb', line 52 def initialize(params) build_stream(params) end |
Instance Method Details
- (Object) build_stream(params)
56 57 58 59 |
# File 'lib/restclient/payload.rb', line 56 def build_stream(params) @stream = StringIO.new(params) @stream.seek(0) end |
- (Object) close
108 109 110 |
# File 'lib/restclient/payload.rb', line 108 def close @stream.close unless @stream.closed? end |
- (Object) flatten_params(params, parent_key = nil)
Flatten parameters by converting hashes of hashes to flat hashes => {keys2 => value} will be transformed into [keys1[key2], value]
69 70 71 72 73 74 75 76 77 78 79 80 81 82 |
# File 'lib/restclient/payload.rb', line 69 def flatten_params(params, parent_key = nil) result = [] params.each do |key, value| calculated_key = parent_key ? "#{parent_key}[#{handle_key(key)}]" : handle_key(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 |
- (Object) flatten_params_array(value, calculated_key)
84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/restclient/payload.rb', line 84 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 |
- (Object) headers
98 99 100 |
# File 'lib/restclient/payload.rb', line 98 def headers {'Content-Length' => size.to_s} end |
- (Object) inspect
112 113 114 115 116 |
# File 'lib/restclient/payload.rb', line 112 def inspect result = to_s.inspect @stream.seek(0) result end |
- (Object) read(bytes = nil) Also known as: to_s
61 62 63 |
# File 'lib/restclient/payload.rb', line 61 def read(bytes=nil) @stream.read(bytes) end |
- (Object) short_inspect
118 119 120 |
# File 'lib/restclient/payload.rb', line 118 def short_inspect (size > 500 ? "#{size} byte(s) length" : inspect) end |
- (Object) size Also known as: length
102 103 104 |
# File 'lib/restclient/payload.rb', line 102 def size @stream.size end |