herbert 0.0.2 → 0.0.3
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.
- data/herbert.gemspec +4 -4
- data/lib/herbert.rb +29 -1
- data/lib/herbert/Jsonify.rb +2 -3
- data/lib/herbert/Log.rb +7 -8
- data/lib/herbert/Resource.rb +1 -1
- data/lib/herbert/Services.rb +1 -1
- data/lib/herbert/Utils.rb +10 -0
- data/lib/herbert/loader.rb +2 -4
- data/lib/herbert/version.rb +1 -1
- metadata +5 -5
data/herbert.gemspec
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
#
|
1
|
+
# encoding: utf-8
|
2
2
|
$:.push File.expand_path("../lib", __FILE__)
|
3
3
|
require "herbert/version"
|
4
4
|
|
@@ -8,14 +8,14 @@ Gem::Specification.new do |s|
|
|
8
8
|
s.platform = Gem::Platform::RUBY
|
9
9
|
s.authors = ["Pavel Kalvoda"]
|
10
10
|
s.email = ["me@pavelkalvoda.com","pavel@drinkwithabraham.com"]
|
11
|
-
|
11
|
+
s.homepage = "https://github.com/PJK/Herbert"
|
12
12
|
s.summary = %q{Sinatra-based toolset for creating JSON API servers backed by Mongo & Memcached}
|
13
13
|
s.description = <<-desc
|
14
14
|
Herbert makes development of JSON REST API servers ridiculously simple.
|
15
|
-
It provides a
|
15
|
+
It provides a set of useful helpers and conventions to speed up development.
|
16
16
|
Input validation, logs and advanced AJAX support are baked in.
|
17
17
|
Herbert is very lightweight and transparent, making it easy to use & modify.
|
18
|
-
desc
|
18
|
+
desc
|
19
19
|
|
20
20
|
s.add_dependency("sinatra","= 1.2.6")
|
21
21
|
s.add_dependency("memcache-client")
|
data/lib/herbert.rb
CHANGED
@@ -1,3 +1,31 @@
|
|
1
|
-
require '
|
1
|
+
require 'sinatra/base'
|
2
|
+
require 'active_support'
|
2
3
|
|
4
|
+
#
|
5
|
+
# By default, if there was an error in Herbert, Sinatra would crash without
|
6
|
+
# catching the error and Rack would repond with empty 200 response afterwards.
|
7
|
+
# This emulates somewhat consistent behaviour and encapsulation.
|
8
|
+
#
|
9
|
+
class Sinatra::Base
|
10
|
+
def call(env)
|
11
|
+
begin
|
12
|
+
call!(env)
|
13
|
+
rescue => e
|
14
|
+
res = [500,{},[]]
|
15
|
+
if (ENV['HERBERT_DEBUG'].to_i==1) || (ENV['RACK_ENV'] =~ /debug/) then
|
16
|
+
res[1] = {"Content-Type" => "application/json;charset=utf-8"}
|
17
|
+
res[2] = ActiveSupport::JSON.encode({
|
18
|
+
:error => {
|
19
|
+
:code => 1,
|
20
|
+
:message => e.to_s,
|
21
|
+
:backtrace => e.backtrace
|
22
|
+
}
|
23
|
+
|
24
|
+
})
|
25
|
+
end
|
26
|
+
res
|
27
|
+
end
|
28
|
+
end
|
29
|
+
end
|
3
30
|
|
31
|
+
require 'herbert/loader'
|
data/lib/herbert/Jsonify.rb
CHANGED
@@ -1,4 +1,3 @@
|
|
1
|
-
|
2
1
|
class True
|
3
2
|
def to_json
|
4
3
|
return 1
|
@@ -43,10 +42,10 @@ module Sinatra
|
|
43
42
|
# Preserves access to underlying @env['rack.input'] #IO.String
|
44
43
|
def body(rack = false)
|
45
44
|
if rack then
|
46
|
-
|
45
|
+
super()
|
47
46
|
else
|
48
47
|
ensure_encoded
|
49
|
-
|
48
|
+
@body_decoded
|
50
49
|
end
|
51
50
|
end
|
52
51
|
|
data/lib/herbert/Log.rb
CHANGED
@@ -16,15 +16,14 @@ module Sinatra
|
|
16
16
|
when :stdout
|
17
17
|
provider = Herbert::LoggingProviders::StdoutProvider.new
|
18
18
|
else
|
19
|
-
|
20
|
-
|
19
|
+
provider = app.log_requests
|
20
|
+
end
|
21
21
|
Herbert::AppLogger.provider = provider
|
22
22
|
# Make the app automatically inject refernce to iteself into the response,
|
23
23
|
# so Sinatra::Response::finish can manipulate it
|
24
|
-
app.before { response.app = self; @timer_start = Time.new }
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
end
|
24
|
+
app.before { response.app = self; @timer_start = Time.new; }
|
25
|
+
app.after { @timer_stop = Time.new}
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
30
29
|
end
|
data/lib/herbert/Resource.rb
CHANGED
@@ -14,7 +14,7 @@ module Herbert
|
|
14
14
|
raise StandardError.new('You are not allowed to instantize this class directly')
|
15
15
|
end
|
16
16
|
|
17
|
-
# Translates
|
17
|
+
# Translates Sinatra DSL calls
|
18
18
|
def self.inherited(subclass)
|
19
19
|
%w{get post put delete}.each do |verb|
|
20
20
|
subclass.define_singleton_method verb.to_sym do |route, &block|
|
data/lib/herbert/Services.rb
CHANGED
@@ -59,7 +59,7 @@ module Sinatra
|
|
59
59
|
Dir.new(resource_dir).each do |verb|
|
60
60
|
next if %w{.. .}.include? verb
|
61
61
|
# And create the <schema_root>::<resource>::<verb_schema> constant
|
62
|
-
validation_module.const_get(resource_name).const_set(/(\w+)
|
62
|
+
validation_module.const_get(resource_name).const_set(/(\w+)(\.yaml|\.yml)/.match(verb)[1].capitalize, YAML.load_file(File.join(resource_dir, verb)))
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
data/lib/herbert/Utils.rb
CHANGED
data/lib/herbert/loader.rb
CHANGED
@@ -1,9 +1,7 @@
|
|
1
1
|
require 'logger'
|
2
2
|
require 'mongo'
|
3
3
|
require 'memcache'
|
4
|
-
require 'sinatra/base'
|
5
4
|
require 'kwalify'
|
6
|
-
require 'active_support'
|
7
5
|
$:.unshift(File.dirname(__FILE__))
|
8
6
|
require 'version'
|
9
7
|
|
@@ -34,9 +32,8 @@ module Herbert
|
|
34
32
|
log.h_info("Here comes Herbert (v#{Herbert::VERSION}). He's a berserker!")
|
35
33
|
# because order matters
|
36
34
|
%w{Utils Jsonify Configurator Error Services Ajaxify AppLogger Log Resource}.each {|file|
|
37
|
-
|
35
|
+
require file
|
38
36
|
}
|
39
|
-
|
40
37
|
# Sets up some default settings and loads all components
|
41
38
|
def self.registered(app)
|
42
39
|
# Set some default
|
@@ -66,6 +63,7 @@ module Herbert
|
|
66
63
|
app.helpers Sinatra::Log
|
67
64
|
app.register Sinatra::Log::Extension
|
68
65
|
app.register Herbert::ResourceLoader if app.respond_to?(:resources) && app.resources
|
66
|
+
app.register Herbert::Utils::Helpers
|
69
67
|
end
|
70
68
|
end
|
71
69
|
end
|
data/lib/herbert/version.rb
CHANGED
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 0
|
8
|
-
-
|
9
|
-
version: 0.0.
|
8
|
+
- 3
|
9
|
+
version: 0.0.3
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Pavel Kalvoda
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2011-06-
|
17
|
+
date: 2011-06-25 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -116,7 +116,7 @@ dependencies:
|
|
116
116
|
version_requirements: *id007
|
117
117
|
description: |
|
118
118
|
Herbert makes development of JSON REST API servers ridiculously simple.
|
119
|
-
It provides a
|
119
|
+
It provides a set of useful helpers and conventions to speed up development.
|
120
120
|
Input validation, logs and advanced AJAX support are baked in.
|
121
121
|
Herbert is very lightweight and transparent, making it easy to use & modify.
|
122
122
|
|
@@ -148,7 +148,7 @@ files:
|
|
148
148
|
- lib/herbert/loader.rb
|
149
149
|
- lib/herbert/version.rb
|
150
150
|
has_rdoc: true
|
151
|
-
homepage:
|
151
|
+
homepage: https://github.com/PJK/Herbert
|
152
152
|
licenses: []
|
153
153
|
|
154
154
|
post_install_message:
|