Class: RestCore::Payload

Inherits:
Object
  • Object
show all
Includes:
RestCore
Defined in:
lib/rest-core/util/payload.rb

Direct Known Subclasses

Streamed

Defined Under Namespace

Classes: Multipart, Streamed, StreamedString, UrlEncoded

Constant Summary

Constants included from RestCore

ASYNC, CLIENT, DRY, FAIL, HIJACK, LOG, PROMISE, REQUEST_HEADERS, REQUEST_METHOD, REQUEST_PATH, REQUEST_PAYLOAD, REQUEST_QUERY, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_SOCKET, RESPONSE_STATUS, Simple, TIMER, Universal, VERSION

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from RestCore

eagerload, id

Constructor Details

#initialize(payload) ⇒ Payload

Returns a new instance of Payload.



52
# File 'lib/rest-core/util/payload.rb', line 52

def initialize payload; @io = payload          ; end

Instance Attribute Details

#ioObject (readonly) Also known as: to_io

Payload API



49
50
51
# File 'lib/rest-core/util/payload.rb', line 49

def io
  @io
end

Class Method Details

.generate(payload) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/rest-core/util/payload.rb', line 24

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



15
16
17
18
19
20
21
22
# File 'lib/rest-core/util/payload.rb', line 15

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



54
# File 'lib/rest-core/util/payload.rb', line 54

def close             ; io.close unless closed?; end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?           ; io.closed?             ; end

#headersObject



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

def headers           ; {}                     ; end

#read(bytes = nil) ⇒ Object



53
# File 'lib/rest-core/util/payload.rb', line 53

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

#sizeObject



58
59
60
61
62
63
64
65
66
# File 'lib/rest-core/util/payload.rb', line 58

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