Skip to content

Instantly share code, notes, and snippets.

@marcoranieri
Created January 21, 2020 15:37
Show Gist options
  • Save marcoranieri/93d022af41de86a4d8cf2ded8adceeb9 to your computer and use it in GitHub Desktop.
Save marcoranieri/93d022af41de86a4d8cf2ded8adceeb9 to your computer and use it in GitHub Desktop.
Parsing&Storing
require 'json'
require 'open-uri'
puts "Username?"
input = gets.chomp
# TODO - Let's fetch name and bio from a given GitHub username
url = "https://api.github.com/users/#{input}"
user_serialized = open(url).read # String
user = JSON.parse(user_serialized) # Hash
# p user
puts "#{user["name"]} has #{user["public_repos"]} repositories"
require 'csv'
# Adding { headers: :first_row } option will convert your csv ROW from Array to Hash
# Important: it will work only if your CSV first row is a HEADER ( meta-data )
# beers.csv
# "Name";"Appearance";"Origin"
# "Edelweiss";"White";"Austria"
# "Cuvée des Trolls";"Blond";"Belgium"
# "Choulette Ambrée";"Amber";"France"
# "Gulden Draak";"Dark";"Belgium"
csv_options = { col_sep: ';', quote_char: '"', headers: :first_row }
filepath = 'data/beers.csv'
CSV.foreach(filepath, csv_options) do |row| # row is a HASH
puts "#{row["Name"]} ( #{row["Appearance"]} ) from #{row["Origin"]}"
end
require 'json'
# TODO - let's read/write data from beers.json
filepath = 'data/beers.json'
serialized_beers = File.read(filepath) # String
json_beers = JSON.parse(serialized_beers) # Hash
# Hash > Array>Hash>String
puts json_beers["beers"][1]["name"]
# data/beers.json
# {
# "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"
# }
# ]
# }
require 'open-uri'
require 'nokogiri'
# Let's scrape recipes from https://www.bbcgoodfood.com
# url = "https://www.bbcgoodfood.com/search/recipes?query=#{ingredient}"
url = "https://www.bbcgoodfood.com/search/recipes?query=chocolate"
html_file = open(url).read
html_doc = Nokogiri::HTML(html_file)
html_doc.search(".teaser-item__title a").each do |element|
# p element
puts element.text
puts element.attribute('href').value
puts "-------"
end
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| # 'wb' == override!
csv << ['Name', 'Appearance', 'Origin'] # First we insert the custom HEADER
beers.each do |beer| # then we iterate our "database" to insert each element
csv << [beer[:name], beer[:appearance], beer[:origin]]
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment