Class: Net::Tofu::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/net/tofu/request.rb,
lib/net/tofu/error.rb

Overview

Stores a client request to a Gemini server.

Defined Under Namespace

Classes: InvalidSchemeError, InvalidURIError

Constant Summary collapse

MAX_URI_BYTESIZE =
1024

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port: nil, trust: false) ⇒ Request

Constructor for the request type.

Parameters:

  • host (String)

    A host string, optionally with the gemini:// scheme.

  • port (Integer) (defaults to: nil)

    Optional parameter to specify the server port to connect to.



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/net/tofu/request.rb', line 36

def initialize(host, port: nil, trust: false)
  @uri = URI(host)
  @uri.port = port unless port.nil?

  parse_head
  parse_tail

  # Make sure the URI isn't too large
  if format.bytesize > MAX_URI_BYTESIZE
    raise InvalidURIError,
          "The URI is too large, should be #{MAX_URI_BYTESIZE} bytes, instead is #{format.bytesize} bytes"
  end

  # Create a socket
  @sock = Socket.new(@host, @port, trust)
end

Instance Attribute Details

#fragmentString (readonly)

Returns A fragment to request from the host.

Returns:

  • (String)

    A fragment to request from the host.



28
29
30
# File 'lib/net/tofu/request.rb', line 28

def fragment
  @fragment
end

#hostString (readonly)

Returns The hostname of the request.

Returns:

  • (String)

    The hostname of the request.



16
17
18
# File 'lib/net/tofu/request.rb', line 16

def host
  @host
end

#pathString (readonly)

Returns The requested path on the host.

Returns:

  • (String)

    The requested path on the host.



22
23
24
# File 'lib/net/tofu/request.rb', line 22

def path
  @path
end

#portInteger (readonly)

Returns The server port to connect on.

Returns:

  • (Integer)

    The server port to connect on.



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

def port
  @port
end

#queriesArray (readonly)

Returns Additional queries to send to the host.

Returns:

  • (Array)

    Additional queries to send to the host.



25
26
27
# File 'lib/net/tofu/request.rb', line 25

def queries
  @queries
end

#respResponse (readonly)

Returns The response from the server after calling ##gets.

Returns:

  • (Response)

    The response from the server after calling ##gets.



31
32
33
# File 'lib/net/tofu/request.rb', line 31

def resp
  @resp
end

#schemeString (readonly)

Returns The request scheme (i.e. gemini://, http://).

Returns:

  • (String)

    The request scheme (i.e. gemini://, http://).



13
14
15
# File 'lib/net/tofu/request.rb', line 13

def scheme
  @scheme
end

#uriURI (readonly)

Returns The full URI object of the request.

Returns:

  • (URI)

    The full URI object of the request.



10
11
12
# File 'lib/net/tofu/request.rb', line 10

def uri
  @uri
end

Instance Method Details

#formatString

Format the URI for sending over a socket to a Gemini server.

Returns:

  • (String)

    The URI string appended with a carriage return and linefeed.



55
56
57
# File 'lib/net/tofu/request.rb', line 55

def format
  "#{@uri}\r\n"
end

#getsObject

Connect to the server and try to fetch data.



60
61
62
63
64
65
# File 'lib/net/tofu/request.rb', line 60

def gets
  @sock.connect
  @resp = @sock.gets(self)
ensure
  @sock.close
end