Class: Vodka::Server::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeResponse

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

#codeObject

Returns the value of attribute code.



4
5
6
# File 'lib/vodka/server/response.rb', line 4

def code
  @code
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/vodka/server/response.rb', line 4

def data
  @data
end

#errorsObject

Returns the value of attribute errors.



4
5
6
# File 'lib/vodka/server/response.rb', line 4

def errors
  @errors
end

#idObject

Returns the value of attribute id.



4
5
6
# File 'lib/vodka/server/response.rb', line 4

def id
  @id
end

#metadataObject

Returns the value of attribute metadata.



4
5
6
# File 'lib/vodka/server/response.rb', line 4

def 
  @metadata
end

#presenterObject

Returns the value of attribute presenter.



4
5
6
# File 'lib/vodka/server/response.rb', line 4

def presenter
  @presenter
end

#successObject

Returns the value of attribute success.



4
5
6
# File 'lib/vodka/server/response.rb', line 4

def success
  @success
end

Instance Method Details

#jsonObject



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

#signatureObject



44
45
46
# File 'lib/vodka/server/response.rb', line 44

def signature
  Vodka::Server.config.digest.hexdigest([id, Vodka::Server.config.response_secret].join)
end