ki 0.4.6 → 0.4.7
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 +4 -4
- data/.rubocop.yml +24 -0
- data/Gemfile.lock +25 -6
- data/Guardfile +11 -5
- data/README.md +78 -0
- data/Rakefile +4 -4
- data/ki.gemspec +24 -23
- data/lib/ki.rb +7 -4
- data/lib/ki/base_request.rb +2 -2
- data/lib/ki/helpers.rb +7 -7
- data/lib/ki/ki.rb +12 -18
- data/lib/ki/ki_app.rb +16 -0
- data/lib/ki/ki_cli.rb +11 -12
- data/lib/ki/ki_config.rb +12 -8
- data/lib/ki/middleware/admin_interface_generator.rb +1 -1
- data/lib/ki/middleware/api_handler.rb +7 -8
- data/lib/ki/middleware/base_middleware.rb +1 -1
- data/lib/ki/middleware/coffee_compiler.rb +2 -2
- data/lib/ki/middleware/doc_generator.rb +5 -4
- data/lib/ki/middleware/haml_compiler.rb +4 -22
- data/lib/ki/{modules/format_of.rb → middleware/helpers/format_of_helper.rb} +3 -3
- data/lib/ki/middleware/helpers/haml_compiler_helper.rb +27 -0
- data/lib/ki/{modules → middleware/helpers}/public_file_helper.rb +2 -2
- data/lib/ki/{modules → middleware/helpers}/view_helper.rb +3 -3
- data/lib/ki/middleware/init_middleware.rb +1 -1
- data/lib/ki/middleware/public_file_server.rb +1 -1
- data/lib/ki/middleware/sass_compiler.rb +3 -3
- data/lib/ki/model.rb +10 -11
- data/lib/ki/modules/{model_helpers.rb → model_helper.rb} +1 -1
- data/lib/ki/modules/query_interface.rb +7 -7
- data/lib/ki/modules/restrictions.rb +4 -5
- data/lib/ki/orm.rb +11 -11
- data/lib/ki/utils/api_error.rb +4 -4
- data/lib/ki/utils/extra_ruby.rb +3 -3
- data/lib/ki/utils/indifferent_hash.rb +1 -1
- data/lib/ki/version.rb +1 -1
- data/lib/ki/views/404.haml +1 -0
- data/lib/ki/views/instadmin.haml +47 -4
- data/spec/examples/base/app.rb +5 -0
- data/spec/examples/json.northpole.ro/Gemfile +1 -1
- data/spec/lib/ki/base_request_spec.rb +24 -24
- data/spec/lib/ki/helpers_spec.rb +1 -1
- data/spec/lib/ki/ki_app_spec.rb +13 -0
- data/spec/lib/ki/ki_config_spec.rb +16 -2
- data/spec/lib/ki/ki_spec.rb +11 -0
- data/spec/lib/ki/middleware/haml_compiler_spec.rb +6 -4
- data/spec/lib/ki/{modules/format_of_spec.rb → middleware/helpers/format_of_helper_spec.rb} +4 -1
- data/spec/lib/ki/model_spec.rb +14 -14
- data/spec/lib/ki/modules/model_helper_spec.rb +4 -0
- data/spec/lib/ki/orm_spec.rb +25 -25
- data/spec/spec_helper.rb +6 -4
- data/spec/util_spec.rb +2 -2
- metadata +36 -14
- data/spec/lib/ki/modules/model_helpers_spec.rb +0 -4
@@ -5,33 +5,33 @@ module Ki
|
|
5
5
|
# sort.
|
6
6
|
# it writes directly to the database
|
7
7
|
module QueryInterface
|
8
|
-
def count
|
8
|
+
def count(hash = {})
|
9
9
|
Orm::Db.instance.count class_name, hash
|
10
10
|
end
|
11
11
|
|
12
|
-
def find
|
12
|
+
def find(hash = {})
|
13
13
|
Orm::Db.instance.find class_name, hash
|
14
14
|
end
|
15
15
|
|
16
|
-
def create
|
16
|
+
def create(hash)
|
17
17
|
Orm::Db.instance.insert class_name, hash
|
18
18
|
end
|
19
19
|
|
20
|
-
def find_or_create
|
20
|
+
def find_or_create(hash)
|
21
21
|
r = find hash
|
22
22
|
r.empty? ? create(hash) : r
|
23
23
|
end
|
24
24
|
|
25
|
-
def update
|
25
|
+
def update(hash)
|
26
26
|
Orm::Db.instance.update class_name, hash
|
27
27
|
end
|
28
28
|
|
29
|
-
def delete
|
29
|
+
def delete(hash)
|
30
30
|
Orm::Db.instance.delete class_name, hash
|
31
31
|
end
|
32
32
|
|
33
33
|
def class_name
|
34
|
-
|
34
|
+
to_s
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -5,7 +5,7 @@ module Ki
|
|
5
5
|
[]
|
6
6
|
end
|
7
7
|
|
8
|
-
def forbid
|
8
|
+
def forbid(*actions)
|
9
9
|
generic_restriction :forbidden_actions, actions
|
10
10
|
end
|
11
11
|
|
@@ -13,7 +13,7 @@ module Ki
|
|
13
13
|
[]
|
14
14
|
end
|
15
15
|
|
16
|
-
def requires
|
16
|
+
def requires(*attributes)
|
17
17
|
generic_restriction :required_attributes, attributes
|
18
18
|
end
|
19
19
|
|
@@ -21,13 +21,13 @@ module Ki
|
|
21
21
|
[]
|
22
22
|
end
|
23
23
|
|
24
|
-
def unique
|
24
|
+
def unique(*attributes)
|
25
25
|
generic_restriction :unique_attributes, attributes
|
26
26
|
end
|
27
27
|
|
28
28
|
private
|
29
29
|
|
30
|
-
def generic_restriction
|
30
|
+
def generic_restriction(method_name, attributes)
|
31
31
|
send :define_method, method_name do
|
32
32
|
attributes
|
33
33
|
end
|
@@ -37,7 +37,6 @@ module Ki
|
|
37
37
|
attributes
|
38
38
|
end
|
39
39
|
end
|
40
|
-
|
41
40
|
end
|
42
41
|
end
|
43
42
|
end
|
data/lib/ki/orm.rb
CHANGED
@@ -30,7 +30,7 @@ module Ki
|
|
30
30
|
#
|
31
31
|
def establish_connection
|
32
32
|
@config = KiConfig.instance.database
|
33
|
-
if ENV[
|
33
|
+
if ENV['MONGODB_URI']
|
34
34
|
@connection = Mongo::Connection.new
|
35
35
|
@db = @connection.db
|
36
36
|
else
|
@@ -42,8 +42,8 @@ module Ki
|
|
42
42
|
|
43
43
|
def connection_string
|
44
44
|
db = KiConfig.instance.database
|
45
|
-
if ENV[
|
46
|
-
ENV[
|
45
|
+
if ENV['MONGODB_URI']
|
46
|
+
ENV['MONGODB_URI']
|
47
47
|
else
|
48
48
|
"#{db['host']}:#{db['port']}/#{db['name']}"
|
49
49
|
end
|
@@ -54,7 +54,7 @@ module Ki
|
|
54
54
|
# An array of all the collection names in the database.
|
55
55
|
#
|
56
56
|
def collection_names
|
57
|
-
@db.collection_names.delete_if{|name| name =~ /^system/}
|
57
|
+
@db.collection_names.delete_if { |name| name =~ /^system/ }
|
58
58
|
end
|
59
59
|
|
60
60
|
# Insert a hash in the database.
|
@@ -75,7 +75,7 @@ module Ki
|
|
75
75
|
# db = Db.instance
|
76
76
|
# db.insert 'users', { name: 'Homer' }
|
77
77
|
#
|
78
|
-
def insert
|
78
|
+
def insert(name, hash)
|
79
79
|
@db[name].insert(hash)
|
80
80
|
[hash].stringify_ids.first
|
81
81
|
end
|
@@ -101,7 +101,7 @@ module Ki
|
|
101
101
|
# db.find 'users', { id: 'id' }
|
102
102
|
# db.find 'users', { name: 'Homer' }
|
103
103
|
#
|
104
|
-
def find
|
104
|
+
def find(name, hash = {})
|
105
105
|
hash = nourish_hash_id hash
|
106
106
|
@db[name].find(hash).to_a.stringify_ids
|
107
107
|
end
|
@@ -126,11 +126,11 @@ module Ki
|
|
126
126
|
# db = Db.instance
|
127
127
|
# db.update('users', { id: 'id', name: 'Sir Homer' })
|
128
128
|
#
|
129
|
-
def update
|
129
|
+
def update(name, hash)
|
130
130
|
hash = nourish_hash_id hash
|
131
131
|
id = hash['_id'].to_s
|
132
132
|
hash.delete('_id')
|
133
|
-
@db[name].update({'_id' => BSON::ObjectId(id)}, hash)
|
133
|
+
@db[name].update({ '_id' => BSON::ObjectId(id) }, hash)
|
134
134
|
hash['id'] = id
|
135
135
|
hash
|
136
136
|
end
|
@@ -152,7 +152,7 @@ module Ki
|
|
152
152
|
# db.delete 'users', { id: 'id' }
|
153
153
|
# db.delete 'users', {}
|
154
154
|
#
|
155
|
-
def delete
|
155
|
+
def delete(name, hash)
|
156
156
|
hash = nourish_hash_id hash
|
157
157
|
@db[name].remove hash
|
158
158
|
{}
|
@@ -179,13 +179,13 @@ module Ki
|
|
179
179
|
# db.count 'users'
|
180
180
|
# db.count 'users', { name: 'Homer' }
|
181
181
|
#
|
182
|
-
def count
|
182
|
+
def count(name, hash = {})
|
183
183
|
@db[name].count hash
|
184
184
|
end
|
185
185
|
|
186
186
|
private
|
187
187
|
|
188
|
-
def nourish_hash_id
|
188
|
+
def nourish_hash_id(hash)
|
189
189
|
hash = { '_id' => BSON::ObjectId(hash) } if hash.class == String
|
190
190
|
if hash['id']
|
191
191
|
hash['_id'] = hash['id']
|
data/lib/ki/utils/api_error.rb
CHANGED
@@ -2,7 +2,7 @@ module Ki
|
|
2
2
|
class ApiError < StandardError #:nodoc:
|
3
3
|
attr_reader :status
|
4
4
|
|
5
|
-
def initialize
|
5
|
+
def initialize(body, status = 400)
|
6
6
|
super body
|
7
7
|
@status = status
|
8
8
|
end
|
@@ -22,19 +22,19 @@ module Ki
|
|
22
22
|
end
|
23
23
|
|
24
24
|
class ForbiddenAction < ApiError #:nodoc:
|
25
|
-
def initialize
|
25
|
+
def initialize(s = 'forbidden', code = 403)
|
26
26
|
super s, code
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
30
30
|
class UnauthorizedError < ApiError #:nodoc:
|
31
|
-
def initialize
|
31
|
+
def initialize(s = 'unauthroized', code = 401)
|
32
32
|
super s, code
|
33
33
|
end
|
34
34
|
end
|
35
35
|
|
36
36
|
class PartialNotFoundError < ApiError #:nodoc:
|
37
|
-
def initialize
|
37
|
+
def initialize(s)
|
38
38
|
super "partial #{s} not found", 404
|
39
39
|
end
|
40
40
|
end
|
data/lib/ki/utils/extra_ruby.rb
CHANGED
@@ -9,10 +9,10 @@ class String
|
|
9
9
|
# "user".to_class == User
|
10
10
|
#
|
11
11
|
def to_class
|
12
|
-
chain =
|
12
|
+
chain = split '::'
|
13
13
|
klass = Kernel
|
14
14
|
chain.each do |klass_string|
|
15
|
-
klass = klass.const_get klass_string.split('_').map
|
15
|
+
klass = klass.const_get klass_string.split('_').map(&:capitalize).join('')
|
16
16
|
end
|
17
17
|
klass.is_a?(Class) ? klass : nil
|
18
18
|
rescue NameError
|
@@ -22,7 +22,7 @@ end
|
|
22
22
|
|
23
23
|
class Array
|
24
24
|
def stringify_ids
|
25
|
-
|
25
|
+
collect do |e|
|
26
26
|
if e['_id']
|
27
27
|
e['id'] = e['_id'].to_s
|
28
28
|
e.delete('_id')
|
data/lib/ki/version.rb
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
%h1 404
|
data/lib/ki/views/instadmin.haml
CHANGED
@@ -1,5 +1,48 @@
|
|
1
|
-
|
1
|
+
= js "//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"
|
2
2
|
|
3
|
-
%
|
4
|
-
|
5
|
-
|
3
|
+
%div{ 'ng-app' => 'instaAdminApp', 'ng-controller' => 'InstaAdmin' }
|
4
|
+
%h1 Ki InstAdmin
|
5
|
+
|
6
|
+
%p
|
7
|
+
Connected to
|
8
|
+
= Ki::Orm::Db.instance.connection_string
|
9
|
+
|
10
|
+
%input{ 'ng-model' => 'verb' }
|
11
|
+
%input{ 'ng-model' => 'resource' }
|
12
|
+
%button{ 'ng-click' => 'executeReq()' } Execute Request
|
13
|
+
|
14
|
+
%textarea{ 'ng-model' => 'input' }
|
15
|
+
%textarea{ 'ng-model' => 'output' }
|
16
|
+
|
17
|
+
:css
|
18
|
+
textarea {
|
19
|
+
width: 100%;
|
20
|
+
height: 300px;
|
21
|
+
padding: 15px;
|
22
|
+
}
|
23
|
+
|
24
|
+
button, input {
|
25
|
+
width: 33%;
|
26
|
+
padding: 15px;
|
27
|
+
}
|
28
|
+
|
29
|
+
:javascript
|
30
|
+
var adminApp = angular.module('instaAdminApp', []);
|
31
|
+
|
32
|
+
adminApp.controller('InstaAdmin', function ($scope, $http) {
|
33
|
+
$scope.verb = 'get';
|
34
|
+
$scope.resource = 'users';
|
35
|
+
$scope.input = '{}'
|
36
|
+
|
37
|
+
$scope.executeReq = function () {
|
38
|
+
$http[$scope.verb]("/" + $scope.resource + ".json", JSON.parse($scope.input)).
|
39
|
+
success(function(data, status, headers, config) {
|
40
|
+
console.log(data);
|
41
|
+
$scope.output = JSON.stringify(data, `undefined`, 2)
|
42
|
+
}).
|
43
|
+
error(function(data, status, headers, config) {
|
44
|
+
console.log(data);
|
45
|
+
$scope.output = JSON.stringify(data, `undefined`, 2)
|
46
|
+
});
|
47
|
+
}
|
48
|
+
});
|
data/spec/examples/base/app.rb
CHANGED
@@ -4,8 +4,8 @@ describe Ki::BaseRequest do
|
|
4
4
|
let(:req) { Ki::BaseRequest }
|
5
5
|
|
6
6
|
it 'knows if current path is root' do
|
7
|
-
req.new({'PATH_INFO' => '/'}).root?.should be_true
|
8
|
-
req.new({'PATH_INFO' => '/foo'}).root?.should be_false
|
7
|
+
req.new({ 'PATH_INFO' => '/' }).root?.should be_true
|
8
|
+
req.new({ 'PATH_INFO' => '/foo' }).root?.should be_false
|
9
9
|
end
|
10
10
|
|
11
11
|
it 'coverts path to class' do
|
@@ -13,37 +13,37 @@ describe Ki::BaseRequest do
|
|
13
13
|
class Bar < Ki::Model; end
|
14
14
|
class FooBar < Ki::Model; end
|
15
15
|
|
16
|
-
req.new({'PATH_INFO' => ''}).to_ki_model_class.should
|
17
|
-
req.new({'PATH_INFO' => '/'}).to_ki_model_class.should
|
18
|
-
req.new({'PATH_INFO' => 'foo'}).to_ki_model_class.should
|
19
|
-
req.new({'PATH_INFO' => '/foo'}).to_ki_model_class.should
|
20
|
-
req.new({'PATH_INFO' => '/Foo.json'}).to_ki_model_class.should
|
21
|
-
req.new({'PATH_INFO' => '/bar.json'}).to_ki_model_class.should
|
22
|
-
req.new({'PATH_INFO' => '/Foo/bar.json'}).to_ki_model_class.should
|
23
|
-
req.new({'PATH_INFO' => '/Foo/bar'}).to_ki_model_class.should
|
24
|
-
req.new({'PATH_INFO' => '/Foo_bar'}).to_ki_model_class.should
|
25
|
-
req.new({'PATH_INFO' => '/foo_bar'}).to_ki_model_class.should
|
26
|
-
req.new({'PATH_INFO' => '/foo_bar.json'}).to_ki_model_class.should
|
16
|
+
req.new({ 'PATH_INFO' => '' }).to_ki_model_class.should be nil
|
17
|
+
req.new({ 'PATH_INFO' => '/' }).to_ki_model_class.should be nil
|
18
|
+
req.new({ 'PATH_INFO' => 'foo' }).to_ki_model_class.should be Foo
|
19
|
+
req.new({ 'PATH_INFO' => '/foo' }).to_ki_model_class.should be Foo
|
20
|
+
req.new({ 'PATH_INFO' => '/Foo.json' }).to_ki_model_class.should be Foo
|
21
|
+
req.new({ 'PATH_INFO' => '/bar.json' }).to_ki_model_class.should be Bar
|
22
|
+
req.new({ 'PATH_INFO' => '/Foo/bar.json' }).to_ki_model_class.should be nil
|
23
|
+
req.new({ 'PATH_INFO' => '/Foo/bar' }).to_ki_model_class.should be nil
|
24
|
+
req.new({ 'PATH_INFO' => '/Foo_bar' }).to_ki_model_class.should be FooBar
|
25
|
+
req.new({ 'PATH_INFO' => '/foo_bar' }).to_ki_model_class.should be FooBar
|
26
|
+
req.new({ 'PATH_INFO' => '/foo_bar.json' }).to_ki_model_class.should be FooBar
|
27
27
|
end
|
28
28
|
|
29
29
|
it 'converts verb to action' do
|
30
|
-
req.new({'REQUEST_METHOD' => 'GET'}).to_action.should
|
31
|
-
req.new({'REQUEST_METHOD' => 'POST'}).to_action.should
|
32
|
-
req.new({'REQUEST_METHOD' => 'PUT'}).to_action.should
|
33
|
-
req.new({'REQUEST_METHOD' => 'DELETE'}).to_action.should
|
34
|
-
req.new({'REQUEST_METHOD' => 'SEARCH'}).to_action.should
|
30
|
+
req.new({ 'REQUEST_METHOD' => 'GET' }).to_action.should be :find
|
31
|
+
req.new({ 'REQUEST_METHOD' => 'POST' }).to_action.should be :create
|
32
|
+
req.new({ 'REQUEST_METHOD' => 'PUT' }).to_action.should be :update
|
33
|
+
req.new({ 'REQUEST_METHOD' => 'DELETE' }).to_action.should be :delete
|
34
|
+
req.new({ 'REQUEST_METHOD' => 'SEARCH' }).to_action.should be :find
|
35
35
|
end
|
36
36
|
|
37
37
|
context 'json' do
|
38
38
|
it 'considers application/json content type as a json request' do
|
39
|
-
req.new({'CONTENT_TYPE' => 'application/xml'}).json?.should be_false
|
39
|
+
req.new({ 'CONTENT_TYPE' => 'application/xml' }).json?.should be_false
|
40
40
|
req.new({}).json?.should be_false
|
41
|
-
req.new({'CONTENT_TYPE' => 'application/json'}).json?.should be_true
|
41
|
+
req.new({ 'CONTENT_TYPE' => 'application/json' }).json?.should be_true
|
42
42
|
end
|
43
43
|
|
44
44
|
it 'considers .json url format as a json request' do
|
45
|
-
req.new({'PATH_INFO' => '/foo'}).json?.should be_false
|
46
|
-
req.new({'PATH_INFO' => '/foo.json'}).json?.should be_true
|
45
|
+
req.new({ 'PATH_INFO' => '/foo' }).json?.should be_false
|
46
|
+
req.new({ 'PATH_INFO' => '/foo.json' }).json?.should be_true
|
47
47
|
end
|
48
48
|
|
49
49
|
# context 'params' do
|
@@ -54,7 +54,7 @@ describe Ki::BaseRequest do
|
|
54
54
|
# 'REQUEST_METHOD' => 'GET',
|
55
55
|
# 'rack.input' => ''
|
56
56
|
# })
|
57
|
-
# asd.params.should
|
57
|
+
# asd.params.should be {}
|
58
58
|
# end
|
59
59
|
|
60
60
|
# it 'handles query params' do
|
@@ -64,7 +64,7 @@ describe Ki::BaseRequest do
|
|
64
64
|
# 'REQUEST_METHOD' => 'GET',
|
65
65
|
# 'rack.input' => ''
|
66
66
|
# })
|
67
|
-
# asd.params.should
|
67
|
+
# asd.params.should be {'foo' => 'bar', 'cez' => ['dar', 'asd']}
|
68
68
|
# end
|
69
69
|
|
70
70
|
# it 'handles both query and body params' do
|
data/spec/lib/ki/helpers_spec.rb
CHANGED
@@ -0,0 +1,13 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Ki::KiApp do
|
4
|
+
it 'html returns 404 if not found' do
|
5
|
+
get '/invalid_url'
|
6
|
+
expect(last_response.status).to eq 404
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'json returns 404 if not found' do
|
10
|
+
get '/invalid_url.json'
|
11
|
+
expect(last_response.status).to eq 404
|
12
|
+
end
|
13
|
+
end
|
@@ -1,12 +1,26 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
3
|
describe Ki::KiConfig do
|
4
|
+
let(:config) { Ki::KiConfig.instance }
|
5
|
+
|
4
6
|
it 'should know db name in test env' do
|
5
|
-
|
7
|
+
config.environment.should == 'test'
|
6
8
|
end
|
7
9
|
|
8
10
|
it 'is overwritten for testing. see spec_helper' do
|
9
|
-
path =
|
11
|
+
path = config.config_file_path
|
10
12
|
path.start_with?('spec/config.yml').should be true
|
11
13
|
end
|
14
|
+
|
15
|
+
it 'defaults cors to true' do
|
16
|
+
expect(config).to be_cors
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'has a database method' do
|
20
|
+
expect(config.database['name']).to eq 'np_test'
|
21
|
+
end
|
22
|
+
|
23
|
+
it 'has a middleware method' do
|
24
|
+
expect(config.middleware).to_not be_empty
|
25
|
+
end
|
12
26
|
end
|