Class: HTTPDisk::Payload

Inherits:
Object
  • Object
show all
Defined in:
lib/httpdisk/payload.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializePayload

Returns a new instance of Payload.



36
37
38
39
40
# File 'lib/httpdisk/payload.rb', line 36

def initialize
  @body = ''
  @comment = ''
  @headers = Faraday::Utils::Headers.new
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



34
35
36
# File 'lib/httpdisk/payload.rb', line 34

def body
  @body
end

#commentObject

Returns the value of attribute comment.



34
35
36
# File 'lib/httpdisk/payload.rb', line 34

def comment
  @comment
end

#headersObject

Returns the value of attribute headers.



34
35
36
# File 'lib/httpdisk/payload.rb', line 34

def headers
  @headers
end

#reason_phraseObject

Returns the value of attribute reason_phrase.



34
35
36
# File 'lib/httpdisk/payload.rb', line 34

def reason_phrase
  @reason_phrase
end

#statusObject

Returns the value of attribute status.



34
35
36
# File 'lib/httpdisk/payload.rb', line 34

def status
  @status
end

Class Method Details

.from_response(response) ⇒ Object



24
25
26
27
28
29
30
31
# File 'lib/httpdisk/payload.rb', line 24

def from_response(response)
  Payload.new.tap do
    _1.body = response.body
    _1.headers = response.headers
    _1.reason_phrase = response.reason_phrase
    _1.status = response.status
  end
end

.read(f, peek: false) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/httpdisk/payload.rb', line 4

def read(f, peek: false)
  Payload.new.tap do |p|
    # comment
    p.comment = f.gets[/^# (.*)/, 1]

    # status line
    m = f.gets.match(/^HTTPDISK (\d+) (.*)$/)
    p.status, p.reason_phrase = m[1].to_i, m[2]

    # headers
    while (line = f.gets.chomp) && !line.empty?
      key, value = line.split(': ', 2)
      p.headers[key] = value
    end

    # body (if not peeking)
    p.body = f.read if !peek
  end
end

Instance Method Details

#error_999?Boolean

Returns:

  • (Boolean)


42
43
44
# File 'lib/httpdisk/payload.rb', line 42

def error_999?
  status == HTTPDisk::ERROR_STATUS
end

#write(f) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/httpdisk/payload.rb', line 46

def write(f)
  # comment
  f.puts "# #{comment}"

  # status line
  f.puts "HTTPDISK #{status} #{reason_phrase}"

  # headers
  headers.each { f.puts("#{_1}: #{_2}") }
  f.puts

  # body
  f.write(body)
end