-
# frozen_string_literal: true
-
-
1
require 'victor'
-
1
require 'repo_small_badge/configuration'
-
-
1
module RepoSmallBadge
-
# Class that creates the Badge SVG image.
-
# Behaviour is defined by the configuration hash.
-
# For the different configurables see RepoSmallBadge::Configuration.
-
1
class Image < Victor::SVGBase
-
# Create new instance.
-
# @config is a Hash of configurables. Keys are symbols.
-
1
def initialize(config = {})
-
4
@config = Configuration.new(config)
-
4
super(template: :html, contentScriptType: 'text/ecmascript',
-
contentStyleType: 'text/css', preserveAspectRatio: 'xMidYMid meet',
-
version: '1.0',
-
height: @config.badge_height, width: @config.badge_width)
-
end
-
-
# Creates the badge.
-
# @name the suffix for the filename (badge_#{name})
-
# @title the title of the badge.
-
# @value is the overall value to be written to the right side of the badge.
-
1
def badge(name, title, value)
-
3
svg_header
-
3
svg_rounded_box
-
3
svg_boxes
-
3
svg_title(title)
-
3
svg_value(value)
-
3
save(@config.filename(name))
-
end
-
-
# Updates the configuration settings and overwrites existing ones.
-
# @config the hash that has to be merged.
-
1
def config_merge(config)
-
1
@config.merge!(config)
-
end
-
-
1
private
-
-
1
def svg_header
-
3
element :linearGradient, id: 'smooth', x2: '0', y2: @config.badge_width do
-
3
element :stop, offset: '0', 'stop-color': '#bbb', 'stop-opacity': '.1'
-
3
element :stop, offset: '1', 'stop-opacity': '.1'
-
end
-
end
-
-
# rubocop:disable Metrics/MethodLength
-
1
def svg_boxes
-
3
element :g, 'clip-path' => 'url(#round)' do
-
3
element :rect, height: @config.badge_height,
-
width: @config.badge_middle,
-
fill: @config.title_background
-
3
element :rect, x: @config.badge_middle,
-
height: @config.badge_height,
-
width: @config.badge_middle,
-
fill: @config.value_background
-
3
element :rect, height: @config.badge_height,
-
width: @config.badge_width, fill: 'url(#smooth)'
-
end
-
# rubocop:enable Metrics/MethodLength
-
end
-
-
1
def svg_rounded_box
-
3
element :clipPath, id: 'round' do
-
3
element :rect, height: @config.badge_height, width: @config.badge_width,
-
rx: @config.rounded_edge_radius, fill: @config.background
-
end
-
end
-
-
1
def svg_title(title)
-
3
element :g, fill: @config.title_color, 'text-anchor': 'middle',
-
'font-family': @config.title_font,
-
'font-size': @config.title_font_size do |_svg|
-
3
element :text, @config.title(title),
-
x: @config.title_middle, y: @config.badge_height - 5,
-
fill: '#010101', 'fill-opacity': '0.3'
-
3
element :text, @config.title(title),
-
x: @config.title_middle, y: @config.badge_height - 6
-
end
-
end
-
-
# rubocop:disable Metrics/MethodLength
-
1
def svg_value(value)
-
3
element :g, fill: @config.value_color, 'text-anchor': 'middle',
-
'font-family': @config.value_font,
-
'font-size': @config.value_font_size do |_svg|
-
3
element :text, value,
-
x: @config.value_middle,
-
y: @config.badge_height - 5,
-
fill: '#010101', 'fill-opacity': '0.3'
-
3
element :text, value,
-
x: @config.value_middle,
-
y: @config.badge_height - 6
-
end
-
end
-
# rubocop:enable Metrics/MethodLength
-
end
-
end