cmu-web-qrnegie 0.3.2

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 ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 62151d8825df8d5c06ae141cc9242f77f7a24b856d52381bf2e30aeaafad7f4a
4
+ data.tar.gz: 49c124b273f30a61721f332c562630a5f6fd07bd301e383ea6eef458bed18012
5
+ SHA512:
6
+ metadata.gz: dc09d931ec78bb4b6aaf20e93fa3cdbdbd9f28fec6a05dcadcc4a38e9688ca401f537825431c05f983c68ef037ab6f755ec52b254b5869e915934de4f29d31e2
7
+ data.tar.gz: 39bfe2b7f91e40e33010d68ba65267bd120e00a6080fd56b3b2d3c7c1fed7bd319238a5c81e2936ce2bfd21343a20a2bee997ab72cc1836f1cbcd8ba309a0246
data/LICENSE ADDED
@@ -0,0 +1,28 @@
1
+ BSD 3-Clause License
2
+
3
+ Copyright (c) 2026, Carnegie Mellon University
4
+
5
+ Redistribution and use in source and binary forms, with or without
6
+ modification, are permitted provided that the following conditions are met:
7
+
8
+ 1. Redistributions of source code must retain the above copyright notice, this
9
+ list of conditions and the following disclaimer.
10
+
11
+ 2. Redistributions in binary form must reproduce the above copyright notice,
12
+ this list of conditions and the following disclaimer in the documentation
13
+ and/or other materials provided with the distribution.
14
+
15
+ 3. Neither the name of the copyright holder nor the names of its
16
+ contributors may be used to endorse or promote products derived from
17
+ this software without specific prior written permission.
18
+
19
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
22
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
23
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
25
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
26
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
27
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
data/README.md ADDED
@@ -0,0 +1,72 @@
1
+ # QRnegie for Ruby and Rails
2
+
3
+ QR codes from [QRnegie](https://qrnegie.andrew.cmu.edu/) in Ruby code and
4
+ Rails views, in Carnegie Mellon styles, as SVG or PNG. The gem only builds
5
+ image addresses: QRnegie draws each code when someone loads it, and the gem
6
+ never calls the service itself.
7
+
8
+ The gem lives in the QRnegie repository's `packages/ruby/`. Each QRnegie
9
+ release publishes it at the same version.
10
+
11
+ ## Install
12
+
13
+ ```ruby
14
+ gem 'cmu-web-qrnegie', '~> 0.3'
15
+ ```
16
+
17
+ ## In Rails Views
18
+
19
+ Rails views get `qrnegie_tag`. It takes the text or link to encode, alt
20
+ text and QRnegie's options; anything else goes to `image_tag`:
21
+
22
+ ```erb
23
+ <%= qrnegie_tag 'https://www.cmu.edu/', alt: 'QR code for cmu.edu' %>
24
+ <%= qrnegie_tag event_url(@event), alt: 'QR code for this event', style: :mosaic, width: 1024 %>
25
+ <%= qrnegie_tag 'https://www.cmu.edu/', alt: 'QR code for cmu.edu', below: 'Scan Me', class: 'qr' %>
26
+ ```
27
+
28
+ Alt text is required. Say where the code goes, not that it's a QR code
29
+ alone.
30
+
31
+ ## In Ruby
32
+
33
+ ```ruby
34
+ require 'cmu-web-qrnegie'
35
+
36
+ QRnegie.url('https://www.cmu.edu/')
37
+ QRnegie.url('https://www.cmu.edu/', style: 'kraus-campo', width: 1024, format: :png)
38
+ QRnegie.url('https://www.cmu.edu/', style: :icon, icon: 'calendar-event')
39
+ ```
40
+
41
+ ## Options
42
+
43
+ | Option | Gives |
44
+ | ------------------ | ---------------------------------------------------------------------------- |
45
+ | `style:` | A style from QRnegie's Styles page, e.g., `mosaic`; Basic Scotty if left out |
46
+ | `width:` | The image's width in pixels, up to 2048; 512 if left out |
47
+ | `above:`, `below:` | A line of text above or below the code, up to 40 characters each |
48
+ | `icon:` | Center Icon's icon, a Bootstrap Icons name such as `wifi` |
49
+ | `format:` | `:svg` (the default) or `:png` |
50
+
51
+ The gem checks the text, width, lines and format; QRnegie checks the rest
52
+ and explains any address it can't draw. The
53
+ [Styles](https://qrnegie.andrew.cmu.edu/styles.html) and
54
+ [Reference](https://qrnegie.andrew.cmu.edu/reference.html) pages list every
55
+ style and the smallest width each one takes. Detailed styles start at 1024.
56
+
57
+ To point at a local copy of QRnegie, set the host once:
58
+
59
+ ```ruby
60
+ QRnegie.host = 'http://127.0.0.1:3000'
61
+ ```
62
+
63
+ ## Work on the Gem
64
+
65
+ ```bash
66
+ cd packages/ruby
67
+ bundle install
68
+ bundle exec rake test build
69
+ ```
70
+
71
+ `rake version` copies the app's license and its version from
72
+ `package.json` into the gem; `rake clean` removes them and the build.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.3.2
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'qrnegie'
@@ -0,0 +1,18 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QRnegie
4
+ # A view helper for Rails, included in every view by the Railtie
5
+ module Helper
6
+ OPTIONS = %i[style width above below icon format].freeze
7
+
8
+ # <%= qrnegie_tag 'https://www.cmu.edu/', alt: 'QR code for cmu.edu', style: :mosaic %>
9
+ # Takes QRnegie's options, and image_tag's for everything else. Alt text
10
+ # is required; say where the code goes.
11
+ def qrnegie_tag(text, alt:, **options)
12
+ code = options.slice(*OPTIONS)
13
+ html = options.except(*OPTIONS)
14
+ # Text above or below makes the image taller, so only the width is known
15
+ image_tag(QRnegie.url(text, **code), alt: alt, width: code.fetch(:width, 512), **html)
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module QRnegie
4
+ # Makes qrnegie_tag available in Rails views
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'qrnegie.helper' do
7
+ ActiveSupport.on_load(:action_view) { include QRnegie::Helper }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module QRnegie
6
+ GEM_ROOT = File.expand_path('../..', __dir__)
7
+ # A built gem carries a VERSION file (rake version); a checkout reads the
8
+ # app's package.json, so the gem and the app share one version
9
+ VERSION = if File.file?(File.join(GEM_ROOT, 'VERSION'))
10
+ File.read(File.join(GEM_ROOT, 'VERSION')).strip
11
+ else
12
+ JSON.parse(File.read(File.expand_path('../../package.json', GEM_ROOT))).fetch('version')
13
+ end.freeze
14
+ end
data/lib/qrnegie.rb ADDED
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require_relative 'qrnegie/version'
5
+
6
+ # Addresses for QRnegie's QR code images (https://qrnegie.andrew.cmu.edu/).
7
+ # Only builds URLs; it never calls the service, which draws each code when
8
+ # it's requested and checks the options itself.
9
+ module QRnegie
10
+ # The longest text a code holds, in UTF-8 bytes, and the longest line of
11
+ # text above or below it, in characters
12
+ TEXT_LIMIT = 1024
13
+ LABEL_LIMIT = 40
14
+ MAX = 2048
15
+ FORMATS = %i[svg png].freeze
16
+
17
+ class << self
18
+ # The service the addresses point to; set it for a local copy
19
+ attr_writer :host
20
+
21
+ def host
22
+ @host || 'https://qrnegie.andrew.cmu.edu'
23
+ end
24
+
25
+ # QRnegie.url('https://www.cmu.edu/', style: :mosaic, width: 1024)
26
+ # QRnegie.url('https://www.cmu.edu/', below: 'Scan Me', format: :png)
27
+ # Left out, the style is Basic Scotty and the width 512 pixels.
28
+ def url(text, style: nil, width: nil, above: nil, below: nil, icon: nil, format: :svg)
29
+ check(text, width, above, below, format)
30
+ query = { text: text, style: style, width: width, above: above, below: below, icon: icon }
31
+ .reject { |_, value| value.nil? || value.to_s.strip.empty? }
32
+ "#{host.chomp('/')}/qr.#{format}?#{URI.encode_www_form(query)}"
33
+ end
34
+
35
+ private
36
+
37
+ def check(text, width, above, below, format)
38
+ if text.to_s.strip.empty? || text.to_s.bytesize > TEXT_LIMIT
39
+ raise ArgumentError, "QRnegie text must have 1 to #{TEXT_LIMIT} UTF-8 bytes"
40
+ end
41
+ if width && !(width.is_a?(Integer) && width.between?(1, MAX))
42
+ raise ArgumentError, "QRnegie widths are whole numbers up to #{MAX}, not #{width.inspect}"
43
+ end
44
+ if [above, below].any? { |line| line.to_s.length > LABEL_LIMIT }
45
+ raise ArgumentError, "Keep QRnegie text above or below the code to #{LABEL_LIMIT} characters"
46
+ end
47
+ return if FORMATS.include?(format&.to_sym)
48
+
49
+ raise ArgumentError, "QRnegie format must be one of #{FORMATS.join(', ')}"
50
+ end
51
+ end
52
+ end
53
+
54
+ require_relative 'qrnegie/helper'
55
+ require_relative 'qrnegie/railtie' if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,51 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmu-web-qrnegie
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.3.2
5
+ platform: ruby
6
+ authors:
7
+ - Carnegie Mellon University
8
+ bindir: bin
9
+ cert_chain: []
10
+ date: 1980-01-02 00:00:00.000000000 Z
11
+ dependencies: []
12
+ description: Builds addresses for QRnegie, which draws QR codes in Carnegie Mellon
13
+ styles as SVG or PNG. Includes a Rails view helper.
14
+ executables: []
15
+ extensions: []
16
+ extra_rdoc_files: []
17
+ files:
18
+ - LICENSE
19
+ - README.md
20
+ - VERSION
21
+ - lib/cmu-web-qrnegie.rb
22
+ - lib/qrnegie.rb
23
+ - lib/qrnegie/helper.rb
24
+ - lib/qrnegie/railtie.rb
25
+ - lib/qrnegie/version.rb
26
+ homepage: https://qrnegie.andrew.cmu.edu/
27
+ licenses:
28
+ - BSD-3-Clause
29
+ metadata:
30
+ source_code_uri: https://github.com/cmu-web/cmu-web-qrnegie
31
+ documentation_uri: https://github.com/cmu-web/cmu-web-qrnegie/tree/main/packages/ruby#readme
32
+ bug_tracker_uri: https://github.com/cmu-web/cmu-web-qrnegie/issues
33
+ rubygems_mfa_required: 'true'
34
+ rdoc_options: []
35
+ require_paths:
36
+ - lib
37
+ required_ruby_version: !ruby/object:Gem::Requirement
38
+ requirements:
39
+ - - ">="
40
+ - !ruby/object:Gem::Version
41
+ version: '3.2'
42
+ required_rubygems_version: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ requirements: []
48
+ rubygems_version: 3.6.9
49
+ specification_version: 4
50
+ summary: QR code image addresses from QRnegie, for Ruby and Rails
51
+ test_files: []