Class: Mockingbird::Script
- Inherits:
-
Object
- Object
- Mockingbird::Script
- Defined in:
- lib/mockingbird/script.rb
Instance Method Summary collapse
-
#add_command(command = nil, &block) ⇒ Object
Not really part of the public API but users could use this to implement their own fancy command.
-
#close ⇒ Object
Do a clean close.
-
#disconnect! ⇒ Object
Perform a hard disconnect by just closing the connection.
- #for_connection(id) ⇒ Object
-
#headers(hash) ⇒ Object
Send the hash of headers to the client.
-
#initialize(&block) ⇒ Script
constructor
A new instance of Script.
-
#on_connection(selector = nil, &block) ⇒ Object
Specifies behavior to run for specific connections based on the id number assigned to that connection.
-
#pipe(string_or_io, opts = {}) ⇒ Object
Send the lines from string_or_io down one at a time.
-
#quit ⇒ Object
Exit the server entirely.
-
#send(data = nil, &block) ⇒ Object
Send some text down to the client.
-
#status(code, message = "") ⇒ Object
Send an HTTP status code to the client.
-
#wait(time = nil, &block) ⇒ Object
Wait a certain number of seconds.
Constructor Details
#initialize(&block) ⇒ Script
Returns a new instance of Script.
4 5 6 7 8 |
# File 'lib/mockingbird/script.rb', line 4 def initialize(&block) @connections = [] @default_connection = ConnectionScript.new instance_eval(&block) end |
Instance Method Details
#add_command(command = nil, &block) ⇒ Object
Not really part of the public API but users could use this to implement their own fancy command
89 90 91 92 |
# File 'lib/mockingbird/script.rb', line 89 def add_command(command=nil,&block) command = Commands::Command.new(&block) if block_given? current_connection.add_command(command) end |
#close ⇒ Object
Do a clean close
72 73 74 |
# File 'lib/mockingbird/script.rb', line 72 def close add_command(Commands::Close.new) end |
#disconnect! ⇒ Object
Perform a hard disconnect by just closing the connection
67 68 69 |
# File 'lib/mockingbird/script.rb', line 67 def disconnect! add_command(Commands::Disconnect.new) end |
#for_connection(id) ⇒ Object
10 11 12 13 14 15 16 17 18 19 |
# File 'lib/mockingbird/script.rb', line 10 def for_connection(id) match = @connections.find do |selector, *| case selector when Range then selector.include?(id) when Numeric then selector == id when Proc then selector.call(id) end end match ? match.last : @default_connection end |
#headers(hash) ⇒ Object
Send the hash of headers to the client.
48 49 50 |
# File 'lib/mockingbird/script.rb', line 48 def headers(hash) current_connection.headers = hash end |
#on_connection(selector = nil, &block) ⇒ Object
Specifies behavior to run for specific connections based on the id number assigned to that connection. Connection ids are 1-based.
The selector can be any of the following:
Number - Run the code on that connection id
Range - Run the code for a connection id in that range
Proc - Call the proc with the id and run if it matches
'*' - Run this code for any connection that doesn't match others
31 32 33 34 35 36 37 38 39 40 |
# File 'lib/mockingbird/script.rb', line 31 def on_connection(selector=nil,&block) if selector.nil? || selector == '*' instance_eval(&block) else @current_connection = ConnectionScript.new instance_eval(&block) @connections << [selector,@current_connection] @current_connection = nil end end |
#pipe(string_or_io, opts = {}) ⇒ Object
Send the lines from string_or_io down one at a time. The :wait option can be used to specify a configurable delay between lines.
83 84 85 |
# File 'lib/mockingbird/script.rb', line 83 def pipe(string_or_io,opts={}) add_command(Commands::Pipe.new(string_or_io,opts[:wait])) end |
#quit ⇒ Object
Exit the server entirely
77 78 79 |
# File 'lib/mockingbird/script.rb', line 77 def quit add_command(Commands::Quit.new) end |
#send(data = nil, &block) ⇒ Object
Send some text down to the client. If a block is specified that block will be called on each connection to determine what text to send. This permits sending randomized data.
55 56 57 |
# File 'lib/mockingbird/script.rb', line 55 def send(data=nil,&block) add_command(Commands::Send.new(data,&block)) end |
#status(code, message = "") ⇒ Object
Send an HTTP status code to the client. e.g. a 403 or 404
43 44 45 |
# File 'lib/mockingbird/script.rb', line 43 def status(code, ="") current_connection.status = [code, ] end |
#wait(time = nil, &block) ⇒ Object
Wait a certain number of seconds. Fractional seconds are allowed. If a block is specified, it will be called on each attempt. This permits things like adding randomized waits.
62 63 64 |
# File 'lib/mockingbird/script.rb', line 62 def wait(time=nil,&block) add_command(Commands::Wait.new(time,&block)) end |