webfx 0.0.3 → 0.0.4
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/lib/webfx.rb +86 -87
- metadata +18 -26
- data/Manifest +0 -4
- data/Rakefile +0 -12
- data/notes +0 -11
- data/webfx.gemspec +0 -30
data/lib/webfx.rb
CHANGED
@@ -1,116 +1,115 @@
|
|
1
1
|
require 'json'
|
2
2
|
|
3
|
-
|
4
|
-
|
5
|
-
@@authenticate = false
|
3
|
+
@@routes = {'get' => [], 'post' => [], 'put' => [], 'delete' => []}
|
4
|
+
@@authenticate = false
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
6
|
+
def handler(pattern, fn)
|
7
|
+
{:regexp => Regexp.new(pattern), :fn => fn}
|
8
|
+
end
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
def get(pattern, &fn)
|
11
|
+
@@routes['get'].push(handler(pattern, fn))
|
12
|
+
end
|
14
13
|
|
15
|
-
|
16
|
-
|
17
|
-
|
14
|
+
def post(pattern, &fn)
|
15
|
+
@@routes['post'].push(handler(pattern, fn))
|
16
|
+
end
|
18
17
|
|
19
|
-
|
20
|
-
|
21
|
-
|
18
|
+
def put(pattern, &fn)
|
19
|
+
@@routes['put'].push(handler(pattern, fn))
|
20
|
+
end
|
22
21
|
|
23
|
-
|
24
|
-
|
25
|
-
|
22
|
+
def delete(pattern, &fn)
|
23
|
+
@@routes['delete'].push(handler(pattern, fn))
|
24
|
+
end
|
26
25
|
|
27
|
-
|
28
|
-
|
29
|
-
|
26
|
+
def authenticate(&fn)
|
27
|
+
@@authenticate = fn
|
28
|
+
end
|
30
29
|
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
end
|
30
|
+
class MyResult
|
31
|
+
attr_reader :env
|
32
|
+
|
33
|
+
def initialize(env)
|
34
|
+
@env = env
|
37
35
|
end
|
36
|
+
end
|
38
37
|
|
39
|
-
|
40
|
-
|
41
|
-
|
38
|
+
def run_app(env)
|
39
|
+
begin
|
40
|
+
request = Rack::Request.new(env)
|
42
41
|
|
43
|
-
|
44
|
-
|
45
|
-
|
42
|
+
def static(type, file)
|
43
|
+
[200, { 'Content-Type' => type, 'Cache-Control' => 'public, max-age=86400' }, IO.read(file)]
|
44
|
+
end
|
46
45
|
|
47
|
-
|
48
|
-
|
49
|
-
|
46
|
+
if request.path == '/'
|
47
|
+
return static('text/html', 'public/index.html')
|
48
|
+
end
|
50
49
|
|
51
|
-
|
52
|
-
|
53
|
-
|
50
|
+
if request.path.match(/(.*).js/)
|
51
|
+
return static('text/javascript', "public/#{$1}.js")
|
52
|
+
end
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
|
54
|
+
if request.path.match(/(.*).css/)
|
55
|
+
return static('text/css', "public/#{$1}.css")
|
56
|
+
end
|
58
57
|
|
59
|
-
|
60
|
-
|
61
|
-
|
58
|
+
if request.path.match(/(.*).jpg/)
|
59
|
+
return static('image/jpg', ".#{$1}.jpg")
|
60
|
+
end
|
62
61
|
|
63
|
-
|
62
|
+
params = request.params
|
64
63
|
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
end
|
64
|
+
if request.post?
|
65
|
+
if not params['file']
|
66
|
+
body = request.body.read
|
67
|
+
params.merge!(JSON.parse(body)) if body != ''
|
70
68
|
end
|
69
|
+
end
|
71
70
|
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
71
|
+
force_method = params['_method']
|
72
|
+
if force_method
|
73
|
+
method = force_method
|
74
|
+
params.delete('_method')
|
75
|
+
else
|
76
|
+
method = request.request_method
|
77
|
+
end
|
78
|
+
method.downcase!
|
80
79
|
|
81
|
-
|
82
|
-
|
83
|
-
|
80
|
+
@@routes[method].each do |item|
|
81
|
+
if request.path[1..-1].match(item[:regexp])
|
82
|
+
me = @@authenticate ? @@authenticate.call(params) : nil
|
84
83
|
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
84
|
+
case $~.length
|
85
|
+
when 1 then result = item[:fn].call(params, me)
|
86
|
+
when 2 then result = item[:fn].call($1, params, me)
|
87
|
+
when 3 then result = item[:fn].call($1, $2, params, me)
|
88
|
+
when 4 then result = item[:fn].call($1, $2, $3, params, me)
|
89
|
+
when 5 then result = item[:fn].call($1, $2, $3, $4, params, me)
|
90
|
+
else result = item[:fn].call($~, params, me)
|
91
|
+
end
|
93
92
|
|
94
|
-
|
93
|
+
return result.env if result.kind_of? MyResult
|
95
94
|
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
end
|
95
|
+
case method
|
96
|
+
when 'get', 'post'
|
97
|
+
if result.kind_of? String and ['{', '['].include? result.slice(0, 1)
|
98
|
+
json = result
|
99
|
+
else
|
100
|
+
json = result.to_json
|
101
|
+
end
|
102
|
+
return [200, {'Content-Type' => 'application/json'}, json]
|
103
|
+
else
|
104
|
+
return [200, {'Content-Type' => 'application/json'}, {:result => :success}.to_json]
|
107
105
|
end
|
108
106
|
end
|
109
|
-
[404, {'Content-Type' => 'text/plain'}, 'Not found.']
|
110
|
-
|
111
|
-
rescue => e
|
112
|
-
result = {:message => e.message, :callstack => e.backtrace.join("\n")}
|
113
|
-
[500, {'Content-Type' => 'text/plain' }, e.message]
|
114
107
|
end
|
108
|
+
[404, {'Content-Type' => 'text/plain'}, 'Not found.']
|
109
|
+
|
110
|
+
rescue => e
|
111
|
+
result = {:message => e.message, :callstack => e.backtrace.join("\n")}
|
112
|
+
[500, {'Content-Type' => 'text/plain' }, e.message]
|
115
113
|
end
|
116
114
|
end
|
115
|
+
|
metadata
CHANGED
@@ -1,48 +1,41 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: webfx
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 23
|
5
5
|
prerelease: false
|
6
6
|
segments:
|
7
7
|
- 0
|
8
8
|
- 0
|
9
|
-
-
|
10
|
-
version: 0.0.
|
9
|
+
- 4
|
10
|
+
version: 0.0.4
|
11
11
|
platform: ruby
|
12
|
-
authors:
|
13
|
-
|
12
|
+
authors: []
|
13
|
+
|
14
14
|
autorequire:
|
15
15
|
bindir: bin
|
16
16
|
cert_chain: []
|
17
17
|
|
18
|
-
date: 2010-12-
|
18
|
+
date: 2010-12-30 00:00:00 -08:00
|
19
19
|
default_executable:
|
20
20
|
dependencies: []
|
21
21
|
|
22
|
-
description:
|
23
|
-
email:
|
22
|
+
description:
|
23
|
+
email:
|
24
24
|
executables: []
|
25
25
|
|
26
26
|
extensions: []
|
27
27
|
|
28
|
-
extra_rdoc_files:
|
29
|
-
|
28
|
+
extra_rdoc_files: []
|
29
|
+
|
30
30
|
files:
|
31
|
-
- Rakefile
|
32
31
|
- lib/webfx.rb
|
33
|
-
- notes
|
34
|
-
- Manifest
|
35
|
-
- webfx.gemspec
|
36
32
|
has_rdoc: true
|
37
|
-
homepage:
|
33
|
+
homepage:
|
38
34
|
licenses: []
|
39
35
|
|
40
36
|
post_install_message:
|
41
|
-
rdoc_options:
|
42
|
-
|
43
|
-
- --inline-source
|
44
|
-
- --title
|
45
|
-
- Webfx
|
37
|
+
rdoc_options: []
|
38
|
+
|
46
39
|
require_paths:
|
47
40
|
- lib
|
48
41
|
required_ruby_version: !ruby/object:Gem::Requirement
|
@@ -59,17 +52,16 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
59
52
|
requirements:
|
60
53
|
- - ">="
|
61
54
|
- !ruby/object:Gem::Version
|
62
|
-
hash:
|
55
|
+
hash: 3
|
63
56
|
segments:
|
64
|
-
-
|
65
|
-
|
66
|
-
version: "1.2"
|
57
|
+
- 0
|
58
|
+
version: "0"
|
67
59
|
requirements: []
|
68
60
|
|
69
|
-
rubyforge_project:
|
61
|
+
rubyforge_project:
|
70
62
|
rubygems_version: 1.3.7
|
71
63
|
signing_key:
|
72
64
|
specification_version: 3
|
73
|
-
summary:
|
65
|
+
summary: a gem
|
74
66
|
test_files: []
|
75
67
|
|
data/Manifest
DELETED
data/Rakefile
DELETED
@@ -1,12 +0,0 @@
|
|
1
|
-
require 'rubygems'
|
2
|
-
require 'rake'
|
3
|
-
require 'echoe'
|
4
|
-
|
5
|
-
Echoe.new('webfx', '0.0.3') do |p|
|
6
|
-
p.description = "A gem for building simple RESTful web sites"
|
7
|
-
p.url = "http://github.com/gregclinton/webfx"
|
8
|
-
p.author = "Greg Clinton"
|
9
|
-
p.email = "greg.clinton@gmail.com"
|
10
|
-
p.ignore_pattern = []
|
11
|
-
p.development_dependencies = []
|
12
|
-
end
|
data/notes
DELETED
data/webfx.gemspec
DELETED
@@ -1,30 +0,0 @@
|
|
1
|
-
# -*- encoding: utf-8 -*-
|
2
|
-
|
3
|
-
Gem::Specification.new do |s|
|
4
|
-
s.name = %q{webfx}
|
5
|
-
s.version = "0.0.3"
|
6
|
-
|
7
|
-
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
-
s.authors = ["Greg Clinton"]
|
9
|
-
s.date = %q{2010-12-29}
|
10
|
-
s.description = %q{A gem for building simple RESTful web sites}
|
11
|
-
s.email = %q{greg.clinton@gmail.com}
|
12
|
-
s.extra_rdoc_files = ["lib/webfx.rb"]
|
13
|
-
s.files = ["Rakefile", "lib/webfx.rb", "notes", "Manifest", "webfx.gemspec"]
|
14
|
-
s.homepage = %q{http://github.com/gregclinton/webfx}
|
15
|
-
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Webfx"]
|
16
|
-
s.require_paths = ["lib"]
|
17
|
-
s.rubyforge_project = %q{webfx}
|
18
|
-
s.rubygems_version = %q{1.3.7}
|
19
|
-
s.summary = %q{A gem for building simple RESTful web sites}
|
20
|
-
|
21
|
-
if s.respond_to? :specification_version then
|
22
|
-
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
23
|
-
s.specification_version = 3
|
24
|
-
|
25
|
-
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
26
|
-
else
|
27
|
-
end
|
28
|
-
else
|
29
|
-
end
|
30
|
-
end
|