ki 0.4.6 → 0.4.7

Sign up to get free protection for your applications and to get access to all the features.
Files changed (54) hide show
  1. checksums.yaml +4 -4
  2. data/.rubocop.yml +24 -0
  3. data/Gemfile.lock +25 -6
  4. data/Guardfile +11 -5
  5. data/README.md +78 -0
  6. data/Rakefile +4 -4
  7. data/ki.gemspec +24 -23
  8. data/lib/ki.rb +7 -4
  9. data/lib/ki/base_request.rb +2 -2
  10. data/lib/ki/helpers.rb +7 -7
  11. data/lib/ki/ki.rb +12 -18
  12. data/lib/ki/ki_app.rb +16 -0
  13. data/lib/ki/ki_cli.rb +11 -12
  14. data/lib/ki/ki_config.rb +12 -8
  15. data/lib/ki/middleware/admin_interface_generator.rb +1 -1
  16. data/lib/ki/middleware/api_handler.rb +7 -8
  17. data/lib/ki/middleware/base_middleware.rb +1 -1
  18. data/lib/ki/middleware/coffee_compiler.rb +2 -2
  19. data/lib/ki/middleware/doc_generator.rb +5 -4
  20. data/lib/ki/middleware/haml_compiler.rb +4 -22
  21. data/lib/ki/{modules/format_of.rb → middleware/helpers/format_of_helper.rb} +3 -3
  22. data/lib/ki/middleware/helpers/haml_compiler_helper.rb +27 -0
  23. data/lib/ki/{modules → middleware/helpers}/public_file_helper.rb +2 -2
  24. data/lib/ki/{modules → middleware/helpers}/view_helper.rb +3 -3
  25. data/lib/ki/middleware/init_middleware.rb +1 -1
  26. data/lib/ki/middleware/public_file_server.rb +1 -1
  27. data/lib/ki/middleware/sass_compiler.rb +3 -3
  28. data/lib/ki/model.rb +10 -11
  29. data/lib/ki/modules/{model_helpers.rb → model_helper.rb} +1 -1
  30. data/lib/ki/modules/query_interface.rb +7 -7
  31. data/lib/ki/modules/restrictions.rb +4 -5
  32. data/lib/ki/orm.rb +11 -11
  33. data/lib/ki/utils/api_error.rb +4 -4
  34. data/lib/ki/utils/extra_ruby.rb +3 -3
  35. data/lib/ki/utils/indifferent_hash.rb +1 -1
  36. data/lib/ki/version.rb +1 -1
  37. data/lib/ki/views/404.haml +1 -0
  38. data/lib/ki/views/instadmin.haml +47 -4
  39. data/spec/examples/base/app.rb +5 -0
  40. data/spec/examples/json.northpole.ro/Gemfile +1 -1
  41. data/spec/lib/ki/base_request_spec.rb +24 -24
  42. data/spec/lib/ki/helpers_spec.rb +1 -1
  43. data/spec/lib/ki/ki_app_spec.rb +13 -0
  44. data/spec/lib/ki/ki_config_spec.rb +16 -2
  45. data/spec/lib/ki/ki_spec.rb +11 -0
  46. data/spec/lib/ki/middleware/haml_compiler_spec.rb +6 -4
  47. data/spec/lib/ki/{modules/format_of_spec.rb → middleware/helpers/format_of_helper_spec.rb} +4 -1
  48. data/spec/lib/ki/model_spec.rb +14 -14
  49. data/spec/lib/ki/modules/model_helper_spec.rb +4 -0
  50. data/spec/lib/ki/orm_spec.rb +25 -25
  51. data/spec/spec_helper.rb +6 -4
  52. data/spec/util_spec.rb +2 -2
  53. metadata +36 -14
  54. 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 hash={}
8
+ def count(hash = {})
9
9
  Orm::Db.instance.count class_name, hash
10
10
  end
11
11
 
12
- def find hash={}
12
+ def find(hash = {})
13
13
  Orm::Db.instance.find class_name, hash
14
14
  end
15
15
 
16
- def create hash
16
+ def create(hash)
17
17
  Orm::Db.instance.insert class_name, hash
18
18
  end
19
19
 
20
- def find_or_create hash
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 hash
25
+ def update(hash)
26
26
  Orm::Db.instance.update class_name, hash
27
27
  end
28
28
 
29
- def delete hash
29
+ def delete(hash)
30
30
  Orm::Db.instance.delete class_name, hash
31
31
  end
32
32
 
33
33
  def class_name
34
- self.to_s
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 *actions
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 *attributes
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 *attributes
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 method_name, attributes
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
@@ -30,7 +30,7 @@ module Ki
30
30
  #
31
31
  def establish_connection
32
32
  @config = KiConfig.instance.database
