cgi-spa 0.1.0
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 +5 -0
- data/README +23 -0
- data/Rakefile +21 -0
- data/cgi-spa.gemspec +39 -0
- data/lib/cgi-spa.rb +160 -0
- data/lib/version.rb +9 -0
- metadata +86 -0
data/Manifest
ADDED
data/README
ADDED
@@ -0,0 +1,23 @@
|
|
1
|
+
Provides the following globals:
|
2
|
+
$cgi - Common Gateway Interface
|
3
|
+
$params - Access to parameters
|
4
|
+
$x - XmlBuilder instance
|
5
|
+
|
6
|
+
$USER - user
|
7
|
+
$HOME - user's home directory
|
8
|
+
$HOST - server host
|
9
|
+
|
10
|
+
$HTTP_GET - request is an HTTP GET
|
11
|
+
$HTTP_POST - request is an HTTP POST
|
12
|
+
$XHR_JSON - request is XmlHttpRequest for JSON
|
13
|
+
|
14
|
+
Helper methods:
|
15
|
+
submit: runs command (or block) as a deamon process
|
16
|
+
|
17
|
+
MonkeyPatches to Builder:
|
18
|
+
indented_text: matches text indentation to markup
|
19
|
+
indented_data: useful for script and styles in HTML syntax
|
20
|
+
traceback! : formats an exception traceback
|
21
|
+
|
22
|
+
method_missing: patched to ensure open tags are closed
|
23
|
+
|
data/Rakefile
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'rake'
|
3
|
+
require 'echoe'
|
4
|
+
|
5
|
+
require File.expand_path(File.dirname(__FILE__) + "/lib/version")
|
6
|
+
|
7
|
+
Echoe.new('cgi-spa', CgiSpa::VERSION::STRING) do |p|
|
8
|
+
p.summary = "CGI Single Page Applications"
|
9
|
+
p.description = <<-EOF
|
10
|
+
Provides a number of globals, helper methods, and monkey patches which
|
11
|
+
simplify the development of single page applications in the form of
|
12
|
+
CGI scripts.
|
13
|
+
EOF
|
14
|
+
p.url = "http://github.com/rubys/cgi-spa"
|
15
|
+
p.author = "Sam Ruby"
|
16
|
+
p.email = "rubys@intertwingly.net"
|
17
|
+
p.dependencies = %w(
|
18
|
+
builder
|
19
|
+
json
|
20
|
+
)
|
21
|
+
end
|
data/cgi-spa.gemspec
ADDED
@@ -0,0 +1,39 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
|
3
|
+
Gem::Specification.new do |s|
|
4
|
+
s.name = %q{cgi-spa}
|
5
|
+
s.version = "0.1.0"
|
6
|
+
|
7
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 1.2") if s.respond_to? :required_rubygems_version=
|
8
|
+
s.authors = ["Sam Ruby"]
|
9
|
+
s.date = %q{2009-11-27}
|
10
|
+
s.description = %q{ Provides a number of globals, helper methods, and monkey patches which
|
11
|
+
simplify the development of single page applications in the form of
|
12
|
+
CGI scripts.
|
13
|
+
}
|
14
|
+
s.email = %q{rubys@intertwingly.net}
|
15
|
+
s.extra_rdoc_files = ["README", "lib/cgi-spa.rb", "lib/version.rb"]
|
16
|
+
s.files = ["Manifest", "README", "Rakefile", "lib/cgi-spa.rb", "lib/version.rb", "cgi-spa.gemspec"]
|
17
|
+
s.homepage = %q{http://github.com/rubys/cgi-spa}
|
18
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Cgi-spa", "--main", "README"]
|
19
|
+
s.require_paths = ["lib"]
|
20
|
+
s.rubyforge_project = %q{cgi-spa}
|
21
|
+
s.rubygems_version = %q{1.3.5}
|
22
|
+
s.summary = %q{CGI Single Page Applications}
|
23
|
+
|
24
|
+
if s.respond_to? :specification_version then
|
25
|
+
current_version = Gem::Specification::CURRENT_SPECIFICATION_VERSION
|
26
|
+
s.specification_version = 3
|
27
|
+
|
28
|
+
if Gem::Version.new(Gem::RubyGemsVersion) >= Gem::Version.new('1.2.0') then
|
29
|
+
s.add_runtime_dependency(%q<builder>, [">= 0"])
|
30
|
+
s.add_runtime_dependency(%q<json>, [">= 0"])
|
31
|
+
else
|
32
|
+
s.add_dependency(%q<builder>, [">= 0"])
|
33
|
+
s.add_dependency(%q<json>, [">= 0"])
|
34
|
+
end
|
35
|
+
else
|
36
|
+
s.add_dependency(%q<builder>, [">= 0"])
|
37
|
+
s.add_dependency(%q<json>, [">= 0"])
|
38
|
+
end
|
39
|
+
end
|
data/lib/cgi-spa.rb
ADDED
@@ -0,0 +1,160 @@
|
|
1
|
+
#!/usr/bin/ruby
|
2
|
+
require 'cgi'
|
3
|
+
require 'rubygems'
|
4
|
+
require 'builder'
|
5
|
+
require 'json'
|
6
|
+
|
7
|
+
# standard objects
|
8
|
+
$cgi = CGI.new
|
9
|
+
$param = $cgi.params
|
10
|
+
$x = Builder::XmlMarkup.new :indent => 2
|
11
|
+
|
12
|
+
# fast path for accessing CGI parameters
|
13
|
+
def $param.method_missing(name)
|
14
|
+
self[name.to_s].join
|
15
|
+
end
|
16
|
+
|
17
|
+
# environment objects
|
18
|
+
$USER = ENV['USER'] ||
|
19
|
+
if RUBY_PLATFORM =~ /darwin/
|
20
|
+
`dscl . -search /Users UniqueID #{Process.uid}`.split.first
|
21
|
+
else
|
22
|
+
`getent passwd #{Process.uid}`.split(':').first
|
23
|
+
end
|
24
|
+
|
25
|
+
$HOME = ENV['HOME'] || File.expand_path('~' + $USER)
|
26
|
+
$SERVER = ENV['HTTP_HOST'] || `hostname`.chomp
|
27
|
+
|
28
|
+
# request types
|
29
|
+
$HTTP_GET = ($cgi.request_method == 'GET')
|
30
|
+
$HTTP_POST = ($cgi.request_method == 'POST')
|
31
|
+
$XHR_JSON = (ENV['HTTP_ACCEPT'] =~ /json/)
|
32
|
+
|
33
|
+
# run command/block as a background daemon
|
34
|
+
def submit(cmd=nil)
|
35
|
+
fork do
|
36
|
+
# detach from tty
|
37
|
+
Process.setsid
|
38
|
+
fork and exit
|
39
|
+
|
40
|
+
# clear working directory and mask
|
41
|
+
Dir.chdir '/'
|
42
|
+
File.umask 0000
|
43
|
+
|
44
|
+
# close open files
|
45
|
+
STDIN.reopen '/dev/null'
|
46
|
+
STDOUT.reopen '/dev/null', 'a'
|
47
|
+
STDERR.reopen STDOUT
|
48
|
+
|
49
|
+
# setup environment
|
50
|
+
ENV['USER'] ||= $USER
|
51
|
+
ENV['HOME'] ||= $HOME
|
52
|
+
|
53
|
+
# run cmd and/or block
|
54
|
+
system cmd if cmd
|
55
|
+
yield if block_given?
|
56
|
+
end
|
57
|
+
end
|
58
|
+
|
59
|
+
# add indented_text!, indented_data! and traceback! methods to builder
|
60
|
+
module Builder
|
61
|
+
class XmlMarkup
|
62
|
+
unless method_defined? :indented_text!
|
63
|
+
def indented_text!(text)
|
64
|
+
indented_data!(text) {|data| text! data}
|
65
|
+
end
|
66
|
+
end
|
67
|
+
|
68
|
+
unless method_defined? :indented_data!
|
69
|
+
def indented_data!(data)
|
70
|
+
return if data.strip.length == 0
|
71
|
+
lines = data.gsub(/\n\s*\n/,"\n")
|
72
|
+
unindent = lines.scan(/^ */).map {|str| str.length}.min
|
73
|
+
|
74
|
+
before = Regexp.new('^'.ljust(unindent+1))
|
75
|
+
after = " " * (@level * @indent)
|
76
|
+
data = data.gsub(before, after)
|
77
|
+
|
78
|
+
if block_given?
|
79
|
+
yield data
|
80
|
+
else
|
81
|
+
self << data
|
82
|
+
end
|
83
|
+
|
84
|
+
_newline unless data =~ /\s$/
|
85
|
+
end
|
86
|
+
end
|
87
|
+
|
88
|
+
unless method_defined? :traceback!
|
89
|
+
def traceback!(exception=$!, klass='traceback')
|
90
|
+
pre :class=>klass do
|
91
|
+
text! exception.inspect
|
92
|
+
_newline
|
93
|
+
exception.backtrace.each {|frame| text!((' '*@indent)+frame + "\n")}
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
99
|
+
|
100
|
+
# monkey patch to ensure that tags are closed
|
101
|
+
test =
|
102
|
+
Builder::XmlMarkup.new.html do |x|
|
103
|
+
x.body do
|
104
|
+
begin
|
105
|
+
x.p do
|
106
|
+
raise Exception.new('boom')
|
107
|
+
end
|
108
|
+
rescue Exception => e
|
109
|
+
x.pre e
|
110
|
+
end
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
if test.index('<p>') and !test.index('</p>')
|
115
|
+
module Builder
|
116
|
+
class XmlMarkup
|
117
|
+
def method_missing(sym, *args, &block)
|
118
|
+
text = nil
|
119
|
+
attrs = nil
|
120
|
+
sym = "#{sym}:#{args.shift}" if args.first.kind_of?(Symbol)
|
121
|
+
args.each do |arg|
|
122
|
+
case arg
|
123
|
+
when Hash
|
124
|
+
attrs ||= {}
|
125
|
+
attrs.merge!(arg)
|
126
|
+
else
|
127
|
+
text ||= ''
|
128
|
+
text << arg.to_s
|
129
|
+
end
|
130
|
+
end
|
131
|
+
if block
|
132
|
+
unless text.nil?
|
133
|
+
raise ArgumentError, "XmlMarkup cannot mix a text argument with a block"
|
134
|
+
end
|
135
|
+
_indent
|
136
|
+
_start_tag(sym, attrs)
|
137
|
+
_newline
|
138
|
+
begin ### Added
|
139
|
+
_nested_structures(block)
|
140
|
+
ensure ### Added
|
141
|
+
_indent
|
142
|
+
_end_tag(sym)
|
143
|
+
_newline
|
144
|
+
end ### Added
|
145
|
+
elsif text.nil?
|
146
|
+
_indent
|
147
|
+
_start_tag(sym, attrs, true)
|
148
|
+
_newline
|
149
|
+
else
|
150
|
+
_indent
|
151
|
+
_start_tag(sym, attrs)
|
152
|
+
text! text
|
153
|
+
_end_tag(sym)
|
154
|
+
_newline
|
155
|
+
end
|
156
|
+
@target
|
157
|
+
end
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
data/lib/version.rb
ADDED
metadata
ADDED
@@ -0,0 +1,86 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: cgi-spa
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sam Ruby
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-11-27 00:00:00 -05:00
|
13
|
+
default_executable:
|
14
|
+
dependencies:
|
15
|
+
- !ruby/object:Gem::Dependency
|
16
|
+
name: builder
|
17
|
+
type: :runtime
|
18
|
+
version_requirement:
|
19
|
+
version_requirements: !ruby/object:Gem::Requirement
|
20
|
+
requirements:
|
21
|
+
- - ">="
|
22
|
+
- !ruby/object:Gem::Version
|
23
|
+
version: "0"
|
24
|
+
version:
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: json
|
27
|
+
type: :runtime
|
28
|
+
version_requirement:
|
29
|
+
version_requirements: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: "0"
|
34
|
+
version:
|
35
|
+
description: " Provides a number of globals, helper methods, and monkey patches which\n simplify the development of single page applications in the form of\n CGI scripts.\n"
|
36
|
+
email: rubys@intertwingly.net
|
37
|
+
executables: []
|
38
|
+
|
39
|
+
extensions: []
|
40
|
+
|
41
|
+
extra_rdoc_files:
|
42
|
+
- README
|
43
|
+
- lib/cgi-spa.rb
|
44
|
+
- lib/version.rb
|
45
|
+
files:
|
46
|
+
- Manifest
|
47
|
+
- README
|
48
|
+
- Rakefile
|
49
|
+
- lib/cgi-spa.rb
|
50
|
+
- lib/version.rb
|
51
|
+
- cgi-spa.gemspec
|
52
|
+
has_rdoc: true
|
53
|
+
homepage: http://github.com/rubys/cgi-spa
|
54
|
+
licenses: []
|
55
|
+
|
56
|
+
post_install_message:
|
57
|
+
rdoc_options:
|
58
|
+
- --line-numbers
|
59
|
+
- --inline-source
|
60
|
+
- --title
|
61
|
+
- Cgi-spa
|
62
|
+
- --main
|
63
|
+
- README
|
64
|
+
require_paths:
|
65
|
+
- lib
|
66
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
67
|
+
requirements:
|
68
|
+
- - ">="
|
69
|
+
- !ruby/object:Gem::Version
|
70
|
+
version: "0"
|
71
|
+
version:
|
72
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
73
|
+
requirements:
|
74
|
+
- - ">="
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: "1.2"
|
77
|
+
version:
|
78
|
+
requirements: []
|
79
|
+
|
80
|
+
rubyforge_project: cgi-spa
|
81
|
+
rubygems_version: 1.3.5
|
82
|
+
signing_key:
|
83
|
+
specification_version: 3
|
84
|
+
summary: CGI Single Page Applications
|
85
|
+
test_files: []
|
86
|
+
|