Class: Praxis::Response

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

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(status: self.class.status, headers: {}, body: '') ⇒ Response

Returns a new instance of Response.



21
22
23
24
25
26
27
28
# File 'lib/praxis/response.rb', line 21

def initialize(status:self.class.status, headers:{}, body:'')
  @name    = response_name
  @status  = status
  @headers = headers
  @body    = body
  @form_data = nil
  @parts = Hash.new
end

Class Attribute Details

.response_nameObject

Returns the value of attribute response_name.



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

def response_name
  @response_name
end

.statusObject

Returns the value of attribute status.



13
14
15
# File 'lib/praxis/response.rb', line 13

def status
  @status
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



8
9
10
# File 'lib/praxis/response.rb', line 8

def body
  @body
end

#headersObject

Returns the value of attribute headers.



7
8
9
# File 'lib/praxis/response.rb', line 7

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



3
4
5
# File 'lib/praxis/response.rb', line 3

def name
  @name
end

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

#requestObject

Returns the value of attribute request.



9
10
11
# File 'lib/praxis/response.rb', line 9

def request
  @request
end

#statusObject

Returns the value of attribute status.



6
7
8
# File 'lib/praxis/response.rb', line 6

def status
  @status
end

Class Method Details

.inherited(klass) ⇒ Object



16
17
18
19
# File 'lib/praxis/response.rb', line 16

def self.inherited(klass)
  klass.response_name = klass.name.demodulize.underscore.to_sym
  klass.status = self.status if self.status
end

Instance Method Details

#add_part(name = nil, part) ⇒ Object



49
50
51
52
53
54
55
56
57
58
59
# File 'lib/praxis/response.rb', line 49

def add_part(name=nil, part)
  @form_data ||= begin
    form = MIME::Multipart::FormData.new
    @headers.merge! form.headers.headers
    form
  end

  name ||= "part-#{part.object_id}"

  @parts[name.to_s] = part
end

#content_typeMediaTypeIdentifier

Determine the content type of this response.

Returns:



33
34
35
# File 'lib/praxis/response.rb', line 33

def content_type
  MediaTypeIdentifier.load(headers['Content-Type']).freeze
end

#content_type=(identifier) ⇒ String

TODO:

DRY this out (also used in Multipart::Part)

Set the content type for this response.

Parameters:

Returns:

  • (String)


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

def content_type=(identifier)
  headers['Content-Type'] = MediaTypeIdentifier.load(identifier).to_s
end

#encode!Object



68
69
70
71
72
73
74
75
76
77
78
# File 'lib/praxis/response.rb', line 68

def encode!
  case @body
  when Hash, Array
    # response payload is structured data; transform it into an entity using the handler
    # implied by the response's media type. If no handler is registered for this
    # name, assume JSON as a default handler.
    handlers = Praxis::Application.instance.handlers
    handler = (content_type && handlers[content_type.handler_name]) || handlers['json']
    @body = handler.generate(@body)
  end
end

#finishObject



80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/praxis/response.rb', line 80

def finish
  format!
  encode!

  @body = Array(@body)

  if @form_data
    if @body.any?
      unless @body.last =~ /\n$/
        @body << "\r\n"
      end
    end

    @parts.each do |name, part|
      part.encode!
      entity = MIME::Text.new(part.body)

      part.headers.each do |header_name, header_value|
        entity.headers.set header_name, header_value
      end

      @form_data.add entity, name
    end

    @body << @form_data.body.to_s
  end

  [@status, @headers, @body]
end

#format!Object



65
66
# File 'lib/praxis/response.rb', line 65

def format!
end

#handleObject



46
47
# File 'lib/praxis/response.rb', line 46

def handle
end

#response_nameObject



61
62
63
# File 'lib/praxis/response.rb', line 61

def response_name
  self.class.response_name
end

#validate(action) ⇒ Object

Validates the response

Parameters:

  • action (Object)


115
116
117
118
119
120
121
122
123
# File 'lib/praxis/response.rb', line 115

def validate(action)
  return if response_name == :validation_error
  unless ( response_definition = action.responses[response_name] )
    raise ArgumentError, "Attempting to return a response with name #{response_name} " \
      "but no response definition with that name can be found"
  end

  response_definition.validate(self)
end