cathode 0.0.1 → 0.1.0

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 (77) hide show
  1. checksums.yaml +4 -4
  2. data/README.md +346 -125
  3. data/Rakefile +1 -0
  4. data/app/controllers/cathode/base_controller.rb +28 -45
  5. data/app/models/cathode/token.rb +21 -0
  6. data/config/routes.rb +16 -0
  7. data/db/migrate/20140425164100_create_cathode_tokens.rb +11 -0
  8. data/lib/cathode.rb +0 -13
  9. data/lib/cathode/_version.rb +2 -1
  10. data/lib/cathode/action.rb +197 -39
  11. data/lib/cathode/action_dsl.rb +60 -0
  12. data/lib/cathode/base.rb +81 -12
  13. data/lib/cathode/create_request.rb +21 -0
  14. data/lib/cathode/custom_request.rb +5 -0
  15. data/lib/cathode/debug.rb +25 -0
  16. data/lib/cathode/destroy_request.rb +13 -0
  17. data/lib/cathode/engine.rb +9 -0
  18. data/lib/cathode/exceptions.rb +20 -0
  19. data/lib/cathode/index_request.rb +40 -0
  20. data/lib/cathode/object_collection.rb +49 -0
  21. data/lib/cathode/query.rb +24 -0
  22. data/lib/cathode/railtie.rb +21 -0
  23. data/lib/cathode/request.rb +139 -7
  24. data/lib/cathode/resource.rb +50 -19
  25. data/lib/cathode/resource_dsl.rb +46 -0
  26. data/lib/cathode/show_request.rb +13 -0
  27. data/lib/cathode/update_request.rb +26 -0
  28. data/lib/cathode/version.rb +112 -23
  29. data/lib/tasks/cathode_tasks.rake +5 -4
  30. data/spec/dummy/app/api/api.rb +0 -0
  31. data/spec/dummy/app/models/payment.rb +3 -0
  32. data/spec/dummy/app/models/product.rb +1 -0
  33. data/spec/dummy/app/models/sale.rb +5 -0
  34. data/spec/dummy/app/models/salesperson.rb +3 -0
  35. data/spec/dummy/db/development.sqlite3 +0 -0
  36. data/spec/dummy/db/migrate/20140409183635_create_sales.rb +11 -0
  37. data/spec/dummy/db/migrate/20140423172419_create_salespeople.rb +11 -0
  38. data/spec/dummy/db/migrate/20140424181343_create_payments.rb +10 -0
  39. data/spec/dummy/db/schema.rb +31 -1
  40. data/spec/dummy/db/test.sqlite3 +0 -0
  41. data/spec/dummy/log/development.log +1167 -0
  42. data/spec/dummy/log/test.log +180602 -0
  43. data/spec/dummy/spec/factories/payments.rb +8 -0
  44. data/spec/dummy/spec/factories/products.rb +1 -1
  45. data/spec/dummy/spec/factories/sales.rb +9 -0
  46. data/spec/dummy/spec/factories/salespeople.rb +7 -0
  47. data/spec/dummy/spec/requests/requests_spec.rb +434 -0
  48. data/spec/lib/cathode/action_spec.rb +136 -0
  49. data/spec/lib/cathode/base_spec.rb +34 -0
  50. data/spec/lib/cathode/create_request_spec.rb +40 -0
  51. data/spec/lib/cathode/custom_request_spec.rb +31 -0
  52. data/spec/lib/cathode/debug_spec.rb +25 -0
  53. data/spec/lib/cathode/destroy_request_spec.rb +28 -0
  54. data/spec/lib/cathode/index_request_spec.rb +62 -0
  55. data/spec/lib/cathode/object_collection_spec.rb +66 -0
  56. data/spec/lib/cathode/query_spec.rb +28 -0
  57. data/spec/lib/cathode/request_spec.rb +58 -0
  58. data/spec/lib/cathode/resource_spec.rb +482 -0
  59. data/spec/lib/cathode/show_request_spec.rb +23 -0
  60. data/spec/lib/cathode/update_request_spec.rb +41 -0
  61. data/spec/lib/cathode/version_spec.rb +416 -0
  62. data/spec/models/cathode/token_spec.rb +62 -0
  63. data/spec/spec_helper.rb +8 -2
  64. data/spec/support/factories/payments.rb +3 -0
  65. data/spec/support/factories/sale.rb +3 -0
  66. data/spec/support/factories/salespeople.rb +3 -0
  67. data/spec/support/factories/token.rb +3 -0
  68. data/spec/support/helpers.rb +13 -2
  69. metadata +192 -47
  70. data/app/helpers/cathode/application_helper.rb +0 -4
  71. data/spec/dummy/app/api/dummy_api.rb +0 -4
  72. data/spec/integration/api_spec.rb +0 -88
  73. data/spec/lib/action_spec.rb +0 -140
  74. data/spec/lib/base_spec.rb +0 -28
  75. data/spec/lib/request_spec.rb +0 -5
  76. data/spec/lib/resources_spec.rb +0 -78
  77. data/spec/lib/versioning_spec.rb +0 -104
