summaryrefslogtreecommitdiff
path: root/lib/chef_zero/rest_base.rb
diff options
context:
space:
mode:
authorChris Doherty <cdoherty@chef.io>2015-10-27 16:54:06 -0700
committerChris Doherty <cdoherty@chef.io>2015-11-10 14:50:22 -0800
commit3627567298769fcc3e6d9904c0f52a810e5a26b4 (patch)
treebdd211c9e16e7a57e8be431ebfbf8ba7f5886d5f /lib/chef_zero/rest_base.rb
parent1fb96253a01920001ad9b1471309518bc28a93a0 (diff)
downloadchef-zero-3627567298769fcc3e6d9904c0f52a810e5a26b4.tar.gz
Implement oc_pedant's REST routes for policyfiles.
Diffstat (limited to 'lib/chef_zero/rest_base.rb')
-rw-r--r--lib/chef_zero/rest_base.rb39
1 files changed, 35 insertions, 4 deletions
diff --git a/lib/chef_zero/rest_base.rb b/lib/chef_zero/rest_base.rb
index 715d705..b4f8605 100644
--- a/lib/chef_zero/rest_base.rb
+++ b/lib/chef_zero/rest_base.rb
@@ -195,12 +195,13 @@ module ChefZero
data_store.exists_dir?(rest_path)
end
- def error(response_code, error)
- json_response(response_code, {"error" => [error]})
+ def error(response_code, error, opts={})
+ json_response(response_code, {"error" => [error]}, 0, 0, opts)
end
- def json_response(response_code, json, request_version=0, response_version=0)
- already_json_response(response_code, FFI_Yajl::Encoder.encode(json, :pretty => true), request_version, response_version)
+ def json_response(response_code, json, request_version=0, response_version=0, opts={pretty: true})
+ do_pretty_json = opts[:pretty] && true
+ already_json_response(response_code, FFI_Yajl::Encoder.encode(json, :pretty => do_pretty_json), request_version, response_version)
end
def text_response(response_code, text)
@@ -238,5 +239,35 @@ module ChefZero
def populate_defaults(request, response)
response
end
+
+ def parse_json(json)
+ FFI_Yajl::Parser.parse(json, create_additions: false)
+ end
+
+ def to_json(data)
+ FFI_Yajl::Encoder.encode(data, :pretty => true)
+ end
+
+ def get_data_or_else(request, path, or_else_value)
+ if exists_data?(request, path)
+ parse_json(get_data(request, path))
+ else
+ or_else_value
+ end
+ end
+
+ def list_data_or_else(request, path, or_else_value)
+ if exists_data_dir?(request, path)
+ list_data(request, path)
+ else
+ or_else_value
+ end
+ end
+
+ def policy_name_invalid?(name)
+ !name.is_a?(String) ||
+ name.size > 255 ||
+ name =~ /[+ !]/
+ end
end
end