lib/angelo/base.rb in angelo-0.2.4 vs lib/angelo/base.rb in angelo-0.3.0
- old
+ new
@@ -4,28 +4,12 @@
include ParamsParser
include Celluloid::Logger
extend Forwardable
def_delegators :@responder, :content_type, :headers, :redirect, :request, :transfer_encoding
- def_delegators :@klass, :websockets, :sses, :sse_event, :sse_message
+ def_delegators :@klass, :report_errors?, :sse_event, :sse_message, :sses, :websockets
- @@addr = DEFAULT_ADDR
- @@port = DEFAULT_PORT
-
- @@ping_time = DEFAULT_PING_TIME
- @@log_level = DEFAULT_LOG_LEVEL
-
- @@report_errors = false
-
- if ARGV.any? and not Kernel.const_defined?('Minitest')
- require 'optparse'
- OptionParser.new { |op|
- op.on('-p port', 'set the port (default is 4567)') { |val| @@port = Integer(val) }
- op.on('-o addr', "set the host (default is #{@@addr})") { |val| @@addr = val }
- }.parse!(ARGV.dup)
- end
-
attr_accessor :responder
class << self
attr_accessor :app_file, :server
@@ -38,10 +22,16 @@
# bring RequestError into this namespace
#
subclass.class_eval 'class RequestError < Angelo::RequestError; end'
+ subclass.addr DEFAULT_ADDR
+ subclass.port DEFAULT_PORT
+
+ subclass.ping_time DEFAULT_PING_TIME
+ subclass.log_level DEFAULT_LOG_LEVEL
+
class << subclass
def root
@root ||= File.expand_path '..', app_file
end
@@ -58,10 +48,38 @@
end
end
+ def addr a = nil
+ @addr = a if a
+ @addr
+ end
+
+ def log_level ll = nil
+ @log_level = ll if ll
+ @log_level
+ end
+
+ def ping_time pt = nil
+ @ping_time = pt if pt
+ @ping_time
+ end
+
+ def port p = nil
+ @port = p if p
+ @port
+ end
+
+ def report_errors!
+ @report_errors = true
+ end
+
+ def report_errors?
+ !!@report_errors
+ end
+
def compile! name, &block
define_method name, &block
method = instance_method name
remove_method name
method
@@ -142,17 +160,17 @@
def content_type type
Responder.content_type type
end
- def run! addr = @@addr, port = @@port
- run addr, port, true
+ def run! _addr = addr, _port = port
+ run _addr, _port, true
end
- def run addr = @@addr, port = @@port, blocking = false
- Celluloid.logger.level = @@log_level
- @server = Angelo::Server.new self, addr, port
+ def run _addr = addr, _port = port, blocking = false
+ Celluloid.logger.level = log_level
+ @server = Angelo::Server.new self, _addr, _port
@server.async.ping_websockets
if blocking
trap "INT" do
@server.terminate if @server and @server.alive?
exit
@@ -224,11 +242,11 @@
websockets.remove_socket ws
end
end
task :ping_websockets do
- every(@@ping_time) do
+ every(@base.ping_time) do
websockets.all_each do |ws|
ws.socket << ::WebSocket::Message.ping.to_data
end
end
end
@@ -236,11 +254,11 @@
task :handle_event_source do |socket, block|
begin
block[socket]
rescue Reel::SocketError, IOError, SystemCallError => e
# probably closed on client
- warn e.message if @@report_errors
+ warn e.message if report_errors
socket.close unless socket.closed?
rescue => e
error e.inspect
socket.close unless socket.closed?
end
@@ -294,13 +312,9 @@
def eventsource &block
headers SSE_HEADER
async :handle_event_source, EventSource.new(responder), block
halt 200, :sse
- end
-
- def report_errors?
- @@report_errors
end
def sleep time
Celluloid.sleep time
end