rgviz-rails 0.26 → 0.28

Sign up to get free protection for your applications and to get access to all the features.
data/lib/rgviz_rails.rb CHANGED
@@ -1,4 +1,6 @@
1
1
  require 'rgviz_rails/executor.rb'
2
+ require 'rgviz_rails/js_renderer.rb'
3
+ require 'rgviz_rails/tqx.rb'
2
4
  require 'rgviz_rails/adapters/mysql_adapter.rb'
3
5
  require 'rgviz_rails/adapters/postgresql_adapter.rb'
4
6
  require 'rgviz_rails/adapters/sqlite_adapter.rb'
@@ -6,6 +6,7 @@ module Rgviz
6
6
  def initialize(model_class, query)
7
7
  @model_class = model_class
8
8
  @query = query
9
+ @query = Parser.new(query).parse unless query.kind_of?(Query)
9
10
  @selects = []
10
11
  @joins = {}
11
12
  @labels = {}
@@ -0,0 +1,19 @@
1
+ module Rgviz
2
+ module JsRenderer
3
+ def self.render(table, tqx)
4
+ response_handler = tqx['responseHandler']
5
+ req_id = tqx['reqId']
6
+ version = tqx['version']
7
+ "#{response_handler}({reqId:'#{req_id}',status:'ok',version:'#{version}',table:#{table.to_json}});"
8
+ end
9
+
10
+ def self.render_error(reason, message, tqx)
11
+ reason = reason.gsub("'", "\\'")
12
+ message = message.gsub("'", "\\'")
13
+ response_handler = tqx['responseHandler']
14
+ req_id = tqx['reqId']
15
+ version = tqx['version']
16
+ "#{response_handler}({reqId:'#{req_id}',status:'error',version:'#{version}',errors:[{reason:'#{reason}', message:'#{message}'}]});"
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,22 @@
1
+ module Rgviz
2
+ module Tqx
3
+ def self.parse(tqx)
4
+ tqx ||= ''
5
+ pieces = tqx.split ';'
6
+
7
+ map = {
8
+ 'reqId' => '0',
9
+ 'version' => '0.6',
10
+ 'responseHandler' => 'google.visualization.Query.setResponse',
11
+ 'out' => 'json'
12
+ }
13
+
14
+ pieces.each do |p|
15
+ key_value = p.split ':'
16
+ map[key_value[0]] = key_value[1] if key_value.length == 2
17
+ end
18
+
19
+ map
20
+ end
21
+ end
22
+ end
data/rails/init.rb CHANGED
@@ -15,29 +15,8 @@ config.after_initialize do
15
15
  conditions = hash[:conditions]
16
16
  query = params[:tq]
17
17
  tqx = params[:tqx] || ''
18
- pieces = tqx.split ';'
19
- reqId = '0'
20
- version = '0.6'
21
- responseHandler = 'google.visualization.Query.setResponse'
22
- out = 'json'
23
- outFileName = nil
24
- pieces.each do |p|
25
- key_value = p.split ':'
26
- if key_value.length == 2
27
- case key_value[0]
28
- when 'reqId'
29
- reqId = key_value[1]
30
- when 'version'
31
- version = key_value[1]
32
- when 'responseHandler'
33
- responseHandler = key_value[1]
34
- when 'out'
35
- out = key_value[1]
36
- when 'outFileName'
37
- outFileName = key_value[1]
38
- end
39
- end
40
- end
18
+
19
+ tqx = Rgviz::Tqx.parse(tqx)
41
20
 
42
21
  begin
43
22
  executor = Rgviz::Executor.new model, Rgviz::Parser.new(query).parse
@@ -45,31 +24,31 @@ config.after_initialize do
45
24
  options[:conditions] = conditions if conditions
46
25
  table = executor.execute options
47
26
 
48
- case out
27
+ yield table if block_given?
28
+
29
+ case tqx['out']
49
30
  when 'json'
50
- original_render :text => "#{responseHandler}({reqId:'#{reqId}',status:'ok',version:'#{version}',table:#{table.to_json}});"
31
+ original_render :text => Rgviz::JsRenderer.render(table, tqx)
51
32
  when 'html'
52
33
  original_render :text => Rgviz::HtmlRenderer.render(table)
53
34
  when 'csv'
54
35
  csv_output = Rgviz::CsvRenderer.render(table)
55
- if outFileName
36
+ if tqx['outFileName']
56
37
  send_data csv_output, :filename => outFileName, :type => 'text/csv'
57
38
  else
58
39
  original_render :text => csv_output
59
40
  end
60
41
  else
61
- original_render :text => "#{responseHandler}({reqId:'#{reqId}',status:'error',version:'#{version}',errors:[{reason:'not_supported', message:'Unsupported output type: #{out}'}]});"
42
+ original_render :text => Rgviz::JsRenderer.render_error('not_supported', "Unsupported output type: #{out}", tqx)
62
43
  end
63
44
  rescue ParseException => e
64
- msg = e.message.gsub("'", "\\'")
65
-
66
45
  case out
67
46
  when 'json'
68
- original_render :text => "#{responseHandler}({reqId:'#{reqId}',status:'error',version:'#{version}',errors:[{reason:'invalid_query', message:'#{msg}'}]});"
47
+ original_render :text => Rgviz::JsRenderer.render_error('invalid_query', e.message)
69
48
  when 'html'
70
- original_render :text => "<b>Error:</b> #{msg}"
49
+ original_render :text => "<b>Error:</b> #{e.message}"
71
50
  when 'csv'
72
- original_render :text => "Error: #{msg}"
51
+ original_render :text => "Error: #{e.message}"
73
52
  else
74
53
  original_render :text => "<b>Unsupported output type:</b> #{out}"
75
54
  end
metadata CHANGED
@@ -1,12 +1,12 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rgviz-rails
3
3
  version: !ruby/object:Gem::Version
4
- hash: 63
4
+ hash: 51
5
5
  prerelease: false
6
6
  segments:
7
7
  - 0
8
- - 26
9
- version: "0.26"
8
+ - 28
9
+ version: "0.28"
10
10
  platform: ruby
11
11
  authors:
12
12
  - Ary Borenszweig
@@ -29,6 +29,8 @@ extra_rdoc_files:
29
29
  files:
30
30
  - lib/rgviz_rails.rb
31
31
  - lib/rgviz_rails/executor.rb
32
+ - lib/rgviz_rails/js_renderer.rb
33
+ - lib/rgviz_rails/tqx.rb
32
34
  - lib/rgviz_rails/view_helper.rb
33
35
  - lib/rgviz_rails/adapters/mysql_adapter.rb
34
36
  - lib/rgviz_rails/adapters/postgresql_adapter.rb