Class: MonoVM::Whois::Response
- Inherits:
-
Object
- Object
- MonoVM::Whois::Response
- 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
-
#body ⇒ Object
readonly
Returns the value of attribute body.
-
#elapsed ⇒ Object
readonly
Returns the value of attribute elapsed.
-
#endpoint ⇒ Object
readonly
Returns the value of attribute endpoint.
-
#query ⇒ Object
readonly
Returns the value of attribute query.
-
#status ⇒ Object
readonly
Returns the value of attribute status.
Instance Method Summary collapse
- #empty? ⇒ Boolean
-
#initialize(body:, endpoint:, query:, status: nil, elapsed: nil) ⇒ Response
constructor
A new instance of Response.
- #inspect ⇒ Object
-
#json ⇒ Object
The parsed RDAP document, or nil when the body is not JSON.
- #json? ⇒ Boolean
- #rdap? ⇒ Boolean
-
#text ⇒ Object
The body as text, with line endings normalised to \n.
- #to_h ⇒ Object
- #whois? ⇒ Boolean
Constructor Details
#initialize(body:, endpoint:, query:, status: nil, elapsed: nil) ⇒ Response
Returns a new instance of Response.
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
#body ⇒ Object (readonly)
Returns the value of attribute body.
13 14 15 |
# File 'lib/monovm/whois/response.rb', line 13 def body @body end |
#elapsed ⇒ Object (readonly)
Returns the value of attribute elapsed.
13 14 15 |
# File 'lib/monovm/whois/response.rb', line 13 def elapsed @elapsed end |
#endpoint ⇒ Object (readonly)
Returns the value of attribute endpoint.
13 14 15 |
# File 'lib/monovm/whois/response.rb', line 13 def endpoint @endpoint end |
#query ⇒ Object (readonly)
Returns the value of attribute query.
13 14 15 |
# File 'lib/monovm/whois/response.rb', line 13 def query @query end |
#status ⇒ Object (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
40 41 42 |
# File 'lib/monovm/whois/response.rb', line 40 def empty? body.strip.empty? end |
#inspect ⇒ Object
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 |
#json ⇒ Object
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
68 69 70 |
# File 'lib/monovm/whois/response.rb', line 68 def json? !json.nil? end |
#rdap? ⇒ Boolean
32 33 34 |
# File 'lib/monovm/whois/response.rb', line 32 def rdap? endpoint.kind == :rdap end |
#text ⇒ Object
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_h ⇒ Object
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
36 37 38 |
# File 'lib/monovm/whois/response.rb', line 36 def whois? endpoint.kind == :whois end |