Class: Butterfly::Server

Inherits:
Object
  • Object
show all
Defined in:
lib/butterfly/server.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

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.default_options[:adaptor_opts].merge(opts)
  @app = self
end

Instance Attribute Details

#adaptor_nameObject

Returns the value of attribute adaptor_name.



4
5
6
# File 'lib/butterfly/server.rb', line 4

def adaptor_name
  @adaptor_name
end

#appObject (readonly)

Returns the value of attribute app.



3
4
5
# File 'lib/butterfly/server.rb', line 3

def app
  @app
end

#envObject (readonly)

Returns the value of attribute env.



3
4
5
# File 'lib/butterfly/server.rb', line 3

def env
  @env
end

#hostObject (readonly)

Returns the value of attribute host.



3
4
5
# File 'lib/butterfly/server.rb', line 3

def host
  @host
end

#portObject (readonly)

Returns the value of attribute port.



3
4
5
# File 'lib/butterfly/server.rb', line 3

def port
  @port
end

Instance Method Details

#adaptorsObject



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 error_message = "Boom!  could not find Butterfly::#{@request.route_param.to_s.camel_case}Adaptor #{e.inspect}"
    error_message
  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

Returns:

  • (Boolean)


26
27
28
# File 'lib/butterfly/server.rb', line 26

def reloading?
  @reloading == true
end

#should_reload?Boolean

Returns:

  • (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