lib/yao/resources/restfully_accessible.rb in yao-0.1.1 vs lib/yao/resources/restfully_accessible.rb in yao-0.2.0.rc1
- old
+ new
@@ -16,14 +16,21 @@
end
def service=(name)
@service = name
end
+ attr_reader :service
- def service
- @service
+ 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}"
+ end
+ api_version
end
+ attr_reader :api_version
def admin=(bool)
@admin = bool
end
@@ -69,27 +76,31 @@
# restful methods
def list(query={})
json = GET(resources_path, query).body
if @return_single_on_querying && !query.empty?
- return_resource(json[resource_name_in_json])
+ return_resource(resource_from_json(json))
else
- return_resources(json[resources_name_in_json])
+ return_resources(resources_from_json(json))
end
end
def list_detail(query={})
- return_resources(GET([resources_path, "detail"].join("/"), query).body[resources_name_in_json])
+ return_resources(
+ resources_from_json(
+ GET([resources_path, "detail"].join("/"), query).body
+ )
+ )
end
def get(id_or_permalink, query={})
res = if id_or_permalink =~ /^https?:\/\//
GET(id_or_permalink, query)
else
GET([resources_path, id_or_permalink].join("/"), query)
end
- return_resource(res.body[resource_name_in_json])
+ return_resource(resource_from_json(res.body))
end
alias find get
def create(resource_params)
params = {
@@ -97,35 +108,37 @@
}
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])
+ 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|
req.body = params.to_json
req.headers['Content-Type'] = 'application/json'
end
- return_resource(res.body[resource_name_in_json])
+ return_resource(resource_from_json(res.body))
end
def destroy(id)
res = DELETE([resources_path, id].join("/"))
res.body
end
private
- def resource_name_in_json
+ def resource_from_json(json)
@resource_name_in_json ||= resource_name.sub(/^os-/, "").tr("-", "_")
+ json[@resource_name_in_json]
end
- def resources_name_in_json
+ def resources_from_json(json)
@resources_name_in_json ||= resources_name.sub(/^os-/, "").tr("-", "_")
+ json[@resources_name_in_json]
end
def return_resource(d)
new(d)
end