Skip to content

Commit

Permalink
Create class Article
Browse files Browse the repository at this point in the history
  • Loading branch information
dtan4 committed Dec 31, 2013
1 parent a6aa4d7 commit 4c9a899
Show file tree
Hide file tree
Showing 7 changed files with 4,087 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Guardfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@

guard :rspec do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
end
1 change: 1 addition & 0 deletions lib/slashdot.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require "slashdot/version"
require "slashdot/article"

module Slashdot
# Your code goes here...
Expand Down
26 changes: 26 additions & 0 deletions lib/slashdot/article.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# -*- coding: utf-8 -*-
require "nokogiri"
require "open-uri"
require "cgi"

module Slashdot
class Article
attr_reader :title, :body

def get(url)
doc = Nokogiri::HTML.parse(open(url).read)
@title = get_title(doc)
@body = get_body(doc)
end

private

def get_title(doc)
doc.css("h2.story > span > a").first.text
end

def get_body(doc)
doc.css("div.body > div.p").first.inner_html.strip
end
end
end
2 changes: 2 additions & 0 deletions ruby-slashdot.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,12 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency "nokogiri"
spec.add_development_dependency "bundler", "~> 1.5"
spec.add_development_dependency "rake"
spec.add_development_dependency "rspec"
spec.add_development_dependency "guard"
spec.add_development_dependency "guard-rspec"
spec.add_development_dependency "coveralls"
spec.add_development_dependency "webmock"
end
Loading

0 comments on commit 4c9a899

Please sign in to comment.