apiway 0.0.2 → 0.0.3

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b0639d207553c6f64190cfee587fa464ce9eb729
4
- data.tar.gz: db8521d27328c3438bab63f8893a131772f10cff
3
+ metadata.gz: 07142ef3ddaf22a0c60dda85351a5f6b8466a501
4
+ data.tar.gz: d5c8d4189c8582734d6d87540f12eee3d46871ec
5
5
  SHA512:
6
- metadata.gz: c1cb5448f5a7e551425f433fff45c1ef2f03c9aa6ce7a98d6cdcbfb64631ea320a6b04ded5e92d1cd29c8fed29928c920a00b6f6683519c57ebeee955856dfae
7
- data.tar.gz: c331b332b90692217a269b86322f155fec29eeab2aec83e954a4e7d8d2792aec028ba39f4e11a03fae63cb64fd5e62af19d4f5c693ac97adc76aabd4d4a8a30f
6
+ metadata.gz: eaa7666ee0109a4af6c9054c77a2b122d5d11834f52beedc98b3cd9d19bd93bb9207928f1bdf908a5571b4fc1b31494a4d640e0eabbd381f1384200ae6250e15
7
+ data.tar.gz: d581e17355df3e9c280e7185480c18eba3dfdc2540eaad49f3f45cc27db6ade436cdaebd6e497031c3cb99d0fd80428ab441cafae07e2239baf2e52659a730e6
data/README.md CHANGED
@@ -1 +1,444 @@
1
- ## Apiway
1
+ # Apiway - Server side
2
+
3
+ [**Client side**](https://github.com/4urbanoff/js.apiway)
4
+
5
+ ## Getting started
6
+
7
+ ```r
8
+ # Install
9
+ $ gem install apiway
10
+
11
+ # Create new app
12
+ $ apiway new Chat && cd Chat
13
+
14
+ # Start server
15
+ $ apiway server
16
+ ```
17
+
18
+ ## Database (ActiveRecord)
19
+
20
+ ```r
21
+ # Create database
22
+ $ bundle exec rake db:create
23
+
24
+ # Creating database mirgation
25
+ $ bundle exec rake db:create_migration NAME=migration_name
26
+
27
+ # Migrate
28
+ $ bundle exec rake db:migrate
29
+
30
+ # Drop database
31
+ $ bundle exec rake db:drop
32
+ ```
33
+
34
+ > ### Examples
35
+ > ```
36
+ > $ bundle exec rake db:create_migration NAME=create_users
37
+ > ```
38
+ > ```ruby
39
+ > # db/mirgations/20140406161815_create_users.rb
40
+ > class CreateUsers < ActiveRecord::Migration
41
+ > def change
42
+ > create_table :users do |t|
43
+ > t.string :token, limit: 32
44
+ > t.string :name, limit: 100
45
+ > t.timestamps null: true
46
+ > t.index :token, unique: true
47
+ > end
48
+ > end
49
+ > end
50
+ > ```
51
+ >
52
+ > ```
53
+ > $ bundle exec rake db:create_migration NAME=create_messages
54
+ > ```
55
+ > ```ruby
56
+ > # db/mirgations/20140409121731_create_messages.rb
57
+ > class CreateMessages < ActiveRecord::Migration
58
+ > def change
59
+ > create_table :messages do |t|
60
+ > t.belongs_to :user
61
+ > t.text :text
62
+ > t.timestamps null: true
63
+ > t.index :user_id
64
+ > end
65
+ > end
66
+ > end
67
+ > ```
68
+ > ```
69
+ > $ bundle exec rake db:migrate
70
+ > ```
71
+
72
+ ## Generator
73
+
74
+ ```r
75
+ # Show help
76
+ $ apiway generate help
77
+
78
+ # Create controller
79
+ $ apiway generate controller ControllerName
80
+
81
+ # Create resource
82
+ $ apiway generate resource ResourceName
83
+
84
+ # Create model
85
+ $ apiway generate model ModelName
86
+ ```
87
+ ## Hierarchy
88
+ ```r
89
+ Base
90
+ |
91
+ |---> ApplicationController
92
+ | |
93
+ | |---> UsersController <--- Apiway::Controller
94
+ |
95
+ |
96
+ |---> ApplicationResource
97
+ |
98
+ |---> UsersResource <----- Apiway::Resource
99
+
100
+
101
+ ActiveRecord::Base
102
+ |
103
+ |---------> User <-------------- Apiway::Model
104
+ ```
105
+
106
+ > ### Examples
107
+ > ```ruby
108
+ > # app/base/base.rb
109
+ > class Base
110
+ >
111
+ > protected
112
+ >
113
+ > def auth?
114
+ > error :auth_error unless client[ :user_id ]
115
+ > end
116
+ >
117
+ > def current_user
118
+ > User.find client[ :user_id ]
119
+ > end
120
+ >
121
+ > end
122
+ > ```
123
+
124
+ ## Model (this is a simple ActiveRecord model)
125
+
126
+ ```ruby
127
+ class Test < ActiveRecord::Base
128
+
129
+ include Apiway::Model
130
+
131
+ # One class & instance method
132
+ #
133
+ # - sync # synchronize model with all the dependent resources
134
+ # # automatically called in ActiveRecord models
135
+ # # after save/destroy
136
+
137
+ end
138
+ ```
139
+
140
+ > ### Examples
141
+ > ```
142
+ > $ apiway generate model User
143
+ > ```
144
+ > ```ruby
145
+ > # app/models/user.rb
146
+ > class User < ActiveRecord::Base
147
+ >
148
+ > include Apiway::Model
149
+ >
150
+ > has_many :messages, inverse_of: :user
151
+ > validates :name, length: { in: 3..100 }
152
+ > validates :token, length: { is: 32 }
153
+ >
154
+ > after_initialize do
155
+ > self.token ||= generate_token
156
+ > end
157
+ >
158
+ > def generate_token
159
+ > begin
160
+ > token = SecureRandom.hex 16
161
+ > end while User.exists? token: token
162
+ > token
163
+ > end
164
+ >
165
+ > end
166
+ > ```
167
+ > ```
168
+ > $ apiway generate model Message
169
+ > ```
170
+ > ```ruby
171
+ > # app/models/message.rb
172
+ > class Message < ActiveRecord::Base
173
+ >
174
+ > include Apiway::Model
175
+ >
176
+ > belongs_to :user, inverse_of: :messages
177
+ > validates :text, length: { in: 3..500 }
178
+ >
179
+ > end
180
+ > ```
181
+ > ```
182
+ > $ apiway generate model Online
183
+ > ```
184
+ > ```ruby
185
+ > # app/models/online.rb
186
+ > class Online # < ActiveRecord::Base - db is not required
187
+ >
188
+ > include Apiway::Model
189
+ >
190
+ > def self.value
191
+ > Apiway::Client.all.size
192
+ > end
193
+ >
194
+ > end
195
+ > ```
196
+
197
+ ## Controller
198
+
199
+ ```ruby
200
+ class TestController < ApplicationController
201
+
202
+ include Apiway::Controller
203
+
204
+ # Class methods:
205
+ #
206
+ # - Define action
207
+ #
208
+ # action :new do
209
+ # < action body >
210
+ # end
211
+ #
212
+ # - Before filters:
213
+ #
214
+ # before_action :action_name
215
+ # before_action :action_name, only: :method_name
216
+ # before_action :action_name, only: [ :method_name, :method_name ]
217
+ # before_action :action_name, except: :method_name
218
+ # before_action :action_name, except: [ :method_name, :method_name ]
219
+ #
220
+ # - After filters:
221
+ #
222
+ # after_action :action_name
223
+ # after_action :action_name, only: :method_name
224
+ # after_action :action_name, only: [ :method_name, :method_name ]
225
+ # after_action :action_name, except: :method_name
226
+ # after_action :action_name, except: [ :method_name, :method_name ]
227
+ #
228
+ #
229
+ # Instance methods:
230
+ #
231
+ # - client # getter to current instance of Apiway::Client
232
+ # - params # getter to options of current request
233
+ # - error( params ) # stopping controller, call failure callback on client
234
+ # - trigger( :event, params ) # call trigger event on client with params
235
+
236
+ end
237
+ ```
238
+ > ### Examples
239
+ > ```
240
+ > $ apiway generate controller Users
241
+ > ```
242
+ > ```ruby
243
+ > # app/controllers/users.rb
244
+ > class UsersController < ApplicationController
245
+ >
246
+ > include Apiway::Controller
247
+ >
248
+ > action :auth_by_name do
249
+ > begin
250
+ > user = User.find_or_create_by! name: params[ :name ]
251
+ > rescue Exception => e
252
+ > error e.message
253
+ > else
254
+ > client[ :user_id ] = user.id
255
+ > user.token
256
+ > end
257
+ > end
258
+ >
259
+ > action :auth_by_token do
260
+ > begin
261
+ > user = User.find_by! token: params[ :token ]
262
+ > rescue
263
+ > nil
264
+ > else
265
+ > client[ :user_id ] = user.id
266
+ > end
267
+ > end
268
+ >
269
+ > end
270
+ > ```
271
+ > ```
272
+ > $ apiway generate controller Messages
273
+ > ```
274
+ > ```ruby
275
+ > # app/controllers/messages.rb
276
+ > class MessagesController < ApplicationController
277
+ >
278
+ > include Apiway::Controller
279
+ >
280
+ > before_action :auth?
281
+ >
282
+ > action :new do
283
+ > begin
284
+ > current_user.messages.create! text: params[ :text ]
285
+ > rescue Exception => e
286
+ > error e.message
287
+ > else
288
+ > true
289
+ > end
290
+ > end
291
+ >
292
+ >
293
+ > end
294
+ > ```
295
+
296
+ ## Resource
297
+
298
+ ```ruby
299
+ # app/resources/messages.rb
300
+ class TestResource < ApplicationResource
301
+
302
+ include Apiway::Resource
303
+
304
+ # Class methods:
305
+ #
306
+ # - Define dependencies
307
+ #
308
+ # depend_on ModelName, ModelName
309
+ #
310
+ # - Define access check
311
+ #
312
+ # access do
313
+ # < body >
314
+ # end
315
+ #
316
+ # - Define returned data
317
+ #
318
+ # data do
319
+ # < body >
320
+ # end
321
+ #
322
+ #
323
+ # Instance methods:
324
+ #
325
+ # - client # getter to current instance of Apiway::Client
326
+ # - params # getter to resource options
327
+ # - error( params ) # call resource error event on client
328
+
329
+ end
330
+ ```
331
+
332
+ > ### Examples
333
+ > ```
334
+ > $ apiway generate resource CurrentUser
335
+ > ```
336
+ > ```ruby
337
+ > # app/resources/current_user.rb
338
+ > class CurrentUserResource < ApplicationResource
339
+ >
340
+ > include Apiway::Resource
341
+ >
342
+ > depend_on User
343
+ >
344
+ > access do
345
+ > auth?
346
+ > end
347
+ >
348
+ > data do
349
+ > {
350
+ > id: current_user.id,
351
+ > name: current_user.name,
352
+ > token: current_user.token
353
+ > }
354
+ > end
355
+ >
356
+ > end
357
+ >
358
+ > ```
359
+ > ```
360
+ > $ apiway generate resource Messages
361
+ > ```
362
+ > ```ruby
363
+ > # app/resources/messages.rb
364
+ > class MessagesResource < ApplicationResource
365
+ >
366
+ > include Apiway::Resource
367
+ >
368
+ > depend_on Message, User
369
+ >
370
+ > access do
371
+ > auth?
372
+ > end
373
+ >
374
+ > data do
375
+ > Message.limit( params[ :limit ] ).order( created_at: :desc ).map do |message|
376
+ > {
377
+ > id: message.id,
378
+ > text: message.text,
379
+ > user: {
380
+ > id: message.user.id,
381
+ > name: message.user.name
382
+ > }
383
+ > }
384
+ > end
385
+ > end
386
+ >
387
+ > end
388
+ >
389
+ > ```
390
+ > ```
391
+ > $ apiway generate resource Online
392
+ > ```
393
+ > ```ruby
394
+ > # app/resources/online.rb
395
+ > class OnlineResource < ApplicationResource
396
+ >
397
+ > include Apiway::Resource
398
+ >
399
+ > depend_on Online
400
+ >
401
+ > data do
402
+ > Online.value
403
+ > end
404
+ >
405
+ > end
406
+ >
407
+ > ```
408
+
409
+ ## Your client events handlers
410
+
411
+ ```ruby
412
+ # app/base/client.rb
413
+ module Apiway
414
+ class Client
415
+
416
+ on_connected do
417
+ # Your handler
418
+ end
419
+
420
+ on_message do |message|
421
+ # Your handler
422
+ end
423
+
424
+ on_disconnected do
425
+ # Your handler
426
+ end
427
+ end
428
+ end
429
+ ```
430
+
431
+ > ### Example
432
+ >
433
+ > ```ruby
434
+ > #...
435
+ > on_connected do
436
+ > Online.sync
437
+ > end
438
+ >
439
+ > on_disconnected do
440
+ > Online.sync
441
+ > end
442
+ > #...
443
+ > ```
444
+
@@ -6,26 +6,10 @@ module Apiway
6
6
  set root: File.expand_path( '.' )
7
7
  set static: true
8
8
  set apiway_log: true
9
- set active_record_log: true
10
- set log_level: :debug
9
+ set active_record_log: true
11
10
  set database_file: File.join( root, 'config/database.yml' )
12
11
 
13
-
14
- register Sinatra::ActiveRecordExtension
15
-
16
-
17
- configure :development do
18
- register Sinatra::Reloader
19
- also_reload File.join( root, '**/*.rb' )
20
- # also_reload File.join( Apiway.path, '**/*.rb' )
21
- end
22
-
23
-
24
- get '*' do
25
- request.websocket? ? request.websocket{ |ws| Apiway::Client.new ws } : pass
26
- end
27
-
28
-
12
+
29
13
  %W(
30
14
  lib/**/*.rb
31
15
  config/environments/#{ environment.to_s }.rb
@@ -39,9 +23,24 @@ module Apiway
39
23
  .flatten.uniq.each{ |path| require path }
40
24
 
41
25
 
26
+ register Sinatra::ActiveRecordExtension
27
+
28
+
42
29
  LoggerBase::apiway_log_level apiway_log
43
30
  LoggerBase::activerecord_log_level activerecord_log
44
-
31
+
32
+
33
+ configure :development do
34
+ register Sinatra::Reloader
35
+ also_reload File.join( root, '**/*.rb' )
36
+ # also_reload File.join( Apiway.path, '**/*.rb' )
37
+ end
38
+
39
+
40
+ get '*' do
41
+ request.websocket? ? request.websocket{ |ws| Apiway::Client.new ws } : pass
42
+ end
43
+
45
44
 
46
45
  def self.tasks
47
46
  require 'sinatra/activerecord/rake'
@@ -1,5 +1,5 @@
1
1
  module Apiway
2
2
 
3
- VERSION = '0.0.2'
3
+ VERSION = '0.0.3'
4
4
 
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: apiway
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.2
4
+ version: 0.0.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - 4urbanoff
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-16 00:00:00.000000000 Z
11
+ date: 2015-09-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: thin
@@ -167,7 +167,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
167
167
  version: '0'
168
168
  requirements: []
169
169
  rubyforge_project:
170
- rubygems_version: 2.0.14
170
+ rubygems_version: 2.0.14.1
171
171
  signing_key:
172
172
  specification_version: 4
173
173
  summary: Framework for developing async API for web applications