Skip to content

Commit

Permalink
Fixes for 1.8.6 + various cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
jarib committed Jan 9, 2011
1 parent 059dcc5 commit 66ab835
Show file tree
Hide file tree
Showing 7 changed files with 78 additions and 35 deletions.
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1 +1 @@
--color --backtrace
--color
17 changes: 17 additions & 0 deletions lib/har.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,3 +13,20 @@ class ValidationError < StandardError; end
require 'har/archive'
require 'har/viewer'

module Enumerable
def group_by
assoc = {}

each do |element|
key = yield(element)

if assoc.has_key?(key)
assoc[key] << element
else
assoc[key] = [element]
end
end

assoc
end unless [].respond_to?(:group_by)
end
3 changes: 3 additions & 0 deletions lib/har/page.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,8 @@ def title
@data.fetch 'title'
end

# a little sugar
alias_method :timings, :page_timings

end
end
34 changes: 18 additions & 16 deletions lib/har/schema_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,41 @@
module HAR
class SchemaType
include Serializable

class << self
private

def load_from(file)
schema = JSON.parse(File.read(file))

klass = Class.new(self) do
schema['properties'].each do |name, definition|
define_property name, definition
end
end

HAR.const_set type_name_from(schema['id']), klass
end

def define_property(name, definition)
required = definition['required']
type = definition['type']

wrapper = extract_wrapper(name, definition)
fetch_code = required ? "@data.fetch #{name.inspect}" : "@data[#{name.inspect}]"

fetch_code = required ? "@data.fetch(#{name.inspect})" : "@data[#{name.inspect}]"
fetch_code = "#{wrapper} #{fetch_code}" if wrapper

class_eval "
def #{snake_case name}
@#{name} ||= #{fetch_code}
end
"
end

def extract_wrapper(name, definition)
type = definition['type']

case type
when 'object'
return "#{name.capitalize}.new"
Expand All @@ -53,21 +53,23 @@ def extract_wrapper(name, definition)

"#{type_name_from(ref)}.new" if ref
end

def type_name_from(str)
t = str[/^(.+)Type/, 1] or raise Error, "could not extract type name from #{str.inspect}"
t.capitalize
t[0] = t[0].chr.upcase

t
end

def snake_case(str)
str.gsub(/\B[A-Z][^A-Z]/, '_\&').downcase.gsub(' ', '_')
end
end

def initialize(data)
@data = data
end

# load in schemas
Dir[File.expand_path("../schemas/*", __FILE__)].each do |file|
load_from file
Expand Down
34 changes: 17 additions & 17 deletions spec/har/archive_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,18 @@ module HAR
end

it "can be created from a file" do
Archive.from_file(har_path("browser-blocking-time.har")).should be_kind_of(Archive)
Archive.from_file(har_path("browser-blocking-time")).should be_kind_of(Archive)
end

it "saves the archive URI if created from a file" do
ar = Archive.from_file(har_path("browser-blocking-time.har"))
ar = Archive.from_file(har_path("browser-blocking-time"))
ar.uri.should_not be_nil
ar.uri.should include("browser-blocking-time.har")
ar.uri.should include("browser-blocking-time")
end
end

context "fetching data" do
let(:archive) { Archive.from_file har_path("browser-blocking-time.har") }
let(:archive) { Archive.from_file har_path("browser-blocking-time") }

it "has a list of pages" do
ps = archive.pages
Expand All @@ -34,24 +34,24 @@ module HAR

context "comparing" do
it "knows when two archives are equal" do
a = Archive.from_file har_path("browser-blocking-time.har")
b = Archive.from_file har_path("browser-blocking-time.har")
a = Archive.from_file har_path("browser-blocking-time")
b = Archive.from_file har_path("browser-blocking-time")

a.should == b
end
end

context "merging" do
it "raises a TypeError if the argument is not an Archive" do
a = Archive.from_file har_path("browser-blocking-time.har")
a = Archive.from_file har_path("browser-blocking-time")

lambda { a.merge(1) }.should raise_error(TypeError)
lambda { a.merge!(1) }.should raise_error(TypeError)
end

it "merges two archives, returning a new archive" do
a = Archive.from_file har_path("browser-blocking-time.har")
b = Archive.from_file har_path("google.com.har")
a = Archive.from_file har_path("browser-blocking-time")
b = Archive.from_file har_path("google.com")

c = a.merge b

Expand All @@ -66,9 +66,9 @@ module HAR
end

it "merges one archive into the other" do
ref = Archive.from_file har_path("browser-blocking-time.har")
a = Archive.from_file har_path("browser-blocking-time.har")
b = Archive.from_file har_path("google.com.har")
ref = Archive.from_file har_path("browser-blocking-time")
a = Archive.from_file har_path("browser-blocking-time")
b = Archive.from_file har_path("google.com")

# caching..
a.pages
Expand All @@ -81,20 +81,20 @@ module HAR
end

it "adds a comment to pages about their origin" do
a = Archive.from_file har_path("browser-blocking-time.har")
b = Archive.from_file har_path("google.com.har")
a = Archive.from_file har_path("browser-blocking-time")
b = Archive.from_file har_path("google.com")


c = a.merge(b)

c.pages.last.comment.should include("google.com.har")
c.pages.last.comment.should include("google.com")
b.pages.last.comment.should be_nil
end
end

context "validating" do
let(:valid) { Archive.from_file har_path("browser-blocking-time.har") }
let(:invalid) { Archive.from_file har_path("bad.har") }
let(:valid) { Archive.from_file har_path("browser-blocking-time") }
let(:invalid) { Archive.from_file har_path("bad") }

it "returns true if the archive is valid" do
valid.should be_valid
Expand Down
17 changes: 17 additions & 0 deletions spec/har/page_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,22 @@
module HAR
describe Page do

context "data" do
let(:page) { Archive.from_file(google_path).pages.first }

it "has entries" do
page.entries.size.should == 5
page.entries.each { |e| e.pageref.should == page.id }
end

it "has a title" do
page.title.should == 'Google'
end

it "has a PageTimings instance" do
page.timings.should be_kind_of(PageTimings)
end
end

end # Page
end # HAR
6 changes: 5 additions & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def fixture_path(name)
end

def har_path(name)
fixture_path File.join("hars", name)
fixture_path File.join("hars", "#{name}.har")
end

def json(path)
Expand All @@ -19,6 +19,10 @@ def all_hars
Dir[fixture_path("hars/*.har")]
end

def google_path
har_path "google.com"
end

def good_hars
all_hars.reject { |e| e =~ /bad/ }
end
Expand Down

0 comments on commit 66ab835

Please sign in to comment.