ki 0.2.3 → 0.2.4

Sign up to get free protection for your applications and to get access to all the features.
Files changed (6) hide show
  1. data/VERSION +1 -1
  2. data/ki.gemspec +2 -2
  3. data/lib/db.rb +6 -2
  4. data/lib/req.rb +20 -1
  5. data/lib/resp.rb +18 -8
  6. metadata +3 -3
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.3
1
+ 0.2.4
data/ki.gemspec CHANGED
@@ -5,11 +5,11 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = "ki"
8
- s.version = "0.2.3"
8
+ s.version = "0.2.4"
9
9
 
10
10
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
11
11
  s.authors = ["Cristian Mircea Messel"]
12
- s.date = "2013-02-18"
12
+ s.date = "2013-02-19"
13
13
  s.description = "Fuck setting up a database for your prototypes. Some call it an ORM with a RESTful interface. Some say its for prototyping."
14
14
  s.email = "mess110@gmail.com"
15
15
  s.executables = ["ki"]
data/lib/db.rb CHANGED
@@ -48,8 +48,12 @@ module Ki
48
48
 
49
49
  def config_db
50
50
  config_path = File.join(Dir.pwd, 'config.yml')
51
- yaml = YAML.load_file(config_path)
52
- config yaml['database_host'], yaml['database_port'], yaml['database_name']
51
+ if File.exists? config_path
52
+ yaml = YAML.load_file(config_path)
53
+ config yaml['database_host'], yaml['database_port'], yaml['database_name']
54
+ else
55
+ config '127.0.0.1', 27017, 'ki_default_db'
56
+ end
53
57
  end
54
58
 
55
59
  def config host, port, db_name
data/lib/req.rb CHANGED
@@ -17,10 +17,29 @@ module Ki
17
17
 
18
18
  def route
19
19
  @klass = Util.base(path).to_class
20
- @format = Util.format(path)
20
+ @format = content_type == 'application/json' ? :json : Util.format(path)
21
21
  @code = klass || path == '/' ? 200 : 404
22
22
  end
23
23
 
24
+ def json?
25
+ @format == :json
26
+ end
27
+
28
+ def html?
29
+ @format == :html
30
+ end
31
+
32
+ def index?
33
+ path == '/'
34
+ end
35
+
36
+ def content_type_for_result
37
+ {
38
+ :json => 'application/json',
39
+ :html => 'text/html'
40
+ }[@format]
41
+ end
42
+
24
43
  private
25
44
 
26
45
  def static_path_requested?
@@ -9,22 +9,24 @@ module Ki
9
9
  def initialize req
10
10
  @req = req
11
11
  headers = {
12
- 'Content-Type' => respond_to(@req.format)
12
+ 'Content-Type' => @req.content_type_for_result
13
13
  }
14
14
 
15
+ # TODO think of a way to clean this up
15
16
  klass = @req.klass
16
17
  if klass.responds_to_formats.include? @req.format
17
18
  if @req.code == 404
18
- body = @req.format == :html ? partial('404') : { :error => 404 }
19
+ body = @req.html? ? partial('404') : { :error => 404 }
19
20
  else
20
- if @req.path == '/'
21
- body = @req.format == :html ? partial('index') : { :index => 200 }
21
+ if @req.index?
22
+ body = @req.html? ? partial('index') : { :index => 200 }
22
23
  else
23
24
  klass_view = Util.root_view_path(klass.to_s.downcase)
24
- if @req.format == :html
25
+ if @req.html?
25
26
  body = File.exists?(klass_view) ? partial(klass.to_s.downcase) : partial('index')
26
27
  else
27
28
  begin
29
+ interpret_json_params
28
30
  body = klass.new(@req).to_json
29
31
  rescue Exception => e
30
32
  @req.code = 400
@@ -35,7 +37,7 @@ module Ki
35
37
  end
36
38
  else
37
39
  @req.code = 406
38
- body = @req.format == :html ? partial('406') : { :error => 'format not allowed' }.to_json
40
+ body = @req.html? ? partial('406') : { :error => 'format not allowed' }.to_json
39
41
  end
40
42
 
41
43
  super [body], @req.code, headers
@@ -47,8 +49,16 @@ module Ki
47
49
  Util.render_partial s, { 'env' => @req.env }
48
50
  end
49
51
 
50
- def respond_to resp
51
- resp == :html ? 'text/html' : 'application/json'
52
+ def interpret_json_params
53
+ if @req.json? && @req.body.length > 0
54
+ begin
55
+ @req.body.rewind
56
+ @req.env.update('rack.request.form_hash' => JSON.parse(@req.body.string),
57
+ 'rack.request.form_input' => @req.env['rack.input'])
58
+ rescue JSON::ParserError
59
+ raise "json body invalid: #{@req.body.string}"
60
+ end
61
+ end
52
62
  end
53
63
  end
54
64
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ki
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-02-18 00:00:00.000000000 Z
12
+ date: 2013-02-19 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rack
@@ -343,7 +343,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
343
343
  version: '0'
344
344
  segments:
345
345
  - 0
346
- hash: 2930016913991517107
346
+ hash: 3465853450355248841
347
347
  required_rubygems_version: !ruby/object:Gem::Requirement
348
348
  none: false
349
349
  requirements: