Class: Net::GeminiResponse

Inherits:
Object
  • Object
show all
Includes:
Gemini::GmiParser, Gemini::ReflowText
Defined in:
lib/net/gemini/response.rb

Overview

The syntax of Gemini Responses are defined in the Gemini specification, section 3.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status = nil, meta = nil) ⇒ GeminiResponse

Returns a new instance of GeminiResponse.



42
43
44
45
46
47
48
49
50
# File 'lib/net/gemini/response.rb', line 42

def initialize(status = nil, meta = nil)
  @status = status
  @meta = meta
  @header = parse_meta
  @uri = nil
  @body = nil
  @links = []
  @preformatted_blocks = []
end

Instance Attribute Details

#body(flowed: nil) ⇒ Object



68
69
70
71
72
# File 'lib/net/gemini/response.rb', line 68

def body(flowed: nil)
  return '' if @body.nil? # Maybe not ready?
  return @body if flowed.nil? || @header[:format] == 'fixed'
  reformat_body(flowed)
end

#headerObject (readonly)

The Gemini response <META> as a qualified Hash.



30
31
32
# File 'lib/net/gemini/response.rb', line 30

def header
  @header
end

All links found on a Gemini response of MIME text/gemini, as an

array.


40
41
42
# File 'lib/net/gemini/response.rb', line 40

def links
  @links
end

#metaObject (readonly)

The Gemini response <META> message sent by the server as a string.

For example, ‘text/gemini’.



27
28
29
# File 'lib/net/gemini/response.rb', line 27

def meta
  @meta
end

#statusObject (readonly)

The Gemini response <STATUS> string.

For example, ‘20’.



22
23
24
# File 'lib/net/gemini/response.rb', line 22

def status
  @status
end

#uriObject

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

Raises:



75
76
77
78
79
80
81
82
83
84
# File 'lib/net/gemini/response.rb', line 75

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 GeminiBadResponse, "wrong status line: #{str.dump}" if m.nil?
  new(*m.captures)
end

Instance Method Details

#body_permitted?Boolean

Returns:

  • (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