33
- if ENV["MONGODB_URI"]
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["MONGODB_URI"]
46
- ENV["MONGODB_URI"]
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 name, hash
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 name, hash={}
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 name, hash
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 name, hash
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 name, hash={}
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 hash
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']
@@ -2,7 +2,7 @@ module Ki
2
2
  class ApiError < StandardError #:nodoc:
3
3
  attr_reader :status
4
4
 
5
- def initialize body, status=400
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 s='forbidden', code=403
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 s='unauthroized', code=401
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 s
37
+ def initialize(s)
38
38
  super "partial #{s} not found", 404
39
39
  end
40
40
  end
@@ -9,10 +9,10 @@ class String
9
9
  # "user".to_class == User
10
10
  #
11
11
  def to_class
12
- chain = self.split "::"
12
+ chain = split '::'
13
13
  klass = Kernel
14
14
  chain.each do |klass_string|
15
- klass = klass.const_get klass_string.split('_').map{|w| w.capitalize}.join('')
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
- self.collect do |e|
25
+ collect do |e|
26
26
  if e['_id']
27
27
  e['id'] = e['_id'].to_s
28
28
  e.delete('_id')
@@ -1,5 +1,5 @@
1
1
  class IndifferentHash < Hash
2
- def []=(key,val)
2
+ def []=(key, val)
3
3
  key = key.to_sym
4
4
  super(key, val)
5
5
  end
@@ -1,3 +1,3 @@
1
1
  module Ki
2
- VERSION = "0.4.6"
2
+ VERSION = '0.4.7'
3
3
  end
@@ -0,0 +1 @@
1
+ %h1 404
@@ -1,5 +1,48 @@
1
- %h1 Ki InstAdmin
1
+ = js "//cdnjs.cloudflare.com/ajax/libs/angular.js/1.3.15/angular.js"
2
2
 
3
- %p
4
- Connected to
5
- = Ki::Orm::Db.instance.connection_string
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
+ });
@@ -1 +1,6 @@
1
1
  require 'ki'
2
+
3
+ # class Message < Ki::Model
4
+ # requires :title
5
+ # forbid :delete
6
+ # end
@@ -1,6 +1,6 @@
1
1
  source "https://rubygems.org"
2
2
 
3
3
  gem 'ki', :path => '../../..'
4
- gem 'passenger'
4
+ gem 'passenger', '4.0.40'
5
5
  gem 'capistrano'
6
6
  gem 'capistrano-rvm'
@@ -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 == nil
17
- req.new({'PATH_INFO' => '/'}).to_ki_model_class.should == nil
18
- req.new({'PATH_INFO' => 'foo'}).to_ki_model_class.should == Foo
19
- req.new({'PATH_INFO' => '/foo'}).to_ki_model_class.should == Foo
20
- req.new({'PATH_INFO' => '/Foo.json'}).to_ki_model_class.should == Foo
21
- req.new({'PATH_INFO' => '/bar.json'}).to_ki_model_class.should == Bar
22
- req.new({'PATH_INFO' => '/Foo/bar.json'}).to_ki_model_class.should == nil
23
- req.new({'PATH_INFO' => '/Foo/bar'}).to_ki_model_class.should == nil
24
- req.new({'PATH_INFO' => '/Foo_bar'}).to_ki_model_class.should == FooBar
25
- req.new({'PATH_INFO' => '/foo_bar'}).to_ki_model_class.should == FooBar
26
- req.new({'PATH_INFO' => '/foo_bar.json'}).to_ki_model_class.should == FooBar
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 == :find
31
- req.new({'REQUEST_METHOD' => 'POST'}).to_action.should == :create
32
- req.new({'REQUEST_METHOD' => 'PUT'}).to_action.should == :update
33
- req.new({'REQUEST_METHOD' => 'DELETE'}).to_action.should == :delete
34
- req.new({'REQUEST_METHOD' => 'SEARCH'}).to_action.should == :find
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 == {'foo' => 'bar', 'cez' => ['dar', 'asd']}
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
@@ -4,7 +4,7 @@ describe Ki::Helpers do
4
4
  include Ki::Helpers
5
5
 
6
6
  it 'should render_haml' do
7
- render_haml('%div.foo').should == "<div class='foo'></div>\n"
7
+ haml('%div.foo').should == "<div class='foo'></div>\n"
8
8
  end
9
9
 
10
10
  it 'renders css tag' do
@@ -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
- Ki::KiConfig.instance.environment.should == 'test'
7
+ config.environment.should == 'test'
6
8
  end
7
9
 
8
10
  it 'is overwritten for testing. see spec_helper' do
9
- path = Ki::KiConfig.instance.config_file_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
@@ -0,0 +1,11 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ki::Ki do
4
+ class CorsResource < Ki::Model
5
+ end
6
+
7
+ it 'has CORS enabled' do
8
+ get '/cors_resource.json'
9
+ expect(last_response.headers.key?('Vary')).to be true
10
+ end
11
+ end