Class: Shellac::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/shellac.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRunner

Returns a new instance of Runner.



19
20
21
22
# File 'lib/shellac.rb', line 19

def initialize
  @varnishclient = VarnishClient.new
  @varnishlog = VarnishLog.new
end

Instance Attribute Details

#varnishclientObject

Returns the value of attribute varnishclient.



17
18
19
# File 'lib/shellac.rb', line 17

def varnishclient
  @varnishclient
end

#varnishlogObject

Returns the value of attribute varnishlog.



17
18
19
# File 'lib/shellac.rb', line 17

def varnishlog
  @varnishlog
end

Instance Method Details

#make_requestObject

  1. Starts a thread to spawn a ‘varnishlog` subprocess that will catch our HTTP request.

  2. Executes the HTTP request to Varnish.

  3. Reaps the ‘varnishlog` thread so that its output can be parsed into the @varnishlog.response object.

Returns nothing.



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/shellac.rb', line 45

def make_request
  @varnishlog.start_varnishlog_thread(@varnishclient.request.user_agent)
  # Sleeping is a hack to give the varnishlog thread time to get in place.
  # Without it, there's a roughly 50/50 chance (sometimes worse) that the thread
  # will block on I/O as if it missed the HTTP request (because it did).
  sleep(0.5)
  
  @varnishclient.response = @varnishclient.request.make_request

  case @varnishclient.response.code
  when /2\d{2}/
    color = GREEN
  when /3\d{2}/
    color = YELLOW
  when /4\d{2}/
    color = RED
  else
    color = NORMAL
  end
  puts "\n#{@varnishclient.request.uri.to_s}: #{color}#{@varnishclient.response.code} #{@varnishclient.response.message}#{NORMAL}\n\n"

  @varnishlog.reap_thread
end

#runObject



24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/shellac.rb', line 24

def run
  # Inform the user of the default request.
  puts "\nDefault request: #{RED}#{@varnishclient.request.uri.to_s}#{NORMAL}"
  puts "HTTP Port (varnishclient.request.port): #{RED}#{@varnishclient.request.port}#{NORMAL}"
  puts "HTTP Request Path (varnishclient.request.path): #{RED}#{@varnishclient.request.path}#{NORMAL}"
  puts "HTTP Host Header (varnishclient.request.host): #{RED}#{@varnishclient.request.host}#{NORMAL}"
  puts "\n#{RED}You may want to to modify this before calling #{NORMAL}@varnishclient.make_request#{RED}!#{NORMAL}"
  
  # Start our REPL.
  Ripl.config[:prompt] = "\nshellac> "
  # Bind Ripl to the context of this instance so that all of the objects
  # accessible to it (and their methods) are accessible to the user in the REPL.
  # 'binding' is a private method that returns a Binding object; we can only
  # access it here via the #instance_eval method.
  Ripl.start :binding => self.instance_eval{binding}
end