Module: Mockingbird::Server
- Defined in:
- lib/mockingbird/server.rb
Overview
A very simple eventmachine-based streaming server. Before you use a server it must be configured with configuration block:
Mockingbird::Server.configure do
# config goes here, see README for scripting
end
Once configured, a server may be started using
start!(:host=>'0.0.0.0',:port=>NNN)
If you’re using Mockingbird directly from test (test/unit, etc), it’s recommended that you use the simpler convenience interface defined on the Mockingbird module.
Class Method Summary collapse
- .configure(&block) ⇒ Object
- .new_connection_id ⇒ Object
- .script ⇒ Object
- .start!(opts = {}) ⇒ Object
Instance Method Summary collapse
- #new_connection_id ⇒ Object
- #receive_data(data) ⇒ Object
- #script ⇒ Object
- #send_chunk(chunk) ⇒ Object
- #send_header(name, value) ⇒ Object
- #send_status(code = 200, text = "OK") ⇒ Object
- #send_terminal_chunk ⇒ Object
- #start_body ⇒ Object
Class Method Details
.configure(&block) ⇒ Object
31 32 33 |
# File 'lib/mockingbird/server.rb', line 31 def configure(&block) @script = Script.new(&block) end |
.new_connection_id ⇒ Object
39 40 41 42 |
# File 'lib/mockingbird/server.rb', line 39 def new_connection_id @connection_id ||= 0 @connection_id += 1 end |
.script ⇒ Object
35 36 37 |
# File 'lib/mockingbird/server.rb', line 35 def script @script end |
.start!(opts = {}) ⇒ Object
21 22 23 24 25 26 27 28 29 |
# File 'lib/mockingbird/server.rb', line 21 def start!(opts={}) opts = {:host=>'0.0.0.0',:port=>8080}.merge(opts) host = opts[:host] port = opts[:port] EventMachine::run do puts "Mockingbird is mocking you on #{host}:#{port} (pid=#{$$})" unless opts[:quiet] EventMachine::start_server host, port, self end end |
Instance Method Details
#new_connection_id ⇒ Object
52 53 54 |
# File 'lib/mockingbird/server.rb', line 52 def new_connection_id Mockingbird::Server.new_connection_id end |
#receive_data(data) ⇒ Object
46 47 48 49 50 |
# File 'lib/mockingbird/server.rb', line 46 def receive_data(data) conn_id = new_connection_id runner = ScriptRunner.new(self,script.for_connection(conn_id)) runner.run end |
#script ⇒ Object
56 57 58 |
# File 'lib/mockingbird/server.rb', line 56 def script Mockingbird::Server.script end |
#send_chunk(chunk) ⇒ Object
72 73 74 75 76 |
# File 'lib/mockingbird/server.rb', line 72 def send_chunk(chunk) len = chunk_length(chunk) send_data "#{len.to_s(16)}\r\n" send_data "#{chunk}\r\n" end |
#send_header(name, value) ⇒ Object
64 65 66 |
# File 'lib/mockingbird/server.rb', line 64 def send_header(name,value) send_data "#{name}: #{value}\r\n" end |
#send_status(code = 200, text = "OK") ⇒ Object
60 61 62 |
# File 'lib/mockingbird/server.rb', line 60 def send_status(code=200,text="OK") send_data "HTTP/1.1 #{code} #{text}\r\n" end |
#send_terminal_chunk ⇒ Object
78 79 80 |
# File 'lib/mockingbird/server.rb', line 78 def send_terminal_chunk send_data "0\r\n\r\n" end |
#start_body ⇒ Object
68 69 70 |
# File 'lib/mockingbird/server.rb', line 68 def start_body send_data "\r\n" end |