Class: Butterfly::Server
- Inherits:
-
Object
- Object
- Butterfly::Server
- Defined in:
- lib/butterfly/server.rb
Instance Attribute Summary collapse
-
#adaptor_name ⇒ Object
Returns the value of attribute adaptor_name.
-
#app ⇒ Object
readonly
Returns the value of attribute app.
-
#env ⇒ Object
readonly
Returns the value of attribute env.
-
#host ⇒ Object
readonly
Returns the value of attribute host.
-
#port ⇒ Object
readonly
Returns the value of attribute port.
Instance Method Summary collapse
- #adaptors ⇒ Object
- #call(env) ⇒ Object
- #get_adaptor(p = Default.adaptor) ⇒ Object
-
#initialize(host = nil, port = nil, opts = {}) ⇒ Server
constructor
A new instance of Server.
- #reload! ⇒ Object
- #reloading? ⇒ Boolean
- #should_reload? ⇒ Boolean
- #start(opts = {}) ⇒ Object
- #start! ⇒ Object
Constructor Details
#initialize(host = nil, port = nil, opts = {}) ⇒ Server
Returns a new instance of Server.
5 6 7 8 9 10 11 |
# File 'lib/butterfly/server.rb', line 5 def initialize(host=nil, port=nil, opts={}) @host = host || Default.host @port = port || Default.port @env = opts.delete(:env) || Default.env @adaptor_opts = Default.[:adaptor_opts].merge(opts) @app = self end |
Instance Attribute Details
#adaptor_name ⇒ Object
Returns the value of attribute adaptor_name.
4 5 6 |
# File 'lib/butterfly/server.rb', line 4 def adaptor_name @adaptor_name end |
#app ⇒ Object (readonly)
Returns the value of attribute app.
3 4 5 |
# File 'lib/butterfly/server.rb', line 3 def app @app end |
#env ⇒ Object (readonly)
Returns the value of attribute env.
3 4 5 |
# File 'lib/butterfly/server.rb', line 3 def env @env end |
#host ⇒ Object (readonly)
Returns the value of attribute host.
3 4 5 |
# File 'lib/butterfly/server.rb', line 3 def host @host end |
#port ⇒ Object (readonly)
Returns the value of attribute port.
3 4 5 |
# File 'lib/butterfly/server.rb', line 3 def port @port end |
Instance Method Details
#adaptors ⇒ Object
52 53 54 |
# File 'lib/butterfly/server.rb', line 52 def adaptors @adaptors ||= {} end |
#call(env) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/butterfly/server.rb', line 34 def call(env) reload! if should_reload? @request = Request.new env @response = Response.new @request body = begin get_adaptor(@request.route_param).send(:handle_call, @request, @response) rescue get_adaptor(@request.route_param).send(@request.params.first.to_sym, @request, @response) #TODO:refactor this rescue Exception => e @response.fail! puts = "Boom! could not find Butterfly::#{@request.route_param.to_s.camel_case}Adaptor #{e.inspect}" end return @response.return!(body) end |
#get_adaptor(p = Default.adaptor) ⇒ Object
48 49 50 |
# File 'lib/butterfly/server.rb', line 48 def get_adaptor(p=Default.adaptor) adaptors[p] ||= Butterfly.const_get("#{p.to_s.camel_case}Adaptor").new(@adaptor_opts) #rescue raise "Error: undefined adaptor for #{@request.route_param}" end |
#reload! ⇒ Object
22 23 24 |
# File 'lib/butterfly/server.rb', line 22 def reload! Butterfly.reload! end |
#reloading? ⇒ Boolean
26 27 28 |
# File 'lib/butterfly/server.rb', line 26 def reloading? @reloading == true end |
#should_reload? ⇒ Boolean
30 31 32 |
# File 'lib/butterfly/server.rb', line 30 def should_reload? @env == :development end |
#start(opts = {}) ⇒ Object
17 18 19 20 |
# File 'lib/butterfly/server.rb', line 17 def start(opts={}) t_opts = {:port=>@port, :host=>@host}.merge(opts) at_exit {Thin::Controllers::Controller.new(t_opts).start(@app)} end |
#start! ⇒ Object
13 14 15 |
# File 'lib/butterfly/server.rb', line 13 def start! at_exit {Thin::Server.start(@host, @port, app)} end |