Class: OSTSdk::Util::Result
- Inherits:
-
Object
- Object
- OSTSdk::Util::Result
- Defined in:
- lib/ost-sdk-ruby-stag/util/result.rb
Instance Attribute Summary collapse
-
#data ⇒ Object
Returns the value of attribute data.
-
#error ⇒ Object
Returns the value of attribute error.
-
#error_data ⇒ Object
Returns the value of attribute error_data.
-
#error_display_heading ⇒ Object
Returns the value of attribute error_display_heading.
-
#error_display_text ⇒ Object
Returns the value of attribute error_display_text.
-
#error_message ⇒ Object
Returns the value of attribute error_message.
-
#exception ⇒ Object
Returns the value of attribute exception.
-
#message ⇒ Object
Returns the value of attribute message.
Class Method Summary collapse
-
.error(params) ⇒ OSTSdk::Util::Result
Error.
-
.error_fields ⇒ Array
Error Fields.
-
.exception(e, params = {}) ⇒ OSTSdk::Util::Result
Exception.
-
.fields ⇒ Array
Fields.
-
.no_error ⇒ Hash
No Error.
-
.success(params) ⇒ OSTSdk::Util::Result
Success.
Instance Method Summary collapse
-
#[](key) ⇒ Object
Get instance variables Hash style from object.
-
#errors_present? ⇒ Boolean
are errors present?.
-
#exception_backtrace ⇒ String
Exception backtrace.
-
#exception_message ⇒ String
Exception message.
-
#initialize(params = {}) ⇒ Result
constructor
Initialize.
-
#invalid? ⇒ Boolean
is invalid?.
-
#set_error(params) ⇒ Object
Set Error.
-
#set_exception(e) ⇒ Object
Set Exception.
-
#set_message(msg) ⇒ Object
Set Message.
-
#to_hash ⇒ Hash
Create an Hash out of all instance vars.
-
#to_json ⇒ Object
To JSON.
-
#valid? ⇒ Boolean
is valid?.
Constructor Details
#initialize(params = {}) ⇒ Result
Initialize
Arguments:
params: (Hash)
20 21 22 23 24 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 20 def initialize(params = {}) set_error(params) (params[:message]) @data = params[:data] || {} end |
Instance Attribute Details
#data ⇒ Object
Returns the value of attribute data.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def data @data end |
#error ⇒ Object
Returns the value of attribute error.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def error @error end |
#error_data ⇒ Object
Returns the value of attribute error_data.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def error_data @error_data end |
#error_display_heading ⇒ Object
Returns the value of attribute error_display_heading.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def error_display_heading @error_display_heading end |
#error_display_text ⇒ Object
Returns the value of attribute error_display_text.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def error_display_text @error_display_text end |
#error_message ⇒ Object
Returns the value of attribute error_message.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def @error_message end |
#exception ⇒ Object
Returns the value of attribute exception.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def exception @exception end |
#message ⇒ Object
Returns the value of attribute message.
7 8 9 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7 def @message end |
Class Method Details
.error(params) ⇒ OSTSdk::Util::Result
Error
125 126 127 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 125 def self.error(params) new(params) end |
.error_fields ⇒ Array
Error Fields
173 174 175 176 177 178 179 180 181 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 173 def self.error_fields [ :error, :error_message, :error_data, :error_display_text, :error_display_heading ] end |
.exception(e, params = {}) ⇒ OSTSdk::Util::Result
Exception
141 142 143 144 145 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 141 def self.exception(e, params = {}) obj = new(params) obj.set_exception(e) return obj end |
.fields ⇒ Array
Fields
165 166 167 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 165 def self.fields error_fields + [:data, :message] end |
.no_error ⇒ Hash
No Error
151 152 153 154 155 156 157 158 159 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 151 def self.no_error @n_err ||= { error: nil, error_message: nil, error_data: nil, error_display_text: nil, error_display_heading: nil } end |
.success(params) ⇒ OSTSdk::Util::Result
Success
133 134 135 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 133 def self.success(params) new(params.merge!(no_error)) end |
Instance Method Details
#[](key) ⇒ Object
Get instance variables Hash style from object
117 118 119 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 117 def [](key) instance_variable_get("@#{key}") end |
#errors_present? ⇒ Boolean
are errors present?
90 91 92 93 94 95 96 97 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 90 def errors_present? @error.present? || @error_message.present? || @error_data.present? || @error_display_text.present? || @error_display_heading.present? || @exception.present? end |
#exception_backtrace ⇒ String
Exception backtrace
111 112 113 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 111 def exception_backtrace @e_b ||= @exception.present? ? @exception.backtrace : '' end |
#exception_message ⇒ String
Exception message
103 104 105 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 103 def @e_m ||= @exception.present? ? @exception. : '' end |
#invalid? ⇒ Boolean
is invalid?
70 71 72 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 70 def invalid? errors_present? end |
#set_error(params) ⇒ Object
Set Error
30 31 32 33 34 35 36 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 30 def set_error(params) @error = params[:error] if params.key?(:error) @error_message = params[:error_message] if params.key?(:error_message) @error_data = params[:error_data] if params.key?(:error_data) @error_display_text = params[:error_display_text] if params.key?(:error_display_text) @error_display_heading = params[:error_display_heading] if params.key?(:error_display_heading) end |
#set_exception(e) ⇒ Object
Set Exception
50 51 52 53 54 55 56 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 50 def set_exception(e) @exception = e @error_data = { msg: e., trace: e.backtrace } end |
#set_message(msg) ⇒ Object
Set Message
42 43 44 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 42 def (msg) @message = msg end |
#to_hash ⇒ Hash
Create an Hash out of all instance vars
187 188 189 190 191 192 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 187 def to_hash self.class.fields.each_with_object({}) do |key, hash| val = send(key) hash[key] = val if val.present? end end |
#to_json ⇒ Object
To JSON
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 196 def to_json hash = self.to_hash if (hash[:error] == nil) h = { success: true }.merge(hash) h else { success: false, err: { code: hash[:error], msg: hash[:error_message], display_text: hash[:error_display_text].to_s, display_heading: hash[:error_display_heading].to_s, error_data: hash[:error_data] || {} }, data: hash[:data] } end end |
#valid? ⇒ Boolean
is valid?
62 63 64 |
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 62 def valid? !invalid? end |