Class: Har
- Inherits:
-
Object
- Object
- Har
- Defined in:
- lib/weblicate/har.rb
Overview
put file bundle into a dir append our own hostname onto it
Instance Attribute Summary collapse
-
#dest_domain ⇒ Object
Returns the value of attribute dest_domain.
-
#name ⇒ Object
Returns the value of attribute name.
Instance Method Summary collapse
- #dns ⇒ Object
- #entries ⇒ Object
- #files ⇒ Object
- #get_files ⇒ Object
- #hosts ⇒ Object
- #hosts_file ⇒ Object
-
#initialize(harfile, options = {}) ⇒ Har
constructor
A new instance of Har.
- #save_file(url, contents) ⇒ Object
- #vhosts ⇒ Object
Constructor Details
#initialize(harfile, options = {}) ⇒ Har
Returns a new instance of Har.
13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/weblicate/har.rb', line 13 def initialize(harfile, ={}) @dest_domain = [:dest_domain] || 'localhost' if File.exists? harfile contents = File.read(harfile) else contents = harfile end @name = File.basename(harfile, '.har') @har = JSON.parse(contents) end |
Instance Attribute Details
#dest_domain ⇒ Object
Returns the value of attribute dest_domain.
11 12 13 |
# File 'lib/weblicate/har.rb', line 11 def dest_domain @dest_domain end |
#name ⇒ Object
Returns the value of attribute name.
11 12 13 |
# File 'lib/weblicate/har.rb', line 11 def name @name end |
Instance Method Details
#dns ⇒ Object
89 90 91 92 93 94 95 |
# File 'lib/weblicate/har.rb', line 89 def dns hosts.collect do |host| zone = @dest_domain.sub /^.*?\./, '' name = host + '.' + @dest_domain.sub('.'+zone, '') "slicehost-dns add_cname #{zone} #{name} #{@dest_domain}.\n" end end |
#entries ⇒ Object
33 34 35 |
# File 'lib/weblicate/har.rb', line 33 def entries @har['log']['entries'] end |
#files ⇒ Object
37 38 39 |
# File 'lib/weblicate/har.rb', line 37 def files @har['log']['entries'].collect{|e| e['request']['url']} end |
#get_files ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 |
# File 'lib/weblicate/har.rb', line 41 def get_files # files.each {|file| get_file(file)} entries.each do |entry| if entry['response']['content']['text'] contents = entry['response']['content']['text'] else contents = `curl -s '#{entry['request']['url']}'` end hosts.each { |host| contents.gsub! host, host+'.'+@dest_domain } save_file entry['request']['url'], contents end end |
#hosts ⇒ Object
25 26 27 28 29 30 31 |
# File 'lib/weblicate/har.rb', line 25 def hosts @har['log']['entries'].collect{ |e| e['request']['headers'].select{|h| h['name'] == 'Host' }.first['value'] }.uniq.sort end |
#hosts_file ⇒ Object
83 84 85 86 87 |
# File 'lib/weblicate/har.rb', line 83 def hosts_file hosts.collect do |host| "127.0.0.1 #{host}.#{@dest_domain}\n" end end |
#save_file(url, contents) ⇒ Object
54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/weblicate/har.rb', line 54 def save_file(url, contents) uri = URI.parse(url) dest = File.join("#{@name}-files", uri.host+'.'+@dest_domain, uri.path) dest << 'index.html' if dest[-1,1] == '/' begin FileUtils.mkdir_p File.dirname dest rescue puts "Error! Could not create #{File.dirname dest}" end File.open(dest, "w") { |file| file.write contents } rescue puts "Failed to parse url (#{url})" end |
#vhosts ⇒ Object
68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/weblicate/har.rb', line 68 def vhosts hosts.collect do |host| <<-EOF <VirtualHost *:80> ServerName #{host}.#{@dest_domain} DocumentRoot /var/www/weblicate/#{@name}-files/#{host}.#{@dest_domain} <Directory /var/www/weblicate/#{@name}-files/#{host}.#{@dest_domain}> Order allow,deny Allow from all </Directory> </VirtualHost> EOF end end |