Class: Webmate::Responders::Response

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data, options = {}) ⇒ Response

Returns a new instance of Response.



5
6
7
8
9
10
11
12
# File 'lib/webmate/responders/response.rb', line 5

def initialize(data, options = {})
  @data = data
  @status = options[:status] || 200
  @params = options[:params] || {}
  @action = options[:action] || @params[:action] || ''
   = options[:metadata] || {}
  @path = options[:path] || "/"
end

Instance Attribute Details

#actionObject

Returns the value of attribute action.



4
5
6
# File 'lib/webmate/responders/response.rb', line 4

def action
  @action
end

#dataObject

Returns the value of attribute data.



4
5
6
# File 'lib/webmate/responders/response.rb', line 4

def data
  @data
end

#metadataObject

Returns the value of attribute metadata.



4
5
6
# File 'lib/webmate/responders/response.rb', line 4

def 
  
end

#paramsObject

Returns the value of attribute params.



4
5
6
# File 'lib/webmate/responders/response.rb', line 4

def params
  @params
end

#pathObject

Returns the value of attribute path.



4
5
6
# File 'lib/webmate/responders/response.rb', line 4

def path
  @path
end

#statusObject

Returns the value of attribute status.



4
5
6
# File 'lib/webmate/responders/response.rb', line 4

def status
  @status
end

Instance Method Details

#jsonObject



14
15
16
# File 'lib/webmate/responders/response.rb', line 14

def json
  Yajl::Encoder.new.encode(self.packed)
end

#packedObject



18
19
20
# File 'lib/webmate/responders/response.rb', line 18

def packed
  { action: @action, resource: @resource, response: @data, params: safe_params }
end

#rack_formatObject



32
33
34
# File 'lib/webmate/responders/response.rb', line 32

def rack_format
  [@status, {}, @data]
end

#safe_paramsObject



22
23
24
25
26
27
28
29
30
# File 'lib/webmate/responders/response.rb', line 22

def safe_params
  safe_params = {}
  params.each do |key, value|
    if value.is_a?(String) || value.is_a?(Integer)
      safe_params[key] = value
    end
  end
  safe_params
end