Class: Net::Gemini::Response
- Inherits:
-
Object
- Object
- Net::Gemini::Response
- Defined in:
- lib/net/gemini/response.rb,
lib/net/gemini/response/parser.rb
Overview
Reopen Response class to add specific private method to parse text/gemini documents.
Instance Attribute Summary collapse
-
#body(reflow_at: -1)) ⇒ String
Return the response body (i.e. the requested document content).
-
#header ⇒ Hash
readonly
The Gemini response <META>.
-
#links ⇒ Array<String>
readonly
All links found on a Gemini response of MIME text/gemini.
-
#meta ⇒ String
readonly
The Gemini response <META> message sent by the server.
-
#status ⇒ String
readonly
The Gemini response <STATUS> string.
-
#uri ⇒ Object
The URI related to this response as an URI object.
Class Method Summary collapse
Instance Method Summary collapse
- #body_permitted? ⇒ Boolean
-
#initialize(status = nil, meta = nil) ⇒ Response
constructor
A new instance of Response.
- #reading_body(sock) ⇒ Object
Constructor Details
#initialize(status = nil, meta = nil) ⇒ Response
Returns a new instance of Response.
42 43 44 45 46 47 48 49 50 |
# File 'lib/net/gemini/response.rb', line 42 def initialize(status = nil, = nil) @status = status @meta = @header = @uri = nil @body = nil @links = [] @preformatted_blocks = [] end |
Instance Attribute Details
#body(reflow_at: -1)) ⇒ String
Return the response body (i.e. the requested document content).
73 74 75 76 77 78 |
# File 'lib/net/gemini/response.rb', line 73 def body(reflow_at: -1) return '' if @body.nil? # Maybe not ready? return @body if reflow_at < 0 || @header[:format] == 'fixed' Net::Text::Reflow.format_body(@body, reflow_at) end |
#header ⇒ Hash (readonly)
Returns The Gemini response <META>.
30 31 32 |
# File 'lib/net/gemini/response.rb', line 30 def header @header end |
#links ⇒ Array<String> (readonly)
Returns All links found on a Gemini response of MIME text/gemini.
40 41 42 |
# File 'lib/net/gemini/response.rb', line 40 def links @links end |
#meta ⇒ String (readonly)
Returns The Gemini response <META> message sent by the server.
27 28 29 |
# File 'lib/net/gemini/response.rb', line 27 def @meta end |
#status ⇒ String (readonly)
Returns The Gemini response <STATUS> string.
23 24 25 |
# File 'lib/net/gemini/response.rb', line 23 def status @status end |
#uri ⇒ Object
The URI related to this response as an URI object.
36 37 38 |
# File 'lib/net/gemini/response.rb', line 36 def uri @uri end |
Class Method Details
.read_new(sock) ⇒ Object
81 82 83 84 85 86 87 88 89 90 |
# File 'lib/net/gemini/response.rb', line 81 def read_new(sock) # Read up to 1029 bytes: # - 3 bytes for code and space separator # - 1024 bytes max for the message # - 2 bytes for <CR><LF> str = sock.gets($INPUT_RECORD_SEPARATOR, 1029) m = /\A([1-6]\d) (.*)\r\n\z/.match(str) raise BadResponse, "wrong status line: #{str.dump}" if m.nil? new(*m.captures) end |
Instance Method Details
#body_permitted? ⇒ Boolean
52 53 54 |
# File 'lib/net/gemini/response.rb', line 52 def body_permitted? @status && @status[0] == '2' end |
#reading_body(sock) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/net/gemini/response.rb', line 56 def reading_body(sock) return self unless body_permitted? raw_body = [] while (line = sock.gets) raw_body << line end @body = encode_body(raw_body.join) return self unless @header[:mimetype] == 'text/gemini' parse_body self end |