Module: Usagi

Defined in:
lib/usagi.rb,
lib/usagi/api_response.rb

Defined Under Namespace

Classes: ApiResponse

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.optionsObject

Returns the value of attribute options.



5
6
7
# File 'lib/usagi.rb', line 5

def options
  @options
end

.pidObject

Returns the value of attribute pid.



5
6
7
# File 'lib/usagi.rb', line 5

def pid
  @pid
end

.portObject

Returns the value of attribute port.



5
6
7
# File 'lib/usagi.rb', line 5

def port
  @port
end

.rspecObject

Returns the value of attribute rspec.



5
6
7
# File 'lib/usagi.rb', line 5

def rspec
  @rspec
end

.suite_optionsObject

Returns the value of attribute suite_options.



5
6
7
# File 'lib/usagi.rb', line 5

def suite_options
  @suite_options
end

Class Method Details

.define_matcher(name, &block) ⇒ Object

Matchers methods



58
59
60
61
62
63
# File 'lib/usagi.rb', line 58

def define_matcher(name, &block)
  if matchers[name.to_s.upcase]
    raise ArgumentError("already defined matcher #{name.to_s.upcase}")
  end
  matchers[name.to_s.upcase] = Matcher.new(name.to_s.upcase, &block)
end

.matchersObject



79
80
81
# File 'lib/usagi.rb', line 79

def matchers
  @matchers ||= MatcherContainer.new
end

.remove_matcher(name) ⇒ Object



65
66
67
68
69
70
# File 'lib/usagi.rb', line 65

def remove_matcher(name)
  unless matchers[name.to_s.upcase]
    raise ArgumentError("undefined matcher #{name.to_s.upcase}")
  end
  matchers.delete(name.to_s.upcase)
end

.rename_matcher(old_name, new_name) ⇒ Object



72
73
74
75
76
77
# File 'lib/usagi.rb', line 72

def rename_matcher(old_name, new_name)
  unless matchers[old_name.to_s.upcase]
    raise ArgumentError("undefined matcher #{old_name.to_s.upcase}")
  end
  matchers[new_name.to_s.upcase] = matchers.delete(old_name.to_s.upcase)
end

.start(*opts) ⇒ Object



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/usagi.rb', line 7

def start(*opts)
  @options = {}
  opts.each{|opt| @options[opt.gsub(/^--usagi-/,'').gsub('-','_').to_sym] = true }
  @port = (rand * 65535).to_i until defined?(@port) && @port > 1024
  @io = IO.popen([{'RAILS_ENV' => 'test'},['rails', 'bundle'], 'server', '-p', @port.to_s])
  Thread.new(@io) do |rails_io|
    buffer = ''
    until rails_io.eof?
      buffer += rails_io.readpartial(1024)
      while buffer["\n"]
        minibuf = buffer.split("\n").first
        buffer = buffer[(minibuf.length + 1)..-1]
        puts "[rails]>> #{minibuf}" if Usagi.options[:rails_output]
      end
    end
    puts "[rails]>> #{buffer}" if buffer.length > 0 && Usagi.options[:rails_output]
  end
  @pid = @io.pid
  puts "[usagi][#{@pid}] Running rails server on port #{@port}"

  Signal.trap('INT') do
    stop
    Process.exit
  end

  sleep 1 until `curl --silent "localhost:#{@port}"`.length > 0

  puts "[usagi][#{@pid}] Rails server listening on port #{@port}"
  true
end

.stopObject



38
39
40
41
42
43
44
45
46
47
# File 'lib/usagi.rb', line 38

def stop
  begin
    Process.kill('INT', @pid)
    puts "[usagi][#{@pid}] SIGINT sent to rails server"
    puts "[usagi][#{@pid}] Waiting for rails server to ack kill command..."
    Process.wait(Usagi.pid)
  rescue
  end
  puts "[usagi][#{@pid}] Killed rails server"
end