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