Class: Sabbath::Server
- Inherits:
-
Object
- Object
- Sabbath::Server
- Defined in:
- lib/sabbath/server.rb
Defined Under Namespace
Classes: DeferrableBody, MethodOverride, StatsProvider
Constant Summary collapse
- AsyncResponse =
[-1, {}, []].freeze
Instance Attribute Summary collapse
-
#backend ⇒ Object
readonly
Returns the value of attribute backend.
-
#cookie_name ⇒ Object
readonly
Returns the value of attribute cookie_name.
-
#rackup ⇒ Object
readonly
Returns the value of attribute rackup.
-
#web_host ⇒ Object
readonly
Returns the value of attribute web_host.
-
#web_port ⇒ Object
readonly
Returns the value of attribute web_port.
Instance Method Summary collapse
- #builder ⇒ Object
- #call(env) ⇒ Object
-
#initialize(backend, web_host, web_port, rackup, cookie_name = 'sabbath_id') ⇒ Server
constructor
A new instance of Server.
- #start ⇒ Object
Constructor Details
#initialize(backend, web_host, web_port, rackup, cookie_name = 'sabbath_id') ⇒ Server
Returns a new instance of Server.
90 91 92 93 94 95 96 97 98 99 |
# File 'lib/sabbath/server.rb', line 90 def initialize(backend, web_host, web_port, rackup, = 'sabbath_id') @backend = backend @web_host, @web_port, @cookie_name, @rackup = web_host, web_port, , rackup @router = Usher.new(:request_methods => [:request_method], :delimiters => ['/', '.']) @router.add_route('/:tube', :conditions => {:request_method => 'GET'}) .name(:get_latest_job) @router.add_route('/:tube/:job_id', :conditions => {:request_method => 'GET'}) .name(:get_job) @router.add_route('/:tube', :conditions => {:request_method => 'POST'}) .name(:create_job) @router.add_route('/:tube/:job_id', :conditions => {:request_method => 'DELETE'}) .name(:delete_job) @router.add_route('/:tube/:job_id/release', :conditions => {:request_method => 'PUT'}) .name(:release_job) end |
Instance Attribute Details
#backend ⇒ Object (readonly)
Returns the value of attribute backend.
88 89 90 |
# File 'lib/sabbath/server.rb', line 88 def backend @backend end |
#cookie_name ⇒ Object (readonly)
Returns the value of attribute cookie_name.
88 89 90 |
# File 'lib/sabbath/server.rb', line 88 def @cookie_name end |
#rackup ⇒ Object (readonly)
Returns the value of attribute rackup.
88 89 90 |
# File 'lib/sabbath/server.rb', line 88 def rackup @rackup end |
#web_host ⇒ Object (readonly)
Returns the value of attribute web_host.
88 89 90 |
# File 'lib/sabbath/server.rb', line 88 def web_host @web_host end |
#web_port ⇒ Object (readonly)
Returns the value of attribute web_port.
88 89 90 |
# File 'lib/sabbath/server.rb', line 88 def web_port @web_port end |
Instance Method Details
#builder ⇒ Object
101 102 103 104 105 106 107 108 |
# File 'lib/sabbath/server.rb', line 101 def builder builder = Rack::Builder.new builder.use(StatsProvider, backend.name, backend.host, backend.port) builder.instance_eval(File.read(rackup)) if rackup builder.use(MethodOverride) builder.run(self) builder end |
#call(env) ⇒ Object
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 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 165 166 167 168 169 170 171 |
# File 'lib/sabbath/server.rb', line 110 def call(env) p env request = Rack::Request.new(env) query_params = Rack::Utils.parse_query(request.query_string) id = request.[] || UUID.new.generate common_response_headers = {'Content-Type' => 'text/javascript'} common_response_headers['Set-cookie'] = Rack::Utils.build_query( => id) unless request.[] body = DeferrableBody.new # Get the headers out there asap, let the client know we're alive... EventMachine::next_tick { body.jsonp_callback = query_params['callback'] case response = @router.recognize(request) when nil env['async.callback'].call([404, {}, []]) else params = Hash[response.params] p response.path.route.named p params case response.path.route.named when :get_latest_job env['async.callback'].call([200, common_response_headers, body]) backend.get_latest_job(id, params[:tube], params['timeout']) {|job| body.succeed_with(:id => job.id, :body => job.body) }.on_error {|| body.succeed_with(:error => ) } when :get_job env['async.callback'].call([200, common_response_headers, body]) backend.get_job(id, params[:tube], params[:job_id]) {|job| body.succeed_with(:id => job.id, :body => job.body) }.on_error {|| body.succeed_with(:error => ) } when :create_job env['async.callback'].call([200, common_response_headers, body]) backend.create_job(id, params[:tube], query_params['body']) {|id| body.succeed_with(:id => id) }.on_error {|| body.succeed_with(:error => ) } when :delete_job env['async.callback'].call([200, common_response_headers, body]) backend.delete_job(id, params[:tube], params[:job_id]) { body.succeed_with(:success => true) }.on_error {|| body.succeed_with(:error => ) } when :release_job env['async.callback'].call([200, common_response_headers, body]) backend.release_job(id, params[:tube], params[:job_id]) { body.succeed_with(:success => true) }.on_error {|| body.succeed_with(:error => ) } end end } AsyncResponse end |