Permalink
Browse files

Merge pull request #1 from mathias/develop

Sorry for the delay. Merged.
  • Loading branch information...
2 parents 3b97aa0 + 47ab996 commit f1f52b82348625276b3bea2605178c48882561c2 @devn devn committed Nov 29, 2011
Showing with 100 additions and 63 deletions.
  1. +1 −1 .rvmrc
  2. +1 −0 Gemfile
  3. +2 −0 Gemfile.lock
  4. +4 −3 README.markdown
  5. +6 −50 bin/awesome_digest
  6. +4 −1 lib/credentials.rb.template
  7. +46 −0 lib/parse.rb
  8. +23 −0 lib/send_mail.rb
  9. +0 −7 spec/jeweler_with_rspec_bundler_spec.rb
  10. +0 −1 spec/spec_helper.rb
  11. +6 −0 views/email.haml
  12. +7 −0 views/link.haml
View
2 .rvmrc
@@ -1 +1 @@
-rvm use 1.9.2@awesome_digest
+rvm use 1.9.2-p136@awesome_digest
View
@@ -3,6 +3,7 @@ source "http://rubygems.org"
# gem 'cronedit'
gem 'nokogiri'
gem 'pony'
+gem 'haml'
group :development do
gem 'bundler'
View
@@ -5,6 +5,7 @@ GEM
archive-tar-minitar (0.5.2)
columnize (0.3.2)
diff-lcs (1.1.2)
+ haml (3.1.2)
i18n (0.5.0)
linecache19 (0.5.11)
ruby_core_source (>= 0.1.4)
@@ -46,6 +47,7 @@ PLATFORMS
DEPENDENCIES
bundler
+ haml
mocha
nokogiri
pony
View
@@ -7,15 +7,16 @@ It's opinionated and uses sendgrid at the moment, but you could change
that up if you want.
If you wanna wire things up fill in the relevant constants in
-bin/awesome_digest.rb and run it. Simple.
+lib/credentials.rb and run it. Simple.
----------
## Todo ##
-* Split out the relevant portions into lib
+* Split out the relevant portions into lib (extracted two methods)
* Read in a YAML config file for the settings
-* Allow some flexibility with the layout
+* Complete the transition to using HAML
+ * Make the html_digest render take a collection of scraped_links rather than just passing it rendered li_links
* Setup a simple way to cron this from a user's perspective (rake task)
* Gemify
* Release
View
@@ -7,61 +7,17 @@ require 'nokogiri'
require 'open-uri'
require 'pony'
require 'ruby-debug'
+require 'haml'
# awesome_digest requires
require 'array_ext'
require 'credentials'
-# require 'awesome_digest'
+require 'parse'
+require 'send_mail'
url = "#{HN_URL}/active"
-doc = Nokogiri::HTML(open(url))
+document = Nokogiri::HTML(open(url))
-zipped_elements = doc.css("tr td.title[3]").zip(doc.css("tr td.subtext"))
+digests = parse_feed(document)
-scraped_posts = []
-
-# Parses the subtext under the news article for:
-# "1 point(s)", "username", "5", "minute(s), hours, etc.",
-# "1 comment(s) (or "discuss" if there are no comments)"
-subtext_regex = /\A(?<points>\d+ points?)\Wby\W(?<poster>\w+)\W(?<num_of>\d+)\W(?<time_type>minutes?|hours?|days?|weeks?|months?|years?)\Wago\W+\|\W(?<comments>\d+ comments?|discuss)/
-
-zipped_elements.each do |e|
- m = subtext_regex.match(e.second.text)
-
- if m[:time_type] =~ /minutes?|hours?/
- post_map = {}
- post_map[:full_link] = e.first.xpath("a").to_html
- post_map[:text_link] = e.first.at_xpath("a[@href]").values.first
- post_map[:time_ago] = "#{m[:num_of]} #{m[:time_type]} ago"
- post_map[:points] = m[:points]
- post_map[:comments] = m[:comments] # if m[:comments] != "discuss"
- post_map[:poster] = m[:poster]
- scraped_posts << post_map
- end
-end
-
-li_links = scraped_posts.map {|p| "<li>#{p[:full_link]}<br />#{p[:text_link]}<br />by #{p[:poster]}, #{p[:points]}</li>"}.join("<br />")
-
-text_digest = scraped_posts.map {|p| p[:text_link] + '\r\n'}
-
-html_digest = "<html><body><h2>#{EMAIL_HEADER}</h2><ul>#{li_links}</ul></body></html>"
-
-# Extract to sendgrid_setup
-server_settings = {
- address: SMTP_SERVER,
- port: SERVER_PORT,
- user_name: EMAIL_USERNAME,
- password: EMAIL_PASSWORD,
- authentication: SERVER_AUTH,
- domain: SERVER_DOMAIN }
-
-
-if Pony.mail(to: EMAIL_TO,
- from: EMAIL_FROM,
- via: :smtp,
- via_options: server_settings,
- subject: "#{SUBJECT_PREFIX} - #{Time.now.strftime('%m-%d-%Y')}",
- html_body: html_digest,
- body: text_digest)
- puts "Digest sent..."
-end
+send_mail(digests)
@@ -1,6 +1,10 @@
# NEWS CONFIG
HN_URL ='http://news.ycombinator.com' # no trailing slash
+# DIGEST CONFIG
+SEND_EMPTY = true
+EMPTY_MESSAGE = "There were no posts. Submit stories here: #{HN_URL}/submit"
+
# SERVER CONFIG
SMTP_SERVER ='smtp.example.com'
SERVER_PORT ='25'
@@ -16,4 +20,3 @@ EMAIL_HEADER ='Awesome Heading'
# SENDGRID CONFIG
EMAIL_USERNAME='username'
EMAIL_PASSWORD='password'
-
View
@@ -0,0 +1,46 @@
+def parse_feed(doc)
+ zipped_elements = doc.css("tr td.title[3]").zip(doc.css("tr td.subtext"))
+
+ scraped_posts = []
+
+ # Parses the subtext under the news article for:
+ # "1 point(s)", "username", "5", "minute(s), hours, etc.",
+ # "1 comment(s) (or "discuss" if there are no comments)"
+ subtext_regex = /\A(?<points>\d+ points?)\Wby\W(?<poster>\w+)\W(?<num_of>\d+)\W(?<time_type>minutes?|hours?|days?|weeks?|months?|years?)\Wago\W+\|\W(?<comments>\d+ comments?|discuss)/
+
+ zipped_elements.each do |e|
+ m = subtext_regex.match(e.second.text)
+
+ if m[:time_type] =~ /minutes?|hours?/
+ post_map = {}
+ post_map[:full_link] = e.first.xpath("a").to_html
+ post_map[:text_link] = e.first.at_xpath("a[@href]").values.first
+ post_map[:time_ago] = "#{m[:num_of]} #{m[:time_type]} ago"
+ post_map[:points] = m[:points]
+ post_map[:comments] = m[:comments] # if m[:comments] != "discuss"
+ post_map[:poster] = m[:poster]
+ scraped_posts << post_map
+ end
+ end
+
+ if scraped_posts.present?
+ li_links = scraped_posts.map { |post|
+ Haml::Engine.new(File.read('views/link.haml')).render(Object.new, {:post => post})
+ #"<li>#{p[:full_link]}<br />#{p[:text_link]}<br />by #{p[:poster]}, #{p[:points]}</li>"
+ }.join("<br />")
+
+ text_digest = scraped_posts.map {|p| p[:text_link] + '\r\n'}
+
+ html_digest = Haml::Engine.new(File.read('views/email.haml')).render(Object.new, {:li_links => li_links})
+ # "<html><body><h2>#{EMAIL_HEADER}</h2><ul>#{li_links}</ul></body></html>"
+
+ return { text_digest: text_digest,
+ html_digest: html_digest }
+ elsif SEND_EMPTY
+ return { text_digest: EMPTY_MESSAGE,
+ html_digest: Haml::Engine.new(File.read('views/email.haml')).render(Object.new, {:li_links => EMPTY_MESSAGE }) }
+ else
+ puts "No new posts to send!"
+ Kernel.exit
+ end
+end
View
@@ -0,0 +1,23 @@
+def send_mail(digests)
+ text_digest = digests[:text_digest]
+ html_digest = digests[:html_digest]
+
+ # Extract to sendgrid_setup
+ server_settings = { address: SMTP_SERVER,
+ port: SERVER_PORT,
+ user_name: EMAIL_USERNAME,
+ password: EMAIL_PASSWORD,
+ authentication: SERVER_AUTH,
+ domain: SERVER_DOMAIN }
+
+
+ if Pony.mail(to: EMAIL_TO,
+ from: EMAIL_FROM,
+ via: :smtp,
+ via_options: server_settings,
+ subject: "#{SUBJECT_PREFIX} - #{Time.now.strftime('%m-%d-%Y')}",
+ html_body: html_digest,
+ body: text_digest)
+ puts "Digest sent..."
+ end
+end
@@ -1,7 +0,0 @@
-require File.expand_path(File.dirname(__FILE__) + '/spec_helper')
-
-describe "JewelerWithRspecBundler" do
- it "fails" do
- fail "hey buddy, you should probably rename this file and start specing for real"
- end
-end
View
@@ -1,7 +1,6 @@
$LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
$LOAD_PATH.unshift(File.dirname(__FILE__))
require 'rspec'
-require 'jeweler_with_rspec_bundler'
# Requires supporting files with custom matchers and macros, etc,
# in ./support/ and its subdirectories.
View
@@ -0,0 +1,6 @@
+%html
+ %body
+ %h2
+ = EMAIL_HEADER
+ %ul
+ = li_links
View
@@ -0,0 +1,7 @@
+%li
+ = post[:full_link]
+ %br
+ %a{:href => post[:text_link]}
+ = post[:text_link]
+ %br
+ = "by #{post[:poster]}, #{post[:points]}"

0 comments on commit f1f52b8

Please sign in to comment.