lib/yao/resources/restfully_accessible.rb in yao-0.4.1 vs lib/yao/resources/restfully_accessible.rb in yao-0.4.2

- old
+ new

@@ -18,19 +18,19 @@ def service=(name) @service = name end attr_reader :service + def api_version + @api_version || '' + end + def api_version=(v) raise("Set api_version after service is declared") unless service @api_version = v - if cli = client - cli.url_prefix.path += "/#{api_version}" unless cli.url_prefix.to_s.include?("/#{api_version}") - end api_version end - attr_reader :api_version def admin=(bool) @admin = bool end @@ -74,11 +74,11 @@ result end # restful methods def list(query={}) - json = GET(resources_path, query).body + json = GET(create_url([api_version, resources_path]), query).body if @return_single_on_querying && !query.empty? return_resource(resource_from_json(json)) else return_resources(resources_from_json(json)) end @@ -86,11 +86,11 @@ def get(id_or_name_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) + GET(create_url([api_version, resources_path, id_or_name_or_permalink]), query) else get_by_name(id_or_name_or_permalink, query) end return_resource(resource_from_json(res.body)) @@ -103,34 +103,38 @@ def create(resource_params) params = { resource_name_in_json => resource_params } - res = POST(resources_path) do |req| + res = POST(create_url([api_version, resources_path])) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end return_resource(resource_from_json(res.body)) end def update(id, resource_params) params = { resource_name_in_json => resource_params } - res = PUT([resources_path, id].join("/")) do |req| + res = PUT(create_url([api_version, resources_path, id])) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end return_resource(resource_from_json(res.body)) end def destroy(id) - res = DELETE([resources_path, id].join("/")) + res = DELETE(create_url([api_version, resources_path, id])) res.body end private + def create_url(paths) + paths.select{|s| s != ''}.join('/') + end + def resource_name_in_json @_resource_name_in_json ||= resource_name.sub(/^os-/, "").tr("-", "_") end def resource_from_json(json) @@ -155,16 +159,16 @@ 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) + GET(create_url([api_version, resources_path, name]), 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) + GET(create_url([api_version, resources_path, item.first.id]), query) end end end end