Class: HTTPDisk::Payload
- Inherits:
-
Object
- Object
- HTTPDisk::Payload
- Defined in:
- lib/httpdisk/payload.rb
Instance Attribute Summary collapse
-
#body ⇒ Object
Returns the value of attribute body.
-
#comment ⇒ Object
Returns the value of attribute comment.
-
#headers ⇒ Object
Returns the value of attribute headers.
-
#reason_phrase ⇒ Object
Returns the value of attribute reason_phrase.
-
#status ⇒ Object
Returns the value of attribute status.
Class Method Summary collapse
Instance Method Summary collapse
- #error? ⇒ Boolean
-
#initialize ⇒ Payload
constructor
A new instance of Payload.
- #write(f) ⇒ Object
- #write_header(f) ⇒ Object
Constructor Details
#initialize ⇒ Payload
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
#body ⇒ Object
Returns the value of attribute body.
34 35 36 |
# File 'lib/httpdisk/payload.rb', line 34 def body @body end |
#comment ⇒ Object
Returns the value of attribute comment.
34 35 36 |
# File 'lib/httpdisk/payload.rb', line 34 def comment @comment end |
#headers ⇒ Object
Returns the value of attribute headers.
34 35 36 |
# File 'lib/httpdisk/payload.rb', line 34 def headers @headers end |
#reason_phrase ⇒ Object
Returns the value of attribute reason_phrase.
34 35 36 |
# File 'lib/httpdisk/payload.rb', line 34 def reason_phrase @reason_phrase end |
#status ⇒ Object
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? ⇒ Boolean
42 43 44 |
# File 'lib/httpdisk/payload.rb', line 42 def error? status >= 400 end |
#write(f) ⇒ Object
57 58 59 60 61 |
# File 'lib/httpdisk/payload.rb', line 57 def write(f) write_header(f) f.puts f.write(body) end |
#write_header(f) ⇒ Object
46 47 48 49 50 51 52 53 54 55 |
# File 'lib/httpdisk/payload.rb', line 46 def write_header(f) # comment f.puts "# #{comment}" # status line f.puts "HTTPDISK #{status} #{reason_phrase}" # headers headers.each { f.puts("#{_1}: #{_2}") } end |