Class: MonoVM::Whois::Endpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/monovm/whois/endpoint.rb

Overview

Where a query gets sent, and over which protocol.

Server definitions describe two very different things with one string: socket://whois.nic.uk is a host to open a TCP connection to, while https://rdap.verisign.com/com/v1/domain/ is a URL prefix. This object normalises both so Transport::Factory can pick a transport by asking #kind rather than by re-parsing strings all over the codebase.

Constant Summary collapse

SOCKET_SCHEME =
"socket"
DEFAULT_WHOIS_PORT =
43
PLACEHOLDER =

Substituted with the queried name when present, so an unusual endpoint can put the domain somewhere other than the end of the URL.

"{domain}"

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(uri) ⇒ Endpoint

Returns a new instance of Endpoint.



37
38
39
40
41
42
# File 'lib/monovm/whois/endpoint.rb', line 37

def initialize(uri)
  @uri = uri
  @scheme = uri.split("://", 2).first.to_s.downcase
  parse_target!
  freeze
end

Instance Attribute Details

#hostObject (readonly)

Returns the value of attribute host.



23
24
25
# File 'lib/monovm/whois/endpoint.rb', line 23

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



23
24
25
# File 'lib/monovm/whois/endpoint.rb', line 23

def port
  @port
end

#schemeObject (readonly)

Returns the value of attribute scheme.



23
24
25
# File 'lib/monovm/whois/endpoint.rb', line 23

def scheme
  @scheme
end

#uriObject (readonly)

Returns the value of attribute uri.



23
24
25
# File 'lib/monovm/whois/endpoint.rb', line 23

def uri
  @uri
end

Class Method Details

.parse(uri) ⇒ Endpoint

Parameters:

  • uri (String)

    e.g. socket://whois.nic.uk, socket://host:4343, or https://rdap.example/domain/

Returns:

Raises:



30
31
32
33
34
# File 'lib/monovm/whois/endpoint.rb', line 30

def parse(uri)
  raise DefinitionsError, "endpoint is empty" if uri.nil? || uri.to_s.strip.empty?

  new(uri.to_s.strip)
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



78
79
80
# File 'lib/monovm/whois/endpoint.rb', line 78

def ==(other)
  other.is_a?(Endpoint) && other.uri == uri
end

#hashObject



83
84
85
# File 'lib/monovm/whois/endpoint.rb', line 83

def hash
  [self.class, uri].hash
end

#http?Boolean

True for RDAP or any other HTTP-delivered service.

Returns:

  • (Boolean)


50
51
52
# File 'lib/monovm/whois/endpoint.rb', line 50

def http?
  %w[http https].include?(scheme)
end

#inspectObject



87
88
89
# File 'lib/monovm/whois/endpoint.rb', line 87

def inspect
  "#<#{self.class.name} #{uri}>"
end

#kindObject

A coarse label used to select a transport and to tag a Response.



59
60
61
# File 'lib/monovm/whois/endpoint.rb', line 59

def kind
  socket? ? :whois : :rdap
end

#socket?Boolean

True for port 43 WHOIS.

Returns:

  • (Boolean)


45
46
47
# File 'lib/monovm/whois/endpoint.rb', line 45

def socket?
  scheme == SOCKET_SCHEME
end

#tls?Boolean

Returns:

  • (Boolean)


54
55
56
# File 'lib/monovm/whois/endpoint.rb', line 54

def tls?
  scheme == "https"
end

#to_sObject



74
75
76
# File 'lib/monovm/whois/endpoint.rb', line 74

def to_s
  uri
end

#url_for(name) ⇒ Object

The URL to fetch for name. Only meaningful for HTTP endpoints.

Endpoint.parse("https://rdap.example/domain/").url_for("a.com")
# => "https://rdap.example/domain/a.com"

Raises:



67
68
69
70
71
72
# File 'lib/monovm/whois/endpoint.rb', line 67

def url_for(name)
  raise DefinitionsError, "#{uri} is not an HTTP endpoint" unless http?
  return uri.sub(PLACEHOLDER, name) if uri.include?(PLACEHOLDER)

  "#{uri}#{name}"
end