Created
July 16, 2019 10:48
-
-
Save marcoranieri/ac80c4e8852772995d6593041b5fd239 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' | |
# Documentation - GitHub API | |
# https://developer.github.com/v3/ | |
url = 'https://api.github.com/users/ssaunier' # endpoint of Github API | |
# We open & read what is returning the API call | |
user_serialized = open(url).read # is a STRING | |
user = JSON.parse(user_serialized) # is a HASH ( JSON.parse(...) ) | |
# _HASH_ _HASH_ | |
puts "#{user['name']} - #{user['bio']}" |
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 = 'beers.csv' | |
# headers: :first_row => transform each row from _ARRAY_ to _HASH_ | |
csv_options = { col_sep: ',', quote_char: '"', headers: :first_row } | |
CSV.foreach(filepath, csv_options) do |row| # ROW is a _HASH_ | |
puts "#{row['Name']}, a #{row['Appearance']} beer from #{row['Origin']}" | |
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 'csv' | |
filepath = 'data/new_beers.csv' | |
csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' } | |
beers = [ | |
{name: "Asahi", appereance: "Lager", origin: "Japan"}, | |
{name: "Guinness", appereance: "Stout", origin: "Ireland"} | |
] | |
CSV.open(filepath, 'wb', csv_options) do |csv| # "WB" => overwrite the entire file | |
csv << ['Name', 'Appearance', 'Origin'] # HEADER | |
beers.each do |beer| # For each beer (HASH) we want to create an Array and inject into CSV | |
csv << [beer[:name], beer[:appereance], beer[:origin]] # We can use symbol cause we are inside Ruby | |
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 | |
beers = JSON.parse(serialized_beers) # is a HASH | |
# _HASH__ARRAY_HASH_ | |
p 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 'json' | |
filepath = "data/new_beers.json" | |
beers = { beers: [ | |
{ | |
name: 'Edelweiss', | |
appearance: 'White', | |
origin: 'Austria' | |
}, | |
{ | |
name: 'Guinness', | |
appearance: 'Stout', | |
origin: 'Ireland' | |
} | |
]} | |
File.open(filepath, 'wb') do |file| # |file| => it's the entire file and we want to write in it | |
file.write(JSON.generate(beers)) # JSON.generate will return a serialize obj (STRING) | |
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' # OPEN a Web page | |
require 'nokogiri' # help us to query the HTML nodes | |
ingredient = 'chocolate' | |
url = "http://www.letscookfrench.com/recipes/find-recipe.aspx?s=#{ingredient}" | |
html_file = open(url).read # serialize the website (STRING) | |
html_doc = Nokogiri::HTML(html_file) # for each node (h1 - p - div etc) create a Nokogiri Obj | |
# .search is a Nokogiri method that allow us to search, inside HTML, for nodes using CSS Selector | |
# PARENT CHILD | |
html_doc.search('.m_titre_resultat a').each do |element| | |
puts element.text.strip | |
puts element.attribute('href').value | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment