menilite 0.3.0 → 0.4.0

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: 7ff9db82163232317d82fcbfe864f020e713e3dc
4
- data.tar.gz: b4511f2ece8e027dd0f0cac02632e3676d1f1fe3
3
+ metadata.gz: dba2750ee3e0e16c13025d583535ed1197b59939
4
+ data.tar.gz: 3e0cbfa1fb5892f78627cec8318b6a5cd4cc0c36
5
5
  SHA512:
6
- metadata.gz: b18f844aca4213efcd77f13c6598a5b2ed16326849221d221e177d668cd751a34054e358c6c7788c23a3ef484278c4326fefe3a3b29faec3d249e1718d9a7f6b
7
- data.tar.gz: 004d4eaeceb5e821e0457aeb4416930cde8e769d04a0a33fe0f61b62919f82834927c2d03387cf787b333addf9e25f09cc4857132e36c0973aa2ef5975f4f639
6
+ metadata.gz: fd787757426405abf65e00c60627362518a2c396b031bb5eb18f91abef2d95d289a5f302b195675417b381c69174847e5b347b117fabcf2745417b9efdd4857a
7
+ data.tar.gz: b47e9ed1c085bb612f0c1c8214fcbb711a947f0052f44d297fbbe5d6c90f1ca9b55ee08726cb7b754127ba38f75ace9d45c2410a595d0f8af69989a7ffcbb5cb
data/README.md CHANGED
@@ -51,9 +51,8 @@ You can specify another type by the following way, for example.
51
51
 
52
52
  ```ruby
53
53
  class User < Menilite::Model
54
- action :signup, on_create: true do |password|
54
+ action :signup, save: true do |password|
55
55
  self.password = BCrypt::Password.create(password)
56
- self.save
57
56
  end
58
57
  end
59
58
  ```
data/lib/menilite.rb CHANGED
@@ -12,8 +12,9 @@ else
12
12
  require 'opal'
13
13
  require 'menilite/model'
14
14
  require 'menilite/controller'
15
- require 'menilite/privilege'
16
- require 'menilite/router'
15
+ require 'menilite/server/error_with_status_code'
16
+ require 'menilite/server/privilege'
17
+ require 'menilite/server/router'
17
18
  require 'menilite/server/activerecord_store'
18
19
 
19
20
  Opal.append_path File.expand_path('../', __FILE__).untaint
@@ -227,10 +227,19 @@ module Menilite
227
227
  end
228
228
  else
229
229
  method = Proc.new do |model, *args, &callback| # todo: should adopt keyword parameters
230
- action_url = options[:on_create] || options[:class] ? "api/#{self}/#{name}" : "api/#{self}/#{model.id}/#{name}"
231
- post_data = {}
232
- post_data[:model] = model.to_h if options[:on_create]
233
- post_data[:args] = args
230
+ action_url = options[:save] || options[:class] ? "api/#{self}/#{name}" : "api/#{self}/#{model.id}/#{name}"
231
+ post_data = { args: args }
232
+
233
+ if options[:save]
234
+ begin
235
+ model.validate_all
236
+ rescue => e
237
+ callback.call(:validation_error, e) if callback
238
+ return
239
+ end
240
+ post_data[:model] = model.to_h
241
+ end
242
+
234
243
  Browser::HTTP.post(action_url, post_data.to_json) do
235
244
  on :success do |res|
236
245
  callback.call(:success, res) if callback
@@ -0,0 +1,22 @@
1
+ module Menilite
2
+ class ErrorWithStatusCode < StandardError
3
+ def self.code(code)
4
+ klazz = Class.new(ErrorWithStatusCode)
5
+ klazz.instance_eval do
6
+ define_method(:code) { code }
7
+ end
8
+ klazz
9
+ end
10
+
11
+ def code
12
+ 500
13
+ end
14
+ end
15
+
16
+ BadRequest = ErrorWithStatusCode.code(400)
17
+ Unauthorized = ErrorWithStatusCode.code(401)
18
+ PaymentRequired = ErrorWithStatusCode.code(402)
19
+ NotFound = ErrorWithStatusCode.code(404)
20
+ InternalServerError = ErrorWithStatusCode.code(500)
21
+ NotImplemented = ErrorWithStatusCode.code(501)
22
+ end
File without changes
@@ -53,6 +53,25 @@ module Menilite
53
53
  classes = @classes
54
54
  router = self
55
55
  Sinatra.new do
56
+ def with_error_handler(&block)
57
+ block.call
58
+ rescue Menilite::ErrorWithStatusCode => e
59
+ content_type :json
60
+ status e.code
61
+
62
+ {:result => 'error', :message => e.message}.to_json
63
+ rescue Menilite::Model::ValidationError => e
64
+ content_type :json
65
+ status 403
66
+
67
+ {:result => 'error', :message => e.message}.to_json
68
+ rescue => e
69
+ content_type :json
70
+ status 500
71
+
72
+ {:result => 'error', :message => e.message}.to_json
73
+ end
74
+
56
75
  enable :sessions
