Class: BrowserMob::CLI::HarParser

Inherits:
Object
  • Object
show all
Defined in:
lib/browsermob/cli/har_parser.rb

Instance Method Summary collapse

Constructor Details

#initializeHarParser

Returns a new instance of HarParser.



9
10
11
# File 'lib/browsermob/cli/har_parser.rb', line 9

def initialize
  @harfile = File.open("/tmp/traffic.har")
end

Instance Method Details

#parse_har_fileObject



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
# File 'lib/browsermob/cli/har_parser.rb', line 13

def parse_har_file
  archive = nil
  begin
    archive = JSON.parse File.read(@harfile)
  rescue => err
    puts "could not pase archive file: #{err}"
    exit 1
  end
  entries = archive['log']['entries']
  hosts = entries.map do |entry|
    Addressable::URI.parse(entry['request']['url']).host
  end
  output = hosts.uniq.map do |host|
    urls = entries.map do |entry|
      next unless entry['request']['url'].match("http://#{host}/")
      {
        entry['request']['url'] => entry['timings']
      }
    end
    urls.reject!(&:nil?)
    { host => urls }
  end

  data = File.open("/tmp/traffic.yml", 'w')
  data << output.to_yaml
  # puts output.to_a
end