Last active
August 29, 2015 14:02
-
-
Save jcdavison/78697e5897ab9b63454f to your computer and use it in GitHub Desktop.
Get your daily dose of food...
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
| # email everyone in the group a list of things we might like to read ? | |
| # steps required to accomplish this, ie, pseudocode | |
| # 1. indentify site to visit # once there decide to access specific | |
| # 2. obtain content in html format of that url | |
| # 3. figure out which part of the html to get (nokogiri) | |
| # 4. Extract that data we want - | |
| # 5. formatting the data in a way that we want (emailing the data) | |
| # 6. email the links | |
| # make sure to run 'gem install new_gem_name' | |
| # for any gem not already installed on your system | |
| # run 'gem list' to see all the gems on your system | |
| require 'open-uri' | |
| require 'nokogiri' | |
| require 'gmail' | |
| def get_page(url) | |
| Nokogiri::HTML(open(url)) | |
| end | |
| def extract_data(page) | |
| articles = [] | |
| page.css('.entry-title a').each do |article| | |
| articles.push({:title => article.text, :href => article["href"]}) | |
| end | |
| articles | |
| end | |
| def generate_message(articles) | |
| message = "" | |
| articles.each do |article| | |
| message << "<a href='#{article[:href]}'>#{article[:title]}</a> </br>" | |
| end | |
| message | |
| end | |
| def send_mail(recipient, message, subject) | |
| return unless recipient | |
| gmail = Gmail.connect(ENV['GMAIL_EMAIL'], ENV['GMAIL_PWD']) | |
| gmail.deliver do | |
| to recipient | |
| subject subject | |
| html_part do | |
| body message | |
| end | |
| end | |
| end | |
| if __FILE__ == $0 | |
| page = get_page("http://sfist.com/tags/burritos") | |
| articles = extract_data(page) | |
| message = generate_message(articles) | |
| subject = "The Daily Dose of Food" | |
| email = nil #replace this with the actual recipient | |
| send_mail(email, message, subject) | |
| end | |
| # original code that we wrote during class | |
| # .each_with_object isn't such a common method | |
| # def find_links(url) | |
| # page = Nokogiri::HTML(open(url)) | |
| # page.css('.entry-title a').each_with_object([]) {|element, object| object.push(element)} | |
| # end | |
| # def extract_data(links) | |
| # extracted_hrefs = links.each_with_object([]) do |link, object| | |
| # object.push({:title => link.text, :href => link["href"]}) | |
| # end | |
| # end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment