require File.dirname(__FILE__) + '/../../../spec_helper' require 'useless/doc/core/api' require 'useless/doc/core/body' require 'useless/doc/core/domain' 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' require 'useless/doc/serialization/dump' require 'useless/doc/serialization/load' describe Useless::Doc::Serialization::Dump do describe '.hash_to_json' do it 'should convert a hash to a JSON document' do hash = Useless::Doc::Serialization::Dump.hash_to_json({'abc' => 123}) hash.should == '{"abc":123}' end it 'should act as an identity if a JSON document is specified' do json = '{"abc":123}' Useless::Doc::Serialization::Dump.hash_to_json(json).should == json end end describe '.domain' do it 'should convert the specified Core::Domain instance to JSON' do api = Useless::Doc::Core::API.new \ name: 'Twiddles API', url: 'twiddles.useless.io', description: 'Pretty much, like, everything you\'re looking for' domain = Useless::Doc::Core::Domain.new \ name: 'Useless', url: 'http://useless.io', timestamp: Time.parse('2013-03-06 11:13 PM'), description: 'A collection of useless APIs.', apis: [api] json = Useless::Doc::Serialization::Dump.domain(domain) hash = Useless::Doc::Serialization::Load.json_to_hash(json) hash['name'].should == 'Useless' hash['url'].should == 'http://useless.io' hash['timestamp'].should == Time.parse('2013-03-06 11:13 PM').iso8601 hash['description'].should == 'A collection of useless APIs.' api_hash = Useless::Doc::Serialization::Load.json_to_hash(hash['apis'][0]) api_hash['name'].should == 'Twiddles API' api_hash['url'].should == 'twiddles.useless.io' api_hash['description'].should == 'Pretty much, like, everything you\'re looking for' end end describe '.api' do it 'should convert the specified Core::API instance to JSON' do concept = Useless::Doc::Core::Stage.new \ credit: ['Joe Blue'], progress: 'complete' specification = Useless::Doc::Core::Stage.new \ credit: ['Bill Grey'], progress: 'in-progress' implementation = Useless::Doc::Core::Stage.new \ credit: ['Bill Grey', 'Joe Blue'], progress: 'pending' resource = Useless::Doc::Core::Resource.new \ path: '/twiddles', description: 'The full lot of twiddles.', requests: [] api = Useless::Doc::Core::API.new \ name: 'Twiddles API', url: 'twiddles.useless.io', timestamp: Time.parse('2013-03-06 11:13 PM'), description: 'Pretty much, like, everything you\'re looking for', resources: [resource], concept: concept, specification: specification, implementation: implementation json = Useless::Doc::Serialization::Dump.api(api) hash = Useless::Doc::Serialization::Load.json_to_hash(json) hash['name'].should == 'Twiddles API' hash['url'].should == 'twiddles.useless.io' hash['timestamp'].should == Time.parse('2013-03-06 11:13 PM').iso8601 hash['description'].should == 'Pretty much, like, everything you\'re looking for' concept_hash = Useless::Doc.load.json_to_hash(hash['concept']) concept_hash['credit'].should == ['Joe Blue'] concept_hash['progress'].should == 'complete' specification_hash = Useless::Doc.load.json_to_hash(hash['specification']) specification_hash['credit'].should == ['Bill Grey'] specification_hash['progress'].should == 'in-progress' implementation_hash = Useless::Doc.load.json_to_hash(hash['implementation']) implementation_hash['credit'].should == ['Bill Grey', 'Joe Blue'] implementation_hash['progress'].should == 'pending' resource_hash = Useless::Doc::Serialization::Load.json_to_hash(hash['resources'][0]) resource_hash['path'].should == '/twiddles' resource_hash['description'].should == 'The full lot of twiddles.' end end describe '.resource' do it 'should convert the specified Core::Resource instance into JSON' do get_response_header = Useless::Doc::Core::Header.new \ key: 'Type', description: 'The response type.' get_response_body_subattribute = Useless::Doc::Core::Body::Attribute.new \ key: 'nickname', type: 'string', required: true, default: nil, description: 'The nickname of the user' get_response_body_attribute = Useless::Doc::Core::Body::Attribute.new \ key: 'name', type: 'string', required: true, default: nil, description: 'The name of the twiddle.', attributes: [get_response_body_subattribute] get_response_body = Useless::Doc::Core::Body.new \ content_type: 'application/json', attributes: [get_response_body_attribute] get_response = Useless::Doc::Core::Response.new \ code: 200, description: 'The list was successfully returned.', headers: [get_response_header], body: get_response_body get_request_parameter = Useless::Doc::Core::Request::Parameter.new \ type: Useless::Doc::Core::Request::Parameter::Type::PATH, key: 'page', required: false, default: 1, description: 'The number of the page of twiddles.' get_request_header = Useless::Doc::Core::Header.new \ key: 'Content', description: 'The type of content.' get_request = Useless::Doc::Core::Request.new \ method: Useless::Doc::Core::Request::Method::GET, description: 'See a whole list of twiddles.', authentication_required: false, parameters: [get_request_parameter], headers: [get_request_header], body: nil, responses: [get_response] resource = Useless::Doc::Core::Resource.new \ path: '/twiddles', description: 'The full lot of twiddles.', requests: [get_request] json = Useless::Doc::Serialization::Dump.resource(resource) hash = Useless::Doc::Serialization::Load.json_to_hash(json) hash['path'].should == '/twiddles' hash['description'].should == 'The full lot of twiddles.' get_request_hash = Useless::Doc::Serialization::Load.json_to_hash(hash['requests'][0]) get_request_hash['description'].should == 'See a whole list of twiddles.' get_request_hash['method'].should == 'GET' get_request_hash['authentication_required'].should be_false get_request_paramter_hash = Useless::Doc::Serialization::Load.json_to_hash(get_request_hash['parameters'][0]) get_request_paramter_hash['type'].should == 'path' get_request_paramter_hash['key'].should == 'page' get_request_paramter_hash['required'].should == false get_request_paramter_hash['default'].should == 1 get_request_paramter_hash['description'].should == 'The number of the page of twiddles.' get_request_header_hash = Useless::Doc::Serialization::Load.json_to_hash(get_request_hash['headers'][0]) get_request_header_hash['key'].should == 'Content' get_request_header_hash['description'].should == 'The type of content.' get_response_hash = Useless::Doc::Serialization::Load.json_to_hash(get_request_hash['responses'][0]) get_response_hash['code'].should == 200 get_response_hash['description'].should == 'The list was successfully returned.' get_response_header_hash = Useless::Doc::Serialization::Load.json_to_hash(get_response_hash['headers'][0]) get_response_header_hash['key'].should == 'Type' get_response_header_hash['description'].should == 'The response type.' get_response_body_hash = Useless::Doc::Serialization::Load.json_to_hash(get_response_hash['body']) get_response_body_hash['content_type'].should == 'application/json' get_request_body_attribute_hash = Useless::Doc::Serialization::Load.json_to_hash(get_response_body_hash['attributes'][0]) get_request_body_attribute_hash['key'].should == 'name' get_request_body_attribute_hash['type'].should == 'string' get_request_body_attribute_hash['required'].should == true get_request_body_attribute_hash['default'].should be_nil get_request_body_attribute_hash['description'].should == 'The name of the twiddle.' get_request_body_subattribute_hash = Useless::Doc::Serialization::Load.json_to_hash(get_request_body_attribute_hash['attributes'][0]) get_request_body_subattribute_hash['key'].should == 'nickname' get_request_body_subattribute_hash['type'].should == 'string' get_request_body_subattribute_hash['required'].should == true get_request_body_subattribute_hash['default'].should be_nil get_request_body_subattribute_hash['description'].should == 'The nickname of the user' end end end