Class: Har

Inherits:
Object
  • Object
show all
Defined in:
lib/weblicate/har.rb

Overview

put file bundle into a dir append our own hostname onto it

Instance Attribute Summary collapse

Instance Method Summary collapse

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, options={})
  @dest_domain = options[: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_domainObject

Returns the value of attribute dest_domain.



11
12
13
# File 'lib/weblicate/har.rb', line 11

def dest_domain
  @dest_domain
end

#nameObject

Returns the value of attribute name.



11
12
13
# File 'lib/weblicate/har.rb', line 11

def name
  @name
end

Instance Method Details

#dnsObject



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

#entriesObject



33
34
35
# File 'lib/weblicate/har.rb', line 33

def entries
  @har['log']['entries']
end

#filesObject



37
38
39
# File 'lib/weblicate/har.rb', line 37

def files
  @har['log']['entries'].collect{|e| e['request']['url']}
end

#get_filesObject



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

#hostsObject



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_fileObject



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

#vhostsObject



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