@@ -1,4 +0,0 @@
1
- module Cathode
2
- module ApplicationHelper
3
- end
4
- end
@@ -1,4 +0,0 @@
1
- puts 'here'
2
- class DummyApi < Cathode::Base
3
- resource :products, actions: [:index]
4
- end
@@ -1,88 +0,0 @@
1
- require 'spec_helper'
2
-
3
- def make_request(method, path, params = nil, version = '1.0.0')
4
- send(method, path, params, { 'Accept-Version' => version })
5
- end
6
-
7
- describe 'API' do
8
- context 'with the default API (all actions)' do
9
- before(:all) do
10
- use_api do
11
- version 1.5 do
12
- resource :products, actions: [:all]
13
- end
14
- end
15
- end
16
-
17
- let!(:products) { create_list(:product, 5) }
18
-
19
- describe 'GET #index' do
20
- subject { make_request :get, 'api/products' }
21
-
22
- it 'responds with all records' do
23
- subject
24
- expect(response.body).to eq(products.to_json)
25
- end
26
- end
27
-
28
- describe 'GET #show' do
29
- subject { make_request :get, 'api/products/1', nil, '1.5' }
30
-
31
- it 'responds with the record' do
32
- subject
33
- expect(response.body).to eq(products.first.to_json)
34
- end
35
- end
36
-
37
- describe 'POST #create' do
38
- subject { make_request :post, 'api/products', product: { title: 'hello', cost: 1900 } }
39
-
40
- it 'responds with the new record' do
41
- subject
42
- expect(response.body).to eq(Product.new(title: 'hello', cost: 1900).to_json)
43
- end
44
- end
45
-
46
- describe 'DELETE #destroy' do
47
- subject { make_request :delete, 'api/products/1' }
48
-
49
- it 'responds with success' do
50
- expect(subject).to eq(200)
51
- end
52
- end
53
- end
54
-
55
- context 'with an access filter' do
56
- subject { make_request :get, 'api/products/1' }
57
-
58
- let!(:products) { create_list(:product, 5) }
59
-
60
- context 'when inaccessible' do
61
- before(:all) do
62
- use_api do
63
- resource :products do
64
- action(:show) { access_filter { false } }
65
- end
66
- end
67
- end
68
-
69
- it 'responds with unauthorized' do
70
- expect(subject).to eq(401)
71
- end
72
- end
73
-
74
- context 'when accessible' do
75
- before(:all) do
76
- use_api do
77
- resource :products do
78
- action(:show) { access_filter { true } }
79
- end
80
- end
81
- end
82
-
83
- it 'responds with success' do
84
- expect(subject).to eq(200)
85
- end
86
- end
87
- end
88
- end
@@ -1,140 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Cathode::Action do
4
- describe '.create' do
5
- subject { Cathode::Action.create(action, :products) }
6
-
7
- context 'with :index' do
8
- let(:action) { :index }
9
-
10
- it 'creates a IndexAction' do
11
- expect(subject.class).to eq(Cathode::IndexAction)
12
- end
13
- end
14
-
15
- context 'with :show' do
16
- let(:action) { :show }
17
-
18
- it 'creates a ShowAction' do
19
- expect(subject.class).to eq(Cathode::ShowAction)
20
- end
21
- end
22
-
23
- context 'with :create' do
24
- let(:action) { :create }
25
-
26
- it 'creates a CreateAction' do
27
- expect(subject.class).to eq(Cathode::CreateAction)
28
- end
29
- end
30
-
31
- context 'with :update' do
32
- let(:action) { :update }
33
-
34
- it 'creates an UpdateAction' do
35
- expect(subject.class).to eq(Cathode::UpdateAction)
36
- end
37
- end
38
-
39
- context 'with :destroy' do
40
- let(:action) { :destroy }
41
-
42
- it 'creates a DestroyAction' do
43
- expect(subject.class).to eq(Cathode::DestroyAction)
44
- end
45
- end
46
- end
47
-
48
- describe '#perform' do
49
- let!(:products) { create_list(:product, 5) }
50
-
51
- subject { Cathode::Action.create(action, :products).perform(params) }
52
-
53
- context ':index' do
54
- let(:action) { :index }
55
- let(:params) { {} }
56
-
57
- it 'sets status as ok' do
58
- expect(subject[:status]).to eq(:ok)
59
- end
60
-
61
- it 'sets body as all resource records' do
62
- expect(subject[:body]).to eq(Product.all)
63
- end
64
- end
65
-
66
- context ':show' do
67
- let(:action) { :show }
68
- let(:params) { { :id => 3 } }
69
-
70
- subject { Cathode::Action.create(action, :products, &block).perform(params) }
71
-
72
- context 'with access filter' do
73
- context 'when accessible' do
74
- let(:block) { proc { access_filter(&proc { true }) } }
75
-
76
- it 'sets status as ok' do
77
- expect(subject[:status]).to eq(:ok)
78
- end
79
-
80
- it 'sets body as the record' do
81
- expect(subject[:body]).to eq(products[2])
82
- end
83
- end
84
-
85
- context 'when inaccessible' do
86
- let(:block) { proc { access_filter(&proc { false }) } }
87
-
88
- it 'sets status as unauthorized' do
89
- expect(subject[:status]).to eq(:unauthorized)
90
- end
91
-
92
- it 'sets body as nil' do
93
- expect(subject[:body]).to be_nil
94
- end
95
- end
96
- end
97
- end
98
-
99
- context ':create' do
100
- let(:action) { :create }
101
- let(:params) { { :title => 'cool product' } }
102
-
103
- it 'sets status as ok' do
104
- expect(subject[:status]).to eq(:ok)
105
- end
106
-
107
- it 'sets body as the new record' do
108
- expect(subject[:body].title).to eq('cool product')
109
- end
110
- end
111
-
112
- context ':update' do
113
- let(:action) { :update }
114
- let(:params) { { :id => product.id, :title => 'cooler product' } }
115
- let(:product) { create(:product, title: 'cool product') }
116
-
117
- it 'sets status as ok' do
118
- expect(subject[:status]).to eq(:ok)
119
- end
120
-
121
- it 'sets body as the updated record' do
122
- expect(subject[:body].title).to eq('cooler product')
123
- end
124
- end
125
-
126
- context ':destroy' do
127
- let(:action) { :destroy }
128
- let(:params) { { :id => product.id } }
129
- let!(:product) { create(:product) }
130
-
131
- it 'sets status as ok' do
132
- expect(subject[:status]).to eq(:ok)
133
- end
134
-
135
- it 'removes the record' do
136
- expect { subject }.to change { Product.count }.by(-1)
137
- end
138
- end
139
- end
140
- end
@@ -1,28 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Cathode::Base do
4
- describe '#version' do
5
- subject { Cathode::Base.version 1.5 do
6
- resource :products
7
- end }
8
-
9
- it 'creates a new version' do
10
- expect(subject.version).to eq('1.5.0')
11
- end
12
-
13
- it 'contains the resources' do
14
- expect(subject.resources.keys).to eq([:products])
15
- end
16
- end
17
-
18
- describe '#resource' do
19
- context 'with resource name' do
20
- subject { Cathode::Base.resource(:products) }
21
-
22
- it 'initializes version 1.0.0' do
23
- subject
24
- expect(Cathode::Version.all['1.0.0'].resources[:products]).to_not be_nil
25
- end
26
- end
27
- end
28
- end
@@ -1,5 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Cathode::Request do
4
-
5
- end
@@ -1,78 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Cathode::Resource do
4
- describe '.new' do
5
- context 'with a nonexistent resource' do
6
- subject { Cathode::Resource.new(:boxes, [:all]) }
7
-
8
- it 'raises an error' do
9
- expect { subject }.to raise_error(Cathode::UnknownResourceError)
10
- end
11
- end
12
-
13
- context 'with an existing resource' do
14
- context 'with all methods' do
15
- subject { Cathode::Resource.new(:products, actions: [:all]) }
16
-
17
- it 'creates a controller' do
18
- subject
19
- expect(Cathode::ProductsController).to_not be_nil
20
- end
21
-
22
- it 'adds index, create, update, show, delete routes' do
23
- subject
24
- expect(Cathode::Engine.routes.recognize_path('products', method: :get)).to be_true
25
- expect(Cathode::Engine.routes.recognize_path('products/1', method: :get)).to be_true
26
- expect(Cathode::Engine.routes.recognize_path('products', method: :post)).to be_true
27
- expect(Cathode::Engine.routes.recognize_path('products/1', method: :put)).to be_true
28
- expect(Cathode::Engine.routes.recognize_path('products/1', method: :delete)).to be_true
29
- end
30
-
31
- it 'does not add the edit route' do
32
- expect { Cathode::Engine.routes.recognize_path('products/1/edit', method: :get) }.to raise_error
33
- end
34
- end
35
-
36
- context 'with subset of methods' do
37
- subject { Cathode::Resource.new(:products, actions: [:index, :show]) }
38
-
39
- it 'creates a controller' do
40
- subject
41
- expect(Cathode::ProductsController).to_not be_nil
42
- end
43
-
44
- it 'adds routes only for the defined actions' do
45
- subject
46
- expect(Cathode::Engine.routes.recognize_path('products', method: :get)).to be_true
47
- expect(Cathode::Engine.routes.recognize_path('products/1', method: :get)).to be_true
48
- end
49
-
50
- it 'does not add the other routes' do
51
- subject
52
- expect { Cathode::Engine.routes.recognize_path('products/1/edit', method: :get) }.to raise_error
53
- expect { Cathode::Engine.routes.recognize_path('products', method: :post) }.to raise_error
54
- expect { Cathode::Engine.routes.recognize_path('products/1', method: :put) }.to raise_error
55
- expect { Cathode::Engine.routes.recognize_path('products/1', method: :delete) }.to raise_error
56
- end
57
- end
58
-
59
- context 'with a block action' do
60
- subject { Cathode::Resource.new(:products, &block) }
61
-
62
- context 'and plain actions' do
63
- let(:block) { proc { action :index } }
64
-
65
- it 'adds the route' do
66
- subject
67
- expect(Cathode::Engine.routes.recognize_path('products', method: :get)).to be_true
68
- end
69
-
70
- it 'does not add other actions' do
71
- subject
72
- expect { Cathode::Engine.routes.recognize_path('products', method: :post) }.to raise_error
73
- end
74
- end
75
- end
76
- end
77
- end
78
- end
@@ -1,104 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Cathode::Version do
4
- describe '.new' do
5
- subject { Cathode::Version.new(version, &block) }
6
-
7
- describe 'creation of semvers with flexible version input' do
8
- let(:block) { nil }
9
-
10
- context 'with bad version number' do
11
- let(:version) { 'a' }
12
-
13
- it 'raises an error' do
14
- expect { subject }.to raise_error(ArgumentError)
15
- end
16
- end
17
-
18
- context 'with good version' do
19
- context 'with integer' do
20
- let(:version) { 1 }
21
-
22
- it 'sets the semantic version' do
23
- expect(subject.version).to eq('1.0.0')
24
- end
25
- end
26
-
27
- context 'with float' do
28
- let(:version) { 1.5 }
29
-
30
- it 'sets the semantic version' do
31
- expect(subject.version).to eq('1.5.0')
32
- end
33
- end
34
-
35
- context 'with string' do
36
- let(:version) { '1.5.1' }
37
-
38
- it 'sets the semantic version' do
39
- expect(subject.version).to eq('1.5.1')
40
- end
41
- end
42
-
43
- context 'with prerelease' do
44
- let(:version) { '1.6.0-pre' }
45
-
46
- it 'sets the semantic version' do
47
- expect(subject.version).to eq('1.6.0-pre')
48
- end
49
- end
50
- end
51
- end
52
-
53
- describe 'creation of resources' do
54
- let(:version) { 1 }
55
-
56
- context 'with no params or block' do
57
- let(:block) { proc do
58
- resource :products
59
- end }
60
-
61
- it 'creates the resource' do
62
- expect(Cathode::Resource).to receive(:new) do |resource, params, &block|
63
- expect(resource).to eq(:products)
64
- expect(params).to be_nil
65
- expect(block).to be_nil
66
- end
67
- subject
68
- end
69
- end
70
-
71
- context 'with params' do
72
- let(:block) { proc do
73
- resource :sales, actions: [:index, :create]
74
- end }
75
-
76
- it 'creates the resource' do
77
- expect(Cathode::Resource).to receive(:new) do |resource, params, &block|
78
- expect(resource).to eq(:sales)
79
- expect(params).to eq(actions: [:index, :create])
80
- expect(block).to be_nil
81
- end
82
- subject
83
- end
84
- end
85
-
86
- context 'with params and block' do
87
- let(:block) { proc do
88
- resource :salespeople, actions: [:index] do
89
- action :create
90
- end
91
- end }
92
-
93
- it 'creates the resource' do
94
- expect(Cathode::Resource).to receive(:new) do |resource, params, &block|
95
- expect(resource).to eq(:salespeople)
96
- expect(params).to eq(actions: [:index])
97
- expect(block).to_not be_nil
98
- end
99
- subject
100
- end
101
- end
102
- end
103
- end
104
- end