Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Let user specify --validate and --port

  • Loading branch information...
commit 07338bdf0fe8611160c35b7ab9e1c79c30b5e2fc 1 parent 0dbcf26
@jarib authored
Showing with 55 additions and 19 deletions.
  1. +48 −17 lib/har/viewer.rb
  2. +7 −2 spec/har/viewer_spec.rb
View
65 lib/har/viewer.rb
@@ -1,5 +1,6 @@
require "webrick"
require "launchy"
+require "optparse"
module HAR
class Viewer
@@ -21,39 +22,61 @@ def show
private
def archives_from(hars)
- hars = hars.map { |path| Archive.from_file(path) }
- hars.each { |h| h.validate! }
+ progress("Validating archives...") {
+ hars = hars.map { |path| Archive.from_file(path) }
+ hars.each { |h| h.validate! } if @options[:validate]
+
+ hars
+ }
end
def create_root
- viewer = File.expand_path("../viewer", __FILE__)
- tmp_dir = Dir.mktmpdir("harviewer")
+ progress("Creating viewer...") {
+ viewer = File.expand_path("../viewer", __FILE__)
+ tmp_dir = Dir.mktmpdir("harviewer")
- at_exit { FileUtils.rm_rf tmp_dir }
- FileUtils.cp_r viewer, tmp_dir
+ at_exit { FileUtils.rm_rf tmp_dir }
+ FileUtils.cp_r viewer, tmp_dir
- har.save_to File.join(tmp_dir, 'viewer', url_friendly(@har.uri))
+ har.save_to File.join(tmp_dir, 'viewer', url_friendly(@har.uri))
- tmp_dir
+ tmp_dir
+ }
end
def merge(hars)
- har = hars.shift or raise Error, "no HARs given"
+ progress("Merging hars...") {
+ har = hars.shift or raise Error, "no HARs given"
- unless hars.empty?
- hars.each { |h| har.merge! h }
- end
+ unless hars.empty?
+ hars.each { |h| har.merge! h }
+ end
- har
+ har
+ }
end
DEFAULT_OPTIONS = {
- :port => 9292
+ :port => 9292,
+ :validate => false
}
def parse(args)
- # TODO: parse command line
- DEFAULT_OPTIONS.dup
+ options = DEFAULT_OPTIONS.dup
+
+ OptionParser.new do |opts|
+ opts.banner = "Usage: har [options] [files]"
+
+ opts.on "-p", "--port PORT", Integer do |int|
+ options[:port] = int
+ end
+
+ opts.on "-v", "--validate" do
+ options[:validate] = true
+ end
+ end.parse!(args)
+
+ options
end
def url
@@ -74,7 +97,7 @@ def start_hook
def server(root)
Thread.new do
- puts "Starting HAR Viewer Server..."
+ puts "Starting server..."
puts "Type ^C to exit\n\n"
server = WEBrick::HTTPServer.new(:Port => port,
@@ -94,5 +117,13 @@ def launch_browser
Launchy.open url
end
+ def progress(msg, &blk)
+ print msg
+ res = yield
+ puts "done."
+
+ res
+ end
+
end # Viewer
end # HAR
View
9 spec/har/viewer_spec.rb
@@ -4,13 +4,18 @@ module HAR
describe Viewer do
context "creating" do
- it "validates the given HARs" do
- lambda { Viewer.new(all_hars) }.should raise_error(ValidationError)
+ it "validates the given HARs if asked to" do
+ lambda { Viewer.new(["--validate", *all_hars]) }.should raise_error(ValidationError)
end
it "has a merged archive" do
Viewer.new(good_hars).har.should be_kind_of(Archive)
end
+
+ it "parses options" do
+ v = Viewer.new(["-p", "1234", *good_hars])
+ v.options[:port].should == 1234
+ end
end
end

0 comments on commit 07338bd

Please sign in to comment.
Something went wrong with that request. Please try again.