require 'oj' module Useless module Doc module Serialization module Dump # Converts a hash to a JSON representation. # # @param [Hash, String] hash the hash to be converted. # # @raise [ArgumentError] if json is not a Hash, String or IO. # # @return [String] a JSON representation corresponding to the # specified hash. # def self.hash_to_json(hash) hash.is_a?(String) ? hash : Oj.dump(hash) end # Converts +Core::Domain+ instance to a JSON representation. # # @param [Core::Domain] domain the domain to be converted to JSON. # # @return [String] a JSON representation of the specified domain. # def self.domain(domain) if domain hash_to_json \ 'name' => domain.name, 'url' => domain.url, 'description' => domain.description, 'timestamp' => domain.timestamp ? domain.timestamp.iso8601 : nil, 'apis' => domain.apis.map { |api| api(api) } end end # Converts +Core::API+ instance to a JSON representation. # # @param [Core::API] api the API to be converted to JSON. # # @return [String] a JSON representation of the specified API. # def self.api(api) if api hash_to_json \ 'name' => api.name, 'url' => api.url, 'description' => api.description, 'timestamp' => api.timestamp ? api.timestamp.iso8601 : nil, 'resources' => api.resources.map { |resource| resource(resource) } end end # Converts a +Core::Resource+ instance to a JSON representation. # # @param [Core::Resource] resource the resource to be converted to JSON. # # @return [String] a JSON representation of the specified resource. # def self.resource(resource) if resource hash_to_json \ 'path' => resource.path, 'description' => resource.description, 'requests' => resource.requests.map { |request| request(request) } end end # @api private def self.request(request) if request hash_to_json \ 'method' => request.method, 'description' => request.description, 'authentication_required' => request.authentication_required, 'parameters' => request.parameters.map { |parameter| request_parameter(parameter) }, 'headers' => request.headers.map { |header| header(header) }, 'body' => body(request.body), 'responses' => request.responses.map { |response| response(response) } end end # @api private def self.response(response) if response hash_to_json \ 'code' => response.code, 'description' => response.description, 'headers' => response.headers.map { |header| header(header) }, 'body' => body(response.body) end end # @api private def self.request_parameter(parameter) if parameter hash_to_json \ 'type' => parameter.type, 'key' => parameter.key, 'required' => parameter.required, 'default' => parameter.default, 'description' => parameter.description end end # @api private def self.header(header) if header hash_to_json \ 'key' => header.key, 'description' => header.description end end # @api private def self.response_status(status) if status hash_to_json \ 'code' => status.code, 'description' => status.description end end # @api private def self.body(body) if body hash_to_json \ 'content_type' => body.content_type, 'attributes' => body.attributes.map { |attribute| body_attribute(attribute) } end end # @api private def self.body_attribute(attribute) if attribute hash_to_json \ 'key' => attribute.key, 'type' => attribute.type, 'required' => attribute.required, 'default' => attribute.default, 'description' => attribute.description end end end end end end