grape-resources 0.0.1.alpha
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +22 -0
- data/.travis.yml +14 -0
- data/Gemfile +8 -0
- data/LICENSE.txt +22 -0
- data/README.md +73 -0
- data/Rakefile +2 -0
- data/grape-resources.gemspec +36 -0
- data/lib/grape/resources.rb +103 -0
- data/lib/grape/resources/version.rb +5 -0
- data/spec/factories/car_factory.rb +7 -0
- data/spec/factories/user_factory.rb +6 -0
- data/spec/lib/grape/resources_spec.rb +218 -0
- data/spec/spec_helper.rb +56 -0
- data/spec/support/examples/api_example.rb +7 -0
- data/spec/support/models/car.rb +3 -0
- data/spec/support/models/user.rb +4 -0
- data/spec/support/schema.rb +13 -0
- metadata +255 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 837961eaaf5f621932eec50334d3cf76b3036b2b
|
4
|
+
data.tar.gz: 618ca3bb4d8ef74cf314018e0a4de72a3c476426
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: f14e1a80d8a32848d668b6010a192a8acb103c9812c09d02361ff6cc603f761578a5a8a1b67bc8f935df2739ba8213e6cf6aaa19bbe83c8c8d77864d1b6a556c
|
7
|
+
data.tar.gz: 2ca3c1960f2d0e6b72260c41c46f3b583d410b07806c519cbcc0bc2832ce5651be670ed3c2cb41b4a5b53a32556aab2cacc0e9bbf06ff79493b321bab98cae8d
|
data/.gitignore
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
*.gem
|
2
|
+
*.rbc
|
3
|
+
.bundle
|
4
|
+
.config
|
5
|
+
.yardoc
|
6
|
+
Gemfile.lock
|
7
|
+
InstalledFiles
|
8
|
+
_yardoc
|
9
|
+
coverage
|
10
|
+
doc/
|
11
|
+
lib/bundler/man
|
12
|
+
pkg
|
13
|
+
rdoc
|
14
|
+
spec/reports
|
15
|
+
test/tmp
|
16
|
+
test/version_tmp
|
17
|
+
tmp
|
18
|
+
*.bundle
|
19
|
+
*.so
|
20
|
+
*.o
|
21
|
+
*.a
|
22
|
+
mkmf.log
|
data/.travis.yml
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
language: ruby
|
2
|
+
cache: bundler
|
3
|
+
|
4
|
+
rvm:
|
5
|
+
- 2.1.2
|
6
|
+
|
7
|
+
script: 'CODECLIMATE_REPO_TOKEN=7bddc45388d2b42ac29153b0539f60836f5bc3acf7dd0b93dfa3bd8e4f2adcbc bundle exec rspec'
|
8
|
+
|
9
|
+
notifications:
|
10
|
+
email:
|
11
|
+
recipients:
|
12
|
+
- my@email.com
|
13
|
+
on_failure: change
|
14
|
+
on_success: never
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Israel De La Hoz
|
2
|
+
|
3
|
+
MIT License
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
6
|
+
a copy of this software and associated documentation files (the
|
7
|
+
"Software"), to deal in the Software without restriction, including
|
8
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
9
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
10
|
+
permit persons to whom the Software is furnished to do so, subject to
|
11
|
+
the following conditions:
|
12
|
+
|
13
|
+
The above copyright notice and this permission notice shall be
|
14
|
+
included in all copies or substantial portions of the Software.
|
15
|
+
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
18
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
19
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
20
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
21
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
22
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,73 @@
|
|
1
|
+
[![Code Climate](https://codeclimate.com/github/wawandco/grape-resources.png)](https://codeclimate.com/github/wawandco/grape-resources)
|
2
|
+
[![Build Status](https://travis-ci.org/wawandco/grape-resources.svg?branch=config-adding-travis)](https://travis-ci.org/wawandco/grape-resources)
|
3
|
+
[![Test Coverage](https://codeclimate.com/github/wawandco/grape-resources/badges/coverage.svg)](https://codeclimate.com/github/wawandco/grape-resources)
|
4
|
+
# Grape::Resources
|
5
|
+
|
6
|
+
Grape-resources is an extension of the grape API framework that allows to scaffold easily models that could not contain much logic, it allows you also to specify wich of the REST methods to scaffold in case you dont want to generate those all.
|
7
|
+
|
8
|
+
|
9
|
+
## Installation
|
10
|
+
|
11
|
+
Add this line to your application's Gemfile:
|
12
|
+
|
13
|
+
gem 'grape-resources'
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install grape-resources
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Imagine you want to build the basic CRUD for one of your models inside your grap API, for the example lets say its called Player.
|
26
|
+
|
27
|
+
In your api/my_api.rb
|
28
|
+
|
29
|
+
```ruby
|
30
|
+
|
31
|
+
class MyApi::API < Grape::API
|
32
|
+
resources_for(Player)
|
33
|
+
end
|
34
|
+
|
35
|
+
```
|
36
|
+
This should generate by default the following routes:
|
37
|
+
|
38
|
+
GET /players
|
39
|
+
GET /player/:id
|
40
|
+
POST /player
|
41
|
+
PUT /player/:id
|
42
|
+
DELETE /player/:id
|
43
|
+
|
44
|
+
In case you only want some of the routes you can specify these to the resource method like:
|
45
|
+
|
46
|
+
```ruby
|
47
|
+
|
48
|
+
class MyApi::API < Grape::API
|
49
|
+
resources_for(Player, [:list, :get])
|
50
|
+
end
|
51
|
+
|
52
|
+
```
|
53
|
+
|
54
|
+
And again this should only generate the following routes:
|
55
|
+
|
56
|
+
GET /players
|
57
|
+
GET /player/:id
|
58
|
+
|
59
|
+
Available options for routes are:
|
60
|
+
|
61
|
+
:list -> [GET] /players
|
62
|
+
:get -> [GET] /player/:id
|
63
|
+
:post -> [POST] /player
|
64
|
+
:put -> [PUT] /player/:id
|
65
|
+
:delete -> [DELETE /player/:id
|
66
|
+
|
67
|
+
## Contributing
|
68
|
+
|
69
|
+
1. Fork it ( https://github.com/[my-github-username]/grape-resources/fork )
|
70
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
71
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
72
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
73
|
+
5. Create a new Pull Request
|
data/Rakefile
ADDED
@@ -0,0 +1,36 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'grape/resources/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "grape-resources"
|
8
|
+
spec.version = Grape::Resources::VERSION
|
9
|
+
spec.authors = ["Antonio Pagano, Israel De La Hoz"]
|
10
|
+
spec.email = ["israeldelahoz@gmail.com", "ap@wawand.co"]
|
11
|
+
spec.summary = %q{ Grape resources provides the initial scaffolding for a model in a Grape api object }
|
12
|
+
spec.description = %q{ Grape resources provides the initial scaffolding for a model in a Grape api object, this gem in inspired by inherited-resources gem, and aims to solve this need inside a grape API.}
|
13
|
+
spec.homepage = "https://github.com/wawandco/grape-resources"
|
14
|
+
spec.license = "MIT"
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
17
|
+
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
|
18
|
+
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
|
19
|
+
spec.require_paths = ["lib"]
|
20
|
+
|
21
|
+
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
+
spec.add_development_dependency "rake", '~> 10'
|
23
|
+
spec.add_development_dependency "rspec", '~> 3'
|
24
|
+
spec.add_development_dependency "sqlite3", '~> 1.3'
|
25
|
+
spec.add_development_dependency "factory_girl", '~> 3'
|
26
|
+
spec.add_development_dependency 'pry', '~> 0'
|
27
|
+
spec.add_development_dependency 'database_cleaner', '~> 1.3'
|
28
|
+
spec.add_development_dependency "rack-test", '~> 0'
|
29
|
+
spec.add_development_dependency "codeclimate-test-reporter", '~> 0'
|
30
|
+
|
31
|
+
|
32
|
+
spec.add_runtime_dependency "grape", '~> 0.9'
|
33
|
+
spec.add_runtime_dependency "activerecord", '~> 4'
|
34
|
+
spec.add_runtime_dependency "activesupport", '~> 4'
|
35
|
+
spec.add_runtime_dependency 'active_model_serializers', '~> 0.9'
|
36
|
+
end
|
@@ -0,0 +1,103 @@
|
|
1
|
+
require "grape"
|
2
|
+
require "grape/resources/version"
|
3
|
+
|
4
|
+
module Grape
|
5
|
+
class API
|
6
|
+
include Grape::Resources
|
7
|
+
class << self
|
8
|
+
def resources_for( clazz, methods=[:list, :get, :post, :put, :delete])
|
9
|
+
singular_name = clazz.name.underscore
|
10
|
+
plural_name = clazz.name.pluralize.underscore
|
11
|
+
|
12
|
+
raise Error("To use grape_resources on a given class it should inherit from ActiveRecord::Base.( at least for now buddy ;) )") unless clazz < ActiveRecord::Base
|
13
|
+
|
14
|
+
Grape::Resources.list_endpoint_for( clazz, self ) if methods.include?(:list)
|
15
|
+
Grape::Resources.get_endpoint_for( clazz, self ) if methods.include?(:get)
|
16
|
+
Grape::Resources.create_endpoint_for( clazz, self ) if methods.include?(:post)
|
17
|
+
Grape::Resources.update_endpoint_for( clazz, self ) if methods.include?(:put)
|
18
|
+
Grape::Resources.delete_endpoint_for(clazz, self) if methods.include?(:delete)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
module Resources
|
24
|
+
class << self
|
25
|
+
|
26
|
+
def list_endpoint_for(clazz, api_instance)
|
27
|
+
plural_name = clazz.name.pluralize.underscore
|
28
|
+
|
29
|
+
api_instance.route('GET', ["/#{plural_name}"], {} ) do
|
30
|
+
result = Grape::Resources.list(clazz, params)
|
31
|
+
result
|
32
|
+
end
|
33
|
+
end
|
34
|
+
|
35
|
+
def get_endpoint_for(clazz, api_instance)
|
36
|
+
singular_name = singular_name_for clazz
|
37
|
+
api_instance.route('GET', ["/#{singular_name }/:id"], {}) do
|
38
|
+
result = Grape::Resources.find(clazz, params)
|
39
|
+
error!( "#{singular_name} not found", 404) if result.nil?
|
40
|
+
result
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
44
|
+
def delete_endpoint_for(clazz, api_instance)
|
45
|
+
singular_name = singular_name_for clazz
|
46
|
+
|
47
|
+
api_instance.route('DELETE', ["/#{singular_name}/:id"], {}) do
|
48
|
+
result = Grape::Resources.find(clazz, params)
|
49
|
+
error!( "#{singular_name} not found", 404) if result.nil?
|
50
|
+
result.destroy
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
def create_endpoint_for(clazz, api_instance)
|
55
|
+
singular_name = singular_name_for clazz
|
56
|
+
api_instance.route('POST', ["/#{singular_name}"], {}) do
|
57
|
+
result = clazz.new
|
58
|
+
|
59
|
+
Grape::Resources.apply_attributes(result, params)
|
60
|
+
error!( {error: "#{singular_name} is not valid", errors: result.errors.full_messages}, 405) unless result.valid?
|
61
|
+
|
62
|
+
result.save
|
63
|
+
end
|
64
|
+
end
|
65
|
+
|
66
|
+
def update_endpoint_for(clazz, api_instance)
|
67
|
+
singular_name = singular_name_for clazz
|
68
|
+
|
69
|
+
api_instance.route('PUT', ["/#{singular_name}/:id"], {}) do
|
70
|
+
result = clazz.find_by_id(params[:id])
|
71
|
+
error!( {error: "#{singular_name} with id '#{params[:id]}' was not found"}, 404) unless result.present?
|
72
|
+
|
73
|
+
Grape::Resources.apply_attributes(result, params)
|
74
|
+
error!( {error: "#{singular_name} is not valid", errors: result.errors.full_messages}, 405) unless result.valid?
|
75
|
+
|
76
|
+
result.save
|
77
|
+
result
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
def apply_attributes(instance, params)
|
82
|
+
instance.attributes.each do |attribute|
|
83
|
+
attribute_name = attribute[0]
|
84
|
+
instance.send("#{attribute_name}=",params[attribute_name.to_sym]) if params[attribute_name.to_sym]
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
def list(clazz, params)
|
89
|
+
result = clazz.all
|
90
|
+
end
|
91
|
+
|
92
|
+
def find(clazz, params)
|
93
|
+
result = clazz.find_by_id( params[:id])
|
94
|
+
end
|
95
|
+
|
96
|
+
def singular_name_for( clazz )
|
97
|
+
clazz.name.underscore
|
98
|
+
end
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
|
@@ -0,0 +1,218 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'support/examples/api_example'
|
3
|
+
|
4
|
+
describe Grape::Resources do
|
5
|
+
subject {
|
6
|
+
Class.new(APIExample)
|
7
|
+
}
|
8
|
+
|
9
|
+
def app
|
10
|
+
subject
|
11
|
+
end
|
12
|
+
|
13
|
+
describe "gem should ensure the class passed is a subclass of ActiveRecord::Base" do
|
14
|
+
it "should raise an error if the class passed is not a subclass of ActiveRecord::Base" do
|
15
|
+
clazz = Object.const_set("NotActiveRecord".classify, Class.new)
|
16
|
+
expect{
|
17
|
+
subject.resources_for(clazz)
|
18
|
+
}.to raise_error
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe "routes adding feature" do
|
23
|
+
|
24
|
+
before do
|
25
|
+
subject.resources_for(User)
|
26
|
+
subject.format :json
|
27
|
+
end
|
28
|
+
|
29
|
+
it "should respond to [GET] /users.json" do
|
30
|
+
get "/users.json"
|
31
|
+
expect(last_response.status).to eql 200
|
32
|
+
end
|
33
|
+
|
34
|
+
it "should respond to [GET] /user/:id" do
|
35
|
+
user = create(:user)
|
36
|
+
get "/user/#{user.id}"
|
37
|
+
expect(last_response.status).to eql 200
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should respond to [POST] /user" do
|
41
|
+
post "/user", name: "Some name"
|
42
|
+
expect(last_response.status).to eql 201
|
43
|
+
end
|
44
|
+
|
45
|
+
it "should respond to [PUT] /user/:id" do
|
46
|
+
user = create :user
|
47
|
+
put "/user/#{user.id}"
|
48
|
+
expect(last_response.status).to eql 200
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should respond to [DELETE] /user/:id" do
|
52
|
+
user = create(:user)
|
53
|
+
delete "/user/#{user.id}"
|
54
|
+
expect(last_response.status).to eql 200
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
describe "GET endpoint" do
|
59
|
+
before do
|
60
|
+
subject.resources_for(User)
|
61
|
+
end
|
62
|
+
|
63
|
+
it "[GET] /plural should return the list of elements" do
|
64
|
+
user = create(:user)
|
65
|
+
get "/users.json"
|
66
|
+
expect( JSON.parse(last_response.body).size ).to eql User.count
|
67
|
+
end
|
68
|
+
|
69
|
+
it "[GET] /singular/:id should return the instance we're looking for" do
|
70
|
+
user = create(:user)
|
71
|
+
get "/user/#{user.id}.json"
|
72
|
+
expect( JSON.parse(last_response.body)["name"] ).to eql user.name
|
73
|
+
end
|
74
|
+
|
75
|
+
it "[GET] /singular/:id should return 404 if id doesnt match" do
|
76
|
+
get "/user/#{12323}.json"
|
77
|
+
expect( last_response.status ).to be 404
|
78
|
+
end
|
79
|
+
end
|
80
|
+
|
81
|
+
describe "POST endpoint" do
|
82
|
+
before do
|
83
|
+
subject.resources_for(User)
|
84
|
+
end
|
85
|
+
|
86
|
+
it "[POST] /singular should return 405 if model validation returns false" do
|
87
|
+
post "/user", email: "some@email.com"
|
88
|
+
expect(last_response.status).to be 405
|
89
|
+
end
|
90
|
+
|
91
|
+
it "[POST] /singular should not create model if model validation returns false" do
|
92
|
+
expect{
|
93
|
+
post "/user", email: "some@email.com"
|
94
|
+
}.not_to change{ User.count }
|
95
|
+
end
|
96
|
+
|
97
|
+
it "[POST] /singular should return 201 if model was created" do
|
98
|
+
post "/user", email: "some@email.com", name: "Juan Perez"
|
99
|
+
expect(last_response.status).to be 201
|
100
|
+
end
|
101
|
+
|
102
|
+
it "[POST] /singular should create a new record of the instance" do
|
103
|
+
expect{
|
104
|
+
post "/user", email: "some@email.com", name: "Juan Perez"
|
105
|
+
}.to change{ User.count }
|
106
|
+
end
|
107
|
+
|
108
|
+
it "[POST] /singular should create a new record of the instance" do
|
109
|
+
post "/user", email: "some@email.com", name: "Something Special"
|
110
|
+
expect(User.last.name).to eql "Something Special"
|
111
|
+
end
|
112
|
+
|
113
|
+
it "[POST] /singular should return validation errors on the response.body" do
|
114
|
+
post "/user"
|
115
|
+
expect(last_response.body).to include("Name can't be blank")
|
116
|
+
end
|
117
|
+
end
|
118
|
+
|
119
|
+
describe "PUT endpoint" do
|
120
|
+
before do
|
121
|
+
subject.resources_for(User)
|
122
|
+
end
|
123
|
+
|
124
|
+
it "[PUT] /singular/:id should respond 404 if passed id doesnt correspond to valid entity" do
|
125
|
+
put "/user/23888383", name: "Juan", email: "some@email"
|
126
|
+
expect(last_response.status).to eql 404
|
127
|
+
end
|
128
|
+
|
129
|
+
it "[PUT] /singular/:id should 200 if passed id correspond to an existing entity" do
|
130
|
+
user = create(:user)
|
131
|
+
put "/user/#{user.id}", name: "Juan", email: "some@email"
|
132
|
+
expect(last_response.status).to eql 200
|
133
|
+
end
|
134
|
+
|
135
|
+
it "[PUT] /singular/:id should update existing entity we passed the id on the route" do
|
136
|
+
user = create(:user)
|
137
|
+
put "/user/#{user.id}", name: "Juan", email: "some@email"
|
138
|
+
expect(user.reload.name).to eql("Juan")
|
139
|
+
end
|
140
|
+
|
141
|
+
it "[PUT] /singular/:id should respond 405 if passed parameters cause broke validation" do
|
142
|
+
user = create(:user)
|
143
|
+
put "/user/#{user.id}", name: "", email: "some@email"
|
144
|
+
expect( last_response.status).to eql(405)
|
145
|
+
end
|
146
|
+
end
|
147
|
+
|
148
|
+
describe "DELETE endpoint" do
|
149
|
+
before do
|
150
|
+
subject.resources_for(User)
|
151
|
+
end
|
152
|
+
|
153
|
+
it "[DELETE] /singular/:id should delete the row with the same id" do
|
154
|
+
user = create(:user)
|
155
|
+
expect{
|
156
|
+
delete "/user/#{user.id}"
|
157
|
+
}.to change{ User.count }
|
158
|
+
end
|
159
|
+
|
160
|
+
it "[DELETE] /singular/:id should return 404 if id not found" do
|
161
|
+
delete "/user/#{12323}.json"
|
162
|
+
expect( last_response.status ).to be 404
|
163
|
+
end
|
164
|
+
|
165
|
+
it "[DELETE] /singular/:id should return 404 if no id passed" do
|
166
|
+
delete "/user"
|
167
|
+
expect( last_response.status ).to be 405
|
168
|
+
end
|
169
|
+
|
170
|
+
end
|
171
|
+
|
172
|
+
describe "endpoints selection" do
|
173
|
+
before do
|
174
|
+
subject.resources_for(User, [:get] )
|
175
|
+
end
|
176
|
+
|
177
|
+
it "should respond 200 to the GET endpoint" do
|
178
|
+
user = create :user
|
179
|
+
get "/user/#{user.id}"
|
180
|
+
expect(last_response.status).to eql(200)
|
181
|
+
end
|
182
|
+
|
183
|
+
it "should respond 404 to the DELETE endpoint" do
|
184
|
+
user = create :user
|
185
|
+
delete "/user/#{user.id}"
|
186
|
+
expect(last_response.status).to eql(405)
|
187
|
+
end
|
188
|
+
|
189
|
+
end
|
190
|
+
|
191
|
+
describe "it should be able to handle 2 or more resources with different actions" do
|
192
|
+
before do
|
193
|
+
subject.resources_for(User, [:get] )
|
194
|
+
subject.resources_for(Car)
|
195
|
+
end
|
196
|
+
|
197
|
+
it "should respond 200 to the GET endpoint" do
|
198
|
+
user = create :user
|
199
|
+
get "/user/#{user.id}"
|
200
|
+
expect(last_response.status).to eql(200)
|
201
|
+
end
|
202
|
+
|
203
|
+
it "should respond 404 to the DELETE endpoint" do
|
204
|
+
user = create :user
|
205
|
+
delete "/user/#{user.id}"
|
206
|
+
expect(last_response.status).to eql(405)
|
207
|
+
end
|
208
|
+
|
209
|
+
|
210
|
+
it "should respond to the DELETE endpoint with a 201" do
|
211
|
+
car = create :car
|
212
|
+
expect{
|
213
|
+
delete "/car/#{car.id}"
|
214
|
+
}.to change{Car.count}
|
215
|
+
end
|
216
|
+
|
217
|
+
end
|
218
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
|
2
|
+
require "codeclimate-test-reporter"
|
3
|
+
CodeClimate::TestReporter.start
|
4
|
+
|
5
|
+
require "grape"
|
6
|
+
require 'grape/resources'
|
7
|
+
require 'active_record'
|
8
|
+
require 'database_cleaner'
|
9
|
+
require 'factory_girl'
|
10
|
+
require "rack/test"
|
11
|
+
|
12
|
+
ENV["RAILS_ENV"] = "test"
|
13
|
+
|
14
|
+
RSpec.configure do |config|
|
15
|
+
# Remove this line if you don't want RSpec's should and should_not
|
16
|
+
I18n.enforce_available_locales = false
|
17
|
+
ActiveRecord::Base.establish_connection(
|
18
|
+
:adapter => 'sqlite3',
|
19
|
+
:database => ':memory:'
|
20
|
+
)
|
21
|
+
|
22
|
+
load File.dirname(__FILE__) + '/support/schema.rb'
|
23
|
+
|
24
|
+
|
25
|
+
Dir["#{File.dirname(__FILE__)}/support/models/*.rb"].each {|f| require f}
|
26
|
+
Dir["#{File.dirname(__FILE__)}/factories/*.rb"].each {|f| require f }
|
27
|
+
Dir["#{File.dirname(__FILE__)}/support/*.rb"].each{ |f| require f }
|
28
|
+
|
29
|
+
# methods or matchers
|
30
|
+
require 'rspec/expectations'
|
31
|
+
config.include RSpec::Matchers
|
32
|
+
|
33
|
+
config.include Rack::Test::Methods
|
34
|
+
|
35
|
+
# == Mock Framework
|
36
|
+
config.mock_with :rspec
|
37
|
+
|
38
|
+
include FactoryGirl::Syntax::Methods
|
39
|
+
|
40
|
+
config.before(:suite) do
|
41
|
+
DatabaseCleaner.strategy = :transaction
|
42
|
+
end
|
43
|
+
|
44
|
+
config.before :each do
|
45
|
+
DatabaseCleaner.start
|
46
|
+
end
|
47
|
+
|
48
|
+
config.after :each do
|
49
|
+
DatabaseCleaner.clean
|
50
|
+
end
|
51
|
+
|
52
|
+
RSpec.configure do |config|
|
53
|
+
config.include Rack::Test::Methods
|
54
|
+
end
|
55
|
+
|
56
|
+
end
|
@@ -0,0 +1,13 @@
|
|
1
|
+
ActiveRecord::Schema.define(version: 20140212210648) do
|
2
|
+
|
3
|
+
create_table "users", force: true do |t|
|
4
|
+
t.string 'name'
|
5
|
+
t.string 'email'
|
6
|
+
end
|
7
|
+
|
8
|
+
create_table "cars", force: true do |t|
|
9
|
+
t.string 'name'
|
10
|
+
t.string 'model'
|
11
|
+
t.integer 'weight'
|
12
|
+
end
|
13
|
+
end
|
metadata
ADDED
@@ -0,0 +1,255 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: grape-resources
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1.alpha
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Antonio Pagano, Israel De La Hoz
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-09-06 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.6'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.6'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.3'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.3'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: factory_girl
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '3'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '3'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: pry
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: database_cleaner
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - "~>"
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '1.3'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - "~>"
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '1.3'
|
111
|
+
- !ruby/object:Gem::Dependency
|
112
|
+
name: rack-test
|
113
|
+
requirement: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - "~>"
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
type: :development
|
119
|
+
prerelease: false
|
120
|
+
version_requirements: !ruby/object:Gem::Requirement
|
121
|
+
requirements:
|
122
|
+
- - "~>"
|
123
|
+
- !ruby/object:Gem::Version
|
124
|
+
version: '0'
|
125
|
+
- !ruby/object:Gem::Dependency
|
126
|
+
name: codeclimate-test-reporter
|
127
|
+
requirement: !ruby/object:Gem::Requirement
|
128
|
+
requirements:
|
129
|
+
- - "~>"
|
130
|
+
- !ruby/object:Gem::Version
|
131
|
+
version: '0'
|
132
|
+
type: :development
|
133
|
+
prerelease: false
|
134
|
+
version_requirements: !ruby/object:Gem::Requirement
|
135
|
+
requirements:
|
136
|
+
- - "~>"
|
137
|
+
- !ruby/object:Gem::Version
|
138
|
+
version: '0'
|
139
|
+
- !ruby/object:Gem::Dependency
|
140
|
+
name: grape
|
141
|
+
requirement: !ruby/object:Gem::Requirement
|
142
|
+
requirements:
|
143
|
+
- - "~>"
|
144
|
+
- !ruby/object:Gem::Version
|
145
|
+
version: '0.9'
|
146
|
+
type: :runtime
|
147
|
+
prerelease: false
|
148
|
+
version_requirements: !ruby/object:Gem::Requirement
|
149
|
+
requirements:
|
150
|
+
- - "~>"
|
151
|
+
- !ruby/object:Gem::Version
|
152
|
+
version: '0.9'
|
153
|
+
- !ruby/object:Gem::Dependency
|
154
|
+
name: activerecord
|
155
|
+
requirement: !ruby/object:Gem::Requirement
|
156
|
+
requirements:
|
157
|
+
- - "~>"
|
158
|
+
- !ruby/object:Gem::Version
|
159
|
+
version: '4'
|
160
|
+
type: :runtime
|
161
|
+
prerelease: false
|
162
|
+
version_requirements: !ruby/object:Gem::Requirement
|
163
|
+
requirements:
|
164
|
+
- - "~>"
|
165
|
+
- !ruby/object:Gem::Version
|
166
|
+
version: '4'
|
167
|
+
- !ruby/object:Gem::Dependency
|
168
|
+
name: activesupport
|
169
|
+
requirement: !ruby/object:Gem::Requirement
|
170
|
+
requirements:
|
171
|
+
- - "~>"
|
172
|
+
- !ruby/object:Gem::Version
|
173
|
+
version: '4'
|
174
|
+
type: :runtime
|
175
|
+
prerelease: false
|
176
|
+
version_requirements: !ruby/object:Gem::Requirement
|
177
|
+
requirements:
|
178
|
+
- - "~>"
|
179
|
+
- !ruby/object:Gem::Version
|
180
|
+
version: '4'
|
181
|
+
- !ruby/object:Gem::Dependency
|
182
|
+
name: active_model_serializers
|
183
|
+
requirement: !ruby/object:Gem::Requirement
|
184
|
+
requirements:
|
185
|
+
- - "~>"
|
186
|
+
- !ruby/object:Gem::Version
|
187
|
+
version: '0.9'
|
188
|
+
type: :runtime
|
189
|
+
prerelease: false
|
190
|
+
version_requirements: !ruby/object:Gem::Requirement
|
191
|
+
requirements:
|
192
|
+
- - "~>"
|
193
|
+
- !ruby/object:Gem::Version
|
194
|
+
version: '0.9'
|
195
|
+
description: " Grape resources provides the initial scaffolding for a model in a Grape
|
196
|
+
api object, this gem in inspired by inherited-resources gem, and aims to solve this
|
197
|
+
need inside a grape API."
|
198
|
+
email:
|
199
|
+
- israeldelahoz@gmail.com
|
200
|
+
- ap@wawand.co
|
201
|
+
executables: []
|
202
|
+
extensions: []
|
203
|
+
extra_rdoc_files: []
|
204
|
+
files:
|
205
|
+
- ".gitignore"
|
206
|
+
- ".travis.yml"
|
207
|
+
- Gemfile
|
208
|
+
- LICENSE.txt
|
209
|
+
- README.md
|
210
|
+
- Rakefile
|
211
|
+
- grape-resources.gemspec
|
212
|
+
- lib/grape/resources.rb
|
213
|
+
- lib/grape/resources/version.rb
|
214
|
+
- spec/factories/car_factory.rb
|
215
|
+
- spec/factories/user_factory.rb
|
216
|
+
- spec/lib/grape/resources_spec.rb
|
217
|
+
- spec/spec_helper.rb
|
218
|
+
- spec/support/examples/api_example.rb
|
219
|
+
- spec/support/models/car.rb
|
220
|
+
- spec/support/models/user.rb
|
221
|
+
- spec/support/schema.rb
|
222
|
+
homepage: https://github.com/wawandco/grape-resources
|
223
|
+
licenses:
|
224
|
+
- MIT
|
225
|
+
metadata: {}
|
226
|
+
post_install_message:
|
227
|
+
rdoc_options: []
|
228
|
+
require_paths:
|
229
|
+
- lib
|
230
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
231
|
+
requirements:
|
232
|
+
- - ">="
|
233
|
+
- !ruby/object:Gem::Version
|
234
|
+
version: '0'
|
235
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
236
|
+
requirements:
|
237
|
+
- - ">"
|
238
|
+
- !ruby/object:Gem::Version
|
239
|
+
version: 1.3.1
|
240
|
+
requirements: []
|
241
|
+
rubyforge_project:
|
242
|
+
rubygems_version: 2.4.1
|
243
|
+
signing_key:
|
244
|
+
specification_version: 4
|
245
|
+
summary: Grape resources provides the initial scaffolding for a model in a Grape api
|
246
|
+
object
|
247
|
+
test_files:
|
248
|
+
- spec/factories/car_factory.rb
|
249
|
+
- spec/factories/user_factory.rb
|
250
|
+
- spec/lib/grape/resources_spec.rb
|
251
|
+
- spec/spec_helper.rb
|
252
|
+
- spec/support/examples/api_example.rb
|
253
|
+
- spec/support/models/car.rb
|
254
|
+
- spec/support/models/user.rb
|
255
|
+
- spec/support/schema.rb
|