opi 0.3 → 0.3.9

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: d14b10a860811f91e4d861947341d296ab2420a6
4
- data.tar.gz: 086b83d0e8918ae26cde595aaa34e34e220d9d36
3
+ metadata.gz: f6c22ba2d07075d6abcab27a8214a92141d2f479
4
+ data.tar.gz: be7fc8d6a27381d7292b844768409e6d7cab1461
5
5
  SHA512:
6
- metadata.gz: 7328546ad02aaa19bedeb5f220b39b18bc0910f7da8068318c0eb7e895506e9ac9c7b6e0573ca49be9ef36bf48718521bc14bccdba3964ad5bdd9ba17d5bc438
7
- data.tar.gz: 14747631c52650d4710daccbdece06c9d12c9607598c270038e1233a961c50705ceaa99ef500a9b4e8398805e2004e05f07cdb93e94ffa4155469edf82460ca4
6
+ metadata.gz: aa4c5d2e6a3e40cbed0d4a629db0c6dca998b20beba5aa5d6716545b22d0adfce82511dd54056ee9a6d159b602a64b957182c8d68e324371a922328afb63088f
7
+ data.tar.gz: 82d97454308f21225b1833b9f079eb3c1011eb6d2ed6a7141b180e5ea3085fdf14aefa15f958bcf7cc1f3ba737b45de883dd455760d1c1eafade4743abc267f2
data/README.md CHANGED
@@ -12,9 +12,7 @@ so opinionated it is very likely to offend. But that is alright.
12
12
  Opi was born out of frustration with writing too much boilerplate code for api
13
13
  services.
14
14
 
15
- **JSON-only**. The server CANNOT respond with anything else other than JSON.
16
- All error responses are JSON out of the box. You CANNOT respond with HTML.
17
- The response content type is hardcoded to JSON.
15
+ **JSON-default**. All responses default to JSON. Error responses are json-aware.
18
16
 
19
17
  **No Controllers**. Well.. there are route blocks which are the equivalent
20
18
  of the controller but you are strongly encouraged to only ever execute actions
data/lib/opi.rb CHANGED
@@ -1,6 +1,7 @@
1
1
  require 'rack'
2
2
  require 'colored'
3
3
  require 'json'
4
+ require 'logger'
4
5
 
5
6
  require_relative './opi/version'
6
7
  require_relative './opi/router'
@@ -3,8 +3,6 @@ module Opi
3
3
 
4
4
  class << self
5
5
 
6
- puts "* Opi Version: #{Opi::VERSION}".green
7
-
8
6
  def get(path, options={}, &block)
9
7
  route 'GET', path, options, block
10
8
  end
@@ -54,25 +52,41 @@ module Opi
54
52
 
55
53
  end
56
54
 
55
+ attr_reader :logger
56
+
57
+ def initialize(options={})
58
+ puts "* Opi Version: #{Opi::VERSION} initializing".green
59
+ @logger = options[:logger] || Logger.new(STDOUT)
60
+ @logger.level = options[:debug] ? Logger::DEBUG : Logger::WARN
61
+ end
62
+
57
63
  def call(env)
64
+ logger.debug env.inspect.cyan
65
+
66
+ request = Request.new(env)
67
+ response = Response.new
68
+
58
69
  begin
59
70
  Loader.reload!
60
71
 
61
- request = Request.new(env)
62
-
63
72
  route, params = self.class.router.route(request.method, request.path)
64
73
  request.params.merge!(params) if params and params.is_a? Hash
65
74
  request.params.merge!('splat' => params.join(',')) if params and params.is_a? Array
66
75
 
67
- return [404, {'Content-Type' => 'application/json; charset=utf-8'}, ["{\"error\":\"404 Not Found\"}", "\n"]] unless route
76
+ if route
77
+ logger.debug "#{request.method} #{request.path} => route #{route.inspect}".green
78
+ context = Context.new(env, route, request, response, self.class.before_filters, self.class.after_filters)
79
+ response = context.run
80
+ else
81
+ logger.debug "#{request.method} #{request.path} => route not found".red
82
+ response.not_found!
83
+ end
68
84
 
69
- context = Context.new(env, route, request, self.class.before_filters, self.class.after_filters)
70
- response = context.run
71
-
72
- [response.status, response.header, response.body]
73
85
  rescue Exception => e
74
- return [500, {'Content-Type' => 'application/json; charset=utf-8'}, ["{\"error\":\"500 Internal Server Error\", \"message\":\"#{e.message}\"}", "\n"]]
86
+ response.internal_server_error!(e)
75
87
  end
88
+
89
+ [response.status, response.header, response.body]
76
90
  end
77
91
 
78
92
  end
@@ -3,11 +3,11 @@ module Opi
3
3
 
4
4
  attr_reader :env, :route, :request, :response, :before, :after, :error
5
5
 
6
- def initialize(env, route, request, before, after)
6
+ def initialize(env, route, request, response, before, after)
7
7
  @env = env
8
8
  @route = route
9
9
  @request = request
10
- @response = Response.new
10
+ @response = response
11
11
  @before = before
12
12
  @after = after
13
13
  @error = nil
@@ -56,6 +56,11 @@ module Opi
56
56
  response.body = [action.to_json, "\n"]
57
57
  end
58
58
 
59
+ if self.error
60
+ response.status = error[:status]
61
+ response.body = [error[:message].to_json, "\n"]
62
+ end
63
+
59
64
  response
60
65
  end
61
66
 
@@ -31,7 +31,7 @@ module Opi
31
31
 
32
32
  def reload!
33
33
  Dir["#{@prefix}lib/*.rb"].each { |x| funkyload x }
34
- Dir["#{@prefix}actions/**/*.rb"].each { |x| funkyload x }
34
+ Dir["#{@prefix}app/**/*.rb"].each { |x| funkyload x }
35
35
  Dir["#{@prefix}*.rb"].each { |x| funkyload x unless x == 'Rakefile.rb' }
36
36
  end
37
37
 
@@ -10,7 +10,7 @@ module Opi
10
10
  end
11
11
 
12
12
  def path
13
- @env["REQUEST_PATH"]
13
+ @env["REQUEST_PATH"] || @env["PATH_INFO"]
14
14
  end
15
15
 
16
16
  def uri
@@ -9,5 +9,15 @@ module Opi
9
9
  })
10
10
  end
11
11
 
12
+ def not_found!
13
+ @status = 404
14
+ @body = ["{\"error\":\"404 Not Found\"}", "\n"]
15
+ end
16
+
17
+ def internal_server_error!(exception)
18
+ @status = 500
19
+ @body = ["{\"error\":\"500 Internal Server Error\", \"message\":\"#{exception.message}\"}", "\n"]
20
+ end
21
+
12
22
  end
13
23
  end
@@ -1,3 +1,3 @@
1
1
  module Opi
2
- VERSION = "0.3"
2
+ VERSION = "0.3.9"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: opi
3
3
  version: !ruby/object:Gem::Version
4
- version: '0.3'
4
+ version: 0.3.9
5
5
  platform: ruby
6
6
  authors:
7
7
  - Richard Taylor
@@ -14,6 +14,9 @@ dependencies:
14
14
  name: rack
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.5'
17
20
  - - ">="
18
21
  - !ruby/object:Gem::Version
19
22
  version: 1.5.2
@@ -21,6 +24,9 @@ dependencies:
21
24
  prerelease: false
22
25
  version_requirements: !ruby/object:Gem::Requirement
23
26
  requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '1.5'
24
30
  - - ">="
25
31
  - !ruby/object:Gem::Version
26
32
  version: 1.5.2
@@ -28,20 +34,23 @@ dependencies:
28
34
  name: colored
29
35
  requirement: !ruby/object:Gem::Requirement
30
36
  requirements:
31
- - - ">="
37
+ - - "~>"
32
38
  - !ruby/object:Gem::Version
33
39
  version: '1.2'
34
40
  type: :runtime
35
41
  prerelease: false
36
42
  version_requirements: !ruby/object:Gem::Requirement
37
43
  requirements:
38
- - - ">="
44
+ - - "~>"
39
45
  - !ruby/object:Gem::Version
40
46
  version: '1.2'
41
47
  - !ruby/object:Gem::Dependency
42
48
  name: json
43
49
  requirement: !ruby/object:Gem::Requirement
44
50
  requirements:
51
+ - - "~>"
52
+ - !ruby/object:Gem::Version
53
+ version: '1.8'
45
54
  - - ">="
46
55
  - !ruby/object:Gem::Version
47
56
  version: 1.8.1
@@ -49,10 +58,14 @@ dependencies:
49
58
  prerelease: false
50
59
  version_requirements: !ruby/object:Gem::Requirement
51
60
  requirements:
61
+ - - "~>"
62
+ - !ruby/object:Gem::Version
63
+ version: '1.8'
52
64
  - - ">="
53
65
  - !ruby/object:Gem::Version
54
66
  version: 1.8.1
55
- description: The very opinionated API service library.
67
+ description: A Ruby library for creating API services. The very opinionated API service
68
+ library.
56
69
  email: moomerman@gmail.com
57
70
  executables: []
58
71
  extensions: []
@@ -69,7 +82,8 @@ files:
69
82
  - lib/opi/router.rb
70
83
  - lib/opi/version.rb
71
84
  homepage: http://github.com/moomerman/opi
72
- licenses: []
85
+ licenses:
86
+ - MIT
73
87
  metadata: {}
74
88
  post_install_message:
75
89
  rdoc_options:
@@ -89,7 +103,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
89
103
  version: '0'
90
104
  requirements: []
91
105
  rubyforge_project: opi
92
- rubygems_version: 2.2.0.rc.1
106
+ rubygems_version: 2.2.2
93
107
  signing_key:
94
108
  specification_version: 4
95
109
  summary: The very opinionated API service library.