rcscript-client 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/rcscript-client.rb +82 -0
- metadata +55 -0
@@ -0,0 +1,82 @@
|
|
1
|
+
require 'open-uri'
|
2
|
+
require 'rexml/document'
|
3
|
+
|
4
|
+
class RScriptClient
|
5
|
+
include REXML
|
6
|
+
|
7
|
+
attr_reader :doc, :result, :text
|
8
|
+
attr_accessor :package
|
9
|
+
|
10
|
+
def initialize(opts={})
|
11
|
+
|
12
|
+
o = {:hostname => 'rscript.heroku.com', :package => ''}.merge(opts)
|
13
|
+
@hostname = o[:hostname]
|
14
|
+
@package = o[:package]
|
15
|
+
|
16
|
+
if @package.length > 0 then
|
17
|
+
jobs_to_methods(@package)
|
18
|
+
init_content_types
|
19
|
+
end
|
20
|
+
|
21
|
+
end
|
22
|
+
|
23
|
+
def package=(s)
|
24
|
+
if s then
|
25
|
+
@package = s
|
26
|
+
jobs_to_methods(@package)
|
27
|
+
init_content_types
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
private
|
32
|
+
|
33
|
+
def jobs_to_methods(package)
|
34
|
+
url = "http://rorbuilder.info/r/heroku/%s.rsf" % package
|
35
|
+
puts 'url : ' + url
|
36
|
+
doc = Document.new(open(url, 'UserAgent' => 'ClientRscript').read)
|
37
|
+
a = XPath.match(doc.root, 'job/attribute::id')
|
38
|
+
a.each do |attr|
|
39
|
+
method_name = attr.value.to_s.gsub('-','_')
|
40
|
+
method = "def %s(param={}); query_method('%s', param); end" % [method_name, method_name]
|
41
|
+
self.instance_eval(method)
|
42
|
+
end
|
43
|
+
end
|
44
|
+
|
45
|
+
def init_content_types
|
46
|
+
|
47
|
+
@return_type = {}
|
48
|
+
|
49
|
+
xmlproc = Proc.new {
|
50
|
+
@doc = Document.new(@result.sub(/xmlns=["']http:\/\/www.w3.org\/1999\/xhtml["']/,''))
|
51
|
+
summary_node = XPath.match(@doc.root, 'summary/*')
|
52
|
+
if summary_node then
|
53
|
+
summary_node.each do |node|
|
54
|
+
method = "def %s(); '%s'; end" % [node.name, node.text.to_s]
|
55
|
+
self.instance_eval(method)
|
56
|
+
end
|
57
|
+
records = XPath.match(@doc.root, 'records/*/text()')
|
58
|
+
method = "def %s(); %s; end" % [@doc.root.name, records.inspect] if records
|
59
|
+
self.instance_eval(method)
|
60
|
+
end
|
61
|
+
}
|
62
|
+
|
63
|
+
textproc = Proc.new {@text = @result}
|
64
|
+
@return_type['text/plain'] = textproc
|
65
|
+
@return_type['text/html'] = textproc
|
66
|
+
@return_type['text/xml'] = xmlproc
|
67
|
+
@return_type['application/xml'] = xmlproc
|
68
|
+
@return_type['application/rss+xml'] = xmlproc
|
69
|
+
|
70
|
+
end
|
71
|
+
|
72
|
+
def query_method(method, params={})
|
73
|
+
base_url = "http://#{@hostname}/do/#{@package}/"
|
74
|
+
param_list = params.to_a.map{|x| x.join('=')}.join('&')
|
75
|
+
url = "%s%s?%s" % [base_url, method.gsub('_','-'), param_list]
|
76
|
+
response = open(url, 'UserAgent' => 'RScriptClient')
|
77
|
+
@result = response.read
|
78
|
+
@return_type[response.content_type].call
|
79
|
+
return self
|
80
|
+
end
|
81
|
+
|
82
|
+
end
|
metadata
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rcscript-client
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors: []
|
7
|
+
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2009-12-17 00:00:00 +00:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description:
|
17
|
+
email:
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files: []
|
23
|
+
|
24
|
+
files:
|
25
|
+
- lib/rcscript-client.rb
|
26
|
+
has_rdoc: true
|
27
|
+
homepage:
|
28
|
+
licenses: []
|
29
|
+
|
30
|
+
post_install_message:
|
31
|
+
rdoc_options: []
|
32
|
+
|
33
|
+
require_paths:
|
34
|
+
- lib
|
35
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - ">="
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: "0"
|
40
|
+
version:
|
41
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
42
|
+
requirements:
|
43
|
+
- - ">="
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: "0"
|
46
|
+
version:
|
47
|
+
requirements: []
|
48
|
+
|
49
|
+
rubyforge_project:
|
50
|
+
rubygems_version: 1.3.5
|
51
|
+
signing_key:
|
52
|
+
specification_version: 3
|
53
|
+
summary: RCscript-client is a utility for remotely executing Ruby Scripting File (RSF)
|
54
|
+
test_files: []
|
55
|
+
|