Skip to content

Instantly share code, notes, and snippets.

@josephwilk
Forked from tobyclemson/zone_finder.rb
Created August 28, 2008 10:59
Show Gist options
  • Save josephwilk/7703 to your computer and use it in GitHub Desktop.
Save josephwilk/7703 to your computer and use it in GitHub Desktop.
require 'open-uri'
require 'fastercsv'
class ZoneFinder
# Creates a ZoneFinder object
def initialize(network_data_path)
@network_data_path = network_data_path
end
# Returns the network data parsed from a csv file as an array of
# FasterCSV rows.
def fetch_network_data
return FasterCSV.parse(
get_data().gsub('"', '\''),
:headers => true
)
end
def get_data
open(@network_data_path).read
end
end
require 'zone_finder'
describe ZoneFinder, "#fetch_network_data" do
before(:each) do
@network_data_handle = mock('network data handle')
@network_data_handle.stub!(:read).and_return("data")
@zone_finder = ZoneFinder.new("test-network-data-path")
@zone_finder.stub!(:open).and_return(@network_data_handle)
end
it "should attempt to open the file located at the network_data_path" do
@zone_finder.should_receive(:open).with("test-network-data-path").and_return(@network_data_handle)
@zone_finder.fetch_network_data
end
it "should read the data from the network data file" do
@network_data_handle.should_receive(:read).and_return("data")
@zone_finder.fetch_network_data
end
it "should replace double quotes with single quotes in the returned csv document" do
@zone_finder.stub!(:get_data).and_return('"test-speak-marks"')
FasterCSV.should_receive(:parse).with("'test-speak-marks'", anything)
@zone_finder.send(:fetch_network_data)
end
it "should parse the returned csv document and return it" do
@network_data_handle.stub!(:read).and_return("csv-data")
FasterCSV.should_receive(:parse).with("csv-data", :headers => true).and_return("csv-data-parsed")
@zone_finder.send(:fetch_network_data).should == "csv-data-parsed"
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment