cmu-web-placemellon 2.0.0

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: 6e524c128624c27b1e5805e14478ac22e7a8befb682d9a26b0565d5497819f71
4
+ data.tar.gz: 447af951970cf67d4791c04fb3b92985029107cc2e7b081eba86387d778ec388
5
+ SHA512:
6
+ metadata.gz: 13e354c272df2e3ecd6a15443a1995cd7e177686c21b5c0cc1102aea482f15011fba2337ea3e14934c75e3a5ef7f6f1b0764ca930cee4c1465a69d0c9f13c598
7
+ data.tar.gz: 104cb80e4b703698ffd7197b88d5650eb4de5d6c2b51c115a5da8ce8d76849a424bfad86923212d7a997947c84e58e6af17759f24ebcdd0f48d191c68ebaeea6
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,79 @@
1
+ # PlaceMellon for Ruby and Rails
2
+
3
+ Placeholder images from [PlaceMellon](https://placemellon.andrew.cmu.edu/)
4
+ in Ruby code and Rails views: campus photos, Scotty, profile pictures, bio
5
+ placeholders and solid CMU colors, at any size. The gem only builds image
6
+ addresses. It never calls PlaceMellon itself, so it adds nothing to your
7
+ app's requests or tests.
8
+
9
+ The gem lives in the PlaceMellon repository's `packages/ruby/`. Each
10
+ PlaceMellon release publishes it at the same version.
11
+
12
+ ## Install
13
+
14
+ Add it to your `Gemfile`, usually in the development group:
15
+
16
+ ```ruby
17
+ gem 'cmu-web-placemellon', '~> 1.3'
18
+ ```
19
+
20
+ ## In Rails Views
21
+
22
+ Rails views get `placemellon_tag`. It takes a width, an optional height
23
+ (leave it out for a square), alt text and PlaceMellon's options; anything
24
+ else goes to `image_tag`:
25
+
26
+ ```erb
27
+ <%= placemellon_tag 400, 300, alt: 'A campus photo', category: :campus %>
28
+ <%= placemellon_tag 96, alt: 'Scotty', scotty: true, format: :png, class: 'avatar' %>
29
+ <%= placemellon_tag 640, 360, alt: 'A placeholder', solid: true, text: true, color: 'carnegie-red' %>
30
+ ```
31
+
32
+ Alt text is required.
33
+
34
+ ## In Ruby
35
+
36
+ ```ruby
37
+ require 'cmu-web-placemellon'
38
+
39
+ PlaceMellon.url(400, 300) # a photo
40
+ PlaceMellon.url(400, 300, category: :labs) # from one category
41
+ PlaceMellon.url(400, 300, random: true) # a different photo each time
42
+ PlaceMellon.url(400, 300, format: :webp) # a smaller WebP photo
43
+ PlaceMellon.url(120, profile: true) # a default profile picture
44
+ ```
45
+
46
+ ## Options
47
+
48
+ | Option | Gives |
49
+ | --------------- | ------------------------------------------------------------------- |
50
+ | `category:` | Photos from one category: `campus`, `classrooms`, `labs` or `life` |
51
+ | `random: true` | A different photo on each request |
52
+ | `gray: true` | A grayscale photo |
53
+ | `scotty: true` | Scotty instead of a photo; add `gradient: true` for a gray gradient |
54
+ | `profile: true` | A default profile picture |
55
+ | `bio: true` | A bio placeholder |
56
+ | `solid: true` | A solid color; add `color:` (a CMU brand color) and `text: true` |
57
+ | `format:` | `:jpg` or `:webp` for photos, `:svg` or `:png` for the others |
58
+
59
+ Sizes are whole numbers from 1 to 2048. The gem checks sizes, option names
60
+ and formats; PlaceMellon checks the rest and explains any address it can't
61
+ draw. [PlaceMellon's site](https://placemellon.andrew.cmu.edu/) lists every
62
+ category and color.
63
+
64
+ To point at a local copy of PlaceMellon, set the host once:
65
+
66
+ ```ruby
67
+ PlaceMellon.host = 'http://127.0.0.1:3000'
68
+ ```
69
+
70
+ ## Work on the Gem
71
+
72
+ ```bash
73
+ cd packages/ruby
74
+ bundle install
75
+ bundle exec rake test build
76
+ ```
77
+
78
+ `rake version` copies the app's license and its version from
79
+ `package.json` into the gem; `rake clean` removes them and the build.
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 2.0.0
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'place_mellon'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PlaceMellon
4
+ # A view helper for Rails, included in every view by the Railtie
5
+ module Helper
6
+ # <%= placemellon_tag 400, 300, alt: 'Campus', category: :campus %>
7
+ # Takes PlaceMellon's options, and image_tag's for everything else.
8
+ # Alt text is required.
9
+ def placemellon_tag(width, height = nil, alt:, **options)
10
+ image = options.slice(:format, *VALUES, *FLAGS)
11
+ html = options.except(*image.keys)
12
+ image_tag(PlaceMellon.url(width, height, **image), alt: alt, width: width, height: height || width, **html)
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,10 @@
1
+ # frozen_string_literal: true
2
+
3
+ module PlaceMellon
4
+ # Makes placemellon_tag available in Rails views
5
+ class Railtie < ::Rails::Railtie
6
+ initializer 'place_mellon.helper' do
7
+ ActiveSupport.on_load(:action_view) { include PlaceMellon::Helper }
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,14 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+
5
+ module PlaceMellon
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
@@ -0,0 +1,59 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'uri'
4
+ require_relative 'place_mellon/version'
5
+
6
+ # Addresses for PlaceMellon's placeholder images
7
+ # (https://placemellon.andrew.cmu.edu/). Only builds URLs; it never calls the
8
+ # service, which checks the options itself.
9
+ module PlaceMellon
10
+ # The largest width or height the service draws
11
+ MAX = 2048
12
+ # Options with a value: a photo category, or a solid color
13
+ VALUES = %i[category color].freeze
14
+ # Options that are on or off
15
+ FLAGS = %i[random scotty profile bio solid text gradient gray].freeze
16
+ FORMATS = %i[jpg webp svg png].freeze
17
+
18
+ class << self
19
+ # The service the addresses point to; set it for a local copy
20
+ attr_writer :host
21
+
22
+ def host
23
+ @host || 'https://placemellon.andrew.cmu.edu'
24
+ end
25
+
26
+ # PlaceMellon.url(400, 300, category: :campus)
27
+ # PlaceMellon.url(96, scotty: true, format: :png)
28
+ # A height left out makes a square.
29
+ def url(width, height = nil, format: nil, **options)
30
+ sizes = [width, height].compact.map { |size| check_size(size) }
31
+ unknown = options.keys - VALUES - FLAGS
32
+ raise ArgumentError, "Unknown PlaceMellon option: #{unknown.first}" if unknown.any?
33
+ if format && !FORMATS.include?(format.to_sym)
34
+ raise ArgumentError, "PlaceMellon format must be one of #{FORMATS.join(', ')}"
35
+ end
36
+
37
+ sizes[-1] = "#{sizes[-1]}.#{format}" if format
38
+ query = options.filter_map do |key, value|
39
+ if VALUES.include?(key)
40
+ "#{key}=#{URI.encode_www_form_component(value.to_s)}" unless value.nil?
41
+ elsif value
42
+ key.to_s
43
+ end
44
+ end
45
+ "#{host.chomp('/')}/#{sizes.join('/')}#{"?#{query.join('&')}" if query.any?}"
46
+ end
47
+
48
+ private
49
+
50
+ def check_size(size)
51
+ return size if size.is_a?(Integer) && size.between?(1, MAX)
52
+
53
+ raise ArgumentError, "PlaceMellon sizes are whole numbers from 1 to #{MAX}, not #{size.inspect}"
54
+ end
55
+ end
56
+ end
57
+
58
+ require_relative 'place_mellon/helper'
59
+ require_relative 'place_mellon/railtie' if defined?(Rails::Railtie)
metadata ADDED
@@ -0,0 +1,52 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: cmu-web-placemellon
3
+ version: !ruby/object:Gem::Version
4
+ version: 2.0.0
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 PlaceMellon, the Carnegie Mellon placeholder image
13
+ service: campus photos, Scotty, profile pictures and solid colors at any size. Includes
14
+ a Rails view helper.'
15
+ executables: []
16
+ extensions: []
17
+ extra_rdoc_files: []
18
+ files:
19
+ - LICENSE
20
+ - README.md
21
+ - VERSION
22
+ - lib/cmu-web-placemellon.rb
23
+ - lib/place_mellon.rb
24
+ - lib/place_mellon/helper.rb
25
+ - lib/place_mellon/railtie.rb
26
+ - lib/place_mellon/version.rb
27
+ homepage: https://placemellon.andrew.cmu.edu/
28
+ licenses:
29
+ - BSD-3-Clause
30
+ metadata:
31
+ source_code_uri: https://github.com/cmu-web/cmu-web-placemellon
32
+ documentation_uri: https://github.com/cmu-web/cmu-web-placemellon/tree/main/packages/ruby#readme
33
+ bug_tracker_uri: https://github.com/cmu-web/cmu-web-placemellon/issues
34
+ rubygems_mfa_required: 'true'
35
+ rdoc_options: []
36
+ require_paths:
37
+ - lib
38
+ required_ruby_version: !ruby/object:Gem::Requirement
39
+ requirements:
40
+ - - ">="
41
+ - !ruby/object:Gem::Version
42
+ version: '3.2'
43
+ required_rubygems_version: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ requirements: []
49
+ rubygems_version: 3.6.9
50
+ specification_version: 4
51
+ summary: Placeholder image addresses from PlaceMellon, for Ruby and Rails
52
+ test_files: []