herbert 0.0.1 → 0.0.2
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/herbert/Ajaxify.rb +23 -12
- data/lib/herbert/AppLogger.rb +20 -6
- data/lib/herbert/ApplicationError.rb +2 -0
- data/lib/herbert/Configurator.rb +4 -0
- data/lib/herbert/Jsonify.rb +2 -2
- data/lib/herbert/Resource.rb +12 -14
- data/lib/herbert/loader.rb +14 -6
- data/lib/herbert/version.rb +1 -1
- metadata +3 -4
- data/lib/file.rb +0 -2
data/lib/herbert/Ajaxify.rb
CHANGED
@@ -1,5 +1,9 @@
|
|
1
1
|
module Herbert
|
2
|
+
|
3
|
+
# Enhances AJAJ/AJAX support
|
2
4
|
module Ajaxify
|
5
|
+
|
6
|
+
# Headers to send with each request
|
3
7
|
Headers = {
|
4
8
|
'Access-Control-Allow-Methods' => %w{POST GET PUT DELETE OPTIONS},
|
5
9
|
'Access-Control-Allow-Headers' => %w{Content-Type X-Requested-With},
|
@@ -8,6 +12,14 @@ module Herbert
|
|
8
12
|
'X-Build' => [Herbert::Utils.version]
|
9
13
|
}
|
10
14
|
|
15
|
+
# * Loads config/headers.rb
|
16
|
+
#
|
17
|
+
# * Enables header processing
|
18
|
+
#
|
19
|
+
# * Registers CORS proxy for external services
|
20
|
+
#
|
21
|
+
# @param [Sinatra::Base descendant]
|
22
|
+
#
|
11
23
|
def self.registered(app)
|
12
24
|
# Heeeaderzz!!! Gimme heaaaderzzz!!!
|
13
25
|
path = File.join(app.settings.root, 'config','headers.rb')
|
@@ -23,7 +35,7 @@ module Herbert
|
|
23
35
|
end
|
24
36
|
|
25
37
|
app.before do
|
26
|
-
|
38
|
+
# Add the headers to the response
|
27
39
|
Headers.each {|name, value|
|
28
40
|
value = [value] unless value.is_a?(Array)
|
29
41
|
value.map! {|val|
|
@@ -35,16 +47,15 @@ module Herbert
|
|
35
47
|
|
36
48
|
# Proxy for not CORS enables services such as
|
37
49
|
# Google Maps
|
38
|
-
# /proxy
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
50
|
+
# /proxy/url?=
|
51
|
+
app.get '/proxy/' do
|
52
|
+
url = URI.parse(URI.encode(params[:url]))
|
53
|
+
res = Net::HTTP.start(url.host, 80) {|http|
|
54
|
+
http.get(url.path + '?' + url.query)
|
55
|
+
}
|
56
|
+
response['content-type'] = res['content-type']
|
57
|
+
res.body
|
58
|
+
end
|
47
59
|
end
|
48
|
-
|
49
|
-
end
|
60
|
+
end
|
50
61
|
end
|
data/lib/herbert/AppLogger.rb
CHANGED
@@ -2,14 +2,22 @@ module Herbert
|
|
2
2
|
# Full-fledged request & response logger with
|
3
3
|
# several storage providers (console, mongo, cache)
|
4
4
|
class AppLogger
|
5
|
+
|
6
|
+
# Provider getter
|
5
7
|
def self.provider
|
6
8
|
@@provider
|
7
9
|
end
|
8
|
-
|
10
|
+
|
11
|
+
# Provider setter
|
9
12
|
def self.provider=(prov)
|
10
13
|
@@provider = prov
|
11
14
|
end
|
12
15
|
|
16
|
+
# Creates log message and passes it to stograge provider
|
17
|
+
#
|
18
|
+
# @param [Sinatra::Request]
|
19
|
+
# @param [Sinatra::Response]
|
20
|
+
#
|
13
21
|
def self.log(request, response)
|
14
22
|
log = {
|
15
23
|
"request"=> {
|
@@ -44,7 +52,7 @@ module Herbert
|
|
44
52
|
}
|
45
53
|
}
|
46
54
|
|
47
|
-
# Extract
|
55
|
+
# Extract the headers
|
48
56
|
request.env.keys.each do |key|
|
49
57
|
if key =~ /^HTTP_/ then
|
50
58
|
log['request']['headers'][key.gsub(/^HTTP_/,'')] = request.env[key]
|
@@ -52,7 +60,7 @@ module Herbert
|
|
52
60
|
end
|
53
61
|
# do not log bodies from GET/DELETE/HEAD
|
54
62
|
log["request"].delete("body") if %{GET DELETE HEAD}.include?(log["request"]["method"])
|
55
|
-
# If an error occured, add it
|
63
|
+
# If an error has occured, add it
|
56
64
|
log["response"]["error"] = request.env['sinatra.error'].to_hash if request.env['sinatra.error']
|
57
65
|
id = @@provider.save(log)
|
58
66
|
response['X-RequestId'] = id.to_s if @@provider.respond_to?(:id) && response.app.settings.append_log_id
|
@@ -60,6 +68,8 @@ module Herbert
|
|
60
68
|
end
|
61
69
|
|
62
70
|
module LoggingProviders
|
71
|
+
|
72
|
+
# Dumps all logs to STDOUT with pretty formating
|
63
73
|
class StdoutProvider
|
64
74
|
def initialize
|
65
75
|
require 'pp'
|
@@ -70,18 +80,22 @@ module Herbert
|
|
70
80
|
end
|
71
81
|
end
|
72
82
|
|
83
|
+
# Persists log in DB
|
73
84
|
class MongoProvider
|
74
|
-
|
75
|
-
Collection = 'logs'
|
76
|
-
|
85
|
+
Collection = 'logs'
|
77
86
|
def initialize(db)
|
78
87
|
@db = db
|
79
88
|
end
|
80
89
|
|
90
|
+
# @param [Hash]
|
81
91
|
def save(log)
|
82
92
|
@db[Collection].save(log)
|
83
93
|
end
|
84
94
|
|
95
|
+
# FIXME
|
96
|
+
# "Flag" indicating that this provider returns IDs
|
97
|
+
#
|
98
|
+
# @return [Boolean]
|
85
99
|
def id
|
86
100
|
true
|
87
101
|
end
|
data/lib/herbert/Configurator.rb
CHANGED
data/lib/herbert/Jsonify.rb
CHANGED
@@ -20,7 +20,7 @@ module Sinatra
|
|
20
20
|
class Sinatra::Request
|
21
21
|
|
22
22
|
@is_json = false
|
23
|
-
# Encapsulates
|
23
|
+
# Encapsulates Rack::Request.body in order to remove #IO.String
|
24
24
|
# and therefore to enable repeated reads
|
25
25
|
def body_raw
|
26
26
|
@body_raw ||= body(true).read
|
@@ -39,7 +39,7 @@ module Sinatra
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
# Overrides
|
42
|
+
# Overrides Rack::Request.body, returns native #Hash
|
43
43
|
# Preserves access to underlying @env['rack.input'] #IO.String
|
44
44
|
def body(rack = false)
|
45
45
|
if rack then
|
data/lib/herbert/Resource.rb
CHANGED
@@ -1,19 +1,15 @@
|
|
1
1
|
module Herbert
|
2
|
-
|
3
|
-
# Any class that subclasses Herbert::Resource is automatically "merged"
|
4
|
-
# into the application. Resource name will be derived from the class name.
|
2
|
+
# When extended, it virtually "merges" the overriding class into the app.
|
5
3
|
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
# get '/' do
|
9
|
-
# "here's a message for you!"
|
10
|
-
# end
|
4
|
+
# class Messages
|
5
|
+
# get '/'
|
11
6
|
# end
|
12
|
-
#
|
13
|
-
#
|
14
|
-
#
|
15
|
-
|
7
|
+
# end
|
8
|
+
# will enable your app to respond to
|
9
|
+
# GET /messages/
|
16
10
|
class Resource
|
11
|
+
|
12
|
+
# Instantizing this class is forbidden
|
17
13
|
def self.new
|
18
14
|
raise StandardError.new('You are not allowed to instantize this class directly')
|
19
15
|
end
|
@@ -28,15 +24,17 @@ module Herbert
|
|
28
24
|
end
|
29
25
|
end
|
30
26
|
|
31
|
-
# Loads all
|
27
|
+
# Loads all Herberts' resources
|
32
28
|
module ResourceLoader
|
29
|
+
|
30
|
+
# Loads all application resources
|
33
31
|
def self.registered(app)
|
34
32
|
# Inject refence to the app into Resource
|
35
33
|
Resource.class_eval do
|
36
34
|
define_singleton_method :app do
|
37
35
|
app
|
38
36
|
end
|
39
|
-
end
|
37
|
+
end
|
40
38
|
|
41
39
|
# And load all resource definitions
|
42
40
|
path = File.join(app.settings.root, 'Resources')
|
data/lib/herbert/loader.rb
CHANGED
@@ -4,6 +4,8 @@ require 'memcache'
|
|
4
4
|
require 'sinatra/base'
|
5
5
|
require 'kwalify'
|
6
6
|
require 'active_support'
|
7
|
+
$:.unshift(File.dirname(__FILE__))
|
8
|
+
require 'version'
|
7
9
|
|
8
10
|
module Herbert
|
9
11
|
::Logger.class_eval do
|
@@ -19,23 +21,27 @@ module Herbert
|
|
19
21
|
# Plug it in, the monkey style :]
|
20
22
|
module ::Kernel
|
21
23
|
@@logger = Logger.new(STDOUT)
|
24
|
+
|
25
|
+
# @return [Logger] The globally available logger
|
22
26
|
def log
|
23
27
|
@@logger
|
24
28
|
end
|
25
29
|
end
|
26
30
|
|
31
|
+
# Bootstraps Herbert
|
27
32
|
module Loader
|
28
33
|
$HERBERT_PATH = File.dirname(__FILE__)
|
29
|
-
log.h_info("Here comes Herbert. He's a berserker!")
|
34
|
+
log.h_info("Here comes Herbert (v#{Herbert::VERSION}). He's a berserker!")
|
30
35
|
# because order matters
|
31
36
|
%w{Utils Jsonify Configurator Error Services Ajaxify AppLogger Log Resource}.each {|file|
|
32
37
|
require $HERBERT_PATH + "/#{file}.rb"
|
33
38
|
}
|
34
|
-
|
39
|
+
|
40
|
+
# Sets up some default settings and loads all components
|
35
41
|
def self.registered(app)
|
36
42
|
# Set some default
|
37
43
|
# TODO to external file?
|
38
|
-
app.set :log_requests, :db
|
44
|
+
app.set :log_requests, :db unless app.respond_to? :log_requests
|
39
45
|
app.enable :append_log_id # If logs go to Mongo, IDs will be appended to responses
|
40
46
|
## register the ;debug flag patch first to enable proper logging
|
41
47
|
app.register Herbert::Configurator::Prepatch
|
@@ -52,12 +58,14 @@ module Herbert
|
|
52
58
|
app.helpers Sinatra::Database
|
53
59
|
app.register Sinatra::Cache
|
54
60
|
app.helpers Sinatra::Cache
|
55
|
-
app.
|
56
|
-
|
61
|
+
if app.respond_to?(:validation) && app.validation then
|
62
|
+
app.register Sinatra::Validation::Extension
|
63
|
+
app.helpers Sinatra::Validation::Helpers
|
64
|
+
end
|
57
65
|
app.register Herbert::Ajaxify
|
58
66
|
app.helpers Sinatra::Log
|
59
67
|
app.register Sinatra::Log::Extension
|
60
|
-
app.register Herbert::ResourceLoader
|
68
|
+
app.register Herbert::ResourceLoader if app.respond_to?(:resources) && app.resources
|
61
69
|
end
|
62
70
|
end
|
63
71
|
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
|
+
- 2
|
9
|
+
version: 0.0.2
|
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-24 00:00:00 +02:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|
@@ -134,7 +134,6 @@ files:
|
|
134
134
|
- Gemfile
|
135
135
|
- Rakefile
|
136
136
|
- herbert.gemspec
|
137
|
-
- lib/file.rb
|
138
137
|
- lib/herbert.rb
|
139
138
|
- lib/herbert/Ajaxify.rb
|
140
139
|
- lib/herbert/AppLogger.rb
|
data/lib/file.rb
DELETED