57
76
 
58
77
  classes.each do |klass|
@@ -61,17 +80,21 @@ module Menilite
61
80
  klass.init
62
81
  resource_name = klass.name
63
82
  get "/#{resource_name}" do
64
- PrivilegeService.init
65
- router.before_action_handlers(klass, 'index').each {|h| self.instance_eval(&h[:proc]) }
66
- order = params.delete('order')&.split(?,)
67
- data = klass.fetch(filter: params, order: order)
68
- json data.map(&:to_h)
83
+ with_error_handler do
84
+ PrivilegeService.init
85
+ router.before_action_handlers(klass, 'index').each {|h| self.instance_eval(&h[:proc]) }
86
+ order = params.delete('order')&.split(?,)
87
+ data = klass.fetch(filter: params, order: order)
88
+ json data.map(&:to_h)
89
+ end
69
90
  end
70
91
 
71
92
  get "/#{resource_name}/:id" do
72
- PrivilegeService.init
73
- router.before_action_handlers(klass, 'get').each {|h| self.instance_eval(&h[:proc]) }
74
- json klass[params[:id]].to_h
93
+ with_error_handler do
94
+ PrivilegeService.init
95
+ router.before_action_handlers(klass, 'get').each {|h| self.instance_eval(&h[:proc]) }
96
+ json klass[params[:id]].to_h
97
+ end
75
98
  end
76
99
 
77
100
  post "/#{resource_name}" do
@@ -88,32 +111,36 @@ module Menilite
88
111
  end
89
112
 
90
113
  klass.action_info.each do |name, action|
91
- path = action.options[:on_create] || action.options[:class] ? "/#{resource_name}/#{action.name}" : "/#{resource_name}/#{action.name}/:id"
114
+ path = action.options[:save] || action.options[:class] ? "/#{resource_name}/#{action.name}" : "/#{resource_name}/#{action.name}/:id"
92
115
 
93
116
  post path do
94
- PrivilegeService.init
95
- router.before_action_handlers(klass, action.name).each {|h| self.instance_eval(&h[:proc]) }
96
- data = JSON.parse(request.body.read)
97
- result = if action.options[:on_create]
98
- klass.new(data["model"]).send(action.name, *data["args"])
99
- elsif action.options[:class]
100
- klass.send(action.name, *data["args"])
101
- else
102
- klass[params[:id]].send(action.name, *data["args"])
103
- end
104
- json result
117
+ with_error_handler do
118
+ PrivilegeService.init
119
+ router.before_action_handlers(klass, action.name).each {|h| self.instance_eval(&h[:proc]) }
120
+ data = JSON.parse(request.body.read)
121
+ result = if action.options[:save]
122
+ klass.new(data["model"]).send(action.name, *data["args"]).save
123
+ elsif action.options[:class]
124
+ klass.send(action.name, *data["args"])
125
+ else
126
+ klass[params[:id]].send(action.name, *data["args"])
127
+ end
128
+ json result
129
+ end
105
130
  end
106
131
  end
107
132
  when klass.subclass_of?(Menilite::Controller)
108
133
  klass.action_info.each do |name, action|
109
134
  path = klass.respond_to?(:namespace) ? "/#{klass.namespace}/#{action.name}" : "/#{action.name}"
110
- post path do
111
- PrivilegeService.init
112
- router.before_action_handlers(klass, action.name).each {|h| self.instance_eval(&h[:proc]) }
113
- data = JSON.parse(request.body.read)
114
- controller = klass.new(session, settings)
115
- result = controller.send(action.name, *data["args"])
116
- json result
135
+ post path do
136
+ with_error_handler do
137
+ PrivilegeService.init
138
+ router.before_action_handlers(klass, action.name).each {|h| self.instance_eval(&h[:proc]) }
139
+ data = JSON.parse(request.body.read)
140
+ controller = klass.new(session, settings)
141
+ result = controller.send(action.name, *data["args"])
142
+ json result
143
+ end
117
144
  end
118
145
  end
119
146
  end
@@ -1,3 +1,3 @@
1
1
  module Menilite
2
- VERSION = "0.3.0"
2
+ VERSION = "0.4.0"
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: menilite
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - youchan
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2016-11-25 00:00:00.000000000 Z
11
+ date: 2016-11-29 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -129,9 +129,10 @@ files:
129
129
  - lib/menilite/controller.rb
130
130
  - lib/menilite/helper.rb
131
131
  - lib/menilite/model.rb
132
- - lib/menilite/privilege.rb
133
- - lib/menilite/router.rb
134
132
  - lib/menilite/server/activerecord_store.rb
133
+ - lib/menilite/server/error_with_status_code.rb
134
+ - lib/menilite/server/privilege.rb
135
+ - lib/menilite/server/router.rb
135
136
  - lib/menilite/server/store.rb
136
137
  - lib/menilite/version.rb
137
138
  - menilite.gemspec