Class: TriCoreDB::Response

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

Overview

A server RESPONSE: typed data plus how it was produced.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(raw) ⇒ Response

Returns a new instance of Response.

Parameters:

  • raw (Hash) —

    the decoded RESPONSE payload



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" => ...}, ...).

Returns:

  • (Object) —

    the externally tagged ResponseData ("Empty", {"Json" => ...}, ...)



53
54
55
# File 'lib/tricoredb/response.rb', line 53

def data
  @data
end

#diagnostics ⇒ Hash (readonly)

Returns:

  • (Hash)


55
56
57
# File 'lib/tricoredb/response.rb', line 55

def diagnostics
  @diagnostics
end

#request_id ⇒ String (readonly)

Returns:

  • (String)


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.

Returns:

  • (String) —

    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?

Returns:

  • (Object, nil)


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.

Returns:

  • (Boolean)


96
97
98
# File 'lib/tricoredb/response.rb', line 96

def arm?(name)
  @data.is_a?(Hash) && @data.key?(name)
end

#error_code ⇒ String?

Returns:

  • (String, nil)


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.

Returns:

  • (String) —

    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?

Returns:

  • (String, nil)


81
82
83
# File 'lib/tricoredb/response.rb', line 81

def leader_hint
  @diagnostics["leader_hint"]
end

#ok? ⇒ Boolean

Returns:

  • (Boolean)


66
67
68
# File 'lib/tricoredb/response.rb', line 66

def ok?
  @status == "ok"
end

#redirect? ⇒ Boolean

Returns:

  • (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.

Returns:

  • (Integer, nil) —

    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.

Parameters:

  • txn_open (Boolean) (defaults to: false) —

    whether a session transaction was open on the connection

Returns:



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"
  message = "#{text} (server status: #{@status})"
  if redirect?
    message += 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
    message += " The open session transaction is over: it cannot continue on another node." if txn_open
    message += "]"
  end
  ServerError.new(message, 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.

Returns:

  • (Array<String>) —

    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