Class: Spoofer::FakeHost

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

Defined Under Namespace

Modules: Helpers Classes: RequestEcho, StubbedRequest

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ FakeHost

Returns a new instance of FakeHost.



12
13
14
15
16
17
18
19
# File 'lib/spoofer/fake_host.rb', line 12

def initialize(options = {})
  @hostname = options[:hostname]
  @remote_configuration_path = options[:remote_configuration_path]
  @log = options[:log]
  @imports = []
  clear
  build_url_map!
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method, *args, &block) ⇒ Object (private)



78
79
80
# File 'lib/spoofer/fake_host.rb', line 78

def method_missing(method, *args, &block)
  @app.send(method, *args, &block)
end

Instance Attribute Details

#hostnameObject (readonly)

Returns the value of attribute hostname.



9
10
11
# File 'lib/spoofer/fake_host.rb', line 9

def hostname
  @hostname
end

#logObject

Returns the value of attribute log.



10
11
12
# File 'lib/spoofer/fake_host.rb', line 10

def log
  @log
end

#url_mapObject (readonly)

Returns the value of attribute url_map.



9
10
11
# File 'lib/spoofer/fake_host.rb', line 9

def url_map
  @url_map
end

Instance Method Details

#call(env) ⇒ Object



54
55
56
57
# File 'lib/spoofer/fake_host.rb', line 54

def call(env)
  @stubs.each(&:build)
  @app.call(env)
end

#clearObject



59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/spoofer/fake_host.rb', line 59

def clear
  @stubs = []
  @app = Sinatra.new
  @app.use(Rack::CommonLogger, self.log) if self.log
  @app.not_found do
    [404, {}, ""]
  end
  @app.helpers do
    include Helpers
  end
  @imports.each { |file| import(file) }
end

#delete(path, &block) ⇒ Object



37
38
39
# File 'lib/spoofer/fake_host.rb', line 37

def delete(path, &block)
  request("DELETE", path, &block)
end

#get(path, &block) ⇒ Object



25
26
27
# File 'lib/spoofer/fake_host.rb', line 25

def get(path, &block)
  request("GET", path, &block)
end

#head(path, &block) ⇒ Object



41
42
43
# File 'lib/spoofer/fake_host.rb', line 41

def head(path, &block)
  request("HEAD", path, &block)
end

#import(path) ⇒ Object



45
46
47
48
49
50
51
52
# File 'lib/spoofer/fake_host.rb', line 45

def import(path)
  if File.exists?(path)
    @imports << path unless @imports.include?(path)
    instance_eval(File.read(path))
  else
    raise "Could not locate file for stub import: #{path}"
  end
end

#inspectObject



72
73
74
# File 'lib/spoofer/fake_host.rb', line 72

def inspect
  @stubs.inspect
end

#post(path, &block) ⇒ Object



29
30
31
# File 'lib/spoofer/fake_host.rb', line 29

def post(path, &block)
  request("POST", path, &block)
end

#put(path, &block) ⇒ Object



33
34
35
# File 'lib/spoofer/fake_host.rb', line 33

def put(path, &block)
  request("PUT", path, &block)
end

#received_requestsObject



21
22
23
# File 'lib/spoofer/fake_host.rb', line 21

def received_requests
  @stubs.select { |s| s.received }
end