Permalink
Browse files

Initial commit

  • Loading branch information...
jarib committed Jan 9, 2011
0 parents commit 8c2d166846ab15158723997453c9c69693290db8
@@ -0,0 +1,4 @@
+pkg/*
+*.gem
+.bundle
+Gemfile.lock
1 .rspec
@@ -0,0 +1 @@
+--color
@@ -0,0 +1,4 @@
+source "http://rubygems.org"
+
+# Specify your gem's dependencies in har.gemspec
+gemspec
20 LICENSE
@@ -0,0 +1,20 @@
+Copyright (c) 2010 Jari Bakken
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,36 @@
+HAR
+===
+
+Ruby library to work with HTTP archives. Very young. Not very useful yet.
+
+Usage
+=====
+
+ HAR::Archive.from_string(json) #=> #<Har::Archive:0x5e7cac>
+ HAR::Archive.from_file(json) #=> #<Har::Archive:0x5e7cac>
+
+Links
+=====
+
+* http://code.google.com/p/harviewer/
+
+Todo
+====
+
+Validate against HAR schema instead of simply raising {Index,Key}Error.
+
+Note on Patches/Pull Requests
+=============================
+
+* Fork the project.
+* Make your feature addition or bug fix.
+* Add tests for it. This is important so I don't break it in a
+ future version unintentionally.
+* Commit, do not mess with rakefile, version, or history.
+ (if you want to have your own version, that is fine but bump version in a commit by itself I can ignore when I pull)
+* Send me a pull request. Bonus points for topic branches.
+
+Copyright
+=========
+
+Copyright (c) 2010 Jari Bakken. See LICENSE for details.
@@ -0,0 +1,5 @@
+require 'bundler'
+Bundler::GemHelper.install_tasks
+
+require 'rspec/core/rake_task'
+RSpec::Core::RakeTask.new
@@ -0,0 +1,24 @@
+# -*- encoding: utf-8 -*-
+$:.push File.expand_path("../lib", __FILE__)
+require "har/version"
+
+Gem::Specification.new do |s|
+ s.name = "har"
+ s.version = HAR::VERSION
+ s.platform = Gem::Platform::RUBY
+ s.authors = ["Jari Bakken"]
+ s.email = ["jari.bakken@gmail.com"]
+ s.homepage = ""
+ s.summary = %q{Ruby library to work with HTTP archives}
+ s.description = %q{Ruby library to work with HTTP archives}
+
+ s.rubyforge_project = "har"
+
+ s.add_dependency "json"
+ s.add_development_dependency "rspec", "~> 2.0"
+
+ s.files = `git ls-files`.split("\n")
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
+ s.require_paths = ["lib"]
+end
@@ -0,0 +1,11 @@
+module HAR
+ # Your code goes here...
+end
+
+require 'har/version'
+require 'har/entry'
+require 'har/page'
+require 'har/archive'
+
+require 'json'
+require 'time'
@@ -0,0 +1,51 @@
+module HAR
+ class Archive
+ def self.from_string(str)
+ new JSON.parse(str)
+ end
+
+ def self.from_file(path)
+ from_string File.read(path)
+ end
+
+ def initialize(hash)
+ @data = hash.fetch 'log'
+ end
+
+ def pages
+ @data.fetch('pages').map do |page|
+ Page.new page, entries_for(page['id'])
+ end
+ end
+
+ def entries
+ raw_entries.map { |e| Entry.new(e) }
+ end
+
+ # create a new archive by merging this and another archive
+
+ def merge(other)
+ raise NotImplementedError
+ end
+
+ # destructively merge this with the given archive
+
+ def merge!(other)
+ raise NotImplementedError
+ end
+
+ private
+
+ def entries_for(page_id)
+ raw_entries.map { |e|
+ Entry.new(e) if e['pageref'] == page_id
+ }.compact
+ end
+
+ def raw_entries
+ @data.fetch('entries')
+ end
+
+ end # Archive
+end # HAR
+
@@ -0,0 +1,76 @@
+module HAR
+ class Entry
+ class Entry
+ def initialize(data)
+ @data = data
+ end
+
+ def start_time
+ Time.parse @data.fetch('startedDateTime')
+ end
+
+ def time
+ @data.fetch 'time'
+ end
+
+ def method
+ request.fetch 'method'
+ end
+
+ def url
+ request.fetch 'url'
+ end
+
+ def request_headers_size
+ request.fetch 'headersSize'
+ end
+
+ def request_body_size
+ request.fetch 'bodySize'
+ end
+
+ def status
+ response.fetch 'status'
+ end
+
+ def status_text
+ response.fetch 'statusText'
+ end
+
+ def response_headers_size
+ response.fetch 'headersSize'
+ end
+
+ def response_body_size
+ response.fetch 'bodySize'
+ end
+
+ def mime_type
+ content.fetch 'mimeType'
+ end
+
+ def content_size
+ content.fetch 'size'
+ end
+
+ private
+
+ def request
+ @data.fetch 'request'
+ end
+
+ def response
+ @data.fetch 'response'
+ end
+
+ def timings
+ @data.fetch 'timings'
+ end
+
+ def content
+ response.fetch('content')
+ end
+ end
+
+ end # Entry
+end #
@@ -0,0 +1,32 @@
+module HAR
+ class Page
+ attr_reader :entries
+
+ def initialize(data, entries)
+ @data = data
+ @entries = entries
+ end
+
+ def start_time
+ Time.parse @data.fetch('startedDateTime')
+ end
+
+ def title
+ @data.fetch 'title'
+ end
+
+ def on_content_load
+ timings.fetch 'onContentLoad'
+ end
+
+ def on_load
+ timings.fetch 'onLoad'
+ end
+
+ private
+
+ def timings
+ @data.fetch('pageTimings')
+ end
+ end
+end
@@ -0,0 +1,3 @@
+module HAR
+ VERSION = "0.0.1"
+end
Oops, something went wrong.

0 comments on commit 8c2d166

Please sign in to comment.