kanpachi 0.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (83) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/.travis.yml +13 -0
  4. data/CHANGELOG.md +5 -0
  5. data/Gemfile +6 -0
  6. data/Guardfile +14 -0
  7. data/LICENSE.txt +22 -0
  8. data/README.md +131 -0
  9. data/Rakefile +10 -0
  10. data/bin/kanpachi +17 -0
  11. data/examples/twitter.rb +122 -0
  12. data/kanpachi.gemspec +45 -0
  13. data/lib/base_hash.rb +41 -0
  14. data/lib/kanpachi/api.rb +22 -0
  15. data/lib/kanpachi/api_list.rb +61 -0
  16. data/lib/kanpachi/cli/doc.rb +62 -0
  17. data/lib/kanpachi/cli.rb +32 -0
  18. data/lib/kanpachi/commands/new.rb +26 -0
  19. data/lib/kanpachi/documentation/config.rb +84 -0
  20. data/lib/kanpachi/documentation/source/fonts/glyphicons-halflings-regular.eot +0 -0
  21. data/lib/kanpachi/documentation/source/fonts/glyphicons-halflings-regular.svg +228 -0
  22. data/lib/kanpachi/documentation/source/fonts/glyphicons-halflings-regular.ttf +0 -0
  23. data/lib/kanpachi/documentation/source/fonts/glyphicons-halflings-regular.woff +0 -0
  24. data/lib/kanpachi/documentation/source/images/background.png +0 -0
  25. data/lib/kanpachi/documentation/source/images/middleman.png +0 -0
  26. data/lib/kanpachi/documentation/source/index.html.slim +25 -0
  27. data/lib/kanpachi/documentation/source/javascripts/all.js +1 -0
  28. data/lib/kanpachi/documentation/source/javascripts/bootstrap.js +1999 -0
  29. data/lib/kanpachi/documentation/source/javascripts/bootstrap.min.js +6 -0
  30. data/lib/kanpachi/documentation/source/javascripts/html5shiv.js +9 -0
  31. data/lib/kanpachi/documentation/source/javascripts/jquery-1.10.2.min.js +6 -0
  32. data/lib/kanpachi/documentation/source/javascripts/respond.min.js +7 -0
  33. data/lib/kanpachi/documentation/source/layouts/layout.slim +55 -0
  34. data/lib/kanpachi/documentation/source/resource.html.slim +61 -0
  35. data/lib/kanpachi/documentation/source/stylesheets/bootstrap-theme.css +384 -0
  36. data/lib/kanpachi/documentation/source/stylesheets/bootstrap-theme.min.css +1 -0
  37. data/lib/kanpachi/documentation/source/stylesheets/bootstrap.css +6805 -0
  38. data/lib/kanpachi/documentation/source/stylesheets/bootstrap.min.css +9 -0
  39. data/lib/kanpachi/documentation/source/stylesheets/jumbotron.css +5 -0
  40. data/lib/kanpachi/dsl/api.rb +90 -0
  41. data/lib/kanpachi/dsl/error.rb +37 -0
  42. data/lib/kanpachi/dsl/resource.rb +110 -0
  43. data/lib/kanpachi/dsl/response.rb +21 -0
  44. data/lib/kanpachi/dsl/section.rb +29 -0
  45. data/lib/kanpachi/dsl.rb +24 -0
  46. data/lib/kanpachi/error.rb +14 -0
  47. data/lib/kanpachi/error_list.rb +51 -0
  48. data/lib/kanpachi/markdown.rb +11 -0
  49. data/lib/kanpachi/resource/params.rb +9 -0
  50. data/lib/kanpachi/resource.rb +56 -0
  51. data/lib/kanpachi/resource_list.rb +71 -0
  52. data/lib/kanpachi/response.rb +68 -0
  53. data/lib/kanpachi/response_list.rb +51 -0
  54. data/lib/kanpachi/section.rb +15 -0
  55. data/lib/kanpachi/section_list.rb +46 -0
  56. data/lib/kanpachi/templates/.gitignore +19 -0
  57. data/lib/kanpachi/templates/Gemfile +4 -0
  58. data/lib/kanpachi/templates/api/api.rb +17 -0
  59. data/lib/kanpachi/templates/api/errors.rb +13 -0
  60. data/lib/kanpachi/templates/api/posts.rb +32 -0
  61. data/lib/kanpachi/templates/api/users.rb +38 -0
  62. data/lib/kanpachi/version.rb +3 -0
  63. data/lib/kanpachi.rb +7 -0
  64. data/spec/api_list_spec.rb +55 -0
  65. data/spec/api_spec.rb +56 -0
  66. data/spec/dsl/api_spec.rb +74 -0
  67. data/spec/dsl/error_spec.rb +38 -0
  68. data/spec/dsl/resource_spec.rb +77 -0
  69. data/spec/dsl/response_spec.rb +31 -0
  70. data/spec/dsl/section_spec.rb +49 -0
  71. data/spec/dsl_spec.rb +46 -0
  72. data/spec/error_list_spec.rb +43 -0
  73. data/spec/error_spec.rb +34 -0
  74. data/spec/full_api_spec.rb +130 -0
  75. data/spec/markdown_spec.rb +12 -0
  76. data/spec/resource/params_spec.rb +11 -0
  77. data/spec/resource_list_spec.rb +53 -0
  78. data/spec/resource_spec.rb +144 -0
  79. data/spec/response_spec.rb +57 -0
  80. data/spec/section_list_spec.rb +39 -0
  81. data/spec/section_spec.rb +20 -0
  82. data/spec/spec_helper.rb +4 -0
  83. metadata +384 -0
