class Net::GeminiResponse

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

@see gemini.circumlunar.space/docs/specification.html

Attributes

body[W]

The Gemini response main content as a string.

header[R]

The Gemini response <META> as a qualified Hash.

meta[R]

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

For example, 'text/gemini'.

status[R]

The Gemini response <STATUS> string.

For example, '20'.

uri[RW]

The URI related to this response as an URI object.

Public Class Methods

new(status = nil, meta = nil) click to toggle source
# 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
read_new(sock) click to toggle source
# 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

Public Instance Methods

body(flowed: nil) click to toggle source
# 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
body_permitted?() click to toggle source
# File lib/net/gemini/response.rb, line 52
def body_permitted?
  @status && @status[0] == '2'
end
reading_body(sock) click to toggle source
# 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

Private Instance Methods

encode_body(body) click to toggle source
# File lib/net/gemini/response.rb, line 89
def encode_body(body)
  return body unless @header[:mimetype].start_with?('text/')
  if @header[:charset] && @header[:charset] != 'utf-8'
    # If body use another charset than utf-8, we need first to
    # declare the raw byte string as using this chasret
    body.force_encoding(@header[:charset])
    # Then we can safely try to convert it to utf-8
    return body.encode('utf-8')
  end
  # Just declare that the body uses utf-8
  body.force_encoding('utf-8')
end