Skip to content

Commit c19c75f

Browse files
authored
Merge pull request #8 from dblock/with-mongoid
Grape API with Roar and Mongoid.
2 parents 496e3c8 + bb368c0 commit c19c75f

19 files changed

+237
-192
lines changed

.rubocop_todo.yml

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# This configuration was generated by
22
# `rubocop --auto-gen-config`
3-
# on 2015-08-06 12:31:48 -0400 using RuboCop version 0.33.0.
3+
# on 2016-09-25 16:03:07 -0400 using RuboCop version 0.43.0.
44
# The point is for the user to remove these configuration records
55
# one by one as the offenses are removed from the code base.
66
# Note that changes in the inspected code, or installation of new
@@ -10,20 +10,24 @@
1010
Metrics/AbcSize:
1111
Max: 19
1212

13-
# Offense count: 18
14-
# Configuration parameters: AllowURI, URISchemes.
13+
# Offense count: 27
14+
# Configuration parameters: AllowHeredoc, AllowURI, URISchemes.
15+
# URISchemes: http, https
1516
Metrics/LineLength:
16-
Max: 152
17+
Max: 149
1718

18-
# Offense count: 1
19+
# Offense count: 2
1920
# Configuration parameters: CountComments.
2021
Metrics/MethodLength:
2122
Max: 15
2223

23-
# Offense count: 9
24+
# Offense count: 10
2425
Style/Documentation:
2526
Exclude:
27+
- 'spec/**/*'
28+
- 'test/**/*'
2629
- 'api/extensions/crud_extension.rb'
30+
- 'api/extensions/scroll_extension.rb'
2731
- 'api/presenters/paginated_presenter.rb'
2832
- 'api/presenters/root_presenter.rb'
2933
- 'api/presenters/spline_presenter.rb'
@@ -32,8 +36,3 @@ Style/Documentation:
3236
- 'app/acme_app.rb'
3337
- 'app/models/spline.rb'
3438
- 'app/root_endpoint.rb'
35-
36-
# Offense count: 1
37-
Style/DoubleNegation:
38-
Exclude:
39-
- 'app/models/spline.rb'

.travis.yml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
11
sudo: false
22

33
rvm:
4-
- 2.3.0
4+
- 2.3.1
55

66
language: ruby
77

88
cache: bundler
9+
10+
services:
11+
- mongodb

Gemfile

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,10 @@ gem 'grape-roar'
55
gem 'rack-cors'
66
gem 'grape-swagger'
77
gem 'activemodel'
8-
gem 'kaminari', require: 'kaminari/grape'
98
gem 'rack-rewrite'
109
gem 'puma'
10+
gem 'mongoid', '~> 5.0'
11+
gem 'mongoid-scroll'
1112

1213
group :development do
1314
gem 'rake'
@@ -18,4 +19,7 @@ group :test do
1819
gem 'rspec'
1920
gem 'rack-test'
2021
gem 'hyperclient'
22+
gem 'database_cleaner'
23+
gem 'fabrication'
24+
gem 'faker'
2125
end

Gemfile.lock

Lines changed: 59 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,33 @@
11
GEM
22
remote: http://rubygems.org/
33
specs:
4-
actionpack (4.2.6)
5-
actionview (= 4.2.6)
6-
activesupport (= 4.2.6)
7-
rack (~> 1.6)
8-
rack-test (~> 0.6.2)
9-
rails-dom-testing (~> 1.0, >= 1.0.5)
10-
rails-html-sanitizer (~> 1.0, >= 1.0.2)
11-
actionview (4.2.6)
12-
activesupport (= 4.2.6)
4+
activemodel (4.2.7.1)
5+
activesupport (= 4.2.7.1)
136
builder (~> 3.1)
14-
erubis (~> 2.7.0)
15-
rails-dom-testing (~> 1.0, >= 1.0.5)
16-
rails-html-sanitizer (~> 1.0, >= 1.0.2)
17-
activemodel (4.2.6)
18-
activesupport (= 4.2.6)
19-
builder (~> 3.1)
20-
activesupport (4.2.6)
7+
activesupport (4.2.7.1)
218
i18n (~> 0.7)
229
json (~> 1.7, >= 1.7.7)
2310
minitest (~> 5.1)
2411
thread_safe (~> 0.3, >= 0.3.4)
2512
tzinfo (~> 1.1)
26-
ast (2.2.0)
13+
ast (2.3.0)
2714
axiom-types (0.1.1)
2815
descendants_tracker (~> 0.0.4)
2916
ice_nine (~> 0.11.0)
3017
thread_safe (~> 0.3, >= 0.3.1)
18+
bson (4.1.1)
3119
builder (3.2.2)
3220
coercible (1.0.0)
3321
descendants_tracker (~> 0.0.1)
22+
database_cleaner (1.5.3)
3423
descendants_tracker (0.0.4)
3524
thread_safe (~> 0.3, >= 0.3.1)
3625
diff-lcs (1.2.5)
26+
enumerable-lazy (0.0.1)
3727
equalizer (0.0.11)
38-
erubis (2.7.0)
28+
fabrication (2.15.2)
29+
faker (1.6.6)
30+
i18n (~> 0.5)
3931
faraday (0.9.2)
4032
multipart-post (>= 1.2, < 3)
4133
faraday-digestauth (0.2.1)
@@ -46,26 +38,22 @@ GEM
4638
faraday_middleware (0.9.2)
4739
faraday (>= 0.7.4, < 0.10)
4840
futuroscope (0.1.11)
49-
grape (0.15.0)
41+
grape (0.17.0)
5042
activesupport
5143
builder
5244
hashie (>= 2.1.0)
5345
multi_json (>= 1.3.2)
5446
multi_xml (>= 0.5.2)
47+
mustermann19 (~> 0.4.3)
5548
rack (>= 1.3.0)
5649
rack-accept
57-
rack-mount
5850
virtus (>= 1.0.0)
59-
grape-entity (0.4.8)
60-
activesupport
61-
multi_json (>= 1.3.2)
6251
grape-roar (0.3.0)
6352
grape
6453
roar (>= 1.0)
65-
grape-swagger (0.10.4)
66-
grape (>= 0.8.0)
67-
grape-entity (< 0.5.0)
68-
hashie (3.4.3)
54+
grape-swagger (0.24.0)
55+
grape (>= 0.12.0)
56+
hashie (3.4.6)
6957
hyperclient (0.8.1)
7058
faraday
7159
faraday-digestauth
@@ -77,71 +65,70 @@ GEM
7765
i18n (0.7.0)
7866
ice_nine (0.11.2)
7967
json (1.8.3)
80-
kaminari (0.16.3)
81-
actionpack (>= 3.0.0)
82-
activesupport (>= 3.0.0)
83-
loofah (2.0.3)
84-
nokogiri (>= 1.5.9)
85-
mini_portile2 (2.0.0)
86-
minitest (5.8.4)
87-
multi_json (1.11.2)
68+
minitest (5.9.0)
69+
mongo (2.3.0)
70+
bson (~> 4.1)
71+
mongoid (5.1.4)
72+
activemodel (~> 4.0)
73+
mongo (~> 2.1)
74+
origin (~> 2.2)
75+
tzinfo (>= 0.3.37)
76+
mongoid-compatibility (0.4.0)
77+
activesupport
78+
mongoid (>= 2.0)
79+
mongoid-scroll (0.3.4)
80+
i18n
81+
mongoid (>= 3.0)
82+
mongoid-compatibility
83+
multi_json (1.12.1)
8884
multi_xml (0.5.5)
8985
multipart-post (2.0.0)
86+
mustermann19 (0.4.4)
87+
enumerable-lazy
9088
net-http-digest_auth (1.4)
91-
nokogiri (1.6.7.2)
92-
mini_portile2 (~> 2.0.0.rc2)
93-
parser (2.3.0.6)
89+
origin (2.2.0)
90+
parser (2.3.1.4)
9491
ast (~> 2.2)
9592
powerpack (0.1.1)
96-
puma (3.1.0)
97-
rack (1.6.4)
93+
puma (3.6.0)
94+
rack (2.0.1)
9895
rack-accept (0.4.5)
9996
rack (>= 0.4)
10097
rack-cors (0.4.0)
101-
rack-mount (0.8.3)
102-
rack (>= 1.0.0)
10398
rack-rewrite (1.5.1)
10499
rack-test (0.6.3)
105100
rack (>= 1.0)
106-
rails-deprecated_sanitizer (1.0.3)
107-
activesupport (>= 4.2.0.alpha)
108-
rails-dom-testing (1.0.7)
109-
activesupport (>= 4.2.0.beta, < 5.0)
110-
nokogiri (~> 1.6.0)
111-
rails-deprecated_sanitizer (>= 1.0.1)
112-
rails-html-sanitizer (1.0.3)
113-
loofah (~> 2.0)
114101
rainbow (2.1.0)
115-
rake (11.1.1)
102+
rake (11.3.0)
116103
representable (2.3.0)
117104
uber (~> 0.0.7)
118105
roar (1.0.4)
119106
representable (>= 2.0.1, < 2.4.0)
120-
rspec (3.4.0)
121-
rspec-core (~> 3.4.0)
122-
rspec-expectations (~> 3.4.0)
123-
rspec-mocks (~> 3.4.0)
124-
rspec-core (3.4.4)
125-
rspec-support (~> 3.4.0)
126-
rspec-expectations (3.4.0)
107+
rspec (3.5.0)
108+
rspec-core (~> 3.5.0)
109+
rspec-expectations (~> 3.5.0)
110+
rspec-mocks (~> 3.5.0)
111+
rspec-core (3.5.3)
112+
rspec-support (~> 3.5.0)
113+
rspec-expectations (3.5.0)
127114
diff-lcs (>= 1.2.0, < 2.0)
128-
rspec-support (~> 3.4.0)
129-
rspec-mocks (3.4.1)
115+
rspec-support (~> 3.5.0)
116+
rspec-mocks (3.5.0)
130117
diff-lcs (>= 1.2.0, < 2.0)
131-
rspec-support (~> 3.4.0)
132-
rspec-support (3.4.1)
133-
rubocop (0.38.0)
134-
parser (>= 2.3.0.6, < 3.0)
118+
rspec-support (~> 3.5.0)
119+
rspec-support (3.5.0)
120+
rubocop (0.43.0)
121+
parser (>= 2.3.1.1, < 3.0)
135122
powerpack (~> 0.1)
136123
rainbow (>= 1.99.1, < 3.0)
137124
ruby-progressbar (~> 1.7)
138125
unicode-display_width (~> 1.0, >= 1.0.1)
139-
ruby-progressbar (1.7.5)
126+
ruby-progressbar (1.8.1)
140127
thread_safe (0.3.5)
141128
tzinfo (1.2.2)
142129
thread_safe (~> 0.1)
143130
uber (0.0.15)
144-
unicode-display_width (1.0.2)
131+
unicode-display_width (1.1.1)
145132
uri_template (0.7.0)
146133
virtus (1.0.5)
147134
axiom-types (~> 0.1)
@@ -154,11 +141,15 @@ PLATFORMS
154141

