Created
July 30, 2009 21:46
-
-
Save andypearson/158954 to your computer and use it in GitHub Desktop.
This file contains 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
# Service model | |
require 'nokogiri' | |
require 'open-uri' | |
class Service < ActiveRecord::Base | |
def self.download | |
items = [] | |
elements.each do |element| | |
items << element | |
end | |
items | |
end | |
def self.elements | |
document.css('item') | |
end | |
def self.document | |
Nokogiri::XML(open('http://twitter.com/statuses/user_timeline/andypearson.rss')) | |
end | |
end | |
# Service spec | |
require File.dirname(__FILE__) + '/../spec_helper' | |
describe Service do | |
describe 'self.download' do | |
it 'should respond to the download method' do | |
Service.should respond_to(:download) | |
end | |
it 'should return an array of items' do | |
Service.download.should be_an_instance_of(Array) | |
end | |
it 'should return an array of 3 items if there are 3 item elements in the document' do | |
Service.stub!(:document).and_return(Nokogiri::HTML('<item>1</item><item>2</item><item>3</item>')) | |
Service.download.length.should == 3 | |
end | |
end | |
describe 'self.document' do | |
before do | |
Service.stub!(:open).and_return('<item>1</item>') | |
end | |
it 'should respond to the document method' do | |
Service.should respond_to(:document) | |
end | |
it 'should return an instance of Nokogiri::XML::Document' do | |
Service.document.should be_an_instance_of(Nokogiri::XML::Document) | |
end | |
end | |
describe 'self.elements' do | |
before do | |
Service.stub!(:document).and_return(Nokogiri::HTML('<item>1</item>')) | |
end | |
it 'should respond to the elements method' do | |
Service.should respond_to(:elements) | |
end | |
it 'should return an instance of Nokogiri::XML::NodeSet' do | |
Service.elements.should be_an_instance_of(Nokogiri::XML::NodeSet) | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment