Skip to content

Instantly share code, notes, and snippets.

@shinobcrc
Created June 27, 2016 13:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shinobcrc/8e015efde428fe1bc51abf83b63aa873 to your computer and use it in GitHub Desktop.
Save shinobcrc/8e015efde428fe1bc51abf83b63aa873 to your computer and use it in GitHub Desktop.
#Problem 1
require 'rubygems'
require 'rest-client'
require 'open-uri'
wiki_url = "http://en.wikipedia.org/"
wiki_local_filename = "wiki-page.html"
File.open(wiki_local_filename, "w") do |file|
file.write(RestClient.get(wiki_url))
end
url = "http://ruby.bastardsbook.com/files/fundamentals/hamlet.txt"
puts open(url).readline
#=> THE TRAGEDY OF HAMLET, PRINCE OF DENMARK
url = "http://ruby.bastardsbook.com/files/fundamentals/hamlet.txt"
local_fname = "hamlet.txt"
File.open(local_fname, "w"){|file| file.write(open(url).read)}
File.open(local_fname, "r") do |file|
file.readlines.each_with_index do |line, idx|
puts line if idx % 42 == 41
end
end
DIRNAME = "data-hold"
Dir.glob("#{DIRNAME}/**/*.*").sort_by{|fname| File.size(fname)}.reverse[0..9].each do |fname|
puts "#{fname}\t#{File.size(fname)}"
end
DIRNAME = "data-hold"
hash = Dir.glob("#{DIRNAME}/**/*.*").inject({}) do |hsh, fname|
ext = File.basename(fname).split('.')[-1].to_s.downcase
hsh[ext] ||= [0,0]
hsh[ext][0] += 1
hsh[ext][1] += File.size(fname)
hsh
end
File.open("file-analysis.txt", "w") do |f|
hash.each do |arr|
txt = arr.flatten.join("\t")
f.puts txt
puts txt
end
end
require 'open-uri'
BASE_URL = "https://chart.googleapis.com/chart?cht=p&chs=500x300"
rows = File.open("file-analysis.txt"){|f| f.readlines.map{|p| p.strip.split("\t")} }
headers = rows[0]
[1,2].each do |idx|
labels = []
values = []
rows[1..-1].each do |row|
labels << row[0]
values << row[idx]
end
remote_google_img = URI.encode"#{BASE_URL}&chl=#{labels.join('|')}&chd=t:#{values.join(',')}"
puts remote_google_img
File.open('file-pie-chart.png', 'w'){|f|
f.write(open(remote_google_img))
}
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment