vega 0.2.5 → 0.2.6

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c067bc284fb8cb8d590bc3840e1903c6da0ba49f55f9c0cc784d88358d0ff5c
4
- data.tar.gz: 22268adc67db09659c8b7701f893f6df63cbd34810845b6bdcd8a359364b0df1
3
+ metadata.gz: 12a40c67ddedf70381a6a20c4224bf028910fbf92e6c44c13ca31e5a2465d1a9
4
+ data.tar.gz: 1e33341d2b573078e6ee30984b8c7ba15638a26d1678c426eaf5ddb248796b8f
5
5
  SHA512:
6
- metadata.gz: 5257af439bd6fdb7530a3c036a75449fedb7b2a28efe0aa426761588f07e150de7327ce52dd8e138aebacb87c3ffdf7a484356d021395b37567c01217d0f4931
7
- data.tar.gz: b360e400d2adb00623c8ec32b1d169260959e6a7b62f4d6723329aefc5177cfb8d60bb07d2d31c966d1442a126e787007bc0dae75eb0846eaf3cadcbc562ed0b
6
+ metadata.gz: f3f04599a9a099d2572b0778ef6d7043993eb15bb46412ef8c31e6e2dff2bd6188ca20851969fd16d9dd1203c7ca6d36ef5ba437ab5e138344ba9564878aa813
7
+ data.tar.gz: caab809560db84c55fc8015804468e97b5bf5c7a89139436c9bc70b0229775f2b39db5c31109b783ab02e31a1fb0c27b5769e0d70165c005ddf594ff88e63495
data/CHANGELOG.md CHANGED
@@ -1,3 +1,9 @@
1
+ ## 0.2.6 (2022-02-02)
2
+
3
+ - Added `vega_chart` helper
4
+ - Added `nonce` option
5
+ - Added `to_html` method
6
+
1
7
  ## 0.2.5 (2022-01-22)
2
8
 
3
9
  - Updated Vega-Lite to 5.2.0 and Vega-Embed to 6.20.5
data/README.md CHANGED
@@ -19,6 +19,7 @@ gem "vega"
19
19
  Then follow the instructions for how you plan to use it:
20
20
 
21
21
  - [Rails 7 / Importmap](#rails-7--importmap)
22
+ - [Rails 7 / esbuild or Webpack](#rails-7--esbuild-or-webpack)
22
23
  - [Rails 6 / Webpacker](#rails-6--webpacker)
23
24
  - [Rails 5 / Sprockets](#rails-5--sprockets)
24
25
  - [iRuby](#iruby)
@@ -41,7 +42,24 @@ import "vega"
41
42
  import "vega-lite"
42
43
  import "vega-embed"
43
44
 
44
- window.dispatchEvent(new Event("vega:load"));
45
+ window.dispatchEvent(new Event("vega:load"))
46
+ ```
47
+
48
+ ### Rails 7 / esbuild or Webpack
49
+
50
+ Run:
51
+
52
+ ```sh
53
+ yarn add vega vega-lite vega-embed
54
+ ```
55
+
56
+ And add to `app/javascript/application.js`:
57
+
58
+ ```js
59
+ import embed from "vega-embed"
60
+
61
+ window.vegaEmbed = embed
62
+ window.dispatchEvent(new Event("vega:load"))
45
63
  ```
46
64
 
47
65
  ### Rails 6 / Webpacker
@@ -74,12 +92,12 @@ No additional set up is needed.
74
92
 
75
93
  ### Other
76
94
 
77
- For Sinatra and other web frameworks, include the Vega JavaScript files on pages with charts:
95
+ For Sinatra and other web frameworks, download [Vega](https://cdn.jsdelivr.net/npm/vega@5), [Vega-Lite](https://cdn.jsdelivr.net/npm/vega-lite@5), and [Vega-Embed](https://cdn.jsdelivr.net/npm/vega-embed@6) and include them on pages with charts:
78
96
 
79
97
  ```html
80
- <script src="https://cdn.jsdelivr.net/npm/vega@5.21.0"></script>
81
- <script src="https://cdn.jsdelivr.net/npm/vega-lite@5.2.0"></script>
82
- <script src="https://cdn.jsdelivr.net/npm/vega-embed@6.20.5"></script>
98
+ <script src="vega.js"></script>
99
+ <script src="vega-lite.js"></script>
100
+ <script src="vega-embed.js"></script>
83
101
  ```
84
102
 
85
103
  ## Getting Started
@@ -324,7 +342,7 @@ spec = {
324
342
  And render it in Rails
325
343
 
326
344
  ```erb
327
- <%= Vega.render(spec) %>
345
+ <%= vega_chart spec %>
328
346
  ```
329
347
 
330
348
  Or display it in iRuby
@@ -367,6 +385,29 @@ File.binwrite("chart.pdf", chart.to_pdf)
367
385
 
368
386
  ## Content Security Policy (CSP)
369
387
 
388
+ ### Styles and Frames
389
+
390
+ Enable unsafe inline styles and blob frames on actions that have charts
391
+
392
+ ```ruby
393
+ class ChartsController < ApplicationController
394
+ content_security_policy only: :index do |policy|
395
+ policy.style_src :self, :unsafe_inline
396
+ policy.frame_src :blob
397
+ end
398
+ end
399
+ ```
400
+
401
+ ### Nonce
402
+
403
+ Automatically add a nonce when configured in Rails with:
404
+
405
+ ```erb
406
+ <%= vega_chart chart %>
407
+ ```
408
+
409
+ ### Interpreter
410
+
370
411
  By default, the Vega parser uses the Function constructor, which [can cause issues with CSP](https://vega.github.io/vega/usage/interpreter/).
371
412
 
372
413
  For Rails 7 / Importmap, add to `config/importmap.rb`:
@@ -18,9 +18,10 @@ module Vega
18
18
  end
19
19
  immutable_method :embed_options
20
20
 
21
- def to_s
22
- Spec.new(spec).to_s
21
+ def to_html(nonce: nil)
22
+ Spec.new(spec).to_html(nonce: nonce)
23
23
  end
24
+ alias_method :to_s, :to_html
24
25
 
25
26
  def to_iruby
26
27
  Spec.new(spec).to_iruby
@@ -0,0 +1,29 @@
1
+ module Vega
2
+ module Helper
3
+ def vega_chart(chart, nonce: true)
4
+ unless chart.is_a?(Vega::BaseChart) || chart.is_a?(Hash)
5
+ raise TypeError, "expected Vega chart or spec"
6
+ end
7
+
8
+ if nonce == true
9
+ # Secure Headers also defines content_security_policy_nonce but it takes an argument
10
+ # Rails 5.2 overrides this method, but earlier versions do not
11
+ if respond_to?(:content_security_policy_nonce) && (content_security_policy_nonce rescue nil)
12
+ # Rails 5.2
13
+ nonce = content_security_policy_nonce
14
+ elsif respond_to?(:content_security_policy_script_nonce)
15
+ # Secure Headers
16
+ nonce = content_security_policy_script_nonce
17
+ else
18
+ nonce = nil
19
+ end
20
+ end
21
+
22
+ if chart.is_a?(Hash)
23
+ Vega.render(chart, nonce: nonce)
24
+ else
25
+ chart.to_html(nonce: nonce)
26
+ end
27
+ end
28
+ end
29
+ end
data/lib/vega/spec.rb CHANGED
@@ -6,11 +6,12 @@ module Vega
6
6
  @spec = spec.transform_keys!(&:to_s)
7
7
  end
8
8
 
9
- def to_s
9
+ def to_html(nonce: nil)
10
10
  html, js = generate_output
11
+ nonce_html = nonce ? " nonce=\"#{ERB::Util.html_escape(nonce)}\"" : nil
11
12
  output = <<~EOS
12
13
  #{html}
13
- <script>
14
+ <script#{nonce_html}>
14
15
  (function() {
15
16
  var createChart = function() { #{js} };
16
17
  if ("vegaEmbed" in window) {
@@ -23,6 +24,7 @@ module Vega
23
24
  EOS
24
25
  output.respond_to?(:html_safe) ? output.html_safe : output
25
26
  end
27
+ alias_method :to_s, :to_html
26
28
 
27
29
  # TODO only load vega-lite if $schema requires it
28
30
  def to_iruby
data/lib/vega/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vega
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.6"
3
3
  end
data/lib/vega.rb CHANGED
@@ -9,6 +9,7 @@ require "vega/base_chart"
9
9
  require "vega/chart"
10
10
  require "vega/lite_chart"
11
11
  require "vega/spec"
12
+ require "vega/helper"
12
13
  require "vega/version"
13
14
 
14
15
  # integrations
@@ -25,8 +26,8 @@ module Vega
25
26
  LiteChart.new
26
27
  end
27
28
 
28
- def render(spec)
29
- Spec.new(spec).to_s
29
+ def render(spec, nonce: nil)
30
+ Spec.new(spec).to_html(nonce: nonce)
30
31
  end
31
32
 
32
33
  def display(spec)
@@ -34,3 +35,9 @@ module Vega
34
35
  end
35
36
  end
36
37
  end
38
+
39
+ if defined?(ActiveSupport.on_load)
40
+ ActiveSupport.on_load(:action_view) do
41
+ include Vega::Helper
42
+ end
43
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: vega
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.5
4
+ version: 0.2.6
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andrew Kane
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-01-23 00:00:00.000000000 Z
11
+ date: 2022-02-03 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email: andrew@ankane.org
@@ -23,6 +23,7 @@ files:
23
23
  - lib/vega/base_chart.rb
24
24
  - lib/vega/chart.rb
25
25
  - lib/vega/engine.rb
26
+ - lib/vega/helper.rb
26
27
  - lib/vega/lite_chart.rb
27
28
  - lib/vega/method_helpers.rb
28
29
  - lib/vega/spec.rb