Skip to content

Commit

Permalink
Take arguments in initializer
Browse files Browse the repository at this point in the history
  • Loading branch information
dtan4 committed Jan 1, 2014
1 parent 23747d2 commit 9fe606c
Show file tree
Hide file tree
Showing 2 changed files with 79 additions and 0 deletions.
10 changes: 10 additions & 0 deletions lib/slashdot/article.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@ module Slashdot
class Article
attr_reader :id, :title, :author, :postdate, :department, :url, :body

def initialize(args = {})
@id = args[:id] || 0
@title = args[:title] || ""
@author = args[:author] || ""
@postdate = args[:postdate] || nil
@department = args[:department] || ""
@url = args[:url] || ""
@body = args[:body] || ""
end

def get(url)
doc = Nokogiri::HTML.parse(open(url).read)
@id = get_id(doc)
Expand Down
69 changes: 69 additions & 0 deletions spec/slashdot/article_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,75 @@ module Slashdot
describe Article do
ARTICLE_URL = "http://slashdot.org/story/13/12/31/1437227"

describe "#initialize" do
context "with no argument" do
let(:article) { Slashdot::Article.new }

it "should set 0 to id" do
expect(article.id).to eq 0
end

it "should set empty to title" do
expect(article.title).to eq ""
end

it "should set empty to author" do
expect(article.author).to eq ""
end

it "should set nil to postdate" do
expect(article.postdate).to be_nil
end

it "should set empty to department" do
expect(article.department).to eq ""
end

it "should set empty to url" do
expect(article.url).to eq ""
end

it "should set empty to body" do
expect(article.body).to eq ""
end
end

context "with arguments" do
let(:article) do
Slashdot::Article.new(id: 123456, title: "title", author: "author",
postdate: Time.parse("2014/01/01"),
department: "department", url: "url",
body: "body")
end

it "should set given value to id" do
expect(article.id).to eq 123456
end

it "should set given value to title" do
expect(article.title).to eq "title"
end

it "should set given value to author" do
expect(article.author).to eq "author"
end

it "should set given value to postdate" do
expect(article.postdate).to eq Time.parse("2014/01/01")
end

it "should set given value to department" do
expect(article.department).to eq "department"
end

it "should set given value to url" do
expect(article.url).to eq "url"
end

it "should set given value to body" do
expect(article.body).to eq "body"
end
end
end

describe "#get" do
Expand Down

0 comments on commit 9fe606c

Please sign in to comment.