@@ -0,0 +1,77 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Kanpachi::DSL::Resource do
4
+ before do
5
+ Kanpachi::APIList.clear
6
+ end
7
+
8
+ subject do
9
+ Kanpachi::DSL::Resource.new(users_resource)
10
+ end
11
+
12
+ let(:users_resource) do
13
+ Kanpachi::Resource.new(:post, '/users')
14
+ end
15
+
16
+ it 'sets the name' do
17
+ subject.name 'MyAPI'
18
+ users_resource.name.must_equal 'MyAPI'
19
+ end
20
+
21
+ it 'sets the description' do
22
+ subject.description 'Just for testing'
23
+ users_resource.description.must_equal 'Just for testing'
24
+ end
25
+
26
+ it 'sets the priority' do
27
+ subject.priority 999
28
+ users_resource.priority.must_equal 999
29
+ end
30
+
31
+ it 'sets ssl' do
32
+ subject.ssl true
33
+ users_resource.ssl.must_equal true
34
+ end
35
+
36
+ it 'sets authentication' do
37
+ subject.authentication true
38
+ users_resource.authentication.must_equal true
39
+ end
40
+
41
+ it 'sets versions' do
42
+ subject.versions '1.0', '1.1'
43
+ users_resource.versions.must_include '1.0'
44
+ users_resource.versions.must_include '1.1'
45
+ end
46
+
47
+ it 'sets formats' do
48
+ subject.formats :xml, :json
49
+ users_resource.formats.must_include :xml
50
+ users_resource.formats.must_include :json
51
+ end
52
+
53
+ it 'sets the response' do
54
+ subject.response do
55
+ status 200
56
+ end
57
+ users_resource.responses.find(:default).wont_be_nil
58
+ users_resource.responses.find(:default).status.must_equal 200
59
+ end
60
+
61
+ it 'sets the response with an explicit name' do
62
+ subject.response :created do
63
+ status 201
64
+ end
65
+ users_resource.responses.find(:created).wont_be_nil
66
+ users_resource.responses.find(:created).status.must_equal 201
67
+ end
68
+
69
+ it 'sets the params' do
70
+ subject.params do
71
+ required do
72
+ integer :id
73
+ end
74
+ end
75
+ users_resource.params?.must_equal true
76
+ end
77
+ end
@@ -0,0 +1,31 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Kanpachi::DSL::Response do
4
+ subject do
5
+ Kanpachi::DSL::Response.new(response)
6
+ end
7
+
8
+ let(:response) do
9
+ Kanpachi::Response.new(:default)
10
+ end
11
+
12
+ it 'sets the status' do
13
+ subject.status 201
14
+ response.status.must_equal 201
15
+ end
16
+
17
+ it 'sets the header' do
18
+ subject.header 'Content-Type', 'application/json'
19
+ response.headers['Content-Type'].must_equal 'application/json'
20
+ end
21
+
22
+ it 'sets the representation' do
23
+ subject.representation do
24
+ property :id, type: Integer
25
+ end
26
+
27
+ id = response.representation.properties['id']
28
+ id.wont_be_nil
29
+ id[:type].must_equal Integer
30
+ end
31
+ end
@@ -0,0 +1,49 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Kanpachi::DSL::Section do
4
+ before do
5
+ Kanpachi::APIList.clear
6
+ end
7
+
8
+ subject do
9
+ Kanpachi::DSL::Section.new(users_section, api_dsl)
10
+ end
11
+
12
+ let(:api_dsl) do
13
+ Kanpachi::DSL::API.new(my_api)
14
+ end
15
+
16
+ let(:my_api) do
17
+ extend Kanpachi::DSL
18
+ api 'MyApp' do
19
+ title 'My App'
20
+ end
21
+ end
22
+
23
+ let(:users_section) do
24
+ Kanpachi::Section.new('Users')
25
+ end
26
+
27
+ it 'sets the description' do
28
+ subject.description 'Just for testing'
29
+ users_section.description.must_equal 'Just for testing'
30
+ end
31
+
32
+ it 'adds a new resource to the resource list' do
33
+ resource = subject.resource :get, '/posts'
34
+ route = [resource.http_verb, resource.url]
35
+ users_section.routes.must_include route
36
+ my_api.resources.find(*route).must_equal resource
37
+ end
38
+
39
+ it 'raises a Kanpachi::ResourceList::DuplicateResource if resource route already exists' do
40
+ subject.resource :get, '/users'
41
+ proc do
42
+ subject.resource :get, '/users' do
43
+ response do
44
+ status 400
45
+ end
46
+ end
47
+ end.must_raise Kanpachi::ResourceList::DuplicateResource
48
+ end
49
+ end
data/spec/dsl_spec.rb ADDED
@@ -0,0 +1,46 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kanpachi::DSL do
4
+ describe 'extend self with Kanpachi::DSL' do
5
+ before do
6
+ extend Kanpachi::DSL
7
+ @api = api 'MyApp' do
8
+ title 'My App'
9
+ end
10
+ @before_api_count = Kanpachi::APIList.all.size
11
+ end
12
+
13
+ it 'defining an API returns that API' do
14
+ @api.must_be_instance_of Kanpachi::API
15
+ end
16
+
17
+ it 'defining a new API adds it to the APIList' do
18
+ your_api = api 'YourApp' do
19
+ end
20
+ Kanpachi::APIList.find('YourApp').must_equal your_api
21
+ Kanpachi::APIList.all.size.must_equal @before_api_count + 1
22
+ end
23
+
24
+ it 'defining a existing API does not add it to the APIList' do
25
+ same_api = api 'MyApp' do
26
+ end
27
+ Kanpachi::APIList.find('MyApp').must_equal @api
28
+ Kanpachi::APIList.all.size.must_equal @before_api_count
29
+ end
30
+
31
+ it 'defining a existing api reopens that API' do
32
+ same_api = api 'MyApp' do
33
+ title 'An App'
34
+ end
35
+ Kanpachi::APIList.find('MyApp').title.must_equal 'An App'
36
+ end
37
+ end
38
+
39
+ it '#api is only available from within self' do
40
+ proc do
41
+ api 'Test' do
42
+ title 'Test'
43
+ end
44
+ end.must_raise NoMethodError
45
+ end
46
+ end
@@ -0,0 +1,43 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kanpachi::ErrorList do
4
+ subject do
5
+ Kanpachi::ErrorList.new
6
+ end
7
+
8
+ let(:not_found_error) do
9
+ Kanpachi::Error.new(:not_found)
10
+ end
11
+
12
+ let(:populated_error_list) do
13
+ list = Kanpachi::ErrorList.new
14
+ list.add(not_found_error)
15
+ list
16
+ end
17
+
18
+ it 'initializes empty' do
19
+ subject.all.must_be_empty
20
+ end
21
+
22
+ it 'returns a hash of the internal list, with error names as the key and a Error object as the value' do
23
+ populated_error_list.to_hash.keys.must_include :not_found
24
+ populated_error_list.to_hash[:not_found].must_equal not_found_error
25
+ end
26
+
27
+ it 'returns an array of Error objects' do
28
+ populated_error_list.all.must_include not_found_error
29
+ end
30
+
31
+ it 'adds a error' do
32
+ subject.add(not_found_error)
33
+ subject.all.must_include not_found_error
34
+ end
35
+
36
+ it 'raises DuplicateError if adding an Error with a name that already exists' do
37
+ proc { populated_error_list.add(not_found_error) }.must_raise Kanpachi::ErrorList::DuplicateError
38
+ end
39
+
40
+ it 'finds a error by name' do
41
+ populated_error_list.find(:not_found).must_equal not_found_error
42
+ end
43
+ end
@@ -0,0 +1,34 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kanpachi::Error do
4
+ subject do
5
+ Kanpachi::Error.new(:not_found).tap do |e|
6
+ e.description = 'Error'
7
+ e.response = Kanpachi::Response.new(:default).tap do |r|
8
+ r.status = 404
9
+ end
10
+ end
11
+ end
12
+
13
+ it 'initialized with the name' do
14
+ subject.name.must_equal :not_found
15
+ end
16
+
17
+ it 'initialized with a Response with the name :default' do
18
+ subject.response.must_be_instance_of Kanpachi::Response
19
+ subject.response.name.must_equal :default
20
+ end
21
+
22
+ it 'has a description accessor' do
23
+ subject.description = 'Error returned with resource is not found'
24
+ subject.description.must_equal 'Error returned with resource is not found'
25
+ end
26
+
27
+ it 'has a response accessor' do
28
+ response = Kanpachi::Response.new(:bad_request).tap do |r|
29
+ r.status = 400
30
+ end
31
+ subject.response = response
32
+ subject.response.must_equal response
33
+ end
34
+ end
@@ -0,0 +1,130 @@
1
+ require_relative 'spec_helper'
2
+ require 'roar/representer/json'
3
+ require 'roar/representer/feature/hypermedia'
4
+
5
+ describe 'Integration Spec' do
6
+ it 'test the entire stack' do
7
+ extend Kanpachi::DSL
8
+
9
+ repo = Class.new do
10
+ attr_accessor :title, :id, :users
11
+ end
12
+
13
+ user = Class.new do
14
+ attr_accessor :first_name, :last_name
15
+ end
16
+
17
+ user_representer = Module.new do
18
+ include Roar::Representer::JSON
19
+ include Roar::Representer::Feature::Hypermedia
20
+ property :first_name, type: String, required: true, doc: "it's the first name"
21
+ property :last_name, type: String, required: true, doc: "it's the last name"
22
+ end
23
+
24
+ a = api 'Twitter' do
25
+ title 'API v1.1'
26
+ description 'This describes the resources that make up the official Twitter API v1.1'
27
+ host 'api.twitter.com'
28
+
29
+ error :malformed_params do
30
+ description 'Sending invalid JSON will result in a 400 Bad Request response.'
31
+
32
+ response do
33
+ status 400
34
+ header 'Content-Type', 'application/json'
35
+ representation do
36
+ property :message, type: String
37
+ end
38
+ end
39
+ end
40
+
41
+ section 'Tweets' do
42
+ description 'Tweets are the atomic building blocks of Twitter, 140-character status updates with additional associated metadata. People tweet for a variety of reasons about a multitude of topics.'
43
+
44
+ resource :get, '/user/repos' do
45
+ name 'list of repos'
46
+ versions '1.1', '2.0'
47
+ ssl true
48
+ formats :json, :xml
49
+
50
+ params do
51
+ required do
52
+ string :title, max_length: 100, nils: false, doc: "test"
53
+ string :gender, in: [:male, :female, :other], doc: "test"
54
+ end
55
+
56
+ optional do
57
+ integer :id, min: 0, max: 1024
58
+ end
59
+ end
60
+
61
+ response do
62
+ status 200
63
+ header 'Content-Type', 'application/json'
64
+ representation do
65
+ property :id, type: Integer, doc: "it's the id", example: 1
66
+ property :title, type: String, doc: "it's the title", example: 'The Title'
67
+ collection :users, extend: user_representer, doc: "it's the users"
68
+ end
69
+ end
70
+
71
+ response :id_list do
72
+ status 200
73
+ header 'Content-Type', 'application/json'
74
+ representation do
75
+ property :id, type: Integer, doc: "it's the id"
76
+ end
77
+ end
78
+ end
79
+ end
80
+ end
81
+
82
+ puts a.name
83
+ puts a.title
84
+ puts a.description
85
+ puts a.host
86
+ puts a.sections
87
+ puts "sections"
88
+ a.sections.all.each do |v|
89
+ puts v.name
90
+ puts v.description
91
+ puts v.routes
92
+ end
93
+
94
+ puts a.resources
95
+
96
+ puts "resources"
97
+ a.resources.to_hash.each do |k, r|
98
+ puts r.http_verb
99
+ puts r.versions.inspect
100
+ puts r.formats.inspect
101
+ puts r.name
102
+ puts r.authentication
103
+ puts r.ssl
104
+ puts r.description
105
+ puts r.params
106
+ puts r.responses
107
+ r.responses.all.first.representation.send :include, Roar::Representer::JSON
108
+ u = user.new
109
+ u.first_name = "Jack"
110
+ u.last_name = "Chu"
111
+ rep = repo.new.extend(r.responses.all.first.representation)
112
+ rep.title = "Hi"
113
+ rep.id = ""
114
+ rep.users = []
115
+ rep.users << u
116
+ puts rep.to_json
117
+
118
+ puts rep
119
+ puts r.url
120
+ res = r.responses.all.first
121
+ puts res.representation.representable_attrs.map &:inspect
122
+ puts res.status
123
+ puts res.headers
124
+ puts res.headers['Content-Type']
125
+
126
+ v = r.params.run(title: "woohoo", id: 1)
127
+ puts "valid: #{v.success?}"
128
+ end
129
+ end
130
+ end
@@ -0,0 +1,12 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kanpachi::Markdown do
4
+ subject do
5
+ Kanpachi::Markdown
6
+ end
7
+
8
+ it 'converts markdown text to html' do
9
+ text = '_omg_ __blah__ <http://example.org/test>'
10
+ subject.to_html(text).must_equal %Q{<p><em>omg</em> <strong>blah</strong> <a href=\"http://example.org/test\">http://example.org/test</a></p>\n}
11
+ end
12
+ end
@@ -0,0 +1,11 @@
1
+ require_relative '../spec_helper'
2
+
3
+ describe Kanpachi::Resource::Params do
4
+ subject do
5
+ Kanpachi::Resource::Params
6
+ end
7
+
8
+ it 'is a kind of Mutations::Command' do
9
+ subject.superclass.must_equal Mutations::Command
10
+ end
11
+ end
@@ -0,0 +1,53 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kanpachi::ResourceList do
4
+ subject do
5
+ Kanpachi::ResourceList.new
6
+ end
7
+
8
+ let(:login_resource) do
9
+ Kanpachi::Resource.new(:post, '/login').tap do |r|
10
+ r.name = 'login'
11
+ end
12
+ end
13
+
14
+ let(:populated_resource_list) do
15
+ list = Kanpachi::ResourceList.new
16
+ list.add(login_resource)
17
+ list
18
+ end
19
+
20
+ it 'initializes empty' do
21
+ subject.all.must_be_empty
22
+ end
23
+
24
+ it 'returns a hash of the internal list, with resource names as the key and a Resource object as the value' do
25
+ populated_resource_list.to_hash.keys.must_include login_resource.route
26
+ populated_resource_list.to_hash[login_resource.route].must_equal login_resource
27
+ end
28
+
29
+ it 'returns an array of Resource objects' do
30
+ populated_resource_list.all.must_include login_resource
31
+ end
32
+
33
+ it 'adds a resource' do
34
+ subject.add(login_resource)
35
+ subject.all.must_include login_resource
36
+ end
37
+
38
+ it 'raises DuplicateResource if adding an Resource with a name that already exists' do
39
+ proc { populated_resource_list.add(login_resource) }.must_raise Kanpachi::ResourceList::DuplicateResource
40
+ end
41
+
42
+ it 'finds a resource by http verb and url' do
43
+ populated_resource_list.find(:post, '/login').must_equal login_resource
44
+ end
45
+
46
+ it 'finds a resource by name' do
47
+ populated_resource_list.named('login').must_equal login_resource
48
+ end
49
+
50
+ it 'raises UnknownResource if resource not found by name' do
51
+ proc { populated_resource_list.named('logout') }.must_raise Kanpachi::ResourceList::UnknownResource
52
+ end
53
+ end
@@ -0,0 +1,144 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kanpachi::Resource do
4
+ describe 'initialized resource' do
5
+ subject do
6
+ Kanpachi::Resource.new(:get, '/test-me')
7
+ end
8
+
9
+ let(:params) do
10
+ Class.new(Kanpachi::Resource::Params) do
11
+ required do
12
+ integer :id
13
+ end
14
+
15
+ optional do
16
+ string :name
17
+ end
18
+ end
19
+ end
20
+
21
+ let(:required_params) do
22
+ Class.new(Kanpachi::Resource::Params) do
23
+ required do
24
+ integer :id
25
+ end
26
+ end
27
+ end
28
+
29
+ let(:optional_params) do
30
+ Class.new(Kanpachi::Resource::Params) do
31
+ optional do
32
+ string :id
33
+ end
34
+ end
35
+ end
36
+
37
+ it 'returns the http verb' do
38
+ subject.http_verb.must_equal :get
39
+ end
40
+
41
+ it 'returns the url' do
42
+ subject.url.must_equal '/test-me'
43
+ end
44
+
45
+ it 'returns the params' do
46
+ subject.params = params
47
+ subject.params.ancestors.must_include Kanpachi::Resource::Params
48
+ end
49
+
50
+ it 'returns the response list' do
51
+ responses = subject.responses
52
+ responses.must_be_instance_of Kanpachi::ResponseList
53
+ responses.all.must_be_empty
54
+ end
55
+
56
+ it 'initializes formats as an empty set' do
57
+ subject.formats.must_equal Set.new
58
+ end
59
+
60
+ it 'initializes versions as an empty set' do
61
+ subject.versions.must_equal Set.new
62
+ end
63
+
64
+ it 'initializes priority to 50' do
65
+ subject.priority.must_equal 50
66
+ end
67
+
68
+ it 'initializes ssl to false' do
69
+ subject.ssl.must_equal false
70
+ end
71
+
72
+ it 'initializes authentication to false' do
73
+ subject.authentication.must_equal false
74
+ end
75
+
76
+ it 'has a description accessor' do
77
+ subject.description = 'This resource tests me.'
78
+ subject.description.must_equal 'This resource tests me.'
79
+ end
80
+
81
+ it 'has a name accessor' do
82
+ subject.name = :test_me
83
+ subject.name.must_equal :test_me
84
+ end
85
+
86
+ it 'has a priority accessor' do
87
+ subject.priority = 100
88
+ subject.priority.must_equal 100
89
+ end
90
+
91
+ it 'has a ssl accessor' do
92
+ subject.ssl = true
93
+ subject.ssl.must_equal true
94
+ end
95
+
96
+ it 'has an authentication accessor' do
97
+ subject.authentication = true
98
+ subject.authentication.must_equal true
99
+ end
100
+
101
+ it 'has a versions accessor' do
102
+ subject.versions << '1.1'
103
+ subject.versions.must_include '1.1'
104
+ end
105
+
106
+ it 'has a formats accessor' do
107
+ subject.formats << :xml
108
+ subject.formats.must_include :xml
109
+ end
110
+
111
+ it 'has a params accessor' do
112
+ subject.params = params
113
+ subject.params.must_equal params
114
+ end
115
+
116
+ it 'returns true if the resource has defined any params' do
117
+ subject.params?.must_equal false
118
+ subject.params = required_params
119
+ subject.params?.must_equal true
120
+ subject.params = optional_params
121
+ subject.params?.must_equal true
122
+ end
123
+
124
+ it 'returns the route' do
125
+ subject.route.must_equal 'GET /test-me'
126
+ end
127
+
128
+ it 'sorts based on priority' do
129
+ middle = subject
130
+ lower = Kanpachi::Resource.new(:get, '/lower')
131
+ lower.priority = -100
132
+ higher = Kanpachi::Resource.new(:get, '/lower')
133
+ higher.priority = 100
134
+ [middle, lower, higher].sort.must_equal [lower, middle, higher]
135
+ end
136
+ end
137
+
138
+ it 'ensures the url is prefixed with a slash' do
139
+ resource = Kanpachi::Resource.new(:get, '/has-slash')
140
+ resource.url.must_equal '/has-slash'
141
+ resource = Kanpachi::Resource.new(:get, 'missing-slash')
142
+ resource.url.must_equal '/missing-slash'
143
+ end
144
+ end
@@ -0,0 +1,57 @@
1
+ require_relative 'spec_helper'
2
+
3
+ describe Kanpachi::Response do
4
+ subject do
5
+ Kanpachi::Response.new(:default)
6
+ end
7
+
8
+ it 'initializes with the status' do
9
+ subject.status.must_equal 200
10
+ end
11
+
12
+ it 'initializes with a headers hash' do
13
+ subject.headers.must_equal Hash.new
14
+ end
15
+
16
+ it 'returns headers' do
17
+ subject.headers['Content-Type'] = 'application/json'
18
+ subject.headers.keys.must_include 'Content-Type'
19
+ end
20
+
21
+ it 'has a representation accessor' do
22
+ representation = Module.new do
23
+ include Kanpachi::Response::Representation
24
+ property :title
25
+ end
26
+ subject.representation = representation
27
+ subject.representation.must_equal representation
28
+ end
29
+
30
+ it 'representation is a Roar representer' do
31
+ subject.representation.ancestors.must_include Roar::Representer
32
+ subject.representation.ancestors.must_include Roar::Representer::Feature::Hypermedia
33
+ end
34
+
35
+ it 'representation returns properties' do
36
+ user_representer = Module.new do
37
+ include Kanpachi::Response::Representation
38
+ property :name, type: String
39
+ property :email, type: String
40
+ end
41
+
42
+ representation = Module.new do
43
+ include Kanpachi::Response::Representation
44
+ property :id, type: Integer
45
+ collection :people, extend: user_representer
46
+ hash :user, extend: user_representer
47
+ end
48
+ representation.properties.keys.must_include 'id'
49
+ representation.properties.keys.must_include 'people'
50
+ representation.properties.keys.must_include 'user'
51
+ representation.properties['id'][:type].must_equal Integer
52
+ representation.properties['people'][:collection].must_equal true
53
+ representation.properties['people'][:default].must_be_empty
54
+ representation.properties['people']['name'][:type].must_equal String
55
+ representation.properties['people']['email'][:type].must_equal String
56
+ end
57
+ end