Class: Appfuel::Response

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

Overview

Every action or command must return a response. A response is either ok or it has errors. You can retrieve the results with the “ok” method or the errors with the “error” method

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ Response

Parameters:

  • data (Hash) (defaults to: {})


46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/appfuel/response.rb', line 46

def initialize(data = {})
  result = format_result_hash(data)

  # when no ok key and no errors key the assume
  # it is a successfull response
  if !result.key?(:ok) && !result.key?(:errors)
    result = {ok: result}
  end

  @ok = result[:ok]
  @errors = nil
  if result.key?(:errors)
    @ok = nil
    @errors = Errors.new(result[:errors])
  end
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



42
43
44
# File 'lib/appfuel/response.rb', line 42

def errors
  @errors
end

#okObject (readonly)

Returns the value of attribute ok.



42
43
44
# File 'lib/appfuel/response.rb', line 42

def ok
  @ok
end

Class Method Details

.error(data) ⇒ Object

Convience method for creating an error response. It understands how to handle a SpCore::Error object. Any thing that is not a hash or can’t be converted to a hash is assumed to be a string and converted into a general_error

Parameters:

  • data

    Hash the errors hash



23
24
25
26
27
# File 'lib/appfuel/response.rb', line 23

def error(data)
  result = format_result_hash(data, default_key: :general_error)
  result = result[:errors] if result.key?(:errors)
  self.new(errors: result)
end

.format_result_hash(data, default_key:) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
# File 'lib/appfuel/response.rb', line 29

def format_result_hash(data, default_key:)
  if data.is_a?(Hash)
    result = data
  elsif data.respond_to?(:to_h)
    result = data.to_h
  else
    result = {default_key => data.to_s}
  end

  result.symbolize_keys
end

.ok(result = nil) ⇒ Object

Convience method for creating a successfull response

Parameters:

  • result (defaults to: nil)

    Hash the successfull resultset



12
13
14
# File 'lib/appfuel/response.rb', line 12

def ok(result = nil)
  self.new(ok: result)
end

Instance Method Details

#error_messagesObject



68
69
70
71
72
# File 'lib/appfuel/response.rb', line 68

def error_messages
  return {} if ok?

  errors.messages
end

#errors?Boolean Also known as: failure?

Returns:

  • (Boolean)


63
64
65
# File 'lib/appfuel/response.rb', line 63

def errors?
  !ok?
end

#ok?Boolean Also known as: success?

Returns:

  • (Boolean)


74
75
76
# File 'lib/appfuel/response.rb', line 74

def ok?
  errors.nil?
end

#to_hObject



79
80
81
82
83
84
85
# File 'lib/appfuel/response.rb', line 79

def to_h
  if ok?
    {ok: ok}
  else
    errors.to_h
  end
end

#to_jsonObject



87
88
89
# File 'lib/appfuel/response.rb', line 87

def to_json
  to_h.to_json
end