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.



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

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.



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

def response_name
  @response_name
end

.statusObject

Returns the value of attribute status.



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

def status
  @status
end

Instance Attribute Details

#bodyObject

Returns the value of attribute body.



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

def body
  @body
end

#headersObject

Returns the value of attribute headers.



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

def headers
  @headers
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#partsObject (readonly)

Returns the value of attribute parts.



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

def parts
  @parts
end

#requestObject

Returns the value of attribute request.



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

def request
  @request
end

#statusObject

Returns the value of attribute status.



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

def status
  @status
end

Class Method Details

.inherited(klass) ⇒ Object



18
19
20
21
# File 'lib/praxis/response.rb', line 18

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



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/praxis/response.rb', line 35

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

#encode!Object



54
55
56
57
58
59
# File 'lib/praxis/response.rb', line 54

def encode!
  case @body
  when Hash, Array
    @body = JSON.pretty_generate(@body)
  end
end

#finishObject



61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/praxis/response.rb', line 61

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



51
52
# File 'lib/praxis/response.rb', line 51

def format!
end

#handleObject



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

def handle
end

#response_nameObject



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

def response_name
  self.class.response_name
end

#validate(action) ⇒ Object

Validates the response

Parameters:

  • action (Object)


96
97
98
99
100
101
102
103
# File 'lib/praxis/response.rb', line 96

def validate(action)
  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