rsc 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.
Files changed (2) hide show
  1. data/lib/rsc.rb +105 -0
  2. metadata +56 -0
@@ -0,0 +1,105 @@
1
+ require 'open-uri'
2
+ require 'cgi'
3
+ require 'rexml/document'
4
+
5
+ class RScriptClient
6
+ include REXML
7
+
8
+ attr_reader :doc, :result, :text
9
+ attr_accessor :package
10
+
11
+ def initialize(opts={})
12
+
13
+ o = {:hostname => 'rscript.heroku.com', :package => ''}.merge(opts)
14
+ @hostname = o[:hostname]
15
+ @package = o[:package]
16
+
17
+ if @package.length > 0 then
18
+ jobs_to_methods(@package)
19
+ init_content_types
20
+ end
21
+
22
+ end
23
+
24
+ def package=(s)
25
+ if s then
26
+ @package = s
27
+ jobs_to_methods(@package)
28
+ init_content_types
29
+ end
30
+ end
31
+
32
+ private
33
+
34
+ def jobs_to_methods(package)
35
+ #url = "http://rorbuilder.info/r/heroku/%s.rsf" % package
36
+ url = "http://%s/view-source/%s" % [@hostname, package]
37
+ puts 'url : ' + url
38
+ doc = Document.new(open(url, 'UserAgent' => 'ClientRscript').read)
39
+ a = XPath.match(doc.root, 'job/attribute::id')
40
+ a.each do |attr|
41
+ method_name = attr.value.to_s.gsub('-','_')
42
+ method = "def %s(param={}); query_method('%s', param); end" % [method_name, method_name]
43
+ self.instance_eval(method)
44
+ end
45
+ end
46
+
47
+ def init_content_types
48
+
49
+ @return_type = {}
50
+
51
+ xmlproc = Proc.new {
52
+ @doc = Document.new(@result.sub(/xmlns=["']http:\/\/www.w3.org\/1999\/xhtml["']/,''))
53
+ summary_node = XPath.match(@doc.root, 'summary/*')
54
+ if summary_node then
55
+ summary_node.each do |node|
56
+
57
+ if node.cdatas.length > 0 then
58
+ if node.cdatas.length == 1 then
59
+ content = node.cdatas.join.strip
60
+ else
61
+ if node.elements["@value='methods'"] then
62
+
63
+ else
64
+ content = node.cdatas.map {|x| x.to_s[/^\{.*\}$/] ? eval(x.to_s) : x.to_s}
65
+ end
66
+
67
+ end
68
+ else
69
+ content = node.text.to_s.gsub(/"/,'\"').gsub(/#/,'\#')
70
+ end
71
+
72
+
73
+ method =<<EOF
74
+ def #{node.name}()
75
+ #{content}
76
+ end
77
+ EOF
78
+ self.instance_eval(method)
79
+ end
80
+ records = XPath.match(@doc.root, 'records/*/text()')
81
+ method = "def %s(); %s; end" % [@doc.root.name, records.inspect] if records
82
+ self.instance_eval(method)
83
+ end
84
+ }
85
+
86
+ textproc = Proc.new {@text = @result}
87
+ @return_type['text/plain'] = textproc
88
+ @return_type['text/html'] = textproc
89
+ @return_type['text/xml'] = xmlproc
90
+ @return_type['application/xml'] = xmlproc
91
+ @return_type['application/rss+xml'] = xmlproc
92
+
93
+ end
94
+
95
+ def query_method(method, params={})
96
+ base_url = "http://#{@hostname}/do/#{@package}/"
97
+ param_list = params.to_a.map{|param, value| "%s=%s" % [param, CGI.escape(value)]}.join('&')
98
+ url = "%s%s?%s" % [base_url, method.gsub('_','-'), param_list]
99
+ response = open(url, 'UserAgent' => 'RScriptClient')
100
+ @result = response.read
101
+ @return_type[response.content_type].call
102
+ return self
103
+ end
104
+
105
+ end
metadata ADDED
@@ -0,0 +1,56 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rsc
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - James Robertson
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-10-01 00:00:00 +01:00
14
+ default_executable:
15
+ dependencies: []
16
+
17
+ description: formerly known as rcscript-client
18
+ email:
19
+ executables: []
20
+
21
+ extensions: []
22
+
23
+ extra_rdoc_files: []
24
+
25
+ files:
26
+ - lib/rsc.rb
27
+ has_rdoc: true
28
+ homepage:
29
+ licenses: []
30
+
31
+ post_install_message:
32
+ rdoc_options: []
33
+
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ none: false
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: "0"
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ none: false
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: "0"
48
+ requirements: []
49
+
50
+ rubyforge_project:
51
+ rubygems_version: 1.5.2
52
+ signing_key:
53
+ specification_version: 3
54
+ summary: rsc
55
+ test_files: []
56
+