vega 0.2.5 → 0.2.7

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 7c067bc284fb8cb8d590bc3840e1903c6da0ba49f55f9c0cc784d88358d0ff5c
4
- data.tar.gz: 22268adc67db09659c8b7701f893f6df63cbd34810845b6bdcd8a359364b0df1
3
+ metadata.gz: 4fa6db813593582eba71e4d7f4b306553fb6530288478b2b03daf20b952a6827
4
+ data.tar.gz: 7b09a35505f87dc7e2498f8318947073a95ae4aa353e4b127c2f48c6baff263a
5
5
  SHA512:
6
- metadata.gz: 5257af439bd6fdb7530a3c036a75449fedb7b2a28efe0aa426761588f07e150de7327ce52dd8e138aebacb87c3ffdf7a484356d021395b37567c01217d0f4931
7
- data.tar.gz: b360e400d2adb00623c8ec32b1d169260959e6a7b62f4d6723329aefc5177cfb8d60bb07d2d31c966d1442a126e787007bc0dae75eb0846eaf3cadcbc562ed0b
6
+ metadata.gz: 32ffb63658b185ee27f46254dafe333f905a47ec4b5aae591009810dbc26815aa71d890f77f7c107c3465631ca5914d9d3edfefec84cfdb151c7e5f8af12813f
7
+ data.tar.gz: bf929161b62990cd9cab96fed031611ab03037b083c9ecebe401990359c9818bcb500bfaac3b709045da5ef880c322f8a3b7723758b50501e1072fd1a514171c
data/CHANGELOG.md CHANGED
@@ -1,3 +1,13 @@
1
+ ## 0.2.7 (2022-09-21)
2
+
3
+ - Updated Vega to 5.22.1, Vega-Lite to 5.5.0, and Vega-Embed to 6.21.0
4
+
5
+ ## 0.2.6 (2022-02-02)
6
+
7
+ - Added `vega_chart` helper
8
+ - Added `nonce` option
9
+ - Added `to_html` method
10
+
1
11
  ## 0.2.5 (2022-01-22)
2
12
 
3
13
  - 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
@@ -32,10 +34,10 @@ module Vega
32
34
  <script>
33
35
  require.config({
34
36
  paths: {
35
- 'vega': 'https://cdn.jsdelivr.net/npm/vega@5.21.0/build/vega.min',
37
+ 'vega': 'https://cdn.jsdelivr.net/npm/vega@5.22.1/build/vega.min',
36
38
  'vega-util': 'https://cdn.jsdelivr.net/npm/vega-util@1.17.0/build/vega-util.min',
37
- 'vega-lite': 'https://cdn.jsdelivr.net/npm/vega-lite@5.2.0/build/vega-lite.min',
38
- 'vega-embed': 'https://cdn.jsdelivr.net/npm/vega-embed@6.20.5/build/vega-embed.min'
39
+ 'vega-lite': 'https://cdn.jsdelivr.net/npm/vega-lite@5.5.0/build/vega-lite.min',
40
+ 'vega-embed': 'https://cdn.jsdelivr.net/npm/vega-embed@6.21.0/build/vega-embed.min'
39
41
  }
40
42
  });
41
43
  require(['vega', 'vega-util', 'vega-lite', 'vega-embed'], function(vega, vegaUtil, vegaLite, vegaEmbed) {
data/lib/vega/version.rb CHANGED
@@ -1,3 +1,3 @@
1
1
  module Vega
2
- VERSION = "0.2.5"
2
+ VERSION = "0.2.7"
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