-
Notifications
You must be signed in to change notification settings - Fork 1
/
caching.rb
43 lines (39 loc) · 1020 Bytes
/
caching.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#!/usr/bin/env ruby
require 'mechanize'
require 'net/http'
require 'uri'
require 'vcr'
require 'fakeweb'
require 'digest/md5'
VCR.configure { |c|
c.cassette_library_dir = 'tmp/'
c.hook_into :fakeweb
c.allow_http_connections_when_no_cassette = true
}
class Caching
def initialize
@agent = Mechanize.new
@agent.user_agent = 'Mozilla/5.0 (Macintosh; Intel Mac OS X 10_7_4) AppleWebKit/536.11 (KHTML, like Gecko) Chrome/20.0.1132.43 Safari/536.11'
@agent.open_timeout = 300
@agent.read_timeout = 300
@agent.html_parser = Nokogiri::HTML
@agent.ssl_version = 'SSLv3'
@agent.keep_alive = false
@agent.idle_timeout = 300
url = ["http://google.com",
"http://yahoo.com",
"http://msnbc.com"]
url.each do |u|
run u
end
end
def run url
digest = Digest::MD5.hexdigest(url)
puts "Hash: #{digest}"
VCR.use_cassette("#{digest}") do
page = @agent.get(url)
puts page.body
end
end
end
Caching.new