lib/yao/resources/restfully_accessible.rb in yao-0.0.6 vs lib/yao/resources/restfully_accessible.rb in yao-0.1.0

- old
+ new

@@ -21,56 +21,92 @@ def service @service end + def admin=(bool) + @admin = bool + end + + def return_single_on_querying=(bool) + @return_single_on_querying = bool + end + + def resources_path + @resources_path || resources_name + end + + def resources_path=(path) + @resources_path = path.sub(%r!^\/!, "") + end + def client - Yao.default_client.pool[service] + if @admin + Yao.default_client.admin_pool[service] + else + Yao.default_client.pool[service] + end end + def as_member(&blk) + if @admin + @admin = false + result = yield(blk) + @admin = true + result + else + yield blk + end + end + # restful methods def list(query={}) - return_resources(GET(resources_name, query).body[resources_name_in_json]) + json = GET(resources_path, query).body + if @return_single_on_querying && !query.empty? + return_resource(json[resource_name_in_json]) + else + return_resources(json[resources_name_in_json]) + end end def list_detail(query={}) - return_resources(GET([resources_name, "detail"].join("/"), query).body[resources_name_in_json]) + return_resources(GET([resources_path, "detail"].join("/"), query).body[resources_name_in_json]) end def get(id_or_permalink, query={}) res = if id_or_permalink =~ /^https?:\/\// GET(id_or_permalink, query) else - GET([resources_name, id_or_permalink].join("/"), query) + GET([resources_path, id_or_permalink].join("/"), query) end return_resource(res.body[resource_name_in_json]) end alias find get def create(resource_params) params = { resource_name_in_json => resource_params } - res = POST(resources_name) do |req| + res = POST(resources_path) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end return_resource(res.body[resource_name_in_json]) end def update(id, resource_params) params = { resource_name_in_json => resource_params } - res = PUT([resources_name, id].join("/")) do |req| + res = PUT([resources_path, id].join("/")) do |req| req.body = params.to_json req.headers['Content-Type'] = 'application/json' end return_resource(res.body[resource_name_in_json]) end def destroy(id) - res = DELETE([resources_name, id].join("/")) + res = DELETE([resources_path, id].join("/")) res.body end private def resource_name_in_json