From f56698caff608527b9f3c2c3dd4c18306589cb3b Mon Sep 17 00:00:00 2001 From: Erik Michaels-Ober Date: Tue, 24 Sep 2013 13:33:30 +0200 Subject: [PATCH] Missing key raises now KeyError, not ArgumentError --- lib/twitter/creator.rb | 10 +++------- lib/twitter/identity.rb | 2 +- spec/twitter/geo_creator_spec.rb | 4 ++-- spec/twitter/identifiable_spec.rb | 4 ++-- spec/twitter/media_creator_spec.rb | 4 ++-- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/lib/twitter/creator.rb b/lib/twitter/creator.rb index e8275a206..f7568f1d6 100644 --- a/lib/twitter/creator.rb +++ b/lib/twitter/creator.rb @@ -2,13 +2,9 @@ module Twitter class Creator def self.new(method, klass, attrs={}) - type = attrs.delete(method.to_sym) - if type - const_name = type.gsub(/\/(.?)/){"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/){$1.upcase} - klass.const_get(const_name.to_sym).new(attrs) - else - raise ArgumentError, "argument must have :#{method} key" - end + type = attrs.fetch(method.to_sym) + const_name = type.gsub(/\/(.?)/){"::#{$1.upcase}"}.gsub(/(?:^|_)(.)/){$1.upcase} + klass.const_get(const_name.to_sym).new(attrs) end end diff --git a/lib/twitter/identity.rb b/lib/twitter/identity.rb index a14e74842..30d4d6796 100644 --- a/lib/twitter/identity.rb +++ b/lib/twitter/identity.rb @@ -12,8 +12,8 @@ class Identity < Twitter::Base # @raise [ArgumentError] Error raised when supplied argument is missing an :id key. # @return [Twitter::Identity] def initialize(attrs={}) + attrs.fetch(:id) super - raise ArgumentError, "argument must have an :id key" unless id end end diff --git a/spec/twitter/geo_creator_spec.rb b/spec/twitter/geo_creator_spec.rb index 339870bc6..b8560c642 100644 --- a/spec/twitter/geo_creator_spec.rb +++ b/spec/twitter/geo_creator_spec.rb @@ -11,8 +11,8 @@ geo = Twitter::GeoCreator.new(:type => "Polygon") expect(geo).to be_a Twitter::Geo::Polygon end - it "raises an ArgumentError when type is not specified" do - expect{Twitter::GeoCreator.new}.to raise_error ArgumentError + it "raises a KeyError when type is not specified" do + expect{Twitter::GeoCreator.new}.to raise_error KeyError end end diff --git a/spec/twitter/identifiable_spec.rb b/spec/twitter/identifiable_spec.rb index 4b5c6ad9b..cf133fca4 100644 --- a/spec/twitter/identifiable_spec.rb +++ b/spec/twitter/identifiable_spec.rb @@ -3,8 +3,8 @@ describe Twitter::Identity do describe "#initialize" do - it "raises an ArgumentError when type is not specified" do - expect{Twitter::Identity.new}.to raise_error ArgumentError + it "raises a KeyError when id is not specified" do + expect{Twitter::Identity.new}.to raise_error KeyError end end diff --git a/spec/twitter/media_creator_spec.rb b/spec/twitter/media_creator_spec.rb index 1730dbd5b..c4159c7ba 100644 --- a/spec/twitter/media_creator_spec.rb +++ b/spec/twitter/media_creator_spec.rb @@ -7,8 +7,8 @@ media = Twitter::MediaCreator.new(:id => 1, :type => "photo") expect(media).to be_a Twitter::Media::Photo end - it "raises an ArgumentError when type is not specified" do - expect{Twitter::MediaCreator.new}.to raise_error ArgumentError + it "raises a KeyError when type is not specified" do + expect{Twitter::MediaCreator.new}.to raise_error KeyError end end