Skip to content

Instantly share code, notes, and snippets.

@jsmestad
Created October 4, 2009 06:46
Show Gist options
  • Save jsmestad/201206 to your computer and use it in GitHub Desktop.
Save jsmestad/201206 to your computer and use it in GitHub Desktop.
require File.dirname(__FILE__) + '/../spec_helper'
# FYI this is using StaleFish, but tests should run regardless.
describe TvRage do
context '.full_schedule' do
it 'should call required functions' do
TvRage.should_receive(:fetch_xml).once
TvRage.full_schedule
end
end
context '.fetch' do
it 'should not require parameters' do
lambda { TvRage.send(:fetch_xml, 'http://services.tvrage.com/feeds/fullschedule.php') }.should_not raise_error(ArgumentError)
end
it 'should return xml document' do
TvRage.send(:fetch_xml,
'http://services.tvrage.com/feeds/fullschedule.php',
'country' => 'US',
'24_format' => '1').should be_a_kind_of(Hpricot::Doc)
end
end
end
class TvRage
def self.full_schedule(country='US')
doc = self.fetch_xml('http://services.tvrage.com/feeds/fullschedule.php', 'country' => country, '24_format' => '1')
p "full_schedule - #{doc.class}"
#doc.search('//show').each {|show| process_show(show) }
end
protected
def self.fetch_xml(url, params={})
uri = url
unless params.empty?
uri << '?' + params.to_a.collect { |param| param.join('=') }.join('&')
end
doc = Hpricot::XML(open(uri))
p "fetch_xml - #{doc.class}"
return doc
end
def self.process_show(show_node)
end
end
Output:
fetch_xml - Hpricot::Doc
full_schedule - NilClass
Any idea why I cannot get fetch_xml to return anything but nil?
but if I do the doc.search in the fetch_xml function, it works flawlessly.
require 'hpricot'
require 'open-uri'
class TvRage
class << self
attr_accessor :doc
end
def self.full_schedule(country='US')
fetch_xml('http://services.tvrage.com/feeds/fullschedule.php', 'country' => country, '24_format' => '1')
doc.search('//show').each {|show| process_show(show) }
end
protected
def self.fetch_xml(uri, params={})
unless params.empty?
uri << '?' + params.to_a.collect { |param| param.join('=') }.join('&')
end
self.doc = open(uri) { |f| Hpricot::XML(f) }
end
def self.process_show(show_node)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment