Class: HTTParrot::Server

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(opts = {}) ⇒ Server

Returns a new instance of Server.



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/httparrot/server.rb', line 19

def initialize(opts={})
  @server_started = @server = nil
  @server_thread = nil
  @secure_started = @secure_server = nil
  @secure_thread = nil

  @parent_thread = Thread.current
  @options = { 
    :Port => 4000, 
    :Host => "127.0.0.1"
  }.merge(HTTParrot::Config.config).merge(opts)

  # hard to believe, but it's true, some people still use Windows! like me.
  is_windows = RUBY_PLATFORM =~ /mswin|windows|mingw/i
  quiet_options = { 
    :Logger => WEBrick::Log::new(is_windows ? "nul" : "/dev/null", 7),
    :AccessLog => []
  }

  @options.merge!(quiet_options) unless HTTParrot::Config.verbose

  self.clear!
end

Instance Attribute Details

#optionsObject

Returns the value of attribute options.



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

def options
  @options
end

Instance Method Details

#call(env) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/httparrot/server.rb', line 69

def call(env)
  req = Rack::Request.new(env)
  response = respond_with(env)

  # TODO move this to WEBrick's builtin logging facilities
  if ENV["PARROT_VERBOSE"] || HTTParrot::Config.verbose
    puts "\n>>>>> REQUEST\n"
    puts req.inspect
    #puts req.body.string

    puts "\n<<<<< RESPONSE\n"
    puts response.inspect
    #response[2].each{ |l| puts l }
  end

  return response

rescue Exception => e
  # reraise the exception in the parent thread if it Errors out
  @parent_thread.raise e
end

#clear!Object



43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/httparrot/server.rb', line 43

def clear!
  blank_handler = {
    :get => [], 
    :post => [], 
    :head => [], 
    :delete => []
  }

  # Using Marshal for deep copy purposes
  # slow from an efficiency perspective, but #clear! 
  # is not called frequently and this gem is for testing
  @call_handlers = Marshal.load(Marshal.dump(blank_handler)) 
  @regex_handlers = Marshal.load(Marshal.dump(blank_handler)) 
  @endpoint_handlers = Marshal.load(Marshal.dump(blank_handler)) 
  @complex_handlers = Marshal.load(Marshal.dump(blank_handler))
  self
end

#register(http_method, method_key, response, opts = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/httparrot/server.rb', line 91

def register(http_method, method_key, response, opts={})
  http_method = http_method.to_s.downcase.to_sym
  raise "http_method in register must be one of [:get, :post, :head, :delete] : #{http_method}" if ![:get, :post, :head, :delete].include?(http_method)

  call_with_env = opts[:call_with_env] || false
  response_handler = OpenStruct.new({
    :method_key => method_key,
    :response => response,
    :env? => call_with_env,
    :response_count => 0, 
    :options => opts
  })

  case
  when method_key.respond_to?(:call) then
    @call_handlers[http_method] << response_handler      
  when method_key.is_a?(Regexp) then
    @regex_handlers[http_method] << response_handler 
  when method_key.is_a?(String) then
    @endpoint_handlers[http_method] << response_handler 
  when method_key.is_a?(Array) then
    @complex_handlers[http_method] << response_handler        
  else
    raise "method_key (Handler) must be callable, Regexp, Array, or String" 
  end

  return response_handler
end

#reset_countsObject



61
62
63
64
65
66
67
# File 'lib/httparrot/server.rb', line 61

def reset_counts
  [@call_handlers, @regex_handlers, @endpoint_handlers, @complex_handlers].each do |handler_type|
    handler_type.each_key do |req_meth|
      handler_type[req_meth].each { |handler| handler.response_count = 0 }
    end
  end
end

#running?Boolean Also known as: started?

Returns:

  • (Boolean)


127
128
129
130
# File 'lib/httparrot/server.rb', line 127

def running?
  secure_run_running = options[:ssl] ? (!@secure_server.nil? && @secure_started) : true
  @server_started && !@server.nil? && secure_run_running 
end

#start(startup_interval = 1) ⇒ Object



120
121
122
123
124
125
# File 'lib/httparrot/server.rb', line 120

def start(startup_interval = 1)
  start_server
  start_secure_server if options[:ssl]
  sleep startup_interval # Ensure the server has time to startup
  sleep startup_interval if !running? # Give it a little more time if they didn't start
end

#stop(shutdown_interval = 0) ⇒ Object



133
134
135
136
137
138
139
140
141
# File 'lib/httparrot/server.rb', line 133

def stop(shutdown_interval = 0)
  @server.shutdown if @server.respond_to?(:shutdown)
  @secure_server.shutdown if @secure_server.respond_to?(:shutdown)
  sleep shutdown_interval
  Thread.kill(@server_thread) if !@server_thread.nil?
  Thread.kill(@secure_thread) if !@secure_thread.nil?
  @server_started = @server = nil
  @secure_started = @secure_server = nil
end