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, REQUEST_URI, RESPONSE_BODY, RESPONSE_HEADERS, RESPONSE_KEY, 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.



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

def initialize payload; @io = payload          ; end

Instance Attribute Details

#ioObject (readonly) Also known as: to_io

Payload API



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

def io
  @io
end

Class Method Details

.generate(payload) ⇒ Object



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

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



19
20
21
22
23
24
25
26
# File 'lib/rest-core/util/payload.rb', line 19

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



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

def close             ; io.close unless closed?; end

#closed?Boolean

Returns:

  • (Boolean)


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

def closed?           ; io.closed?             ; end

#headersObject



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

def headers           ; {}                     ; end

#read(bytes = nil) ⇒ Object



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

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

#sizeObject



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

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