Class: MonoVM::Whois::Response

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

Overview

One raw answer from one server.

Transports return this; availability rules and parsers read it. Keeping the raw body verbatim matters — callers of a WHOIS library legitimately want to see exactly what the registry said, so nothing here mutates #body.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, endpoint:, query:, status: nil, elapsed: nil) ⇒ Response

Returns a new instance of Response.

Parameters:

  • body (String)

    exactly what the server sent

  • endpoint (Endpoint)

    where it came from

  • query (String)

    the name that was asked about

  • status (Integer, nil) (defaults to: nil)

    HTTP status, nil for port 43

  • elapsed (Float, nil) (defaults to: nil)

    seconds spent on the exchange



20
21
22
23
24
25
26
# File 'lib/monovm/whois/response.rb', line 20

def initialize(body:, endpoint:, query:, status: nil, elapsed: nil)
  @body = body.to_s.freeze
  @endpoint = endpoint
  @query = query
  @status = status
  @elapsed = elapsed
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



13
14
15
# File 'lib/monovm/whois/response.rb', line 13

def body
  @body
end

#elapsedObject (readonly)

Returns the value of attribute elapsed.



13
14
15
# File 'lib/monovm/whois/response.rb', line 13

def elapsed
  @elapsed
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



13
14
15
# File 'lib/monovm/whois/response.rb', line 13

def endpoint
  @endpoint
end

#queryObject (readonly)

Returns the value of attribute query.



13
14
15
# File 'lib/monovm/whois/response.rb', line 13

def query
  @query
end

#statusObject (readonly)

Returns the value of attribute status.



13
14
15
# File 'lib/monovm/whois/response.rb', line 13

def status
  @status
end

Instance Method Details

#empty?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/monovm/whois/response.rb', line 40

def empty?
  body.strip.empty?
end

#inspectObject



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

def inspect
  "#<#{self.class.name} query=#{query.inspect} endpoint=#{endpoint} " \
    "status=#{status.inspect} bytes=#{body.length}>"
end

#jsonObject

The parsed RDAP document, or nil when the body is not JSON.

Memoised including the nil case: a non-JSON body is re-checked by several rules and reparsing it each time is wasted work.



57
58
59
60
61
62
63
64
65
66
# File 'lib/monovm/whois/response.rb', line 57

def json
  return @json if defined?(@json)

  @json = begin
    parsed = JSON.parse(body)
    parsed.is_a?(Hash) ? parsed : nil
  rescue JSON::ParserError
    nil
  end
end

#json?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/monovm/whois/response.rb', line 68

def json?
  !json.nil?
end

#rdap?Boolean

Not frozen as a whole: #text and #json are memoised, and parsing a large RDAP document on every rule that asks for it would be wasted work. The body itself is frozen, so the response is still effectively immutable.

Returns:

  • (Boolean)


32
33
34
# File 'lib/monovm/whois/response.rb', line 32

def rdap?
  endpoint.kind == :rdap
end

#textObject

The body as text, with line endings normalised to \n. Availability rules match against this so a pattern anchored to a line start behaves the same whether the registry sent CRLF or LF.



47
48
49
50
51
# File 'lib/monovm/whois/response.rb', line 47

def text
  @text ||= body.encode("UTF-8", invalid: :replace, undef: :replace, replace: "")
                .gsub(/\r\n?/, "\n")
                .freeze
end

#to_hObject



72
73
74
75
76
77
78
79
80
81
# File 'lib/monovm/whois/response.rb', line 72

def to_h
  {
    query: query,
    endpoint: endpoint.to_s,
    kind: endpoint.kind,
    status: status,
    elapsed: elapsed,
    length: body.length
  }
end

#whois?Boolean

Returns:

  • (Boolean)


36
37
38
# File 'lib/monovm/whois/response.rb', line 36

def whois?
  endpoint.kind == :whois
end