Created
October 15, 2019 10:13
-
-
Save marcoranieri/0de182e0f7ca8a6a4bab8d74e201da17 to your computer and use it in GitHub Desktop.
parsing&storing
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 "Give username:" | |
print "> " | |
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 # is a STRING ( serualized! ) | |
# p user_serialized | |
user = JSON.parse(user_serialized) # parsed in to HASH | |
puts "#{user["name"]} has #{user["public_repos"]} 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
Name | Appearance | Origin | |
---|---|---|---|
Edelweiss | White | Austria | |
Cuvée des Trolls | Blond | Belgium | |
Choulette Ambrée | Amber | France | |
Gulden Draak | Dark | Belgium |
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
{ | |
"title": "Great beers", | |
"beers": [ | |
{ | |
"name": "Edelweiss", | |
"appearance": "White", | |
"origin": "Austria" | |
}, | |
{ | |
"name": "Cuvée des Trolls", | |
"appearance": "Blond", | |
"origin": "Belgium" | |
}, | |
{ | |
"name": "Choulette Ambrée", | |
"appearance": "Amber", | |
"origin": "France" | |
}, | |
{ | |
"name": "Gulden Draak", | |
"appearance": "Dark", | |
"origin": "Belgium" | |
} | |
] | |
} |
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' | |
filepath = 'data/beers.csv' | |
csv_options = { | |
col_sep: ',', | |
quote_char: '"', | |
headers: :first_row | |
} | |
CSV.foreach(filepath, csv_options) do |beer| | |
p beer # beer is a HASH (if 'headers: :first_row' option!) | |
end | |
# Create an ARRAY of unique ORIGINs | |
origins = [] | |
CSV.foreach(filepath, csv_options) do |beer| | |
origins << beer["Origin"] # HASH | |
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 'csv' | |
csv_options = { | |
col_sep: ',', | |
force_quotes: true, | |
quote_char: '"' | |
} | |
filepath = 'data/new_beers.csv' | |
beers = [ | |
{name: 'Asahi', appearance: 'Pale Lager', origin: 'japan'}, | |
{name: 'Guinness', appearance: 'Stout', origin: 'Ireland'} | |
] | |
CSV.open(filepath, 'wb', csv_options) do |csv| | |
csv << ['Name', 'Appearance', 'Origin'] # HEADER | |
# BEERS is an ARRAY | |
beers.each do |beer| # beer is a HASH | |
csv << [ beer[:name], beer[:appearance], beer[:origin] ] # we want to append ARRAY into CSV | |
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' | |
filepath = 'data/beers.json' | |
serialized_beers = File.read(filepath) # is a STRING ( serialized! ) | |
json_file = JSON.parse(serialized_beers) # is a HASH | |
# HASH | ARRAY | HASH | |
p json_file["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 # is a STRING ( serialized! ) | |
# p html_file | |
html_doc = Nokogiri::HTML(html_file) # NOKOGIRI node elements | |
# p html_doc | |
# .search is a Nokogiri methods to search nodes by CSS Selector | |
html_doc.search('.m_titre_resultat a').each do |element| | |
puts element.text | |
puts element.attribute('href').value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment