lib/yao/resources/restfully_accessible.rb in yao-0.3.8 vs lib/yao/resources/restfully_accessible.rb in yao-0.4.0

- old
+ new

@@ -83,17 +83,18 @@ return_resources(resources_from_json(json)) end end def get(id_or_name_or_permalink, query={}) - res = if id_or_name_or_permalink =~ /^https?:\/\// - GET(id_or_permalink, query) + 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([resources_path, id_or_name_or_permalink].join("/"), query) else - find_by_name(id_or_name_or_permalink, query) + get_by_name(id_or_name_or_permalink, query) end + return_resource(resource_from_json(res.body)) end alias find get def find_by_name(name, query={}) @@ -149,8 +150,21 @@ arr.map{|d| return_resource(d) } end def uuid?(str) /^[\da-f]{8}-([\da-f]{4}-){3}[\da-f]{12}$/ === str + end + + def get_by_name(name, query={}) + # At first, search by ID. If nothing is found, search by name. + begin + GET([resources_path, name].join("/"), query) + rescue Yao::ItemNotFound + item = find_by_name(name) + if item.size > 1 + raise Yao::TooManyItemFonud.new("More than one resource exists with the name '#{name}'") + end + GET([resources_path, item.first.id].join("/"), query) + end end end end