Class: Net::Gemini::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/net/gemini/request.rb

Overview

The syntax of Gemini Requests are defined in the Gemini specification, section 2.

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri_or_str) ⇒ Request

Returns a new instance of Request.

Raises:



21
22
23
24
25
26
27
28
29
30
# File 'lib/net/gemini/request.rb', line 21

def initialize(uri_or_str)
  # In any case, make some sanity check over this uri-like think
  url = uri_or_str.to_s
  raise BadRequest, "Request too long: #{url.dump}" if url.length > 1024

  @uri = URI(url)
  return if uri.is_a? URI::Gemini

  raise BadRequest, "Not a Gemini URI: #{url.dump}"
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



19
20
21
# File 'lib/net/gemini/request.rb', line 19

def uri
  @uri
end

Class Method Details

.read_new(sock) ⇒ Object

Raises:



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

def read_new(sock)
  # Read up to 1026 bytes:
  # - 1024 bytes max for the URL
  # - 2 bytes for <CR><LF>
  str = sock.gets($INPUT_RECORD_SEPARATOR, 1026)
  m = /\A(.*)\r\n\z/.match(str)
  raise BadRequest, "Malformed request: #{str&.dump}" if m.nil?

  new(m[1])
end

Instance Method Details

#pathObject



32
33
34
# File 'lib/net/gemini/request.rb', line 32

def path
  @uri.path
end

#write(sock) ⇒ Object



36
37
38
# File 'lib/net/gemini/request.rb', line 36

def write(sock)
  sock.puts "#{@uri}\r\n"
end