grape_on_rails 1.0.1 → 1.0.2

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 2ed4afe08908b9f62bf233dc8f68f6e91103353c
4
- data.tar.gz: ed365928cf7d9c8c071f7b34923f1d627ce2a2ec
3
+ metadata.gz: 8e15a907eee08abb034de93dffe9b5deac29ab85
4
+ data.tar.gz: c20a3739450f047ee31ba9929bd59a03dd70ed24
5
5
  SHA512:
6
- metadata.gz: d782be09ea922b65bc9e5cf91983e98dc8e2db0bb2fb93eec442db5dcb771e11d227ee06119c5305309cd552cdde5aa5eebe448b49b16e1d936b6ad1d95c2088
7
- data.tar.gz: 30c72a1cdb951990bf3ad1b9253b98c8d1b50e9133dd082131fc77bdb2df649e5ef1d8aa27db7af4ff626fc3c307e51f36a8068790d6beb9f535d8411c02f955
6
+ metadata.gz: 8aeaad157ca5e2ac6fdf17ceb83603520b32215e63a654ecf0325a8672650cb85d24e871d99f39195583f9af2f3b214d96586b745b0be93bb8ecc7a33161a913
7
+ data.tar.gz: 00ca7877276e156a68cc29f6c0257b3e418363f095da4a8b893ca3b1541fe6c584c9bcd1ea2f8f5b75d53a806cb2aa739bf18f9ee85af1514eb8bc20f6514f16
data/Gemfile CHANGED
@@ -21,13 +21,8 @@ end
21
21
  # Windows does not include zoneinfo files, so bundle the tzinfo-data gem
22
22
  gem 'tzinfo-data', platforms: (@windows_platforms + [:jruby])
23
23
 
24
- group :test do
25
- gem 'pry', '>= 0.10'
26
- gem 'byebug', '~> 8.2' if RUBY_VERSION < '2.2'
27
- gem 'pry-byebug', platform: :ruby
28
- end
29
-
30
24
  group :development, :test do
25
+ gem 'byebug', '~> 8.2' if RUBY_VERSION < '2.2'
31
26
  gem "pry"
32
27
  gem "pry-rails"
33
28
  gem "pry-byebug"
data/README.md CHANGED
@@ -1,10 +1,234 @@
1
- # GrapeOnRails
1
+ # What is GrapeOnRails?
2
+
3
+ Nowadays, web development tends to move into Single Page App style, which is based on XHR request and communication through JSON data. Beside it, the rising of Mobile App follows up with API web service which is a MUST for our business.
4
+
5
+ Rails with nearly 8 years old is not good enough for developing APIs quickly despite the born of Rails API-only mode. Many programmers turn to [Grape](https://github.com/ruby-grape/grape) for a better DSL and more support.
6
+ However, in order to take advantage of lots of benefits in Rails, we have to found ourselves a way to use Grape along with Rails so far.
7
+
8
+ I myself have to mount Grape routes into rails routes as well as leave rails controller and replaces by Grape API declaration. So, I decided to combine the power of two huge Framework and make your app server lightly, your code concisely and clearly.
9
+
10
+ You guys can consider this gem as an updated version for Rails API mode, I tried to mimic the Grape code in Rails without adding Grape gem. Moreover, some useful tools were included to help developing APIs easier than ever
11
+
12
+ # Why GrapeOnRails?
2
13
 
3
14
  * Want to get rid of boilerplate code
4
15
  * Want to keep using Rails api-only mode without mess with Grape
5
16
  * Have an easy life
6
17
 
7
- Introduce gem GrapeOnRails - which brings Grape DSL to Rails-API and help writing APIs easier than ever
18
+ Introduce gem GrapeOnRails - which brings Grape DSL to Rails-API and help writing APIs easier than ever.
19
+
20
+ ## Usage
21
+
22
+ Below is a simple example showing some of the more common features of GrapeOnRails in the context of recreating parts of the Twitter API.
23
+
24
+ #### Routes have written as a normal rails app.
25
+ ```ruby
26
+ Rails.application.routes.draw do
27
+ namespace :api do
28
+ namespace :v1 do
29
+ resources :statuses
30
+ end
31
+ end
32
+ end
33
+ ```
34
+
35
+ #### GrapeOnRails makes rails controller great again.
36
+ ```ruby
37
+ class Api::V1::StatusesController < ApplicationController
38
+ before_action :authenticate!, except: :index
39
+
40
+ def index
41
+ Status.limit(20)
42
+ end
43
+
44
+ show_params do
45
+ requires :id, type: Integer, allow_blank: false
46
+ end
47
+ def show
48
+ r current_user.statuses.find(id)
49
+ end
50
+
51
+ create_params do
52
+ requires :status, type: String, allow_blank: false
53
+ optional :images, type: File
54
+ end
55
+ def create
56
+ r current_user.create_status!(text: status), status: :created
57
+ end
58
+
59
+ update_params do
60
+ requires :id, type: String, allow_blank: false
61
+ requires :status, type: String, allow_blank: false
62
+ optional :images, type: File
63
+ end
64
+ def update
65
+ r current_user.statuses.find(id).update! text: status
66
+ end
67
+
68
+ destroy_params do
69
+ requries :id, type: String, allow_blank: false
70
+ end
71
+ def destroy
72
+ current_user.statuses.find(id).destroy!
73
+ end
74
+ end
75
+ ```
76
+ - You don't need to write any code. `authenticate!` method is there ready for you to get `current_user`.
77
+
78
+ - Params are automatically validate and able to use. Now, you guys can use `id`, `status` variable instead of `params[:id]`, `params[:status]`
79
+
80
+ - You don't need you write `params.require(:id).permit(:id, :status)` as usual, use `declared_params`, it has already filter and take valid params above.
81
+
82
+ - Instead of writting `render json: {}, ...`, special `r model, ...` method give a help to make your code shorter.
83
+
84
+
85
+ #### Make Models suitable for API authentication
86
+ ```ruby
87
+ class User < ApplicationRecord
88
+ has_one :user_token, dependent: :destroy
89
+
90
+ validates :...
91
+
92
+ acts_as :user
93
+ end
94
+
95
+ class UserToken < ApplicationRecord
96
+ belongs_to :user
97
+
98
+ validates :...
99
+
100
+ acts_as :user_token
101
+ end
102
+ ```
103
+ Every backend APIs need a main actor like: User, Admin, ... and Token for authentication. **GrapeOnRails** give you that ability without writing any line of code.
104
+
105
+ Lets `User acts_as :user`. So, you have authentication function on `User`
106
+ ```ruby
107
+ # authenticate user by email and password
108
+ User.authenticate! "user's email", "user's password"
109
+ ```
110
+
111
+ Lets `UserToken acts_as :user_token`. So that, you have following core function
112
+ ```ruby
113
+ # generate a new token for user
114
+ UserToken.generate! user
115
+
116
+ # find user from token
117
+ UserToken.find_token!(token).user
118
+
119
+ # renew token
120
+ user_token.renew!
121
+
122
+ # check token have expired or not
123
+ user_token.expired?
124
+
125
+ # make token expires
126
+ user_token.expires!
127
+ ```
128
+
129
+ **Remember:**
130
+ - User model must contains following columns: **email, password_digest**
131
+ - UserToken model must contains following columns: **token, refresh_token, expires_at**
132
+
133
+
134
+ #### Config the gem to adapt your app
135
+ Let's create grape_on_rails.yml in folder config/
136
+
137
+ `$ touch config/grape_on_rails.yml`
138
+
139
+ Add following content
140
+
141
+ ```yaml
142
+ access_token_header: "X-Auth-Token"
143
+ access_token_value_prefix: "Bearer"
144
+
145
+ token_configs:
146
+ token:
147
+ secure_length: 64
148
+ refresh_token:
149
+ secure_length: 64
150
+ expires_in: <%= 30.days %>
151
+ short_expires_in: <%= 1.days %>
152
+
153
+ error_code_key: "error_code"
154
+ error_message_key: "message"
155
+ errors:
156
+ data_operation:
157
+ code: 600
158
+ skip_create_error: true
159
+ unauthorized:
160
+ code: 601
161
+ en: "Unauthorized"
162
+ ja: "アクセスできません"
163
+ vi: "Khong co quyen truy cap"
164
+ record_not_found:
165
+ code: 602
166
+ skip_create_error: true
167
+ en: "Record not found"
168
+ ja: "レコードが見つかりません"
169
+ vi: "Khong tim thay du lieu"
170
+ record_invalid:
171
+ code: 603
172
+ skip_create_error: true
173
+ en: "Record invalid"
174
+ vi: "Du lieu khong hop le"
175
+ validation_error:
176
+ code: 604
177
+ en: "Validation Error"
178
+ ja: "バリデーションエラー"
179
+ vi: "Xac thuc that bai"
180
+ token_expired:
181
+ code: 605
182
+ en: "Expired token"
183
+ ja: "トークンの有効期限が切れています"
184
+ vi: "Phien lam viec het han"
185
+ unauthenticated:
186
+ code: 606
187
+ en: "Unauthenticated"
188
+ ja: "認証されていません"
189
+ vi: "Xac thuc that bai"
190
+ wrong_email_password:
191
+ code: 607
192
+ en: "Email or password is wrong"
193
+ ja: "メールアドレスまたはパスワードが違います"
194
+ unexpected_exception:
195
+ code: 608
196
+ en: "Unexpected exception"
197
+ ja: "予期されないエクセプション"
198
+ ```
199
+ - *access_token_header* and *access_token_value_prefix* is how token look like in http request header.
200
+
201
+ e.g. `X-Auth-Token: Bearer user-access-token-go-here`
202
+
203
+ - *token_configs* specifies token length, expire time, ...
204
+
205
+ Declare all errors in your API. Error response depend on it and render json response like this:
206
+ ```json
207
+ {
208
+ error_code: 6xx,
209
+ message: "your error message."
210
+ }
211
+ ```
212
+ For example: when you define a error in grape_on_rails.yml
213
+ ```yaml
214
+ wrong_email:
215
+ code: 606
216
+ en: "user email is wrong"
217
+
218
+ ```
219
+ Should generate APIError::WrongEmail class. From there, anywhere in rails app can easily raise exception
220
+ ```ruby
221
+ raise APIError::WrongEmail
222
+ ```
223
+ The error will be automatically handled by **GrapeOnRails** and response this json
224
+ ```json
225
+ {
226
+ error_code: 606,
227
+ message: "user email is wrong"
228
+ }
229
+ ```
230
+ Moreover, you can apply multi-language on each of errors. **GrapeOnRails** detect your locale to response right error message
231
+
8
232
 
9
233
  ## Installation
10
234
 
@@ -16,15 +240,13 @@ gem 'grape_on_rails'
16
240
 
17
241
  And then execute:
18
242
 
19
- $ bundle
243
+ `$ bundle`
20
244
 
21
- Or install it yourself as:
22
245
 
23
- $ gem install grape_on_rails
246
+ Or install it yourself as:
24
247
 
25
- ## Usage
248
+ `$ gem install grape_on_rails`
26
249
 
27
- TODO:
28
250
 
29
251
  ## Development
30
252
 
@@ -34,7 +256,7 @@ To install this gem onto your local machine, run `bundle exec rake install`. To
34
256
 
35
257
  ## Contributing
36
258
 
37
- Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/grape_on_rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
259
+ Bug reports and pull requests are welcome on GitHub at https://github.com/chau-bao-long/grape-on-rails. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
38
260
 
39
261
  ## License
40
262
 
@@ -23,7 +23,7 @@ module GrapeOnRails
23
23
 
24
24
  config.before_configuration{GrapeOnRails.load}
25
25
 
26
- config.after_initialize do
26
+ config.before_initialize do
27
27
  ActiveSupport.on_load(:active_record) do
28
28
  extend GrapeOnRails::Models
29
29
  end
@@ -1,3 +1,3 @@
1
1
  module GrapeOnRails
2
- VERSION = "1.0.1"
2
+ VERSION = "1.0.2"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: grape_on_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - topcbl
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2018-01-26 00:00:00.000000000 Z
11
+ date: 2018-01-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: actionpack