Class: ChefZero::Endpoints::RestListEndpoint

Inherits:
RestBase
  • Object
show all
Defined in:
lib/chef_zero/endpoints/rest_list_endpoint.rb

Overview

Typical REST list endpoint (/roles or /data/BAG)

Constant Summary

Constants inherited from RestBase

RestBase::DEFAULT_REQUEST_VERSION, RestBase::DEFAULT_RESPONSE_VERSION

Instance Attribute Summary collapse

Attributes inherited from RestBase

#server

Instance Method Summary collapse

Methods inherited from RestBase

#accepts?, #already_json_response, build_uri, #build_uri, #call, #check_api_version, #create_data, #create_data_dir, #data_store, #delete_data, #delete_data_dir, #error, #exists_data?, #exists_data_dir?, #get_data, #get_data_or_else, #hashify_list, #head_request, #json_only, #json_response, #list_data, #list_data_or_else, #parse_json, #policy_name_invalid?, #populate_defaults, rfc2396_parser, #set_data, #text_response, #to_json

Constructor Details

#initialize(server, identity_keys = [ "name" ]) ⇒ RestListEndpoint

Returns a new instance of RestListEndpoint.



8
9
10
11
12
# 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

Instance Attribute Details

#identity_keysObject (readonly)

Returns the value of attribute identity_keys.



14
15
16
# File 'lib/chef_zero/endpoints/rest_list_endpoint.rb', line 14

def identity_keys
  @identity_keys
end

Instance Method Details

#get(request) ⇒ Object



16
17
18
19
20
21
22
23
# 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])).to_s
  end
  json_response(200, result_hash)
end

#get_key(contents) ⇒ Object



36
37
38
39
# 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) ⇒ Object



25
26
27
28
29
30
31
32
33
34
# 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(&: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])).to_s })
  end
end