Class: HttpArchive::Archive

Inherits:
Object
  • Object
show all
Defined in:
lib/http_archive/archive.rb

Overview

The main object to interact with. All accessible objects (see below) hold further information about the archive.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(src) ⇒ Archive

Creates a new Archive object with given input.

Parameters:

  • src (String, File)

    must be object type String or File

Raises:

  • (ArgumentError, JSON::ParserError)


29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/http_archive/archive.rb', line 29

def initialize(src)
  unless src.is_a?(File) || src.is_a?(String)
    puts 'Argument must be String or File!'
    raise ArgumentError
  end
  src = src.read if src.is_a?(File)
  begin
    @content = JSON.parse(src)
  rescue
    puts "The input could not be parsed."
    raise JSON::ParserError
  end
  extract_creator
  extract_browser
  extract_pages
  extract_entries
end

Instance Attribute Details

#browserBrowser (readonly)

Returns the ‘Browser’ object with data of the browser

Returns:



15
16
17
# File 'lib/http_archive/archive.rb', line 15

def browser
  @browser
end

#content=(value) ⇒ Hash (writeonly)

For internal use, holds the parsed JSON-data

Returns:

  • (Hash)


24
25
26
# File 'lib/http_archive/archive.rb', line 24

def content=(value)
  @content = value
end

#creatorCreator (readonly)

Returns the ‘Creator’ object with data of the creator of the archive (Firebug etc.)

Returns:



12
13
14
# File 'lib/http_archive/archive.rb', line 12

def creator
  @creator
end

#entriesArray<Entry> (readonly)

Returns all browser interactions with the page as an array of ‘Entry’ objects

Returns:



21
22
23
# File 'lib/http_archive/archive.rb', line 21

def entries
  @entries
end

#pagesArray<Page> (readonly)

Returns general information about the page interaction as an array of ‘Page’ objects, normally just one entry

Returns:



18
19
20
# File 'lib/http_archive/archive.rb', line 18

def pages
  @pages
end

Instance Method Details

#get_row_dataArray<Array<html_method, ressource_name, status_name, status_code, ressource_size, load_duration>>

Gets the data for a row for all entries. Convenience method that can be used for bulk reading of entry data.

Examples:

Example of returned Array

[["GET", "/prototype.js?ver=1.6.1", "200", "OK", "139.85", "1.06"], ... ]

Returns:

  • (Array<Array<html_method, ressource_name, status_name, status_code, ressource_size, load_duration>>)

    An array with row data



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/http_archive/archive.rb', line 83

def get_row_data
  rows = []
  @entries.each do |entry|
    method = entry.request.http_method
    # get part after .com/ if any
    url = entry.request.url
    if url.end_with?("/")
      ressource = entry.request.url
    else
      r = url.rindex("/")
      ressource = url[r..-1]
    end
    # first 30 characters of the ressource name
    ressource = ressource[0, 30]
    status = entry.response.status.to_s
    code = entry.response.status_text
    size = (entry.response.content['size'] / 1000.0).round(2).to_s
    duration = (entry.time / 1000.0).to_s

    rows << [method, ressource, status, code, size, duration]
  end
  rows
end

#get_total_dataArray<page_title, ressource_count, total_download_size, overall_load_time>

Gets the common data for a page. Convenience method that can be used for bulk reading of page data.

Examples:

Example of returned Array

["Software is hard", "26", "0.36", "6.745"]

Returns:

  • (Array<page_title, ressource_count, total_download_size, overall_load_time>)

    An array with page data



70
71
72
73
74
75
# File 'lib/http_archive/archive.rb', line 70

def get_total_data
  size = calc_total_size.to_s
  load_time = (@pages.first.on_load / 1000.0).to_s

  [@pages.first.title, @entries.size.to_s, size, load_time]
end

Prints a table representation of the Http Archive to STDOUT. An example is given on the README-file. Columns are like the “Network”-view of Firebug or Chrome Dev tools.

Returns:

  • (String)

    A string table representation of the archive data



53
54
55
56
57
58
59
60
61
62
# File 'lib/http_archive/archive.rb', line 53

def print_table
  puts

  data = get_total_data
  puts("Metrics for: " +"'" + data[0] + "', " + data[1] + " Requests, " + data[2] + "MB downloaded. "  + "Load time: " + data[3] + "s")

  puts
  print_rows
  puts
end