Skip to content

Instantly share code, notes, and snippets.

@andypearson
Created July 30, 2009 21:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andypearson/158954 to your computer and use it in GitHub Desktop.
Save andypearson/158954 to your computer and use it in GitHub Desktop.
# 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