Skip to content

Instantly share code, notes, and snippets.

@RobertoBarros
Created July 12, 2016 14:50
Show Gist options
  • Save RobertoBarros/9e90cee612570512203f5a8e423241e9 to your computer and use it in GitHub Desktop.
Save RobertoBarros/9e90cee612570512203f5a8e423241e9 to your computer and use it in GitHub Desktop.
Parsing and Storing
require 'csv'
file = 'data/alunos.csv'
options = {headers: true, col_sep: ',', encoding: 'utf-8', force_quotes: true}
CSV.open(file, 'w', options) do |csv|
csv << ['Nome', 'Email', 'Hobby']
csv << ['Roger,', 'rogerpscott@gmail','coding']
csv << ['Lincoln', 'antoniolincoln@live.com','eletrônica']
csv << ['Adriano', 'wiermann@broou.com','surf']
end
# CSV.foreach(file, options) do |row|
# puts "#{row['Name']} #{row['Appearance']} #{row['Origin']}"
# end
require 'json'
file = 'data/alunos.json'
data = {
title: 'Alunos do Le Wagon',
alunos: [
{
nome: 'Roger',
email: 'rogerpscott@gmail',
hobby: 'coding'
},
{
nome: 'Lincoln',
email: 'antoniolincoln@live.com',
hobby: 'eletrônica',
bairro: 'Jardim climax'
}
]
}
File.open(file, 'w') do |file|
file.write(JSON.generate(data))
end
# data_serialized = File.read(file)
# data = JSON.parse(data_serialized)
# puts data['title']
# data['beers'].each do |beer|
# puts "Nome: #{beer['name']} - Aparência: #{beer['appearance']} - Origem: #{beer['origin']}"
# end
require 'json'
require 'open-uri'
puts "Diga um artista:"
artist = gets.chomp
api_url = URI.escape("http://ws.audioscrobbler.com/2.0/?method=artist.gettopalbums&artist=#{artist}&api_key=<YOUR_KEY>&format=json")
serialized_data = open(api_url).read
albums = JSON.parse(serialized_data)['topalbums']['album']
puts "----- Principais Álbuns de #{artist} --------"
position = 1
albums.first(5).each do |album|
puts "#{position} - #{album['name']}"
position += 1
end
require 'open-uri'
require 'nokogiri'
def print_info(url_product)
html = Nokogiri::HTML(open(url_product))
title = html.search('h1').text
price_int = html.search('.product-price .int').first
if price_int.nil?
puts "#{title} - Produto Esgotado"
else
price = "#{ price_int.text}#{ html.search('.product-price .dec').first.text}"
puts "#{title} - R$ #{price}"
end
end
url_category = 'https://www.walmart.com.br/categoria/telefonia/smartphones/?fq=C:4833/4835/4845/&fq=B:146&PS=20&mm=100'
html = Nokogiri::HTML(open(url_category))
html.search('.product-link').each do |link|
url_product = "https://www.walmart.com.br#{link['href']}"
print_info(url_product)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment