-
-
Save anonymous/58145a95a016bf9d089b to your computer and use it in GitHub Desktop.
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 '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 |
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 '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 |
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 '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) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment