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.



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

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

Instance Attribute Details

#uriObject (readonly)

Returns the value of attribute uri.



54
55
56
# File 'lib/har/archive.rb', line 54

def uri
  @uri
end

Class Method Details

.add_schema(path) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def self.add_schema(path)
  data = JSON.parse(File.read(path))
  id = data.fetch('id')

  schemas[id] = data
end

.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

.schemasObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.



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

def self.schemas
  @schemas ||= {}
end

Instance Method Details

#entriesObject



71
72
73
# File 'lib/har/archive.rb', line 71

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

#merge(other) ⇒ Object

create a new archive by merging this and another archive



77
78
79
80
81
82
83
84
# File 'lib/har/archive.rb', line 77

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



88
89
90
91
92
93
94
# File 'lib/har/archive.rb', line 88

def merge!(other)
  assert_archive other
  clear_caches

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

#pagesObject



65
66
67
68
69
# File 'lib/har/archive.rb', line 65

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

#save_to(path) ⇒ Object



96
97
98
# File 'lib/har/archive.rb', line 96

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

#valid?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'lib/har/archive.rb', line 100

def valid?
  Jschematic.validate @data, log_type_schema, :debug => true, :context => self.class.schemas.values
end

#validate!Object



104
105
106
107
108
109
110
111
112
# File 'lib/har/archive.rb', line 104

def validate!
  Jschematic.validate! @data, log_type_schema, :debug => true, :context => self.class.schemas.values
  nil
rescue Jschematic::ValidationError => ex
  msg = ex.message
  msg = "#{@uri}: #{msg}" if @uri

  raise ValidationError, msg
end

#viewObject



61
62
63
# File 'lib/har/archive.rb', line 61

def view
  Viewer.new([self]).show
end