lib/yao/resources/restfully_accessible.rb in yao-0.17.0 vs lib/yao/resources/restfully_accessible.rb in yao-0.18.0

- old
+ new

@@ -129,11 +129,11 @@ # @param id_or_name_or_permalink [Stirng] # @param query [Hash] # @return [Yao::Resources::*] def get(id_or_name_or_permalink, query={}) - res = if id_or_name_or_permalink.start_with?("http://", "https://") + res = if id_or_name_or_permalink&.start_with?("http://", "https://") GET(id_or_name_or_permalink, query) elsif uuid?(id_or_name_or_permalink) GET(create_url(id_or_name_or_permalink), query) else get_by_name(id_or_name_or_permalink, query) @@ -146,11 +146,11 @@ # @param id_or_name_or_permalink [Stirng] # @param query [Hash] # @return [Yao::Resources::*] def get!(id_or_name_or_permalink, query={}) get(id_or_name_or_permalink, query) - rescue Yao::ItemNotFound, Yao::NotFound, Yao::InvalidResponse + rescue Yao::ItemNotFound, Yao::NotFound, Yao::InvalidRequest nil end def find_by_name(name, query={}) list(query.merge({"name" => name})) @@ -233,18 +233,22 @@ # @param name [String] # @param query [Hash] # @return [Yao::Resources::*] def get_by_name(name, query={}) + # 空またnilの場合listと同じURLにリクエストしてしまい意図しないレスポンスが返ってくる + if name.nil? || name.empty? + raise Yao::InvalidRequest.new("Invalid request with empty name or nil") + end + begin GET(create_url(name), query) - rescue => e - raise e unless e.class == Yao::ItemNotFound || e.class == Yao::NotFound + rescue Yao::ItemNotFound, Yao::NotFound => e item = find_by_name(name).select { |r| r.name == name } if item.size > 1 raise Yao::TooManyItemFonud.new("More than one resource exists with the name '#{name}'") elsif item.size.zero? - raise Yao::InvalidResponse.new("No resource exists with the name '#{name}'") + raise e end GET(create_url(item.first.id), query) end end end