Skip to content

Instantly share code, notes, and snippets.

@janester
Created June 27, 2013 18:25
Show Gist options
  • Save janester/5879020 to your computer and use it in GitHub Desktop.
Save janester/5879020 to your computer and use it in GitHub Desktop.
Parsing Exercise for WDI2-SF: you will not be able to run the file unless you have all the gems listed at the top installed!
require "pry"
require "pry-debugger"
require "soundcloud"
require "nokogiri"
require "open-uri"
#1. Parse through the wdi2 hash and make a function that returns a hash where the
#keys are each persons name and the value is their favorite movies
wdi2 = {:instructors => [{:name => "jane", :gender => "female", :favorite_movies => ["the dark knight rises", "the dark knight", "batman begins"], :favorite_foods =>["tacos", "tamales", "enchiladas"]}, {:name => "sumeet", :gender => "male", :favorite_movies => ["x-men", "x2", "x3: the last stand", "x-men: first class"], :favorite_foods=>["pizza", "pasta", "cheese"]}, {:name => "dwayne", :gender => "male", :favorite_movies=>["harry potter 1", "harry potter 2", "harry potter 3", "harry potter 4"]}], :students => [{:name => "tom", :favorite_movie => "the hunger games"},{:name => "christina", :favorite_movie=>"my fair lady"},{:name=>"baylee", :favorite_movie=>"mary poppins"}, {:name =>"jason", :favorite_movie =>"the sound of music"}, {:name => "zara", :favorite_movie => "the princess diaries"}], :date => "6/27/13"}
binding.pry
#2. Write a function that returns a hash where the keys are the contestants
#and the value is their district name/specialty and their first specialty
the_hunger_games_contestants = [{:name => "c1", :district => {:name => "1", :specialty => "luxury goods", :population => "1,000,000"}, :rating => "11", :specialty => ["bow and arrow", "hand to hand combat", "sword throwing"]}, {:name => "c2", :district => {:name => "2", :specialty => "peacekeepers", :population => "1,687,098"}, :rating => "3", :specialty => ["sword throwing", "bow and arrow", "hand to hand combat"]}, {:name => "c3", :district => {:name => "3", :specialty => "electronics", :population => "239,908"}, :rating => "7", :specialty => ["hand to hand combat", "sword throwing", "bow and arrow"]}, {:name => "c4", :district => {:name => "4", :specialty => "fishing", :population => "987,123"}, :rating => "9", :specialty => ["trident throwing","hand to hand combat", "sword throwing", "bow and arrow"]}, {:name => "c5", :district => {:name => "5", :specialty => "power", :population => "123,987,324"}, :rating => "4", :specialty => ["hand to hand combat", "sword throwing", "bow and arrow"]}, {:name => "c6", :district => {:name => "6", :specialty => "transportation", :population => "724,345"}, :rating => "2", :specialty => ["knife throwing", "hand to hand combat", "sword throwing", "bow and arrow"]}, {:name => "c7", :district => {:name => "7", :specialty => "lumber", :population => "724,345"}, :rating => "8", :specialty => ["axe handling", "hand to hand combat", "sword throwing", "bow and arrow"]}, {:name => "c8", :district => {:name => "8", :specialty => "textiles", :population => "724,345"}, :rating => "9", :specialty => ["survival skills", "hand to hand combat", "sword throwing", "bow and arrow"]}, {:name => "c9", :district => {:name => "9", :specialty => "grain", :population => "2,309,832"}, :rating => "1", :specialty => ["survival skills", "bow and arrow"]}, {:name => "c10", :district => {:name => "10", :specialty => "livestock", :population => "2,309,832"}, :rating => "9", :specialty => ["knife throwing", "bow and arrow"]}, {:name => "c11", :district => {:name => "11", :specialty => "agriculture", :population => "2,309,832"}, :rating => "3", :specialty => ["survival skills", "bow and arrow"]}, {:name => "Katniss Everdeen", :district => {:name => "12", :specialty => "mining", :population => "324,789"}, :rating => "12", :specialty => ["bow and arrow"]}]
binding.pry
#3.
##a. Go to http://developers.soundcloud.com/ and get an api key (if you don't
##already have one)
##b. Install the soundcloud gem
##c. Write a method that returns an array of hashes with just the title of the track, duration (bonus: convert it from milliseconds to a real time format), the artwork_url, and the permalink
client = Soundcloud.new(:client_id => '<your key here>')
tracks = client.get('/tracks', :q => 'sea dramas', :licence => 'cc-by-sa')
binding.pry
#4(bonus). Write a method that Scrapes Hacker News (https://news.ycombinator.com/)
#and returns a hash of link titles paired with their actual url
#5(bonus). Alter the scraping method above to go to the next page of links and
#scrape that one and add those new links to the hash
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment