ki 0.4.4 → 0.4.5

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: 0ddd8001c049b74417c89f5da05f779e705b311c
4
- data.tar.gz: a5b2026b178aba8c7c4eac298051faa4182e55ae
3
+ metadata.gz: 1220ce293d5e44656dcd0de47a4dead7235fecd2
4
+ data.tar.gz: 5304aed72ce2b943dbd1a29e9fab5c030876351b
5
5
  SHA512:
6
- metadata.gz: 328c8df2c4df3ac20938130cd48bf27ea04036f108f1e6eecc03b0affeb77cc2d108fa1d950ea9b0fc0d0a42c4ac33976334af971838f2e81fd01f084ba38b5d
7
- data.tar.gz: dbca108a4cd6bd95c417b954158b6267eb6104326880718132012cee449a127660d551cb678b50613176f7475da51459cf5910d9489ff23cc3b1a840c361b63a
6
+ metadata.gz: 79ebf8cb49cbb020aaf8af5e5858bcd89b3d2188eaf747ea7db8434630f597274fcf2eec357e508973b4a617d08d5dfaa1f6d89e386012d92e493159060842ed
7
+ data.tar.gz: fbe87bdd7e91000f2f00ad50e442fe56a4b79ff6019b90c3e2b71b80056fcb74618d985aa1d16e593921512b09a9fb9792fdd875a66901e50588fc58bbec1750
data/.gitignore CHANGED
@@ -2,6 +2,7 @@
2
2
  *.rbc
3
3
  /pkg/
4
4
  /spec/reports/
5
+ /spec/config.yml
5
6
  /tmp/
6
7
 
7
8
  /.bundle/
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.1.0
5
+ services:
6
+ - mongodb
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- ki (0.4.4)
4
+ ki (0.4.5)
5
5
  bson_ext
6
6
  bundler (~> 1.5)
7
7
  coffee-script
@@ -23,12 +23,15 @@ GEM
23
23
  celluloid-io (0.15.0)
24
24
  celluloid (>= 0.15.0)
25
25
  nio4r (>= 0.5.0)
26
+ codeclimate-test-reporter (0.4.7)
27
+ simplecov (>= 0.7.1, < 1.0.0)
26
28
  coderay (1.1.0)
27
29
  coffee-script (2.2.0)
28
30
  coffee-script-source
29
31
  execjs
30
32
  coffee-script-source (1.7.0)
31
33
  diff-lcs (1.2.5)
34
+ docile (1.1.5)
32
35
  execjs (2.0.2)
33
36
  ffi (1.9.3)
34
37
  formatador (0.2.4)
@@ -52,6 +55,7 @@ GEM
52
55
  method_source (0.8.2)
53
56
  mongo (1.9.2)
54
57
  bson (~> 1.9.2)
58
+ multi_json (1.11.0)
55
59
  nio4r (1.0.0)
56
60
  pry (0.9.12.6)
57
61
  coderay (~> 1.0)
@@ -75,6 +79,11 @@ GEM
75
79
  diff-lcs (>= 1.1.3, < 2.0)
76
80
  rspec-mocks (2.14.6)
77
81
  sass (3.3.4)
82
+ simplecov (0.9.2)
83
+ docile (~> 1.1.0)
84
+ multi_json (~> 1.0)
85
+ simplecov-html (~> 0.9.0)
86
+ simplecov-html (0.9.0)
78
87
  slop (3.5.0)
79
88
  thor (0.18.1)
80
89
  tilt (2.0.1)
@@ -84,6 +93,7 @@ PLATFORMS
84
93
  ruby
85
94
 
86
95
  DEPENDENCIES
96
+ codeclimate-test-reporter
87
97
  guard-rspec
88
98
  ki!
89
99
  rack-test
@@ -0,0 +1,45 @@
1
+ # Ki Framework
2
+
3
+ Ki uses middleware to provide a plug-n-play way of customizing requests.
4
+ Rack handles everything™
5
+
6
+ ## Middleware
7
+
8
+ The default enabled middleware list:
9
+
10
+ * ApiHandler
11
+ * CoffeeCompiler
12
+ * SassCompiler
13
+ * HamlCompiler
14
+ * PublicFileServer
15
+
16
+ ### ApiHandler
17
+
18
+ Handles all api calls.
19
+
20
+ ### CoffeeCompiler
21
+
22
+ On request, compiles coffee to js.
23
+
24
+ For example, if you have a file *public/javascripts/app.coffee*, accessing
25
+ */public/javascripts/app.js* will return the compiled coffee file.
26
+
27
+ ### SassCompiler
28
+
29
+ Similar to CoffeeCompiler, compiles sass to css.
30
+
31
+ ### HamlCompiler
32
+
33
+ Similar to CoffeeCompiler, compiles haml to html.
34
+
35
+ ### PublicFileServer
36
+
37
+ Serves static files from the *public/* folder.
38
+
39
+ ## Make your own
40
+
41
+ TODO
42
+
43
+ ### config.yml
44
+
45
+ TODO middleware config example.
data/README.md CHANGED
@@ -1,6 +1,10 @@
1
1
  # Ki Framework
2
2
 
3
- Tiny REST JSON ORM framework.
3
+ [![Build Status](https://travis-ci.org/mess110/ki.svg?branch=master)](https://travis-ci.org/mess110/ki)
4
+ [![Code Climate](https://codeclimate.com/github/mess110/ki/badges/gpa.svg)](https://codeclimate.com/github/mess110/ki)
5
+ [![Test Coverage](https://codeclimate.com/github/mess110/ki/badges/coverage.svg)](https://codeclimate.com/github/mess110/ki)
6
+
7
+ Tiny REST JSON ORM framework with MongoDB.
4
8
 
5
9
  Ki's goal is to help you protoype your ideas blazing fast. It has a db backend
6
10
  and provides a fullblown REST api on top.
@@ -116,6 +120,12 @@ other haml files. The content of the route will be placed in the layout *yield*
116
120
  %p Hello World!
117
121
  ```
118
122
 
123
+ ### Middleware
124
+
125
+ Views, assets, haml, less, sass are all handled through Rack middleware.
126
+
127
+ You can learn more about ki middleware [here](MIDDLEWARE.md).
128
+
119
129
  ### Helpers
120
130
 
121
131
  To reduce complexity in your views, you can use helpers. Here we create a
@@ -286,3 +296,8 @@ heroku config:set MONGODB_URI="mongodb://user:pass@mongo_url:mongo_port/db_name"
286
296
  git push heroku master
287
297
  heroku open
288
298
  ```
299
+
300
+ ## Documentation
301
+
302
+ Ki offers instant documentation. Add the middleware 'DocGenerator' and access
303
+ */instadoc*.
data/ki.gemspec CHANGED
@@ -32,4 +32,5 @@ Gem::Specification.new do |spec|
32
32
  spec.add_development_dependency "rack-test"
33
33
  spec.add_development_dependency "rspec"
34
34
  spec.add_development_dependency "guard-rspec"
35
+ spec.add_development_dependency "codeclimate-test-reporter"
35
36
  end
data/lib/ki.rb CHANGED
@@ -23,7 +23,7 @@ require 'ki/indifferent_hash'
23
23
  require 'ki/ki_config'
24
24
  require 'ki/helpers'
25
25
  require 'ki/orm'
26
+ require 'ki/middleware'
26
27
  require 'ki/ki'
27
28
  require 'ki/model'
28
- require 'ki/middleware'
29
29
  require 'ki/base_request'
@@ -6,6 +6,10 @@ module Ki
6
6
  path == '/'
7
7
  end
8
8
 
9
+ def doc?
10
+ path == '/instadoc'
11
+ end
12
+
9
13
  def json?
10
14
  content_type == 'application/json' || format_of(path) == 'json'
11
15
  end
@@ -34,6 +34,16 @@ class Array
34
34
  e
35
35
  end
36
36
  end
37
+
38
+ def present?
39
+ !nil? && !empty?
40
+ end
41
+ end
42
+
43
+ class NilClass
44
+ def present?
45
+ false
46
+ end
37
47
  end
38
48
 
39
49
  module Ki
@@ -20,11 +20,9 @@ module Ki
20
20
  use Middleware::InitMiddleware
21
21
  # TODO what happens with invalid json?
22
22
  use Rack::Parser, :content_types => { 'application/json' => Proc.new { |body| ::MultiJson.decode body } }
23
- use Middleware::ApiHandler
24
- use Middleware::CoffeeCompiler
25
- use Middleware::SassCompiler
26
- use Middleware::HamlCompiler
27
- use Middleware::PublicFileServer
23
+ KiConfig.instance.middleware.each do |middleware|
24
+ use middleware
25
+ end
28
26
  run KiApp.new
29
27
  end
30
28
  end
@@ -4,13 +4,27 @@ module Ki
4
4
  class KiConfig
5
5
  include Singleton
6
6
 
7
- CONFIG_FILE_PATH = 'config.yml'
8
-
9
7
  attr_reader :config, :environment
10
8
 
11
9
  def read environment
12
10
  @environment = environment
13
- @config = YAML.load_file(CONFIG_FILE_PATH)[environment]
11
+ @config = YAML.load_file(config_file_path)[environment]
12
+ end
13
+
14
+ def config_file_path
15
+ 'config.yml'
16
+ end
17
+
18
+ def middleware
19
+ used_middleware = %w(ApiHandler CoffeeCompiler SassCompiler HamlCompiler PublicFileServer)
20
+
21
+ if @config['middleware'].present?
22
+ used_middleware = @config['middleware']
23
+ end
24
+
25
+ used_middleware.map { |middleware|
26
+ Object.const_get('Ki').const_get('Middleware').const_get(middleware)
27
+ }
14
28
  end
15
29
 
16
30
  def database
@@ -1,160 +1,11 @@
1
- module Ki
2
- module Middleware #:nodoc:
3
- module BaseMiddleware
4
- include Helpers::FormatOf
5
- include Helpers::View
6
- include Helpers::PublicFile
1
+ require 'ki/middleware/base_middleware'
7
2
 
8
- def initialize app
9
- @app = app
10
- end
11
- end
3
+ require 'ki/middleware/init_middleware'
4
+ require 'ki/middleware/api_handler'
5
+ require 'ki/middleware/public_file_server'
12
6
 
13
- class PublicFileServer
14
- include BaseMiddleware
7
+ require 'ki/middleware/sass_compiler'
8
+ require 'ki/middleware/haml_compiler'
9
+ require 'ki/middleware/coffee_compiler'
15
10
 
16
- def call env
17
- req = BaseRequest.new env
18
- if public_file_exists? req
19
- Rack::File.new(Ki::PUBLIC_PATH).call env
20
- else
21
- @app.call env
22
- end
23
- end
24
- end
25
-
26
- class InitMiddleware
27
- include BaseMiddleware
28
-
29
- def call env
30
- req = BaseRequest.new env
31
- if req.root?
32
- if public_file_exists? 'index.html'
33
- env['PATH_INFO'] = '/index.html'
34
- Rack::File.new(Ki::PUBLIC_PATH).call env
35
- else
36
- resp = Rack::Response.new
37
- resp.redirect('/index')
38
- resp.finish
39
- end
40
- else
41
- env['CONTENT_TYPE'] = 'application/json' if format_of(req) == 'json'
42
- @app.call env
43
- end
44
- end
45
- end
46
-
47
- class HamlCompiler
48
- include BaseMiddleware
49
-
50
- def call env
51
- req = BaseRequest.new env
52
- if view_exists?(req)
53
- render_haml view_path(req)
54
- else
55
- @app.call env
56
- end
57
- end
58
-
59
- def render_haml file_path
60
- file_contents = File.read(file_path)
61
-
62
- if view_exists? 'layout'
63
- layout_contents = File.read(view_path('layout'))
64
- else
65
- layout_contents = "= yield"
66
- end
67
-
68
- html = haml(layout_contents).render do
69
- haml(file_contents).render
70
- end
71
-
72
- Rack::Response.new(html).finish
73
- end
74
-
75
- def haml s
76
- Haml::Engine.new("- extend Ki::Helpers\n" + s)
77
- end
78
- end
79
-
80
- class CoffeeCompiler
81
- include BaseMiddleware
82
-
83
- def call env
84
- req = BaseRequest.new env
85
- coffee_path = req.path.to_s[0...-3] + '.coffee'
86
- if !public_file_exists?(req) && format_of(req) == 'js' && public_file_exists?(coffee_path)
87
- js = CoffeeScript.compile(File.read(public_file_path(coffee_path)))
88
- Rack::Response.new(js, 200, {"Content-Type" => "application/javascript"}).finish
89
- else
90
- @app.call env
91
- end
92
- end
93
- end
94
-
95
- class SassCompiler
96
- include BaseMiddleware
97
-
98
- def call env
99
- req = BaseRequest.new env
100
- sass_path = req.path.to_s[0...-4] + '.sass'
101
- # if req ends with css and it does not exist, if a sass file exists instead
102
- if !public_file_exists?(req) && format_of(req) == 'css' && public_file_exists?(sass_path)
103
- eng = Sass::Engine.new(File.read(public_file_path(sass_path)), :syntax => :sass)
104
- Rack::Response.new(eng.render, 200, {"Content-Type" => "text/css"}).finish
105
- else
106
- @app.call env
107
- end
108
- end
109
- end
110
-
111
- # Handles all API calls
112
- #
113
- # Any json request is considered an api call. A request is considered as json
114
- # if the format is .json or Content-Type header is set to 'application/json'
115
- #
116
- # If the query param 'redirect_to' is given, the response will not contain the
117
- # json output from the url, instead it will redirect to the url given
118
- class ApiHandler
119
- include BaseMiddleware
120
-
121
- def call env
122
- req = BaseRequest.new env
123
- if req.json?
124
- begin
125
- klass = req.to_ki_model_class
126
- if Model.descendants.include? klass
127
- # TODO do not have redirect_to param
128
- model = klass.new(req.to_action, req.params)
129
- # TODO document this
130
- if req.params['redirect_to'].nil?
131
- render model
132
- else
133
- # TODO check for injection
134
- redirect_to req.params['redirect_to']
135
- end
136
- else
137
- raise InvalidUrlError.new("invalid url '#{req.path}'", 404)
138
- end
139
- rescue ApiError => e
140
- render e
141
- end
142
- else
143
- @app.call env
144
- end
145
- end
146
-
147
- def redirect_to s
148
- resp = Rack::Response.new
149
- resp.redirect(s)
150
- resp.finish
151
- end
152
-
153
- def render r
154
- resp = Rack::Response.new(r.result.to_json, r.status)
155
- resp['Content-Type'] = 'application/json'
156
- resp.finish
157
- end
158
- end
159
- end
160
- end
11
+ require 'ki/middleware/doc_generator'
@@ -0,0 +1,53 @@
1
+ module Ki
2
+ module Middleware #:nodoc:
3
+
4
+ # Handles all API calls
5
+ #
6
+ # Any json request is considered an api call. A request is considered as json
7
+ # if the format is .json or Content-Type header is set to 'application/json'
8
+ #
9
+ # If the query param 'redirect_to' is given, the response will not contain the
10
+ # json output from the url, instead it will redirect to the url given
11
+ class ApiHandler
12
+ include BaseMiddleware
13
+
14
+ def call env
15
+ req = BaseRequest.new env
16
+ if req.json?
17
+ resourcerize(req)
18
+ else
19
+ @app.call env
20
+ end
21
+ end
22
+
23
+ def resourcerize req
24
+ klass = req.to_ki_model_class
25
+
26
+ unless Model.descendants.include?(klass)
27
+ raise InvalidUrlError.new("invalid url '#{req.path}'", 404)
28
+ end
29
+
30
+ model = klass.new(req.to_action, req.params)
31
+ if req.params['redirect_to'].nil? # TODO document this
32
+ render model
33
+ else
34
+ redirect_to req.params['redirect_to'] # TODO check for injection
35
+ end
36
+ rescue ApiError => e
37
+ render e
38
+ end
39
+
40
+ def redirect_to s
41
+ resp = Rack::Response.new
42
+ resp.redirect(s)
43
+ resp.finish
44
+ end
45
+
46
+ def render r
47
+ resp = Rack::Response.new(r.result.to_json, r.status)
48
+ resp['Content-Type'] = 'application/json'
49
+ resp.finish
50
+ end
51
+ end
52
+ end
53
+ end
@@ -0,0 +1,13 @@
1
+ module Ki
2
+ module Middleware #:nodoc:
3
+ module BaseMiddleware
4
+ include Helpers::FormatOf
5
+ include Helpers::View
6
+ include Helpers::PublicFile
7
+
8
+ def initialize app
9
+ @app = app
10
+ end
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,18 @@
1
+ module Ki
2
+ module Middleware #:nodoc:
3
+ class CoffeeCompiler
4
+ include BaseMiddleware
5
+
6
+ def call env
7
+ req = BaseRequest.new env
8
+ coffee_path = req.path.to_s[0...-3] + '.coffee'
9
+ if !public_file_exists?(req) && format_of(req) == 'js' && public_file_exists?(coffee_path)
10
+ js = CoffeeScript.compile(File.read(public_file_path(coffee_path)))
11
+ Rack::Response.new(js, 200, {"Content-Type" => "application/javascript"}).finish
12
+ else
13
+ @app.call env
14
+ end
15
+ end
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,24 @@
1
+ module Ki
2
+ module Middleware
3
+ class DocGenerator < HamlCompiler
4
+ include BaseMiddleware
5
+
6
+ def call env
7
+ req = BaseRequest.new env
8
+ if req.doc?
9
+ if view_exists?(req)
10
+ render_haml view_path(req)
11
+ else
12
+ render_haml doc_view_path(req)
13
+ end
14
+ else
15
+ @app.call env
16
+ end
17
+ end
18
+
19
+ def doc_view_path path
20
+ File.join(File.dirname(__FILE__), '..', 'views', 'instadoc.haml')
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,36 @@
1
+ module Ki
2
+ module Middleware
3
+ class HamlCompiler
4
+ include BaseMiddleware
5
+
6
+ def call env
7
+ req = BaseRequest.new env
8
+ if view_exists?(req)
9
+ render_haml view_path(req)
10
+ else
11
+ @app.call env
12
+ end
13
+ end
14
+
15
+ def render_haml file_path
16
+ file_contents = File.read(file_path)
17
+
18
+ if view_exists? 'layout'
19
+ layout_contents = File.read(view_path('layout'))
20
+ else
21
+ layout_contents = "= yield"
22
+ end
23
+
24
+ html = haml(layout_contents).render do
25
+ haml(file_contents).render
26
+ end
27
+
28
+ Rack::Response.new(html).finish
29
+ end
30
+
31
+ def haml s
32
+ Haml::Engine.new("- extend Ki::Helpers\n" + s)
33
+ end
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,24 @@
1
+ module Ki
2
+ module Middleware
3
+ class InitMiddleware
4
+ include BaseMiddleware
5
+
6
+ def call env
7
+ req = BaseRequest.new env
8
+ if req.root?
9
+ if public_file_exists? 'index.html'
10
+ env['PATH_INFO'] = '/index.html'
11
+ Rack::File.new(Ki::PUBLIC_PATH).call env
12
+ else
13
+ resp = Rack::Response.new
14
+ resp.redirect('/index')
15
+ resp.finish
16
+ end
17
+ else
18
+ env['CONTENT_TYPE'] = 'application/json' if format_of(req) == 'json'
19
+ @app.call env
20
+ end
21
+ end
22
+ end
23
+ end
24
+ end
@@ -0,0 +1,16 @@
1
+ module Ki
2
+ module Middleware #:nodoc:
3
+ class PublicFileServer
4
+ include BaseMiddleware
5
+
6
+ def call env
7
+ req = BaseRequest.new env
8
+ if public_file_exists? req
9
+ Rack::File.new(Ki::PUBLIC_PATH).call env
10
+ else
11
+ @app.call env
12
+ end
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,19 @@
1
+ module Ki
2
+ module Middleware #:nodoc:
3
+ class SassCompiler
4
+ include BaseMiddleware
5
+
6
+ def call env
7
+ req = BaseRequest.new env
8
+ sass_path = req.path.to_s[0...-4] + '.sass'
9
+ # if req ends with css and it does not exist, if a sass file exists instead
10
+ if !public_file_exists?(req) && format_of(req) == 'css' && public_file_exists?(sass_path)
11
+ eng = Sass::Engine.new(File.read(public_file_path(sass_path)), :syntax => :sass)
12
+ Rack::Response.new(eng.render, 200, {"Content-Type" => "text/css"}).finish
13
+ else
14
+ @app.call env
15
+ end
16
+ end
17
+ end
18
+ end
19
+ end
@@ -6,14 +6,7 @@ module Ki
6
6
  end
7
7
 
8
8
  def forbid *actions
9
- send :define_method, :forbidden_actions do
10
- actions
11
- end
12
-
13
- eigen_class = class << self; self; end
14
- eigen_class.send(:define_method, :forbidden_actions) do
15
- actions
16
- end
9
+ generic_restriction :forbidden_actions, actions
17
10
  end
18
11
 
19
12
  def required_attributes
@@ -21,14 +14,7 @@ module Ki
21
14
  end
22
15
 
23
16
  def requires *attributes
24
- send :define_method, :required_attributes do
25
- attributes
26
- end
27
-
28
- eigen_class = class << self; self; end
29
- eigen_class.send(:define_method, :required_attributes) do
30
- attributes
31
- end
17
+ generic_restriction :required_attributes, attributes
32
18
  end
33
19
 
34
20
  def unique_attributes
@@ -36,15 +22,22 @@ module Ki
36
22
  end
37
23
 
38
24
  def unique *attributes
39
- send :define_method, :unique_attributes do
25
+ generic_restriction :unique_attributes, attributes
26
+ end
27
+
28
+ private
29
+
30
+ def generic_restriction method_name, attributes
31
+ send :define_method, method_name do
40
32
  attributes
41
33
  end
42
34
 
43
35
  eigen_class = class << self; self; end
44
- eigen_class.send(:define_method, :unique_attributes) do
36
+ eigen_class.send(:define_method, method_name) do
45
37
  attributes
46
38
  end
47
39
  end
40
+
48
41
  end
49
42
  end
50
43
  end
@@ -1,3 +1,3 @@
1
1
  module Ki
2
- VERSION = "0.4.4"
2
+ VERSION = "0.4.5"
3
3
  end
@@ -0,0 +1,16 @@
1
+ %h1 Ki InstaDoc
2
+
3
+ %ul
4
+ - Ki::Model.descendants.each do |model|
5
+ %li
6
+ %div
7
+ %h2= model.to_s
8
+ %p
9
+ Required attributes:
10
+ = model.required_attributes
11
+ %p
12
+ Forbidden actions:
13
+ = model.forbidden_actions
14
+ %p
15
+ Unique attributes:
16
+ = model.unique_attributes
@@ -0,0 +1,25 @@
1
+ development:
2
+ database:
3
+ name: np_development
4
+ host: 127.0.0.1
5
+ port: 27017
6
+
7
+ test:
8
+ middleware: [
9
+ 'ApiHandler',
10
+ 'DocGenerator',
11
+ 'CoffeeCompiler',
12
+ 'SassCompiler',
13
+ 'HamlCompiler',
14
+ 'PublicFileServer'
15
+ ]
16
+ database:
17
+ name: np_test
18
+ host: 127.0.0.1
19
+ port: 27017
20
+
21
+ production:
22
+ database:
23
+ name: np
24
+ host: 127.0.0.1
25
+ port: 27017
@@ -7,6 +7,11 @@ describe Ki::Helpers do
7
7
  render_haml('%div.foo').should == "<div class='foo'></div>\n"
8
8
  end
9
9
 
10
- it 'should render a partial' do
10
+ it 'renders css tag' do
11
+ expect(css('asd')).to eq "<link href='asd' rel='stylesheet'>\n"
12
+ end
13
+
14
+ it 'renders js tag' do
15
+ expect(js('asd')).to eq "<script src='asd'></script>\n"
11
16
  end
12
17
  end
@@ -4,4 +4,9 @@ describe Ki::KiConfig do
4
4
  it 'should know db name in test env' do
5
5
  Ki::KiConfig.instance.environment.should == 'test'
6
6
  end
7
+
8
+ it 'is overwritten for testing. see spec_helper' do
9
+ path = Ki::KiConfig.instance.config_file_path
10
+ path.start_with?('spec/config.yml').should be true
11
+ end
7
12
  end
@@ -0,0 +1,8 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ki::Middleware::DocGenerator do
4
+ it 'works' do
5
+ get '/instadoc'
6
+ expect(last_response.body).to include('Ki InstaDoc')
7
+ end
8
+ end
@@ -0,0 +1,16 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ki::Middleware::HamlCompiler do
4
+ it 'works' do
5
+ Ki::Middleware::HamlCompiler.any_instance.stub(:view_exists?).and_return(true)
6
+ File.stub(:read).and_return("%p hello")
7
+ get '/existing_haml'
8
+ expect(last_response.body).to eq "<p>hello</p>\n"
9
+ end
10
+
11
+ it 'passes to next middleware' do
12
+ Ki::Middleware::HamlCompiler.any_instance.stub(:view_exists?).and_return(false)
13
+ get '/inexisting_haml'
14
+ expect(last_response.body).to eq "misplaced in space"
15
+ end
16
+ end
@@ -4,7 +4,7 @@ describe Ki::Middleware do
4
4
  context Ki::Middleware::ApiHandler do
5
5
  it 'should only handle urls maped from objects that extend Ki::Model' do
6
6
  get '/asd.json'
7
- JSON.parse(last_response.body)
7
+ json
8
8
  last_response.status.should == 404
9
9
  end
10
10
  end
@@ -0,0 +1,4 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ki::Model::ModelHelpers do
4
+ end
@@ -1,3 +1,6 @@
1
+ require "codeclimate-test-reporter"
2
+ CodeClimate::TestReporter.start
3
+
1
4
  $LOAD_PATH.unshift(File.join(File.dirname(__FILE__), '..', 'lib'))
2
5
  ENV['RACK_ENV'] = 'test'
3
6
 
@@ -6,10 +9,34 @@ include Rack::Test::Methods
6
9
 
7
10
  require 'ki'
8
11
 
9
- Ki::KiConfig::CONFIG_FILE_PATH = 'spec/examples/base/config.yml'
12
+ module Ki
13
+ class KiConfig
14
+ def config_file_path
15
+ config_yml_path = File.exists?('spec/config.yml') ? 'spec/config.yml' : 'spec/config.yml.example'
16
+ if config_yml_path.end_with?('example')
17
+ puts 'WARNING: spec/config.yml.example used'
18
+ end
19
+ config_yml_path
20
+ end
21
+ end
22
+ end
23
+
10
24
  Ki::KiConfig.instance.read 'test'
11
25
  Ki::Orm::Db.instance.establish_connection
12
26
 
27
+ # http://dhartweg.roon.io/rspec-testing-for-a-json-api
28
+ module Requests
29
+ module JsonHelpers
30
+ def json
31
+ @json = JSON.parse(last_response.body)
32
+ # TODO find out if it is needed to convert to a hash with indifferent
33
+ # access
34
+ #@json = @json.with_indifferent_access if @json.class == Hash
35
+ @json
36
+ end
37
+ end
38
+ end
39
+
13
40
  def app
14
41
  Ki::Ki.new
15
42
  end
@@ -20,5 +47,7 @@ RSpec.configure do |config|
20
47
  config.run_all_when_everything_filtered = true
21
48
  config.filter_run :focus
22
49
 
50
+ config.include Requests::JsonHelpers
51
+
23
52
  config.order = 'random'
24
53
  end
@@ -11,4 +11,11 @@ describe Ki do
11
11
  'cez'.to_class.should == Cez
12
12
  'cez_bar'.to_class.should == CezBar
13
13
  end
14
+
15
+ describe Array do
16
+ it 'responds to present?' do
17
+ expect([]).to_not be_present
18
+ expect([1]).to be_present
19
+ end
20
+ end
14
21
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.4.4
4
+ version: 0.4.5
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cristian Mircea Messel
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-02-09 00:00:00.000000000 Z
11
+ date: 2015-04-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -192,6 +192,20 @@ dependencies:
192
192
  - - ">="
193
193
  - !ruby/object:Gem::Version
194
194
  version: '0'
