diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml
index 7ef50bbd..9907a850 100644
--- a/.github/workflows/ci.yml
+++ b/.github/workflows/ci.yml
@@ -426,6 +426,9 @@ jobs:
ruby-version: "3.4"
- run: ruby -Ilib spec/visualci_spec.rb
working-directory: clients/ruby
+ # The gallery's Ruby renderer: the contract's golden fixtures.
+ - run: ruby -Ilib test/gallery_test.rb
+ working-directory: clients/gallery/ruby
- uses: pnpm/action-setup@v4
with:
package_json_file: frontend/package.json
@@ -437,6 +440,9 @@ jobs:
# real astro build).
- run: pnpm install --frozen-lockfile && pnpm --filter visualci-gallery typecheck && pnpm build && pnpm test
working-directory: clients/gallery
+ # The gem carries the library's built script and stylesheet, and the licence: it builds with them.
+ - run: bin/assets && gem build visualci-gallery.gemspec && tar -xOf visualci-gallery-*.gem data.tar.gz | tar -tz > /tmp/gem-files && grep -x app/assets/javascripts/visualci_gallery.js /tmp/gem-files && grep -x LICENSE /tmp/gem-files
+ working-directory: clients/gallery/ruby
# The Compare two PDFs page runs crates/visualci-wasm in the browser (scripts/wasm.mjs builds it).
- run: rustup target add wasm32-unknown-unknown
# Compile cache (kache, as Cargo's rustc wrapper): the R2 bucket every machine shares (KACHE_BUCKET), else the
diff --git a/.github/workflows/release-gallery-gem.yml b/.github/workflows/release-gallery-gem.yml
new file mode 100644
index 00000000..c02de689
--- /dev/null
+++ b/.github/workflows/release-gallery-gem.yml
@@ -0,0 +1,45 @@
+# Publish the visualci-gallery gem (clients/gallery/ruby) to RubyGems with trusted publishing: RubyGems trusts this
+# workflow through GitHub's OIDC token, so no API key is stored anywhere. Runs for a gallery-ruby-v* tag (matching
+# VisualCI::Gallery::VERSION) or by hand. The gem carries the JS library's ES5 build and stylesheet (bin/assets).
+name: Release gallery gem
+
+on:
+ push:
+ tags: ["gallery-ruby-v*"]
+ workflow_dispatch:
+
+permissions:
+ contents: read
+ id-token: write
+
+jobs:
+ publish:
+ runs-on: ubuntu-latest
+ timeout-minutes: 10
+ steps:
+ - uses: actions/checkout@v4
+ - uses: pnpm/action-setup@v4
+ with:
+ package_json_file: clients/gallery/package.json
+ - uses: actions/setup-node@v4
+ with:
+ node-version: "24"
+ - run: pnpm install --frozen-lockfile && pnpm --filter visualci-gallery build
+ working-directory: clients/gallery
+ - uses: ruby/setup-ruby@v1
+ with:
+ ruby-version: "3.4"
+ - name: The tag matches the gem's version
+ if: startsWith(github.ref, 'refs/tags/gallery-ruby-v')
+ working-directory: clients/gallery/ruby
+ run: |
+ version=$(ruby -r ./lib/visualci/gallery/version -e 'print VisualCI::Gallery::VERSION')
+ test "gallery-ruby-v$version" = "$GITHUB_REF_NAME" || { echo "tag $GITHUB_REF_NAME, gem $version"; exit 1; }
+ - run: ruby -Ilib test/gallery_test.rb
+ working-directory: clients/gallery/ruby
+ - uses: rubygems/configure-rubygems-credentials@v2.2.0
+ with:
+ # The RubyGems.org account that owns the gem (needed for the first push through a pending trusted publisher).
+ owner: "48431"
+ - run: bin/assets && gem build visualci-gallery.gemspec && gem push visualci-gallery-*.gem
+ working-directory: clients/gallery/ruby
diff --git a/clients/gallery/README.md b/clients/gallery/README.md
index 89689e02..152629f7 100644
--- a/clients/gallery/README.md
+++ b/clients/gallery/README.md
@@ -8,6 +8,8 @@ variables.
fixtures every renderer's tests check.
- [`js/`](js/README.md): `visualci-gallery`, the browser library and reference renderer.
- [`astro/`](astro/README.md): `visualci-astro-gallery`, rendered when the site builds and hydrated by the library.
+- [`ruby/`](ruby/README.md): the `visualci-gallery` gem, with a Rails helper and the library's files for the asset
+ pipeline.
-A pnpm workspace: `pnpm install && pnpm build && pnpm test` here. Coming: the Ruby gem, the Python package and the Rust
-crate, each emitting the contract's markup for the JS library to hydrate.
+A pnpm workspace: `pnpm install && pnpm build && pnpm test` here. Coming: the Python package and the Rust crate, each
+emitting the contract's markup for the JS library to hydrate.
diff --git a/clients/gallery/contract/SPEC.md b/clients/gallery/contract/SPEC.md
index a41b40b4..a6c83bff 100644
--- a/clients/gallery/contract/SPEC.md
+++ b/clients/gallery/contract/SPEC.md
@@ -43,7 +43,8 @@ word for word, in every language. An invalid option's message names it in the co
- The manifest is an object with a `groups` array. Each group is an object with a non-empty string `name`, a `title`
that's absent, null or a string, and an `items` array (possibly empty). Each item is an object with a non-empty
- string `name`, a `url`, and a `width` and `height` that are positive integers (JSON numbers with no fraction).
+ string `name`, a `url`, and a `width` and `height` that are whole numbers from 1 to 1000000 (JSON numbers with no
+ fraction; the limit keeps the density maths below exact in every language's numbers).
- An item's `url` must be an absolute http or https URL: it starts with `http://` or `https://` (scheme in any case)
and has no character from U+0000 to U+0020 or U+007F anywhere (browsers drop those, so `java\tscript:` would
still run).
@@ -54,6 +55,8 @@ word for word, in every language. An invalid option's message names it in the co
`themeSwitch` booleans; `layout` `"grid"` or `"strip"`; `seeAll` an object with a string `href` and a `text` that's
absent, null or a string. `seeAll.href` is an absolute URL as above, or a relative one: not empty, no character from
U+0000 to U+0020 or U+007F, and not starting with a scheme (`^[A-Za-z][A-Za-z0-9+.-]*:`).
+- The manifest is checked first, then the options in this order, and the first problem is the error: `headingLevel`,
+ `title`, `id`, `screens`, `groups`, `order`, `groupOrder`, `credit`, `themeSwitch`, `layout`, `seeAll`.
- Other fields, in the manifest or the options, are ignored.
## Reading the manifest
@@ -93,8 +96,9 @@ ASCII digits only, and `.` means any character at all, line breaks included:
3. Otherwise the key is the whole name without its extension, with no theme (it stands for both), no CSS width, not
2x.
-Size: with a CSS width, under 768 is `phone`, else `desktop`. Without one, the group's `size` option when set, else
-an image taller than it is wide (`height > width`) is `phone`, else `desktop`.
+Size: with a CSS width (the number's value: leading zeros don't count, so `0390` is 390), under 768 is `phone`, else
+`desktop`. Without one, the group's `size` option when set, else an image taller than it is wide (`height > width`)
+is `phone`, else `desktop`.
### Pairing
diff --git a/clients/gallery/contract/errors/fractional-size.message b/clients/gallery/contract/errors/fractional-size.message
index fc59956d..6e9c4a45 100644
--- a/clients/gallery/contract/errors/fractional-size.message
+++ b/clients/gallery/contract/errors/fractional-size.message
@@ -1 +1 @@
-gallery manifest group Web, item 0 (a-light-1440): its width and height aren't positive integers
+gallery manifest group Web, item 0 (a-light-1440): its width and height aren't whole numbers from 1 to 1000000
diff --git a/clients/gallery/contract/errors/no-size.message b/clients/gallery/contract/errors/no-size.message
index fc59956d..6e9c4a45 100644
--- a/clients/gallery/contract/errors/no-size.message
+++ b/clients/gallery/contract/errors/no-size.message
@@ -1 +1 @@
-gallery manifest group Web, item 0 (a-light-1440): its width and height aren't positive integers
+gallery manifest group Web, item 0 (a-light-1440): its width and height aren't whole numbers from 1 to 1000000
diff --git a/clients/gallery/contract/errors/too-wide.json b/clients/gallery/contract/errors/too-wide.json
new file mode 100644
index 00000000..f39e0912
--- /dev/null
+++ b/clients/gallery/contract/errors/too-wide.json
@@ -0,0 +1 @@
+{"groups": [{"name": "Web", "items": [{"name": "a-light-1440", "url": "https://app.visualci.com/g/acme/web/a.png", "width": 1000001, "height": 900}]}]}
diff --git a/clients/gallery/contract/errors/too-wide.message b/clients/gallery/contract/errors/too-wide.message
new file mode 100644
index 00000000..6e9c4a45
--- /dev/null
+++ b/clients/gallery/contract/errors/too-wide.message
@@ -0,0 +1 @@
+gallery manifest group Web, item 0 (a-light-1440): its width and height aren't whole numbers from 1 to 1000000
diff --git a/clients/gallery/contract/fixtures/widths/gallery.html b/clients/gallery/contract/fixtures/widths/gallery.html
new file mode 100644
index 00000000..5e8fe8d4
--- /dev/null
+++ b/clients/gallery/contract/fixtures/widths/gallery.html
@@ -0,0 +1,13 @@
+
+
'
+ g['screens'].each { |sc| out << card(sc, g['sizes'], h(@below + 1), true, g['fit']) }
+ out << '
'
+ out << '
'
+ end
+ end
+
+ def strip_size
+ @m['groups'].any? { |g| g['sizes'].include?('desktop') } ? 'desktop' : 'phone'
+ end
+
+ def strip(out)
+ size = strip_size
+ out << %(
)
+ out << '
'
+ out << '
'
+ @m['groups'].each do |g|
+ g['screens'].each do |sc|
+ out << card(sc, [size], h(@below), false, g['fit']) if sc['variants'].any? { |v| v['size'] == size }
+ end
+ end
+ out << '
'
+ out << '
'
+ out << '
'
+ end
+ end
+ end
+end
diff --git a/clients/gallery/ruby/lib/visualci/gallery/version.rb b/clients/gallery/ruby/lib/visualci/gallery/version.rb
new file mode 100644
index 00000000..0211f3cf
--- /dev/null
+++ b/clients/gallery/ruby/lib/visualci/gallery/version.rb
@@ -0,0 +1,7 @@
+# frozen_string_literal: true
+
+module VisualCI
+ module Gallery
+ VERSION = '0.1.0'
+ end
+end
diff --git a/clients/gallery/ruby/test/gallery_test.rb b/clients/gallery/ruby/test/gallery_test.rb
new file mode 100644
index 00000000..64c6448b
--- /dev/null
+++ b/clients/gallery/ruby/test/gallery_test.rb
@@ -0,0 +1,120 @@
+# frozen_string_literal: true
+
+require 'minitest/autorun'
+require 'json'
+require 'socket'
+require_relative '../lib/visualci/gallery'
+
+# The gallery contract's golden fixtures (../contract): every fixture's manifest and options give exactly its
+# model.json and gallery.html, and every manifest in errors/ and option in option-errors/ is refused.
+class GalleryContractTest < Minitest::Test
+ CONTRACT = File.expand_path('../../contract', __dir__)
+ OPTION_NAMES = { 'headingLevel' => :heading_level, 'groupOrder' => :group_order, 'themeSwitch' => :theme_switch,
+ 'seeAll' => :see_all }.freeze
+
+ def self.options(json)
+ json.to_h { |k, v| [OPTION_NAMES.fetch(k, k.to_sym), v] }
+ end
+
+ def read(path) = JSON.parse(File.read(path, encoding: 'UTF-8'))
+
+ Dir[File.join(CONTRACT, 'fixtures', '*')].each do |dir|
+ name = File.basename(dir)
+ define_method("test_fixture_#{name}") do
+ manifest = read(File.join(dir, 'manifest.json'))
+ options = self.class.options(read(File.join(dir, 'options.json')))
+ assert_equal read(File.join(dir, 'model.json')), VisualCI::Gallery.model(manifest, **options)
+ assert_equal File.read(File.join(dir, 'gallery.html'), encoding: 'UTF-8'), VisualCI::Gallery.render(manifest, **options)
+ end
+ end
+
+ # A refusal's message, word for word: .message beside .json.
+ def assert_refused(path)
+ error = assert_raises(VisualCI::Gallery::Error) { yield }
+ assert_equal File.read(path.sub(/\.json\z/, '.message'), encoding: 'UTF-8'), "#{error.message}\n"
+ end
+
+ Dir[File.join(CONTRACT, 'errors', '*.json')].each do |path|
+ define_method("test_refuses_manifest_#{File.basename(path, '.json')}") do
+ assert_refused(path) { VisualCI::Gallery.render(read(path)) }
+ end
+ end
+
+ Dir[File.join(CONTRACT, 'option-errors', '*.json')].each do |path|
+ define_method("test_refuses_option_#{File.basename(path, '.json')}") do
+ manifest = read(File.join(CONTRACT, 'fixtures', 'visualci', 'manifest.json'))
+ assert_refused(path) { VisualCI::Gallery.render(manifest, **self.class.options(read(path))) }
+ end
+ end
+
+ def test_has_fixtures
+ refute_empty Dir[File.join(CONTRACT, 'fixtures', '*')]
+ end
+end
+
+# Ruby spellings: symbol keys work as well as strings.
+class GalleryOptionsTest < Minitest::Test
+ MANIFEST = JSON.parse(File.read(File.expand_path('../../contract/fixtures/fleetlayer/manifest.json', __dir__), encoding: 'UTF-8'))
+
+ def test_symbol_keys
+ html = VisualCI::Gallery.render(MANIFEST, title: nil, screens: { overview: { title: 'Overview!' } },
+ see_all: { href: '/all' })
+ assert_includes html, '
Overview!
'
+ assert_includes html, 'See all screenshots'
+ end
+
+ def test_parse_shot_and_titles
+ assert_equal({ key: 'shots/app/billing', theme: 'dark', size: 'desktop', x2: true, css_width: 1440 },
+ VisualCI::Gallery.parse_shot('shots/app/billing/1440-dark-2x', 2880, 1800))
+ assert_equal 'SSig', VisualCI::Gallery.humanize(['ßig'])
+ assert_equal 'os', VisualCI::Gallery.slugify('İOS')
+ end
+end
+
+# fetch: the manifest over HTTP, and a clear error when it can't be read.
+class GalleryFetchTest < Minitest::Test
+ def serve(status, body, headers = '')
+ server = TCPServer.new('127.0.0.1', 0)
+ thread = Thread.new do
+ loop do
+ client = server.accept
+ client.gets
+ while (line = client.gets) && line != "\r\n"; end
+ client.write("HTTP/1.1 #{status}\r\nContent-Type: application/json\r\nContent-Length: #{body.bytesize}\r\n" \
+ "#{headers}Connection: close\r\n\r\n#{body}")
+ client.close
+ end
+ end
+ yield "http://127.0.0.1:#{server.addr[1]}/g/acme/web.json"
+ ensure
+ thread&.kill
+ server&.close
+ end
+
+ def test_follows_redirects
+ serve('200 OK', '{"groups": []}') do |target|
+ serve('302 Found', '', "Location: #{target}\r\n") do |url|
+ assert_equal({ 'groups' => [] }, VisualCI::Gallery.fetch(url, attempts: 1))
+ end
+ end
+ end
+
+ def test_fetches_and_parses
+ serve('200 OK', '{"groups":[]}') do |url|
+ assert_equal({ 'groups' => [] }, VisualCI::Gallery.fetch(url))
+ end
+ end
+
+ def test_raises_naming_the_url
+ serve('503 Service Unavailable', 'down') do |url|
+ error = assert_raises(VisualCI::Gallery::Error) { VisualCI::Gallery.fetch(url, attempts: 1) }
+ assert_equal "The screenshots gallery couldn't read #{url}: HTTP 503", error.message
+ end
+ end
+
+ def test_raises_on_bad_json
+ serve('200 OK', 'not json') do |url|
+ assert_raises(VisualCI::Gallery::Error) { VisualCI::Gallery.fetch(url, attempts: 1) }
+ end
+ end
+end
diff --git a/clients/gallery/ruby/visualci-gallery.gemspec b/clients/gallery/ruby/visualci-gallery.gemspec
new file mode 100644
index 00000000..ce7fe34e
--- /dev/null
+++ b/clients/gallery/ruby/visualci-gallery.gemspec
@@ -0,0 +1,23 @@
+# frozen_string_literal: true
+
+require_relative 'lib/visualci/gallery/version'
+
+Gem::Specification.new do |s|
+ s.name = 'visualci-gallery'
+ s.version = VisualCI::Gallery::VERSION
+ s.summary = 'A screenshots page from a VisualCI public gallery, rendered in Ruby or Rails'
+ s.description = 'Fetches a VisualCI public gallery manifest and renders a screenshots page (tabs, light and dark, ' \
+ 'desktop and phone, a full-size view) as the markup the visualci-gallery JS library hydrates. ' \
+ 'Includes a Rails helper and the library\'s script and stylesheet for the asset pipeline.'
+ s.authors = ['DocSpring']
+ s.homepage = 'https://visualci.com/docs/gallery/'
+ s.license = 'Apache-2.0'
+ s.required_ruby_version = '>= 3.1'
+ s.files = Dir['lib/**/*.rb'] + Dir['app/assets/**/*.{js,css}'] + ['README.md', 'LICENSE']
+ s.metadata = {
+ 'homepage_uri' => 'https://visualci.com/docs/gallery/',
+ 'source_code_uri' => 'https://github.com/DocSpring/visualci/tree/main/clients/gallery/ruby',
+ 'bug_tracker_uri' => 'https://github.com/DocSpring/visualci/issues',
+ 'rubygems_mfa_required' => 'true'
+ }
+end