Class: Faye::RackAdapter
- Inherits:
-
Object
- Object
- Faye::RackAdapter
- Defined in:
- lib/faye/adapters/rack_adapter.rb
Defined Under Namespace
Classes: DeferredBody
Constant Summary collapse
- ASYNC_RESPONSE =
Only supported under Thin
[-1, {}, []].freeze
- DEFAULT_ENDPOINT =
'/bayeux'
- SCRIPT_PATH =
File.join(ROOT, 'faye-browser-min.js')
- TYPE_JSON =
{'Content-Type' => 'application/json'}
- TYPE_SCRIPT =
{'Content-Type' => 'text/javascript'}
- TYPE_TEXT =
{'Content-Type' => 'text/plain'}
Instance Method Summary collapse
- #add_extension(extension) ⇒ Object
- #call(env) ⇒ Object
- #get_client ⇒ Object
-
#initialize(app = nil, options = nil) ⇒ RackAdapter
constructor
A new instance of RackAdapter.
- #listen(port) ⇒ Object
- #remove_extension(extension) ⇒ Object
Constructor Details
#initialize(app = nil, options = nil) ⇒ RackAdapter
Returns a new instance of RackAdapter.
20 21 22 23 24 25 26 27 |
# File 'lib/faye/adapters/rack_adapter.rb', line 20 def initialize(app = nil, = nil) @app = app if app.respond_to?(:call) @options = [app, ].grep(Hash).first || {} @endpoint = @options[:mount] || DEFAULT_ENDPOINT @endpoint_re = Regexp.new('^' + @endpoint + '(/[^/]*)*(\\.js)?$') @server = Server.new(@options) end |
Instance Method Details
#add_extension(extension) ⇒ Object
29 30 31 |
# File 'lib/faye/adapters/rack_adapter.rb', line 29 def add_extension(extension) @server.add_extension(extension) end |
#call(env) ⇒ Object
46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 |
# File 'lib/faye/adapters/rack_adapter.rb', line 46 def call(env) Faye.ensure_reactor_running! request = Rack::Request.new(env) unless request.path_info =~ @endpoint_re env['faye.client'] = get_client return @app ? @app.call(env) : [404, TYPE_TEXT, ["Sure you're not looking for #{@endpoint} ?"]] end if env['HTTP_UPGRADE'] == 'WebSocket' return handle_upgrade(request) end if request.path_info =~ /\.js$/ return [200, TYPE_SCRIPT, File.new(SCRIPT_PATH)] end begin json_msg = (request) = JSON.parse(json_msg) jsonp = request.params['jsonp'] || JSONP_CALLBACK type = request.get? ? TYPE_SCRIPT : TYPE_JSON callback = env['async.callback'] body = DeferredBody.new @server.flush_connection() if request.get? callback.call [200, type, body] @server.process(, false) do |replies| response = JSON.unparse(replies) response = "#{ jsonp }(#{ response });" if request.get? body.succeed(response) end ASYNC_RESPONSE rescue [400, TYPE_TEXT, 'Bad request'] end end |
#get_client ⇒ Object
37 38 39 |
# File 'lib/faye/adapters/rack_adapter.rb', line 37 def get_client @client ||= Client.new(@server) end |
#listen(port) ⇒ Object
41 42 43 44 |
# File 'lib/faye/adapters/rack_adapter.rb', line 41 def listen(port) handler = Rack::Handler.get('thin') handler.run(self, :Port => port) end |
#remove_extension(extension) ⇒ Object
33 34 35 |
# File 'lib/faye/adapters/rack_adapter.rb', line 33 def remove_extension(extension) @server.remove_extension(extension) end |