155142
DEPENDENCIES
156143
activemodel
144+
database_cleaner
145+
fabrication
146+
faker
157147
grape
158148
grape-roar
159149
grape-swagger
160150
hyperclient
161-
kaminari
151+
mongoid (~> 5.0)
152+
mongoid-scroll
162153
puma
163154
rack-cors
164155
rack-rewrite
@@ -168,4 +159,4 @@ DEPENDENCIES
168159
rubocop
169160

170161
BUNDLED WITH
171-
1.10.6
162+
1.12.5

README.md

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,11 @@
1-
Grape API with Roar
2-
===================
1+
Grape API with Roar & Mongoid
2+
=============================
33

4-
[![Build Status](https://travis-ci.org/ruby-grape/grape-with-roar.svg?branch=master)](https://travis-ci.org/ruby-grape/grape-with-roar)
5-
[![Dependency Status](https://gemnasium.com/ruby-grape/grape-with-roar.svg)](https://gemnasium.com/ruby-grape/grape-with-roar)
6-
[![Code Climate](https://codeclimate.com/github/ruby-grape/grape-with-roar.svg)](https://codeclimate.com/github/ruby-grape/grape-with-roar)
4+
[![Build Status](https://travis-ci.org/ruby-grape/grape-with-roar.svg?branch=with_mongoid)](https://travis-ci.org/ruby-grape/grape-with-roar?branch=with_mongoid)
5+
[![Dependency Status](https://gemnasium.com/ruby-grape/grape-with-roar.svg?branch=with_mongoid)](https://gemnasium.com/ruby-grape/grape-with-roar?branch=with_mongoid)
6+
[![Code Climate](https://codeclimate.com/github/ruby-grape/grape-with-roar.svg?branch=with_mongoid)](https://codeclimate.com/github/ruby-grape/grape-with-roar?branch=with_mongoid)
77

8-
A [Grape](http://github.com/ruby-grape/grape) API that uses [Roar](https://github.com/apotonick/roar).
9-
10-
See
11-
---
12-
13-
Try me [here](http://grape-with-roar.herokuapp.com/). In Chrome, best viewed with the [JSONView Plugin](https://chrome.google.com/webstore/detail/jsonview/chklaanhfefbnpoihckbnefhakgolnmc?hl=en).
8+
A [Grape](http://github.com/ruby-grape/grape) API that uses [Roar](https://github.com/apotonick/roar) and Mongoid with a MongoDB database.
149

1510
Run
1611
---
@@ -36,7 +31,7 @@ This is a good demo to use with [Hyperclient](https://github.com/codegram/hyperc
3631
ActiveRecord
3732
------------
3833

39-
This project uses ActiveModel and defines a dummy [Spline](app/models/spline.rb) class. Check out the [with-activerecord branch](https://github.com/ruby-grape/grape-with-roar/tree/with-activerecord) for a complete example that uses ActiveRecord and a PostgreSQL database.
34+
This project uses Mongoid and defines a dummy [Spline](app/models/spline.rb) class. Check out the [master branch](https://github.com/ruby-grape/grape-with-roar) for a complete example that uses ActiveModel and [with-activerecord branch](https://github.com/ruby-grape/grape-with-roar/tree/with-activerecord) for a complete example that uses ActiveRecord and a PostgreSQL database.
4035

4136
Copyright and License
4237
---------------------

api/extensions/crud_extension.rb

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@ module Grape
22
module Extensions
33
module CrudExtension
44
def create(type, options = {})
5-
instance = type.new
6-
instance.extend options[:with]
7-
instance.from_hash options[:from]
8-
yield instance if block_given?
9-
instance.save!
5+
instance = type.create! options[:from].to_h
106
present instance, with: options[:with]
117
end
128

139
def update(instance, options = {})
14-
instance.extend options[:with]
15-
instance.from_hash options[:from]
16-
yield instance if block_given?
17-
instance.save!
10+
instance.update_attributes! options[:from].to_h
1811
present instance, with: options[:with]
1912
end
2013

2114
def delete(instance, options = {})
22-
yield instance if block_given?
2315
instance.destroy
2416
present instance, with: options[:with]
2517
end

api/extensions/scroll_extension.rb

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
module Grape
2+
module Extensions
3+
module ScrollExtension
4+
# apply cursor-based pagination to a collection
5+
# returns a hash:
6+
# results: (paginated collection subset)
7+
# next: (cursor to the next page)
8+
def paginate_by_cursor(coll, &_block)
9+
results = { results: [], next: nil }
10+
size = (params[:size] || 10).to_i
11+
# some items may be skipped with a block
12+
coll.limit(size).scroll(params[:cursor]) do |record, next_cursor|
13+
results[:results] << record
14+
results[:next] = next_cursor.to_s
15+
end
16+
results
17+
end
18+
19+
Grape::Endpoint.send(:include, self)
20+
end
21+
end
22+
end

0 commit comments

Comments
 (0)