Class: USPSFlags::Generate

Inherits:
Object
  • Object
show all
Defined in:
lib/usps_flags/generate.rb

Overview

Controller class for generating files.

Since:

  • 0.1.5

Class Method Summary collapse

Class Method Details

.all(svg: true, png: true, zips: true, reset: true) ⇒ Object

Generate all static SVG and PNG files, and automaticall generates zip archives for download.

Parameters:

  • svg (Boolean) (defaults to: true)

    Whether to generate SVG images.

  • png (Boolean) (defaults to: true)

    Whether to generate PNG images.

  • zips (Boolean) (defaults to: true)

    Whether to create zip archives for all images created. Does not create a zip for skipped formats.

  • reset (Boolean) (defaults to: true)

    Whether to delete all previous files before generating new files.

Since:

  • 0.1.5



50
51
52
53
54
55
56
57
58
# File 'lib/usps_flags/generate.rb', line 50

def all(svg: true, png: true, zips: true, reset: true)
  all_arg_error unless any_all_arg?(svg, png, zips, reset)

  track_time do
    remove_static_files if reset
    images(svg: svg, png: png) if svg || png
    zips(svg: svg, png: png) if zips
  end
end

.images(svg: true, png: true) ⇒ Object

Generate static image files.

Parameters:

  • svg (Boolean) (defaults to: true)

    Generate static SVG images.

  • png (Boolean) (defaults to: true)

    Generate static PNG images.

Since:

  • 0.1.5



80
81
82
83
84
85
# File 'lib/usps_flags/generate.rb', line 80

def images(svg: true, png: true)
  static_generation_header
  USPSFlags::Helpers.valid_flags(:all).each do |flag|
    generate_static_images_for(flag, svg: svg, png: png)
  end
end

.png(svg, outfile: nil, trim: false, background: 'none') ⇒ Object

Convert SVG data into a PNG file.

Parameters:

  • svg (String)

    The SVG data.

  • outfile (String) (defaults to: nil)

    The path to save the PNG file to. (Required because the file is not accessible if this is left blank.)

  • trim (Boolean) (defaults to: false)

    Whether to trim the generated PNG file of excess transparency.

  • background (String) (defaults to: 'none')

    Background color. Defaults to ‘none’ (transparent).

Since:

  • 0.1.5



32
33
34
35
36
37
38
39
40
41
42
# File 'lib/usps_flags/generate.rb', line 32

def png(svg, outfile: nil, trim: false, background: 'none')
  raise USPSFlags::Errors::PNGGenerationError.new(svg: svg) if outfile.nil? || outfile.empty?

  set_temp_svg(svg)

  USPSFlags::Helpers.ensure_dir_for_file(outfile)

  generate_png(background, trim, outfile)
ensure
  ::File.delete(@temp_svg_path) if delete_temp_svg?
end

.spec(outfile: nil, fly: USPSFlags::Config::BASE_FLY, unit: nil, scale: nil, scaled_border: false) ⇒ String

Generate trident spec sheet as an SVG image.

Parameters:

  • outfile (String) (defaults to: nil)

    The path to save the SVG file to. If not set, prints to console.

  • fly (Integer) (defaults to: USPSFlags::Config::BASE_FLY)

    The nominal fly length of an appropriate flag field for the generated tridents. Size labels scale to this size.

  • outfile (String) (defaults to: nil)

    The unit to append to all trident measurements.

  • scale (String) (defaults to: nil)

    The image scale divisor factor.

Returns:

  • (String)

    Returns the SVG data.

Since:

  • 0.1.5



94
95
96
97
98
99
100
101
# File 'lib/usps_flags/generate.rb', line 94

def spec(outfile: nil, fly: USPSFlags::Config::BASE_FLY, unit: nil, scale: nil, scaled_border: false)
  svg = +''
  svg << USPSFlags::Core.headers(scale: scale, title: 'USPS Trident Specifications')
  svg << USPSFlags::Core.trident_spec(fly: fly, unit: unit, scaled_border: scaled_border)
  svg << USPSFlags::Core.footer

  USPSFlags::Helpers.output(svg, outfile: outfile)
end

.svg(flag, outfile: nil, scale: nil, field: true) ⇒ String

The primary controller method. Generates an SVG file or SVG data.

Parameters:

  • flag (String)

    The flag type to generate.

  • outfile (String) (defaults to: nil)

    The path to save the SVG file to. If not set, prints to console.

  • field (Boolean) (defaults to: true)

    Whether to generate the flag field (including any border).

  • scale (String) (defaults to: nil)

    The image scale divisor factor.

Returns:

  • (String)

    Returns the SVG data.

Since:

  • 0.1.5



16
17
18
19
20
21
22
23
24
# File 'lib/usps_flags/generate.rb', line 16

def svg(flag, outfile: nil, scale: nil, field: true)
  flag = flag.upcase.delete('/', '_', 'PENNANT')

  USPSFlags::Helpers.ensure_dir_for_file(outfile)

  output = special_flag(flag, outfile, scale)
  output ||= USPSFlags::Generate::Flag.officer(rank: flag, outfile: outfile, scale: scale, field: field)
  output
end

.zips(svg: true, png: true) ⇒ Object

Generate zip archives of current static image files.

Parameters:

  • svg (Boolean) (defaults to: true)

    Generate zip archive of SVG images.

  • png (Boolean) (defaults to: true)

    Generate zip archive of PNG images.

Since:

  • 0.1.5



64
65
66
67
68
69
70
71
72
73
74
# File 'lib/usps_flags/generate.rb', line 64

def zips(svg: true, png: true)
  unless svg || png
    raise(
      USPSFlags::Errors::ZipGenerationError,
      'At least one argument switch must be true out of [svg, png].'
    )
  end

  generate_zip('svg') if svg
  generate_zip('png') if png
end