195
+ - !ruby/object:Gem::Dependency
196
+ name: codeclimate-test-reporter
197
+ requirement: !ruby/object:Gem::Requirement
198
+ requirements:
199
+ - - ">="
200
+ - !ruby/object:Gem::Version
201
+ version: '0'
202
+ type: :development
203
+ prerelease: false
204
+ version_requirements: !ruby/object:Gem::Requirement
205
+ requirements:
206
+ - - ">="
207
+ - !ruby/object:Gem::Version
208
+ version: '0'
195
209
  description: it said optional
196
210
  email:
197
211
  - mess110@gmail.com
@@ -204,10 +218,12 @@ files:
204
218
  - ".rspec"
205
219
  - ".ruby-gemset"
206
220
  - ".ruby-version"
221
+ - ".travis.yml"
207
222
  - Gemfile
208
223
  - Gemfile.lock
209
224
  - Guardfile
210
225
  - LICENSE.md
226
+ - MIDDLEWARE.md
211
227
  - README.md
212
228
  - Rakefile
213
229
  - bin/ki
@@ -221,6 +237,14 @@ files:
221
237
  - lib/ki/ki_cli.rb
222
238
  - lib/ki/ki_config.rb
223
239
  - lib/ki/middleware.rb
240
+ - lib/ki/middleware/api_handler.rb
241
+ - lib/ki/middleware/base_middleware.rb
242
+ - lib/ki/middleware/coffee_compiler.rb
243
+ - lib/ki/middleware/doc_generator.rb
244
+ - lib/ki/middleware/haml_compiler.rb
245
+ - lib/ki/middleware/init_middleware.rb
246
+ - lib/ki/middleware/public_file_server.rb
247
+ - lib/ki/middleware/sass_compiler.rb
224
248
  - lib/ki/model.rb
225
249
  - lib/ki/modules/callbacks.rb
226
250
  - lib/ki/modules/format_of.rb
@@ -231,6 +255,8 @@ files:
231
255
  - lib/ki/modules/view_helper.rb
232
256
  - lib/ki/orm.rb
233
257
  - lib/ki/version.rb
258
+ - lib/ki/views/instadoc.haml
259
+ - spec/config.yml.example
234
260
  - spec/examples/base/.ruby-gemset
235
261
  - spec/examples/base/.ruby-version
236
262
  - spec/examples/base/Gemfile
@@ -354,9 +380,12 @@ files:
354
380
  - spec/lib/ki/helpers_spec.rb
355
381
  - spec/lib/ki/indifferent_hash_spec.rb
356
382
  - spec/lib/ki/ki_config_spec.rb
383
+ - spec/lib/ki/middleware/doc_generator_spec.rb
384
+ - spec/lib/ki/middleware/haml_compiler_spec.rb
357
385
  - spec/lib/ki/middleware_spec.rb
358
386
  - spec/lib/ki/model_spec.rb
359
387
  - spec/lib/ki/modules/format_of_spec.rb
388
+ - spec/lib/ki/modules/model_helpers_spec.rb
360
389
  - spec/lib/ki/orm_spec.rb
361
390
  - spec/spec_helper.rb
362
391
  - spec/util_spec.rb
@@ -386,6 +415,7 @@ signing_key:
386
415
  specification_version: 4
387
416
  summary: it said optional
388
417
  test_files:
418
+ - spec/config.yml.example
389
419
  - spec/examples/base/.ruby-gemset
390
420
  - spec/examples/base/.ruby-version
391
421
  - spec/examples/base/Gemfile
@@ -509,9 +539,12 @@ test_files:
509
539
  - spec/lib/ki/helpers_spec.rb
510
540
  - spec/lib/ki/indifferent_hash_spec.rb
511
541
  - spec/lib/ki/ki_config_spec.rb
542
+ - spec/lib/ki/middleware/doc_generator_spec.rb
543
+ - spec/lib/ki/middleware/haml_compiler_spec.rb
512
544
  - spec/lib/ki/middleware_spec.rb
513
545
  - spec/lib/ki/model_spec.rb
514
546
  - spec/lib/ki/modules/format_of_spec.rb
547
+ - spec/lib/ki/modules/model_helpers_spec.rb
515
548
  - spec/lib/ki/orm_spec.rb
516
549
  - spec/spec_helper.rb
517
550
  - spec/util_spec.rb