class ChefZero::Endpoints::RestListEndpoint
Typical REST list endpoint (/roles or /data/BAG)
Attributes
identity_keys[R]
Public Class Methods
new(server, identity_keys = [ "name" ])
click to toggle source
Calls superclass method
# File lib/chef_zero/endpoints/rest_list_endpoint.rb, line 8 def initialize(server, identity_keys = [ "name" ]) super(server) identity_keys = [ identity_keys ] if identity_keys.is_a?(String) @identity_keys = identity_keys end
Public Instance Methods
get(request)
click to toggle source
# File lib/chef_zero/endpoints/rest_list_endpoint.rb, line 16 def get(request) # Get the result result_hash = {} list_data(request).sort.each do |name| result_hash[name] = "#{build_uri(request.base_uri, request.rest_path + [name])}" end json_response(200, result_hash) end
get_key(contents)
click to toggle source
# File lib/chef_zero/endpoints/rest_list_endpoint.rb, line 36 def get_key(contents) json = FFI_Yajl::Parser.parse(contents) identity_keys.map { |k| json[k] }.select { |v| v }.first end
post(request)
click to toggle source
# File lib/chef_zero/endpoints/rest_list_endpoint.rb, line 25 def post(request) contents = request.body key = get_key(contents) if key.nil? error(400, "Must specify #{identity_keys.map { |k| k.inspect }.join(' or ')} in JSON") else create_data(request, request.rest_path, key, contents) json_response(201, { "uri" => "#{build_uri(request.base_uri, request.rest_path + [key])}" }) end end