rack-rscript 0.3.1 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (3) hide show
  1. data/lib/rack-rscript.rb +86 -0
  2. metadata +33 -11
  3. data/lib/rack_rscript.rb +0 -28
@@ -0,0 +1,86 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ # file: rack-rscript.rb
4
+
5
+
6
+ require 'rscript'
7
+ require 'app-routes'
8
+
9
+ class RackRscript
10
+ include AppRoutes
11
+
12
+ def initialize(raw_opts={})
13
+
14
+ opts = {logfile: '', logrotate: 'daily', pkg_src: ''}.merge(raw_opts)
15
+ @url_base = opts[:pkg_src] # web server serving the RSF files
16
+ @url_base += '/' unless @url_base[-1] == '/'
17
+ @log = false
18
+
19
+ if opts[:logfile].length > 0 then
20
+ @log = true
21
+ @logger = Logger.new(opts[:logfile], opts[:logrotate])
22
+ end
23
+
24
+ super() # required for app-routes initialize method to exectue
25
+ default_routes(@params)
26
+ end
27
+
28
+ def call(env)
29
+ request = env['REQUEST_URI'][/https?:\/\/[^\/]+(.*)/,1]
30
+
31
+ log Time.now.to_s + ": " + request
32
+ content, content_type, status_code = run_route(request)
33
+ content, status_code = "404: page not found", 404 if content.nil?
34
+
35
+ content_type ||= 'text/html'
36
+ status_code ||= 200
37
+ [status_code, {"Content-Type" => content_type}, [content]]
38
+ end
39
+
40
+ private
41
+
42
+ def default_routes(params)
43
+
44
+ get '/do/:package/:job' do |package,job|
45
+ run_job("%s%s.rsf" % [@url_base, package], "//job:" + job, params)
46
+ end
47
+
48
+ get '/do/:package/:job/*' do |package, job|
49
+ raw_args = params[:splat]
50
+ args = raw_args.join.split('/')[1..-1]
51
+ run_job("%s%s.rsf" % [@url_base, package], "//job:" + job, params, args)
52
+ end
53
+ end
54
+
55
+ def run_job(url, jobs, params={}, *qargs)
56
+ if params[:splat] and params[:splat].length > 0 then
57
+ h = params[:splat].first[1..-1].split('&').inject({}) do |r,x|
58
+ k, v = x.split('=')
59
+ r.merge(k.to_sym => v)
60
+ end
61
+ params.merge! h
62
+ end
63
+
64
+ result, args = RScript.new.read([url, jobs.split(/\s/), \
65
+ qargs].flatten)
66
+ rws = self
67
+
68
+ begin
69
+ r = eval result
70
+ @params = {}
71
+ return r
72
+
73
+ rescue Exception => e
74
+
75
+ err_label = e.message.to_s + " :: \n" + e.backtrace.join("\n")
76
+ log(err_label)
77
+ ($!).to_s
78
+ end
79
+ end
80
+
81
+ def log(msg)
82
+ if @log == true then
83
+ @logger.debug msg
84
+ end
85
+ end
86
+ end
metadata CHANGED
@@ -1,18 +1,40 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rack-rscript
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ prerelease:
5
+ version: 0.4.0
5
6
  platform: ruby
6
- authors: []
7
-
7
+ authors:
8
+ - James Robertson
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
12
 
12
- date: 2010-01-15 00:00:00 +00:00
13
+ date: 2012-02-26 00:00:00 +00:00
13
14
  default_executable:
14
- dependencies: []
15
-
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: app-routes
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: rscript
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :runtime
37
+ version_requirements: *id002
16
38
  description:
17
39
  email:
18
40
  executables: []
@@ -22,7 +44,7 @@ extensions: []
22
44
  extra_rdoc_files: []
23
45
 
24
46
  files:
25
- - lib/rack_rscript.rb
47
+ - lib/rack-rscript.rb
26
48
  has_rdoc: true
27
49
  homepage:
28
50
  licenses: []
@@ -33,23 +55,23 @@ rdoc_options: []
33
55
  require_paths:
34
56
  - lib
35
57
  required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
36
59
  requirements:
37
60
  - - ">="
38
61
  - !ruby/object:Gem::Version
39
62
  version: "0"
40
- version:
41
63
  required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
42
65
  requirements:
43
66
  - - ">="
44
67
  - !ruby/object:Gem::Version
45
68
  version: "0"
46
- version:
47
69
  requirements: []
48
70
 
49
71
  rubyforge_project:
50
- rubygems_version: 1.3.5
72
+ rubygems_version: 1.5.2
51
73
  signing_key:
52
74
  specification_version: 3
53
- summary: rack-rscript
75
+ summary: Rack-Rscript is a light-weight alternative to Sinatra-Rscript.
54
76
  test_files: []
55
77
 
data/lib/rack_rscript.rb DELETED
@@ -1,28 +0,0 @@
1
- require 'rcscript'
2
-
3
- class RackRscript
4
-
5
- def initialize(url='http://rorbuilder.info/r/heroku-rack/')
6
- @@version = '0.3.1'
7
- @url_base = url
8
- end
9
-
10
- def run_rcscript(rsf_url, jobs, arg='')
11
- args = [jobs.map {|x| "//job:" + x}.join("\n"), rsf_url]
12
- rs = RScript.new()
13
- rs.run(args)
14
- end
15
-
16
- def call(env)
17
- req = Rack::Request.new(env)
18
- package_id, *jobs = req.params['package'], req.params['job']
19
- *args = req.params['arg']
20
-
21
- url = "%s%s.rsf" % [@url_base, package_id]
22
- @content_type = "text/html"
23
-
24
- code = run_rcscript(url, jobs, args).first
25
- content = eval(code)
26
- [200, {"Content-Type" => @content_type}, [content]]
27
- end
28
- end