Last active
July 14, 2020 21:53
-
-
Save Haumer/dc3741d65ba756ed2e7754433f49fd86 to your computer and use it in GitHub Desktop.
This file contains hidden or 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' | |
| # Lets call the github API | |
| url = 'https://api.github.com/users/haumer/gists' | |
| user_serialized = open(url).read | |
| user = JSON.parse(user_serialized) | |
| puts "#{user['name']} - #{user['bio']}" |
This file contains hidden or 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
| Asahi | Pale Lager | Japan | |
|---|---|---|---|
| Guinness | Stout | Ireland | |
| Bintang | Lager | Indonesian |
This file contains hidden or 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 hidden or 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
| <?xml version="1.0" encoding="UTF-8"?> | |
| <beers> | |
| <title>Great beers</title> | |
| <beer> | |
| <name>Edelweiss</name> | |
| <appearance>White</appearance> | |
| <origin>Austria</origin> | |
| </beer> | |
| <beer> | |
| <name>Cuvée des Trolls</name> | |
| <appearance>Blond</appearance> | |
| <origin>Belgium</origin> | |
| </beer> | |
| </beers> |
This file contains hidden or 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' | |
| # PARSING CSV | |
| # TODO - let's read/write data from beers.csv | |
| filepath = 'data/beers.csv' | |
| csv_options = { col_sep: ',', quote_char: '"', headers: :first_row } | |
| CSV.foreach(filepath, csv_options) do |row| | |
| puts "#{row["Name"]} is a #{row["Appearance"]} beer from #{row["Origin"]}" | |
| end | |
| # STORING CSV | |
| csv_options = { col_sep: ',', force_quotes: true, quote_char: '"' } | |
| beers = [ | |
| { | |
| name: 'Asahi', | |
| appearance: 'Pale Lager', | |
| origin: 'Japan' | |
| }, | |
| { | |
| name: 'Millier Lile', | |
| appearance: 'Light Pilsner', | |
| origin: 'USA' | |
| } | |
| ] | |
| CSV.open(filepath, 'wb', csv_options) do |csv| | |
| beers.each do |beer| | |
| csv << [beer[:name], beer[:appearance], beer[:origin]] | |
| end | |
| end |
This file contains hidden or 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' | |
| # PARSE JSON | |
| filepath = 'data/beers.json' | |
| serialized_beers = File.read(filepath) | |
| p serialized_beers.class | |
| beers = JSON.parse(serialized_beers) | |
| selected = beers['beers'].each do |beer| | |
| p beer['origin'] | |
| end | |
| # STORE JSON | |
| beers = { beers: [ | |
| { | |
| name: 'Edelweiss', | |
| appearance: 'White', | |
| origin: 'Austria' | |
| }, | |
| { | |
| name: 'Guinness', | |
| appearance: 'Stout', | |
| origin: 'Ireland' | |
| } | |
| ]} | |
| File.open(filepath, 'wb') do |file| | |
| file.write(JSON.generate(beers)) | |
| end |
This file contains hidden or 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 | |
| ingredient = 'cookie' | |
| url = "https://www.bbcgoodfood.com/search/recipes?query=#{ingredient}" | |
| selector = '.teaser-item__title' | |
| # id would be with #id_name | |
| html_file = open(url).read | |
| html_doc = Nokogiri::HTML(html_file) | |
| html_doc.search(selector).each do |recipe| | |
| p recipe.text.strip | |
| end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment