Class: CassetteExplorer::Server

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

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Server

Returns a new instance of Server.



49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/cassette_explorer/server.rb', line 49

def initialize(options = {})
  @options = {
    port: 2332,
    path: './spec/fixtures/vcr_cassettes/',
    template: __dir__ + '/templates/index.erb',
    relative_urls: true,
    watch: true,
    load_in_iframe: false
  }.merge(options)

  @reader = Reader.new(@options[:path])
end

Class Method Details

.from_command_line(argv) ⇒ Object



3
4
5
6
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
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/cassette_explorer/server.rb', line 3

def self.from_command_line(argv)
  options = {}

  parser = OptionParser.new do |opts|
    opts.banner = 'Usage: cassette_explorer /path/to/vcr/cassettes/directory [options]'

    opts.on('-r', '--no-replace-urls', 'Replace relative URLs with absolute URLs. Default: true') do |v|
      options[:relative_urls] = !v
    end

    opts.on(
      '-f',
      '--load-in-iframe',
      'Default the views to load in an iframe (it can be toggled from the page). Default: false'
    ) do |v|
      options[:load_in_iframe] = v
    end

    opts.on('-w', '--watch', 'Watches the cassettes for changes. Default: true') do |v|
      options[:watch] = v
    end

    opts.on('-p', '--port PORT', 'Port to mount the server') do |v|
      options[:port] = v
    end

    opts.on('-h', '--help', 'Prints this help') do |v|
      options[:help] = v
    end
  end

  begin
    parser.parse!(argv)
    raise OptionParser::InvalidOption if argv.size != 1
    options[:path] = argv.pop
  rescue OptionParser::InvalidOption
    options[:help] = true
  end

  if options[:help]
    puts parser.help
  else
    new options
  end
end

Instance Method Details

#render_index(data) ⇒ Object



101
102
103
# File 'lib/cassette_explorer/server.rb', line 101

def render_index(data)
  Template.new(files: data, load_in_iframe: @options[:load_in_iframe]).render(@options[:template])
end

#run(blocking = true) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# File 'lib/cassette_explorer/server.rb', line 62

def run(blocking = true)
  @sections = @reader.read

  listener = Listen.to(@options[:path]) do
    puts 'Change detected, reloading cassettes'
    @sections = @reader.read
  end

  listener.start # doesn't block

  server = WEBrick::HTTPServer.new Port: @options[:port]
  server.mount_proc '/' do |req, res|
    url = req.query['url']
    file = req.query['file']
    data = req.query['data']

    if file and url
      section = @sections[file]
      page = section.detect{ |p| p.url == url }

      if data
        res.body = YAML::dump(page.request)
      else
        res.body = page.html(@options[:relative_urls])
      end
    else
      res.body = render_index(@sections)
    end
  end

  if blocking
    server.start
  else
    Thread.new do
      server.start
    end
  end
end