Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Support reading a HAR from stdin.

Chrome's Network panel exports to the clipboard by default.
  • Loading branch information...
commit db75a7eb0728058862bdf9a5788162e5d7c1b6db 1 parent a5a9f2a
@jarib authored
View
1  bin/har
@@ -3,6 +3,7 @@
require "har"
begin
+ ARGV << "-" if ARGV.empty?
HAR::Viewer.new(ARGV).show
rescue HAR::Error => ex
abort "\n\n#{ex.message}"
View
15 lib/har/archive.rb
@@ -6,8 +6,19 @@ def self.from_string(str, uri = nil)
new JSON.parse(str), uri
end
- def self.from_file(path)
- from_string File.read(path), path
+ def self.from_file(path_or_io)
+ case path_or_io
+ when String
+ from_string File.read(path_or_io), path_or_io
+ when IO
+ from_string path_or_io.read, path_or_io.to_s
+ else
+ unless path_or_io.respond_to?(:to_io)
+ raise TypeError, "expected String, IO or #to_io"
+ end
+
+ from_file path_or_io.to_io
+ end
end
def self.by_merging(hars)
View
14 lib/har/viewer.rb
@@ -14,9 +14,15 @@ def initialize(args)
args = validate(args)
end
- progress("Merging HARs...") {
- @har = Archive.by_merging(args)
- }
+ if args == ["-"]
+ progress("Reading HAR from stdin...") {
+ @har = Archive.from_file $stdin
+ }
+ else
+ progress("Merging HARs...") {
+ @har = Archive.by_merging args
+ }
+ end
end
def show
@@ -59,7 +65,7 @@ def parse(args)
options = DEFAULT_OPTIONS.dup
OptionParser.new do |opts|
- opts.banner = "Usage: har [options] [files]"
+ opts.banner = "Usage: har [options] [files|-]"
opts.on "-p", "--port PORT", Integer do |int|
options[:port] = int
View
6 spec/har/archive_spec.rb
@@ -12,6 +12,12 @@ module HAR
Archive.from_file(har_path("browser-blocking-time")).should be_kind_of(Archive)
end
+ it "can be created from an IO" do
+ File.open(har_path("browser-blocking-time"), "r") do |io|
+ Archive.from_file(io).should be_kind_of(Archive)
+ end
+ end
+
it "saves the archive URI if created from a file" do
ar = Archive.from_file(har_path("browser-blocking-time"))
ar.uri.should_not be_nil

0 comments on commit db75a7e

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