Class: Angelo::Responder
- Inherits:
-
Object
show all
- Includes:
- Celluloid::Logger
- Defined in:
- lib/angelo/responder.rb,
lib/angelo/responder/websocket.rb,
lib/angelo/responder/eventsource.rb
Defined Under Namespace
Classes: Eventsource, Websocket
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
Constructor Details
#initialize(&block) ⇒ Responder
Returns a new instance of Responder.
39
40
41
|
# File 'lib/angelo/responder.rb', line 39
def initialize &block
@response_handler = Base.compile! :request_handler, &block
end
|
Class Attribute Details
24
25
26
27
|
# File 'lib/angelo/responder.rb', line 24
def
||=
end
|
Instance Attribute Details
#base=(value) ⇒ Object
37
38
39
|
# File 'lib/angelo/responder.rb', line 37
def base=(value)
@base = value
end
|
#connection ⇒ Object
Returns the value of attribute connection.
35
36
37
|
# File 'lib/angelo/responder.rb', line 35
def connection
@connection
end
|
#request ⇒ Object
Returns the value of attribute request.
36
37
38
|
# File 'lib/angelo/responder.rb', line 36
def request
@request
end
|
Class Method Details
.content_type(type) ⇒ Object
12
13
14
15
16
17
18
19
20
21
22
|
# File 'lib/angelo/responder.rb', line 12
def content_type type
dhs = self.
case type
when :json
self. = dhs.merge => JSON_TYPE
when :html
self. = dhs.merge => HTML_TYPE
else
raise ArgumentError.new "invalid content_type: #{type}"
end
end
|
.symhash ⇒ Object
29
30
31
|
# File 'lib/angelo/responder.rb', line 29
def symhash
Hash.new {|hash,key| hash[key.to_s] if Symbol === key }
end
|
Instance Method Details
#content_type(type) ⇒ Object
111
112
113
114
115
116
117
118
119
120
121
122
|
# File 'lib/angelo/responder.rb', line 111
def content_type type
case type
when :json
=> JSON_TYPE
when :html
=> HTML_TYPE
when :js
=> JS_TYPE
else
raise ArgumentError.new "invalid content_type: #{type}"
end
end
|
#error_message(_error) ⇒ Object
91
92
93
94
95
96
97
98
99
100
101
102
103
|
# File 'lib/angelo/responder.rb', line 91
def error_message _error
case
when respond_with?(:json)
{ error: _error.message }.to_json
else
case _error.message
when Hash
_error.message.to_s
else
_error.message
end
end
end
|
#handle_error(_error, type = :internal_server_error, report = @base.report_errors?) ⇒ Object
80
81
82
83
84
85
86
87
88
89
|
# File 'lib/angelo/responder.rb', line 80
def handle_error _error, type = :internal_server_error, report = @base.report_errors?
err_msg = error_message _error
Angelo.log @connection, @request, nil, type, err_msg.size
@connection.respond type, , err_msg
@connection.close
if report
error "#{_error.class} - #{_error.message}"
::STDERR.puts _error.backtrace
end
end
|
#handle_request ⇒ Object
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
|
# File 'lib/angelo/responder.rb', line 51
def handle_request
if @response_handler
@base.before if @base.respond_to? :before
@body = catch(:halt) { @response_handler.bind(@base).call || EMPTY_STRING }
case @body
when HALT_STRUCT
if @body.body != :sse and @base.respond_to? :after
@base.after
end
else
@base.after if @base.respond_to? :after
end
respond
else
raise NotImplementedError
end
rescue JSON::ParserError => jpe
handle_error jpe, :bad_request
rescue FormEncodingError => fee
handle_error fee, :bad_request
rescue RequestError => re
handle_error re, re.type
rescue => e
handle_error e
end
|
105
106
107
108
109
|
# File 'lib/angelo/responder.rb', line 105
def hs = nil
||= self.class..dup
.merge! hs if hs
end
|
#redirect(url) ⇒ Object
166
167
168
|
# File 'lib/angelo/responder.rb', line 166
def redirect url
@redirect = url
end
|
#respond ⇒ Object
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
|
# File 'lib/angelo/responder.rb', line 133
def respond
status = nil
case @body
when HALT_STRUCT
status = @body.status
@body = @body.body
@body = nil if @body == :sse
if Hash === @body
@body = {error: @body} if status != :ok or status < 200 && status >= 300
@body = @body.to_json if respond_with? :json
end
when String
JSON.parse @body if respond_with? :json
when Hash
raise 'html response requires String' if respond_with? :html
@body = @body.to_json if respond_with? :json
when NilClass
@body = EMPTY_STRING
end
status ||= @redirect.nil? ? :ok : :moved_permanently
=> @redirect if @redirect
size = @body.nil? ? 0 : @body.size
Angelo.log @connection, @request, nil, status, size
@request.respond status, , @body
rescue => e
handle_error e, :internal_server_error
end
|
#respond_with?(type) ⇒ Boolean
124
125
126
127
128
129
130
131
|
# File 'lib/angelo/responder.rb', line 124
def respond_with? type
case []
when JSON_TYPE
type == :json
else
type == :html
end
end
|