class Spider::VisitRecorder
Public Class Methods
activate!()
click to toggle source
# File lib/recorder.rb, line 27 def activate! @@active = true end
config(dir)
click to toggle source
# File lib/recorder.rb, line 43 def config(dir) @@directory = dir end
deactivate!()
click to toggle source
# File lib/recorder.rb, line 35 def deactivate! @@active = false end
pause!()
click to toggle source
# File lib/recorder.rb, line 39 def pause! @@recording = false end
recall(*args) { |*args| ... }
click to toggle source
# File lib/recorder.rb, line 47 def recall(*args) if @@active url = args.first.to_s data = '' store = locate_file(url) if store.size == 0 raise "Unexpected request: #{url}" unless @@recording data = yield(*args) if block_given? begin store.write(package(url, data)) rescue StandardError => e puts e.message puts "On file #{store.path}" end else data = unpackage(store, url) end return data elsif block_given? yield(*args) end end
record!()
click to toggle source
# File lib/recorder.rb, line 31 def record! @@recording = true end
Private Class Methods
locate_file(url)
click to toggle source
# File lib/recorder.rb, line 78 def locate_file(url) key = Digest::MD5.hexdigest(url) path = File.expand_path(key, @@directory) fsize = File.size?(path) (fsize.nil? || fsize.zero? ? File.open(path, 'w') : File.open(path, 'r')) end
package(url, data)
click to toggle source
# File lib/recorder.rb, line 85 def package(url, data) payload = {} payload[:url] = url.encode('UTF-8') payload[:data] = Base64.encode64(data) unless data.http_status.nil? payload[:response] = data.http_status end unless data.http_headers.nil? payload[:headers] = Base64.encode64(data.http_headers) end payload.to_yaml end
unpackage(store, url)
click to toggle source
# File lib/recorder.rb, line 101 def unpackage(store, url) raw = YAML.load(store.read) raise 'URL mismatch in recording' unless raw[:url] == url data = Base64.decode64(raw[:data]) data.http_headers = Base64.decode64(raw[:headers]) data.http_status = raw[:response] data end