class RScriptClient
Attributes
doc[R]
package[RW]
result[R]
text[R]
Public Class Methods
new(opts={})
click to toggle source
# File lib/rcscript-client.rb, line 11 def initialize(opts={}) o = {:hostname => 'rscript.heroku.com', :package => ''}.merge(opts) @hostname = o[:hostname] @package = o[:package] if @package.length > 0 then jobs_to_methods(@package) init_content_types end end
Public Instance Methods
package=(s)
click to toggle source
# File lib/rcscript-client.rb, line 24 def package=(s) if s then @package = s jobs_to_methods(@package) init_content_types end end
Private Instance Methods
init_content_types()
click to toggle source
# File lib/rcscript-client.rb, line 47 def init_content_types @return_type = {} xmlproc = Proc.new { @doc = Document.new(@result.sub(/xmlns=["']http:\/\/www.w3.org\/1999\/xhtml["']/,'')) summary_node = XPath.match(@doc.root, 'summary/*') if summary_node then summary_node.each do |node| if node.cdatas.length > 0 then if node.cdatas.length == 1 then content = node.cdatas.join.strip else if node.elements["@value='methods'"] then else content = node.cdatas.map {|x| x.to_s[/^\{.*\}$/] ? eval(x.to_s) : x.to_s} end end else content = node.text.to_s.gsub(/"/,'\"').gsub(/#/,'\#') end method =<<EOF def #{node.name}() #{content} end EOF self.instance_eval(method) end records = XPath.match(@doc.root, 'records/*/text()') method = "def %s(); %s; end" % [@doc.root.name, records.inspect] if records self.instance_eval(method) end } textproc = Proc.new {@text = @result} @return_type['text/plain'] = textproc @return_type['text/html'] = textproc @return_type['text/xml'] = xmlproc @return_type['application/xml'] = xmlproc @return_type['application/rss+xml'] = xmlproc end
jobs_to_methods(package)
click to toggle source
# File lib/rcscript-client.rb, line 34 def jobs_to_methods(package) #url = "http://rorbuilder.info/r/heroku/%s.rsf" % package url = "http://%s/view-source/%s" % [@hostname, package] puts 'url : ' + url doc = Document.new(open(url, 'UserAgent' => 'ClientRscript').read) a = XPath.match(doc.root, 'job/attribute::id') a.each do |attr| method_name = attr.value.to_s.gsub('-','_') method = "def %s(param={}); query_method('%s', param); end" % [method_name, method_name] self.instance_eval(method) end end
query_method(method, params={})
click to toggle source
# File lib/rcscript-client.rb, line 95 def query_method(method, params={}) base_url = "http://#{@hostname}/do/#{@package}/" param_list = params.to_a.map{|param, value| "%s=%s" % [param, CGI.escape(value)]}.join('&') url = "%s%s?%s" % [base_url, method.gsub('_','-'), param_list] response = open(url, 'UserAgent' => 'RScriptClient') @result = response.read @return_type[response.content_type].call return self end