require 'oj' require 'useless/doc/core/api' require 'useless/doc/core/body' require 'useless/doc/core/header' require 'useless/doc/core/request' require 'useless/doc/core/resource' require 'useless/doc/core/response' require 'useless/doc/core/stage' module Useless module Doc module Serialization module Load # Converts a JSON representation to a hash. # # @param [String, Hash] json the JSON representation to be converted. # # @raise [ArgumentError] if json is not a Hash, String or IO. # # @return [Hash] a hash corresponding to the specified JSON representation. # def self.json_to_hash(json) json.is_a?(Hash) ? json : Oj.load(json) end def self.load(json) hash = json_to_hash json if hash['apis'] self.domain(hash) elsif hash['url'] self.api(hash) elsif hash['path'] self.resource(hash) end end # Converts a JSON represntation to an instance of +Core::Domain+ # # @param [String, Hash] json the JSON representation to be converted to # a domain. # # @return [Core::Domain] the domain corresponding to the specified # JSON. # def self.domain(json) hash = json_to_hash json apis = (hash['apis'] || []).map do |json| api json end timestamp = begin Time.parse(hash['timestamp']) rescue TypeError, ArgumentError nil end Useless::Doc::Core::Domain.new \ name: hash['name'], url: hash['url'], timestamp: timestamp, description: hash['description'], apis: apis end # Converts a JSON represntation to an instance of +Core::API+ # # @param [String, Hash] json the JSON representation to be converted to # an API. # # @return [Core::API] the API corresponding to the specified # JSON. # def self.api(json) hash = json_to_hash json resources = (hash['resources'] || []).map do |json| resource json end timestamp = begin Time.parse(hash['timestamp']) rescue TypeError, ArgumentError nil end if hash['concept'] concept = stage hash['concept'] end if hash['specification'] specification = stage hash['specification'] end if hash['implementation'] implementation = stage hash['implementation'] end Useless::Doc::Core::API.new \ name: hash['name'], url: hash['url'], timestamp: timestamp, description: hash['description'], resources: resources, concept: concept, specification: specification, implementation: implementation end # Converts a JSON representation to an instance of +Core::Stage+ # # @param [String, Hash] json the JSON representation to be converted to # a stage. # # @return [Core::Resource] the stage corresponding to the specified # JSON. # def self.stage(json) hash = json_to_hash json Useless::Doc::Core::Stage.new \ credit: hash['credit'], progress: hash['progress'] end # Converts a JSON represntation to an instance of +Core::Resource+ # # @param [String, Hash] json the JSON representation to be converted to # a resource. # # @return [Core::Resource] the resource corresponding to the specified # JSON. # def self.resource(json) hash = json_to_hash json requests = (hash['requests'] || []).map do |json| request json end Useless::Doc::Core::Resource.new \ path: hash['path'], description: hash['description'], requests: requests end # @api private def self.request(json) hash = json_to_hash json parameters = (hash['parameters'] || []).map do |json| request_parameter json end headers = (hash['headers'] || []).map do |json| header json end if hash['body'] body = body hash['body'] end responses = (hash['responses'] || []).map do |json| response json end Useless::Doc::Core::Request.new \ method: hash['method'], description: hash['description'], authentication_required: hash['authentication_required'], parameters: parameters, headers: headers, body: body, responses: responses end # @api private def self.response(json) hash = json_to_hash json headers = (hash['headers'] || []).map do |json| header json end if hash['body'] body = body hash['body'] end Useless::Doc::Core::Response.new \ code: hash['code'], description: hash['description'], headers: headers, body: body end # @api private def self.request_parameter(json) hash = json_to_hash json Useless::Doc::Core::Request::Parameter.new \ type: hash['type'], key: hash['key'], required: hash['required'], default: hash['default'], description: hash['description'] end # @api private def self.header(json) hash = json_to_hash json Useless::Doc::Core::Header.new \ key: hash['key'], description: hash['description'] end # @api private def self.body(json) hash = json_to_hash json attributes = (hash['attributes'] || []).map do |json| body_attribute json end Useless::Doc::Core::Body.new \ content_type: hash['content_type'], attributes: attributes end # @api private def self.body_attribute(json) hash = json_to_hash json Useless::Doc::Core::Body::Attribute.new \ key: hash['key'], type: hash['type'], required: hash['required'], default: hash['default'], description: hash['description'] end end end end end