Class: OSTSdk::Util::Result

Inherits:
Object
  • Object
show all
Defined in:
lib/ost-sdk-ruby-stag/util/result.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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)
  set_message(params[:message])
  @data = params[:data] || {}
end

Instance Attribute Details

#dataObject

Returns the value of attribute data.


7
8
9
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7

def data
  @data
end

#errorObject

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_dataObject

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_headingObject

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_textObject

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_messageObject

Returns the value of attribute error_message.


7
8
9
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7

def error_message
  @error_message
end

#exceptionObject

Returns the value of attribute exception.


7
8
9
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7

def exception
  @exception
end

#messageObject

Returns the value of attribute message.


7
8
9
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 7

def message
  @message
end

Class Method Details

.error(params) ⇒ OSTSdk::Util::Result

Error

Returns:


125
126
127
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 125

def self.error(params)
  new(params)
end

.error_fieldsArray

Error Fields

Returns:

  • (Array)

    returns Array object


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

Returns:


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

.fieldsArray

Fields

Returns:

  • (Array)

    returns Array object


165
166
167
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 165

def self.fields
  error_fields + [:data, :message]
end

.no_errorHash

No Error

Returns:

  • (Hash)

    returns Hash


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

Returns:


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?

Returns:

  • (Boolean)

    returns True / False


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_backtraceString

Exception backtrace

Returns:

  • (String)

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_messageString

Exception message

Returns:

  • (String)

103
104
105
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 103

def exception_message
  @e_m ||= @exception.present? ? @exception.message : ''
end

#invalid?Boolean

is invalid?

Returns:

  • (Boolean)

    returns True / False


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

Parameters:

  • params (Hash)

    is a Hash


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

Parameters:

  • e (Exception)

    is an 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.message,
      trace: e.backtrace
  }
end

#set_message(msg) ⇒ Object

Set Message

Parameters:

  • msg (String)

    is a String


42
43
44
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 42

def set_message(msg)
  @message = msg
end

#to_hashHash

Create an Hash out of all instance vars

Returns:

  • (Hash)

    returns Hash object


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_jsonObject

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?

Returns:

  • (Boolean)

    returns True / False


62
63
64
# File 'lib/ost-sdk-ruby-stag/util/result.rb', line 62

def valid?
  !invalid?
end