Class: RestBuilder::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/rest-builder/payload.rb

Direct Known Subclasses

Streamed

Defined Under Namespace

Classes: Multipart, Streamed, StreamedString, UrlEncoded

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(payload) ⇒ Payload

Returns a new instance of Payload.



55
# File 'lib/rest-builder/payload.rb', line 55

def initialize payload; @io = payload          ; end

Instance Attribute Details

#ioObject (readonly) Also known as: to_io

Payload API



52
53
54
# File 'lib/rest-builder/payload.rb', line 52

def io
  @io
end

Class Method Details

.generate(payload) ⇒ Object



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/rest-builder/payload.rb', line 27

def self.generate payload
  if payload.respond_to?(:read)
    Streamed.new(payload)

  elsif payload.kind_of?(String)
    StreamedString.new(payload)

  elsif payload.kind_of?(Hash)
    if payload.empty?
      nil

    elsif Middleware.contain_binary?(payload)
      Multipart.new(payload)

    else
      UrlEncoded.new(payload)
    end

  else
    raise Error.new("Payload should be either String, Hash, or" \
                    " responding to `read', but: #{payload.inspect}")
  end
end

.generate_with_headers(payload, headers) ⇒ Object



18
19
20
21
22
23
24
25
# File 'lib/rest-builder/payload.rb', line 18

def self.generate_with_headers payload, headers
  h = if p = generate(payload)
        p.headers.merge(headers)
      else
        headers
      end
  [p, h]
end

Instance Method Details

#closeObject



57
# File 'lib/rest-builder/payload.rb', line 57

def close             ; io.close unless closed?; end

#closed?Boolean

Returns:

  • (Boolean)


58
# File 'lib/rest-builder/payload.rb', line 58

def closed?           ; io.closed?             ; end

#headersObject



59
# File 'lib/rest-builder/payload.rb', line 59

def headers           ; {}                     ; end

#read(bytes = nil) ⇒ Object



56
# File 'lib/rest-builder/payload.rb', line 56

def read     bytes=nil; io.read(bytes)         ; end

#sizeObject



61
62
63
64
65
66
67
68
69
# File 'lib/rest-builder/payload.rb', line 61

def size
  if io.respond_to?(:size)
    io.size
  elsif io.respond_to?(:stat)
    io.stat.size
  else
    0
  end
end