secret
anonymous / cia_world_factbook.rb
Created

  • Download Gist
cia_world_factbook.rb
Ruby
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
require 'nokogiri'
 
class CIAFacts
def initialize(xml_file = 'cia-1996.xml')
@doc = Nokogiri::XML(File.open(xml_file))
end
def country_with_most_people
node = @doc.xpath("//country/attribute::population").max_by { |x| x.text.to_i }
[node.parent['name'], node.text]
end
def five_highest_inflation_rates
nodes = @doc.xpath("//country/attribute::inflation").
sort_by { |node| -node.text.to_i }.take(5)
nodes.inject([]) { |acc, node| acc << [node.parent['name'], node.text] }
end
def continents_list
nodes = @doc.xpath("//country/attribute::continent")
hash = Hash.new { |hash, key| hash[key] = [] }
nodes.each { |node| hash[node.text] << node.parent['name'] }
hash
end
end
cia_world_factbook_spec.rb
Ruby
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
require 'cia_world_factbook'
 
describe "CIAFacts" do
before do
@cia = CIAFacts.new
end
it "should get country with most people" do
actual = @cia.country_with_most_people
actual.should == ['China', '1210004956']
end
it "should get five highest inflation rates" do
actual = @cia.five_highest_inflation_rates
actual.length.should == 5
actual[0].should == ['Belarus', '244']
actual[1].should == ['Turkey', '94']
actual[2].should == ['Azerbaijan', '85']
actual[3].should == ['Malawi', '83.3']
actual[4].should == ['Yemen', '71.3']
end
it "should get continents and countries" do
actual = @cia.continents_list
actual.keys.length.should == 6
actual.values.flatten.count.should == 260
actual.keys.sort.should == ['Africa', 'Asia', 'Australia/Oceania', 'Europe', 'North America', 'South America']
actual['Africa'][3].should == 'Benin'
actual['Asia'][3].should == 'Azerbaijan'
actual['Australia/Oceania'][5].should == 'Fiji'
actual['Europe'][2].should == 'Austria'
end
end
report.rb
Ruby
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 44
require 'cia_world_factbook'
 
def print_report(cia)
puts "CIA WORLD FACTBOOK 1996".center(80, '-')
data = cia.country_with_most_people
puts wrap("The country with most people is #{data.first} and " +
"it's population is #{data.last}", 75, 2)
data = cia.five_highest_inflation_rates
puts wrap("The five highest inflation rates are:", 75, 2)
data.each do |datum|
puts wrap("#{datum.first} with an infletion of #{datum.last}", 75, 4)
end
data = cia.continents_list
puts wrap("Continents and countries:", 75, 2)
data.sort.each do |continent, countries|
puts wrap("#{continent}:", 75, 4)
puts wrap(countries.sort.join(", "), 75, 6)
end
puts "#{'-' * 80}"
end
 
def wrap(str, width=80, spaces=0)
lines, line = [], ""
str.split(/\s+/).each do |word|
if line.size + word.size >= width
lines << "#{' ' * spaces}#{line}"
line = word
elsif line.empty?
line = word
else
line << ' ' << word
end
end
lines << "#{' ' * spaces}#{line}" if line
lines.join("\n")
end
 
print_report(CIAFacts.new)

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.