Class: Vodka::Server::Response
- Inherits:
-
Object
- Object
- Vodka::Server::Response
- Defined in:
- lib/vodka/server/response.rb
Instance Attribute Summary collapse
-
#code ⇒ Object
Returns the value of attribute code.
-
#data ⇒ Object
Returns the value of attribute data.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#id ⇒ Object
Returns the value of attribute id.
-
#metadata ⇒ Object
Returns the value of attribute metadata.
-
#presenter ⇒ Object
Returns the value of attribute presenter.
-
#success ⇒ Object
Returns the value of attribute success.
Instance Method Summary collapse
-
#initialize ⇒ Response
constructor
A new instance of Response.
- #json ⇒ Object
- #present(item) ⇒ Object
- #signature ⇒ Object
Constructor Details
#initialize ⇒ Response
Returns a new instance of Response.
6 7 8 9 10 |
# File 'lib/vodka/server/response.rb', line 6 def initialize @code = 200 @metadata = {} @errors = {} end |
Instance Attribute Details
#code ⇒ Object
Returns the value of attribute code.
4 5 6 |
# File 'lib/vodka/server/response.rb', line 4 def code @code end |
#data ⇒ Object
Returns the value of attribute data.
4 5 6 |
# File 'lib/vodka/server/response.rb', line 4 def data @data end |
#errors ⇒ Object
Returns the value of attribute errors.
4 5 6 |
# File 'lib/vodka/server/response.rb', line 4 def errors @errors end |
#id ⇒ Object
Returns the value of attribute id.
4 5 6 |
# File 'lib/vodka/server/response.rb', line 4 def id @id end |
#metadata ⇒ Object
Returns the value of attribute metadata.
4 5 6 |
# File 'lib/vodka/server/response.rb', line 4 def @metadata end |
#presenter ⇒ Object
Returns the value of attribute presenter.
4 5 6 |
# File 'lib/vodka/server/response.rb', line 4 def presenter @presenter end |
#success ⇒ Object
Returns the value of attribute success.
4 5 6 |
# File 'lib/vodka/server/response.rb', line 4 def success @success end |
Instance Method Details
#json ⇒ Object
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 |
# File 'lib/vodka/server/response.rb', line 12 def json [:vodka_action_success] = success unless success.nil? if data.nil? presented_data = {} errors_to_return = errors elsif data.is_a?(Array) presented_data = data.map{ |item| present(item) } errors_to_return = errors else presented_data = present(data) errors_to_return = presented_data.delete(:errors) end response = { data: presented_data, metadata: , errors: errors_to_return } # ap response if Rails.env.development? && defined?(AwesomePrint) MultiJson.dump(response) end |
#present(item) ⇒ Object
35 36 37 38 39 40 41 42 |
# File 'lib/vodka/server/response.rb', line 35 def present(item) return item if item.is_a?(Hash) if presenter.nil? item.try(:present) else presenter.new(item).present end end |