Class: Angelo::Base
- Inherits:
-
Object
show all
- Extended by:
- Forwardable
- Includes:
- ParamsParser, Celluloid::Logger
- Defined in:
- lib/angelo/base.rb
Constant Summary
collapse
- @@addr =
DEFAULT_ADDR
- @@port =
DEFAULT_PORT
- @@ping_time =
DEFAULT_PING_TIME
- @@log_level =
DEFAULT_LOG_LEVEL
- @@report_errors =
false
ParamsParser::AMPERSAND, ParamsParser::EMPTY_JSON, ParamsParser::EQUALS, ParamsParser::SEMICOLON
Class Attribute Summary collapse
Instance Attribute Summary collapse
Class Method Summary
collapse
Instance Method Summary
collapse
#content_type?, #form_encoded?, #json?, #parse_formencoded, #parse_post_body, #parse_query_string, #recurse_symhash
Constructor Details
#initialize(responder) ⇒ Base
Returns a new instance of Base.
154
155
156
157
|
# File 'lib/angelo/base.rb', line 154
def initialize responder
@responder = responder
@klass = self.class
end
|
Class Attribute Details
.app_file ⇒ Object
Returns the value of attribute app_file.
31
32
33
|
# File 'lib/angelo/base.rb', line 31
def app_file
@app_file
end
|
.server ⇒ Object
Returns the value of attribute server.
31
32
33
|
# File 'lib/angelo/base.rb', line 31
def server
@server
end
|
Instance Attribute Details
#responder ⇒ Object
Returns the value of attribute responder.
27
28
29
|
# File 'lib/angelo/base.rb', line 27
def responder
@responder
end
|
Class Method Details
.after(opts = {}, &block) ⇒ Object
82
83
84
|
# File 'lib/angelo/base.rb', line 82
def after opts = {}, &block
define_method :after, &block
end
|
.before(opts = {}, &block) ⇒ Object
78
79
80
|
# File 'lib/angelo/base.rb', line 78
def before opts = {}, &block
define_method :before, &block
end
|
.compile!(name, &block) ⇒ Object
63
64
65
66
67
68
|
# File 'lib/angelo/base.rb', line 63
def compile! name, &block
define_method name, &block
method = instance_method name
remove_method name
method
end
|
.content_type(type) ⇒ Object
.eventsource(path, &block) ⇒ Object
96
97
98
|
# File 'lib/angelo/base.rb', line 96
def eventsource path, &block
routes[:get][path] = Responder::Eventsource.new &block
end
|
.inherited(subclass) ⇒ Object
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
|
# File 'lib/angelo/base.rb', line 33
def inherited subclass
subclass.app_file = caller(1).map {|l| l.split(/:(?=|in )/, 3)[0,1]}.flatten[0]
subclass.class_eval 'class RequestError < Angelo::RequestError; end'
class << subclass
def root
@root ||= File.expand_path '..', app_file
end
def view_dir
v = self.class_variable_get(:@@views) rescue DEFAULT_VIEW_DIR
File.join root, v
end
def public_dir
p = self.class_variable_get(:@@public_dir) rescue DEFAULT_PUBLIC_DIR
File.join root, p
end
end
end
|
.local_path(path) ⇒ Object
135
136
137
138
139
140
|
# File 'lib/angelo/base.rb', line 135
def local_path path
if public_dir
lp = File.join(public_dir, path)
File.file?(lp) ? lp : nil
end
end
|
.on_pong(&block) ⇒ Object
.routes ⇒ Object
70
71
72
73
74
75
76
|
# File 'lib/angelo/base.rb', line 70
def routes
@routes ||= {}
ROUTABLE.each do |m|
@routes[m] ||= {}
end
@routes
end
|
.run(addr = @@addr, port = @@port) ⇒ Object
124
125
126
127
128
129
130
131
132
133
|
# File 'lib/angelo/base.rb', line 124
def run addr = @@addr, port = @@port
Celluloid.logger.level = @@log_level
@server = Angelo::Server.new self, addr, port
@server.async.ping_websockets
trap "INT" do
@server.terminate if @server and @server.alive?
exit
end
sleep
end
|
.sse_event(event_name, data) ⇒ Object
142
143
144
145
|
# File 'lib/angelo/base.rb', line 142
def sse_event event_name, data
data = data.to_json if Hash === data
SSE_EVENT_TEMPLATE % [event_name.to_s, data]
end
|
.sse_message(data) ⇒ Object
147
148
149
150
|
# File 'lib/angelo/base.rb', line 147
def sse_message data
data = data.to_json if Hash === data
SSE_DATA_TEMPLATE % data
end
|
.sses ⇒ Object
114
115
116
117
118
|
# File 'lib/angelo/base.rb', line 114
def sses
@sses ||= Stash::SSE.new server
@sses.reject! &:closed?
@sses
end
|
.task(name, &block) ⇒ Object
104
105
106
|
# File 'lib/angelo/base.rb', line 104
def task name, &block
Angelo::Server.define_task name, &block
end
|
.websocket(path, &block) ⇒ Object
92
93
94
|
# File 'lib/angelo/base.rb', line 92
def websocket path, &block
routes[:websocket][path] = Responder::Websocket.new &block
end
|
.websockets ⇒ Object
108
109
110
111
112
|
# File 'lib/angelo/base.rb', line 108
def websockets
@websockets ||= Stash::Websocket.new server
@websockets.reject! &:closed?
@websockets
end
|
Instance Method Details
#async(meth, *args) ⇒ Object
159
160
161
|
# File 'lib/angelo/base.rb', line 159
def async meth, *args
self.class.server.async.__send__ meth, *args
end
|
#eventsource(&block) ⇒ Object
264
265
266
267
268
|
# File 'lib/angelo/base.rb', line 264
def eventsource &block
SSE_HEADER
async :handle_event_source, responder.connection.detach.socket, block
halt 200, :sse
end
|
#future(meth, *args) ⇒ Object
163
164
165
|
# File 'lib/angelo/base.rb', line 163
def future meth, *args
self.class.server.future.__send__ meth, *args
end
|
#halt(status = 400, body = '') ⇒ Object
218
219
220
|
# File 'lib/angelo/base.rb', line 218
def halt status = 400, body = ''
throw :halt, HALT_STRUCT.new(status, body)
end
|
#params ⇒ Object
167
168
169
170
171
172
173
|
# File 'lib/angelo/base.rb', line 167
def params
@params ||= case request.method
when GET; parse_query_string
when POST; parse_post_body
when PUT; parse_post_body
end
end
|
#report_errors? ⇒ Boolean
270
271
272
|
# File 'lib/angelo/base.rb', line 270
def report_errors?
@@report_errors
end
|
175
176
177
178
179
180
181
182
183
184
|
# File 'lib/angelo/base.rb', line 175
def
@request_headers ||= Hash.new do |hash, key|
if Symbol === key
k = key.to_s.upcase
k.gsub! UNDERSCORE, DASH
rhv = request..select {|,v| .upcase == k}
hash[key] = rhv.values.first
end
end
end
|
#send_data(data, opts = {}) ⇒ Object
#send_file(local_file, opts = {}) ⇒ Object
#sleep(time) ⇒ Object
274
275
276
|
# File 'lib/angelo/base.rb', line 274
def sleep time
Celluloid.sleep time
end
|