spec/weather_services/yahoo_spec.rb in barometer-0.8.0 vs spec/weather_services/yahoo_spec.rb in barometer-0.9.0

- old
+ new

@@ -1,143 +1,60 @@ -require File.expand_path(File.dirname(__FILE__) + '/../spec_helper') -include Barometer +require_relative '../spec_helper' -describe Barometer::WeatherService::Yahoo, :vcr => { - :cassette_name => "WeatherService::Yahoo" +describe Barometer::WeatherService::Yahoo, vcr: { + cassette_name: "WeatherService::Yahoo" } do - before(:each) do - @accepted_formats = [:zipcode, :weather_id, :woe_id] - end - describe "the class methods" do - it "defines accepted_formats" do - WeatherService::Yahoo._accepted_formats.should == @accepted_formats - end - - it "defines source_name" do - WeatherService::Yahoo._source_name.should == :yahoo - end - - it "defines get_all" do - WeatherService::Yahoo.respond_to?("_fetch").should be_true - end + it "auto-registers this weather service as :yahoo" do + Barometer::WeatherService.source(:yahoo).should == Barometer::WeatherService::Yahoo end - describe "building the current data" do - it "defines the build method" do - WeatherService::Yahoo.respond_to?("_build_current").should be_true - end + describe ".call" do + let(:converted_query) { Barometer::ConvertedQuery.new('90210', :zipcode, :metric) } + let(:query) { build_query.tap{|q|q.stub(:convert! => converted_query)} } - it "requires Hash input" do - lambda { WeatherService::Yahoo._build_current }.should raise_error(ArgumentError) - lambda { WeatherService::Yahoo._build_current({}) }.should_not raise_error(ArgumentError) - end + subject { Barometer::WeatherService::Yahoo.call(query) } - it "returns Barometer::CurrentMeasurement object" do - current = WeatherService::Yahoo._build_current({}) - current.is_a?(Measurement::Result).should be_true + it "asks the query to convert to accepted formats" do + query.should_receive(:convert!).with(:zipcode, :weather_id, :woe_id) + subject end - end - describe "building the forecast data" do - it "defines the build method" do - WeatherService::Yahoo.respond_to?("_build_forecast").should be_true - end + it "includes the expected data" do + subject.query.should == '90210' + subject.format.should == :zipcode + subject.should be_metric - it "requires Hash input" do - lambda { WeatherService::Yahoo._build_forecast }.should raise_error(ArgumentError) - lambda { WeatherService::Yahoo._build_forecast({}) }.should_not raise_error(ArgumentError) - end + should have_data(:current, :observed_at).as_format(:time) + should have_data(:current, :stale_at).as_format(:time) - it "returns Array object" do - current = WeatherService::Yahoo._build_forecast({}) - current.is_a?(Array).should be_true - end - end + should have_data(:current, :humidity).as_format(:float) + should have_data(:current, :condition).as_format(:string) + should have_data(:current, :icon).as_format(:number) + should have_data(:current, :temperature).as_format(:temperature) + should have_data(:current, :wind_chill).as_format(:temperature) + should have_data(:current, :wind).as_format(:vector) + should have_data(:current, :pressure).as_format(:pressure) + should have_data(:current, :visibility).as_format(:distance) + should have_data(:current, :sun, :rise).as_format(:time) + should have_data(:current, :sun, :set).as_format(:time) - describe "building the location data" do - it "defines the build method" do - WeatherService::Yahoo.respond_to?("_build_location").should be_true - end + should have_data(:location, :city).as_value("Beverly Hills") + should have_data(:location, :state_code).as_value("CA") + should have_data(:location, :country_code).as_value("US") + should have_data(:location, :latitude).as_value(34.08) + should have_data(:location, :longitude).as_value(-118.4) - it "requires Hash input" do - lambda { WeatherService::Yahoo._build_location }.should raise_error(ArgumentError) - lambda { WeatherService::Yahoo._build_location({}) }.should_not raise_error(ArgumentError) - end + should have_data(:timezone, :to_s).as_format(/^P[DS]T$/i) - it "requires Barometer::Geo input" do - geo = Data::Geo.new({}) - lambda { WeatherService::Yahoo._build_location({}, {}) }.should raise_error(ArgumentError) - lambda { WeatherService::Yahoo._build_location({}, geo) }.should_not raise_error(ArgumentError) - end - - it "returns Barometer::Location object" do - location = WeatherService::Yahoo._build_location({}) - location.is_a?(Data::Location).should be_true - end - end - - describe "when measuring" do - before(:each) do - @query = Barometer::Query.new("90210") - @measurement = Barometer::Measurement.new - end - - describe "all" do - it "responds to _measure" do - WeatherService::Yahoo.respond_to?("_measure").should be_true - end - - it "requires a Barometer::Measurement object" do - lambda { WeatherService::Yahoo._measure(nil, @query) }.should raise_error(ArgumentError) - lambda { WeatherService::Yahoo._measure("invlaid", @query) }.should raise_error(ArgumentError) - - lambda { WeatherService::Yahoo._measure(@measurement, @query) }.should_not raise_error(ArgumentError) - end - - it "requires a Barometer::Query query" do - lambda { WeatherService::Yahoo._measure }.should raise_error(ArgumentError) - lambda { WeatherService::Yahoo._measure(@measurement, 1) }.should raise_error(ArgumentError) - - lambda { WeatherService::Yahoo._measure(@measurement, @query) }.should_not raise_error(ArgumentError) - end - - it "returns a Barometer::Measurement object" do - result = WeatherService::Yahoo._measure(@measurement, @query) - result.is_a?(Barometer::Measurement).should be_true - result.current.is_a?(Measurement::Result).should be_true - result.forecast.is_a?(Array).should be_true - end - end - end - - describe "overall data correctness" do - before(:each) do - @query = Barometer::Query.new("90210") - @measurement = Barometer::Measurement.new - end - - it "should correctly build the data" do - result = WeatherService::Yahoo._measure(@measurement, @query) - - # build current - @measurement.current.sun.rise.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) - @measurement.current.sun.set.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) - - # builds location - @measurement.location.city.should == "Beverly Hills" - - # builds forecasts - @measurement.forecast.size.should == 2 - - @measurement.forecast[0].condition.should match(/^[\w ]+$/i) - @measurement.forecast[0].icon.to_s.should match(/^\d{1,3}$/i) - @measurement.forecast[0].sun.rise.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) - @measurement.forecast[0].sun.set.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) - - @measurement.forecast[1].condition.should match(/^[\w ]+$/i) - @measurement.forecast[1].icon.to_s.should match(/^\d{1,3}$/i) - @measurement.forecast[1].sun.rise.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) - @measurement.forecast[1].sun.set.to_s.should match(/^\d{1,2}:\d{1,2}[ ]?[apmAPM]{0,2}$/i) + subject.forecast.size.should == 5 + should have_forecast(:starts_at).as_format(:time) + should have_forecast(:ends_at).as_format(:time) + should have_forecast(:icon).as_format(:number) + should have_forecast(:condition).as_format(:string) + should have_forecast(:high).as_format(:temperature) + should have_forecast(:low).as_format(:temperature) + should have_forecast(:sun, :rise).as_format(:time) + should have_forecast(:sun, :set).as_format(:time) end end end