Created
April 9, 2019 09:56
-
-
Save marcoranieri/f3da8dfd14762a3378ce01c5700dd8c5 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 'json' | |
require 'open-uri' | |
puts "Username please" | |
username = gets.chomp | |
# TODO - Let's fetch name and bio from a given GitHub username | |
url = "https://api.github.com/users/#{username}" | |
user_serialized = open(url).read | |
user = JSON.parse(user_serialized) | |
puts "#{user["name"]} as #{user["public_repos"]}" |
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 'csv' | |
# TODO - let's read/write data from beers.csv | |
filepath = 'data/beers.csv' | |
csv_options = { col_sep: ',', quote_char: '"', headers: :first_row } | |
origins = [] | |
CSV.foreach(filepath, csv_options) do |row| | |
origins << row["Origin"] | |
end | |
p origins.uniq.sort |
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 'json' | |
# TODO - let's read/write data from beers.json | |
filepath = 'data/beers.json' | |
serialized_beers = File.read(filepath) | |
json_beers = JSON.parse(serialized_beers) | |
p json_beers["beers"][1]["name"] |
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 'open-uri' | |
require 'nokogiri' | |
ingredient = 'chocolate' | |
url = "http://www.letscookfrench.com/recipes/find-recipe.aspx?s=#{ingredient}" | |
html_file = open(url).read | |
html_doc = Nokogiri::HTML(html_file) | |
html_doc.search('.m_titre_resultat a').each do |element| | |
# puts element | |
puts element.text.strip | |
puts element.attribute('href').value | |
puts "===============================================" | |
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 'open-uri' | |
require 'nokogiri' | |
# Let's scrape recipes from http://www.epicurious.com |
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 'csv' | |
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' } | |
filepath = 'data/new_beers.csv' | |
beers = [ | |
{ name: 'Asahi', appereance: 'Pale Lager', origin: 'Japan'}, | |
{ name: 'Guinness', appereance: 'Stout', origin: 'Ireland'}, | |
{ name: 'Cuvée des Trolls', appereance: 'Stout', origin: 'Ireland'}, | |
] | |
CSV.open(filepath, 'wb', csv_options) do |csv| | |
csv << ['Name', 'Appearance', 'Origin'] | |
beers.each do |beer| | |
csv << [beer[:name], beer[:appereance], beer[:origin]] | |
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 'json' | |
beers = [ beers: [ | |
{ | |
name: 'Edelweiss', | |
appearance: 'White', | |
origin: 'Austria' | |
}, | |
{ | |
name: 'Guinness', | |
appearance: 'Stout', | |
origin: 'Ireland' | |
} | |
]] | |
filepath = "data/new_beers.json" | |
File.open(filepath, 'wb') do |file| | |
file.write(JSON.generate(beers)) | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment