Class: Coolio::Http::Payload::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/cool.io/http/payload.rb

Direct Known Subclasses

Multipart, Streamed, UrlEncoded

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Base

Returns a new instance of Base.



55
56
57
# File 'lib/cool.io/http/payload.rb', line 55

def initialize(params)
  build_stream(params)
end

Instance Method Details

#build_stream(params) ⇒ Object



59
60
61
62
# File 'lib/cool.io/http/payload.rb', line 59

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

#closeObject



111
112
113
# File 'lib/cool.io/http/payload.rb', line 111

def close
  @stream.close unless @stream.closed?
end

#flatten_params(params, parent_key = nil) ⇒ Object

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



72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/cool.io/http/payload.rb', line 72

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

#flatten_params_array(value, calculated_key) ⇒ Object



87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cool.io/http/payload.rb', line 87

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

#headersObject



101
102
103
# File 'lib/cool.io/http/payload.rb', line 101

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

#inspectObject



115
116
117
118
119
# File 'lib/cool.io/http/payload.rb', line 115

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

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



64
65
66
# File 'lib/cool.io/http/payload.rb', line 64

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

#short_inspectObject



121
122
123
# File 'lib/cool.io/http/payload.rb', line 121

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

#sizeObject Also known as: length



105
106
107
# File 'lib/cool.io/http/payload.rb', line 105

def size
  @stream.size
end