Class: HAR::Archive

Inherits:
Object
  • Object
show all
Includes:
Serializable
Defined in:
lib/har/archive.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Serializable

#==, #as_json, #inspect, #to_json

Constructor Details

#initialize(input, uri = nil) ⇒ Archive

Returns a new instance of Archive.



39
40
41
42
# File 'lib/har/archive.rb', line 39

def initialize(input, uri = nil)
  @data = input
  @uri   = uri
end

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



37
38
39
# File 'lib/har/archive.rb', line 37

def uri
  @uri
end

Class Method Details

.by_merging(hars) ⇒ Object



24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/har/archive.rb', line 24

def self.by_merging(hars)
  hars = hars.dup

  result = hars.shift or raise ArgumentError, "no HARs given"
  result = from_file(result) unless result.kind_of? self

  hars.each do |har|
    result.merge! har.kind_of?(self) ? har : from_file(har)
  end

  result
end

.from_file(path_or_io) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/har/archive.rb', line 9

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

.from_string(str, uri = nil) ⇒ Object



5
6
7
# File 'lib/har/archive.rb', line 5

def self.from_string(str, uri = nil)
  new JSON.parse(str), uri
end

Instance Method Details

#entriesObject



50
51
52
# File 'lib/har/archive.rb', line 50

def entries
  @entries ||= raw_entries.map { |e| Entry.new(e) }
end

#merge(other) ⇒ Object

create a new archive by merging this and another archive



56
57
58
59
60
61
62
63
# File 'lib/har/archive.rb', line 56

def merge(other)
  assert_archive other

  data = deep_clone(@data)
  merge_data data, other.as_json, other.uri

  self.class.new data
end

#merge!(other) ⇒ Object

destructively merge this with the given archive



67
68
69
70
71
72
73
# File 'lib/har/archive.rb', line 67

def merge!(other)
  assert_archive other
  clear_caches

  merge_data @data, other.as_json, other.uri
  nil
end

#pagesObject



44
45
46
47
48
# File 'lib/har/archive.rb', line 44

def pages
  @pages ||= raw_log.fetch('pages').map { |page|
    Page.new page, entries_for(page['id'])
  }
end

#save_to(path) ⇒ Object



75
76
77
# File 'lib/har/archive.rb', line 75

def save_to(path)
  File.open(path, "w") { |io| io << @data.to_json }
end

#valid?Boolean

Returns:

  • (Boolean)


79
80
81
# File 'lib/har/archive.rb', line 79

def valid?
  JSON::Validator.validate schema_file, @data
end

#validate!Object



83
84
85
86
87
88
89
90
91
92
# File 'lib/har/archive.rb', line 83

def validate!
  JSON::Validator.validate2 schema_file, @data
rescue JSON::ValidationError => ex
  # add archive uri to the message
  if @uri
    raise ValidationError, "#{@uri}: #{ex.message}"
  else
    raise ValidationError, ex.message
  end
end