Created
March 15, 2013 20:06
-
-
Save rogerleite/5172741 to your computer and use it in GitHub Desktop.
Tech talk HTTP Monkey
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "avc" | |
require "fileutils" | |
Donkey = HttpMonkey.build do | |
storage AVC::HttpClient::FileStore.new(File.expand_path("~/.avc/donkey")) | |
middlewares.use AVC::HttpClient::M::CacheByEtag | |
end | |
puts "# Smeagol Assistant\n" | |
Commands = {} | |
Commands[:quit] = nil | |
Commands[:help] = Proc.new do |_| | |
puts "[Smeagol] I obey these commands:\n#{Commands.keys.join("\n")}" | |
end | |
Commands[:get] = Proc.new do |url| | |
puts "[GET] #{url}" | |
response = Donkey.at(url).get | |
puts "#{response.code} #{response.headers.inspect}" | |
end | |
Commands[:lscache] = Proc.new do |_| | |
puts "[CACHE] List of files:" | |
Dir[File.expand_path("~/.avc/donkey/*")].each do |file| | |
puts file.inspect | |
end | |
end | |
Commands[:rmcache] = Proc.new do |filter_expr| | |
Dir[File.expand_path("~/.avc/donkey/*")].each do |file| | |
next unless file.match(/#{filter_expr}/) | |
FileUtils.rm_f(file) | |
puts "[CACHE] File #{file} removed!" | |
end | |
end | |
trap_error = Proc.new do |&block| | |
begin | |
block.call | |
rescue StandardError => ex | |
puts "[ERROR] #{ex.message}" | |
end | |
end | |
smeagol_console = Proc.new do | |
loop do | |
puts "[Smeagol] What you wish master?" | |
commands = gets.chomp.split(" ", 2) | |
cmd = commands.first | |
next if cmd.nil? || cmd.empty? | |
cmd = cmd.to_sym | |
break if cmd == :quit || cmd == :exit | |
if Commands.key?(cmd) | |
trap_error.call { Commands[cmd].call(commands[1]) } | |
else | |
puts "[Smeagol] Don't know this command, my master." | |
Commands[:help].call | |
end | |
end | |
end | |
trap_error.call do | |
Commands[:get].call("http://schemas.api.abril.com.br/") | |
Commands[:get].call("http://schemas.api.abril.com.br/") | |
end | |
smeagol_console.call | |
puts "[Smeagol] Good bye! My master." |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment