Class: TriCoreDB::Response
- Inherits:
-
Object
- Object
- TriCoreDB::Response
- Defined in:
- lib/tricoredb/response.rb
Overview
A server RESPONSE: typed data plus how it was produced.
Instance Attribute Summary collapse
-
#data ⇒ Object
readonly
The externally tagged ResponseData (
"Empty",{"Json" => ...}, ...). - #diagnostics ⇒ Hash readonly
- #request_id ⇒ String readonly
-
#status ⇒ String
readonly
ok,errorornot_implemented.
Instance Method Summary collapse
- #arm(name) ⇒ Object?
-
#arm?(name) ⇒ Boolean
Whether the data carries the named arm (
Json,Rows, ...). - #error_code ⇒ String?
-
#initialize(raw) ⇒ Response
constructor
A new instance of Response.
-
#kind ⇒ String
The kind of data, for messages.
- #leader_hint ⇒ String?
- #ok? ⇒ Boolean
- #redirect? ⇒ Boolean
-
#rows_affected ⇒ Integer?
rows_affectedfrom a SQL write, when the server reported it. -
#to_error(txn_open = false) ⇒ ServerError
Build the error for a response that is not
ok. -
#warnings ⇒ Array<String>
Non-fatal warnings; a partially applied broadcast reports here while still
ok.
Constructor Details
#initialize(raw) ⇒ Response
Returns a new instance of Response.
58 59 60 61 62 63 64 |
# File 'lib/tricoredb/response.rb', line 58 def initialize(raw) raw = {} unless raw.is_a?(Hash) @request_id = raw["request_id"].to_s @status = raw["status"] || "error" @data = raw["data"] @diagnostics = raw["diagnostics"].is_a?(Hash) ? raw["diagnostics"] : {} end |
Instance Attribute Details
#data ⇒ Object (readonly)
Returns the externally tagged ResponseData ("Empty", {"Json" => ...}, ...).
53 54 55 |
# File 'lib/tricoredb/response.rb', line 53 def data @data end |
#diagnostics ⇒ Hash (readonly)
55 56 57 |
# File 'lib/tricoredb/response.rb', line 55 def diagnostics @diagnostics end |
#request_id ⇒ String (readonly)
49 50 51 |
# File 'lib/tricoredb/response.rb', line 49 def request_id @request_id end |
#status ⇒ String (readonly)
Returns ok, error or not_implemented.
51 52 53 |
# File 'lib/tricoredb/response.rb', line 51 def status @status end |
Instance Method Details
#arm(name) ⇒ Object?
101 102 103 |
# File 'lib/tricoredb/response.rb', line 101 def arm(name) @data.is_a?(Hash) ? @data[name] : nil end |
#arm?(name) ⇒ Boolean
Whether the data carries the named arm (Json, Rows, ...). A null value still counts.
96 97 98 |
# File 'lib/tricoredb/response.rb', line 96 def arm?(name) @data.is_a?(Hash) && @data.key?(name) end |
#error_code ⇒ String?
76 77 78 |
# File 'lib/tricoredb/response.rb', line 76 def error_code @diagnostics["error_code"] end |
#kind ⇒ String
Returns the kind of data, for messages.
106 107 108 |
# File 'lib/tricoredb/response.rb', line 106 def kind @data.is_a?(Hash) ? (@data.keys.first || "{}") : @data.inspect end |
#leader_hint ⇒ String?
81 82 83 |
# File 'lib/tricoredb/response.rb', line 81 def leader_hint @diagnostics["leader_hint"] end |
#ok? ⇒ Boolean
66 67 68 |
# File 'lib/tricoredb/response.rb', line 66 def ok? @status == "ok" end |
#redirect? ⇒ Boolean
85 86 87 |
# File 'lib/tricoredb/response.rb', line 85 def redirect? error_code == NOT_LEADER end |
#rows_affected ⇒ Integer?
Returns rows_affected from a SQL write, when the server reported it.
90 91 92 93 |
# File 'lib/tricoredb/response.rb', line 90 def rows_affected json = arm("Json") json.is_a?(Hash) && json["rows_affected"].is_a?(Integer) ? json["rows_affected"] : nil end |
#to_error(txn_open = false) ⇒ ServerError
Build the error for a response that is not ok.
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/tricoredb/response.rb', line 114 def to_error(txn_open = false) text = arm("Message") || (arm?("Json") ? JSON.generate(arm("Json")) : nil) || "request failed" = "#{text} (server status: #{@status})" if redirect? += if leader_hint " [not_leader: the leader serves clients at `#{leader_hint}`. This driver does not follow " \ "the hint on its own; send the request there." else " [not_leader: there is no leader address to name (an election is in progress, or the " \ "leader has no address configured). Wait and try again." end += " The open session transaction is over: it cannot continue on another node." if txn_open += "]" end ServerError.new(, code: error_code, leader_hint: leader_hint, status: @status, response: self) end |
#warnings ⇒ Array<String>
Returns non-fatal warnings; a partially applied broadcast reports here while still ok.
71 72 73 |
# File 'lib/tricoredb/response.rb', line 71 def warnings @diagnostics["warnings"] || [] end |