Skip to content

Instantly share code, notes, and snippets.

@mrchrisadams
Created March 21, 2009 17:58
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 mrchrisadams/38859f947860d4fc45e2 to your computer and use it in GitHub Desktop.
Save mrchrisadams/38859f947860d4fc45e2 to your computer and use it in GitHub Desktop.
require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
module 3rdpartyContentParserHelper
def mock_feed(region)
api_key = 'foo'
xmlfeed = File.read("#{RAILS_ROOT}/spec/fixtures/feeds/3rdparty_places.xml")
url = "http://services.3rdpartys.co.uk/places?userid=#{api_key}&region=#{region}"
3rdpartyContentParser.should_receive(:open).with(url).and_return(xmlfeed)
end
def valid_attributes
{
:title => "Blackmore Farm",
:description => "Come for atmosphere and architecture: the Grade I-listed manor-farmhouse is remarkable. Medieval stone walls, a ceiling open to a beamed roof, ecclesiastical windows, a fire blazing in the Great Hall...",
:photo => "http://www.3rdpartys.co.uk/search/images/335/bbb1775a.jpg",
:region => "somerset"
}
end
end
describe 3rdpartyContentParser, "testing for validated attributes" do
include 3rdpartyContentParserHelper
before(:each) do
@3rdparty = 3rdpartyContentParser.new
end
it "should create a new instance given valid attributes" do
@3rdparty.attributes = valid_attributes
@3rdparty.should be_valid
end
it "should not be valid without a title" do
@3rdparty.attributes = valid_attributes
@3rdparty[:title] = nil
@3rdparty.should_not be_valid
end
end
describe 3rdpartyContentParser, "load_content_from_feed" do
include 3rdpartyContentParserHelper
before(:each) do
region = 'somerset'
mock_feed(region)
@3rdparty = 3rdpartyContentParser.load_content_from_feed(region)
end
it "should populate title" do
@3rdparty.first[:title].should eql("Blackmore Farm")
end
it "should populate description" do
@3rdparty.first[:description].should eql("Come for atmosphere and architecture: the Grade I-listed manor-farmhouse is remarkable. Medieval stone walls, a ceiling open to a beamed roof, ecclesiastical windows, a fire blazing in the Great Hall...")
end
it "should populate photo" do
@3rdparty.first[:photo].should eql("http://www.3rdpartys.co.uk/search/images/335/bbb1775a.jpg")
end
it "should populate region" do
@3rdparty.first[:region].should match(/Somerset/)
end
it "should have a codepro parameter request of somerset"
end
describe 3rdpartyContentParser, "save to content table if new record, update if not" do
include 3rdpartyContentParserHelper
before do
region = 'somerset'
# set 3rdpartyContentParser to fake api calls first
mock_feed(region)
# make api call through faked interface
@3rdparty = 3rdpartyContentParser.load_content_from_feed(region)
end
it "should create a valid content object if no others exist"
it "should populate the content title"
it "should add a photo"
it "should populate the content description"
it "create a content object of type 'hotels' "
end
class 3rdPartyContentParser < ActiveRecord::Base
require 'rubygems'
require 'hpricot'
require 'open-uri'
def self.load_content_from_feed(region, userid = 'foo')
# parse feed
url = "http://services.3rdparty.co.uk/places?userid=foo&region=#{region}"
doc = Hpricot.XML(open(url))
# create empty array to iterate through
3rdpartycontentarray = []
# populate array with stuff from xml
(doc/:place).each do |item|
3rdpartycontentarray.push({
:region => (item/:codepro).inner_html.strip,
:title => (item/:placeName).inner_html.strip,
:description => (item/:teaser).inner_html.strip,
:photo => (item/:URL).inner_html.strip
})
end
# iterate through array, saving each place to the db
3rdpartycontentarray.each do |item|
unless Content.find_by_title(item[:title])
Content.create :title => item[:title],
:description => item[:description],
:photo => item[:photo]
end
end
#finally add request param onto the end, to allow for checking response param was correct
3rdpartycontentarray.push(doc/:requestParams/:where).inner_html.strip
3rdpartycontentarray
end
validates_presence_of :title, :on => :create, :message => "can't be blank"
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment