Class: Net::Gemini::Client

Inherits:
Object
  • Object
show all
Defined in:
lib/net/gemini/client.rb,
lib/net/gemini/client/ssl.rb

Overview

Reopen Client class to add specific private method to handle SSL connection

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(host, port) ⇒ Client

Returns a new instance of Client.



14
15
16
17
18
# File 'lib/net/gemini/client.rb', line 14

def initialize(host, port)
  @host = host
  @port = port
  @certs_path = '~/.cache/gemini/certs'
end

Instance Attribute Details

#certs_path=(value) ⇒ Object (writeonly)

Sets the attribute certs_path

Parameters:

  • value

    the value to set the attribute certs_path to.



12
13
14
# File 'lib/net/gemini/client.rb', line 12

def certs_path=(value)
  @certs_path = value
end

Instance Method Details

#fetch(uri, limit = 5) ⇒ Object

Raises:



44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/net/gemini/client.rb', line 44

def fetch(uri, limit = 5)
  raise Error, 'Too many Gemini redirects' if limit.zero?

  r = request(uri)
  return r unless r.status[0] == '3'

  begin
    uri = handle_redirect(r)
  rescue ArgumentError, URI::InvalidURIError
    return r
  end
  warn "Redirect to #{uri}" if $VERBOSE
  fetch(uri, limit - 1)
end

#request(uri) ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/net/gemini/client.rb', line 30

def request(uri)
  request! uri
rescue OpenSSL::SSL::SSLError => e
  msg = format(
    'SSLError: %<cause>s',
    cause: e.message.sub(/.*state=error: (.+)\Z/, '\1')
  )
  Response.new('59', msg)
ensure
  # Stop remaining connection, even if they should be already cut
  # by the server
  finish
end

#request!(uri) ⇒ Object

This method can raise an OpenSSL::SSL::SSLError



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

def request!(uri)
  init_sockets
  req = Request.new uri
  req.write @ssl_socket
  res = Response.read_new(@ssl_socket)
  res.uri = uri
  res.reading_body(@ssl_socket)
end