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.



28
29
30
31
# File 'lib/har/archive.rb', line 28

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

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



26
27
28
# File 'lib/har/archive.rb', line 26

def uri
  @uri
end

Class Method Details

.by_merging(hars) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
24
# File 'lib/har/archive.rb', line 13

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) ⇒ Object



9
10
11
# File 'lib/har/archive.rb', line 9

def self.from_file(path)
  from_string File.read(path), path
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



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

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

#merge(other) ⇒ Object

create a new archive by merging this and another archive



45
46
47
48
49
50
51
52
# File 'lib/har/archive.rb', line 45

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



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

def merge!(other)
  assert_archive other
  clear_caches

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

#pagesObject



33
34
35
36
37
# File 'lib/har/archive.rb', line 33

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

#save_to(path) ⇒ Object



64
65
66
# File 'lib/har/archive.rb', line 64

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

#valid?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/har/archive.rb', line 68

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

#validate!Object



72
73
74
75
76
77
78
79
80
81
# File 'lib/har/archive.rb', line 72

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