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.

Raises:

  • (USPSFlags::Errors::StaticFilesGenerationError)

Since:

  • 0.1.5



56
57
58
59
60
61
62
63
64
# File 'lib/usps_flags/generate.rb', line 56

def all(svg: true, png: true, zips: true, reset: true)
  raise USPSFlags::Errors::StaticFilesGenerationError, "At least one argument switch must be true out of [svg, png, zips, reset]." unless svg || png || zips || reset

  overall_start_time = Time.now
  remove_static_files if reset
  images(svg: svg, png: png) if svg || png
  zips(svg: svg, png: png) if zips
  USPSFlags::Helpers.log "\nTotal run time: #{Time.now - overall_start_time} s\n\n"
end

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

Generate static image files.

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) ⇒ Object

Convert SVG data into a PNG file.

Since:

  • 0.1.5



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/usps_flags/generate.rb', line 31

def png(svg, outfile: nil, trim: false)
  temp_svg_file = "temp.svg"
  temp_svg = ::File.new(temp_svg_file, "w+")
  temp_svg.write(svg)
  temp_svg.flush

  raise USPSFlags::Errors::PNGGenerationError, svg: svg if outfile.nil? || outfile.empty?

  MiniMagick::Tool::Convert.new do |convert|
    convert << "-background" << "none"
    convert << "-format" << "png"
    convert << "-trim" if trim
    convert << temp_svg.path
    convert << outfile
  end
ensure
  ::File.delete(temp_svg_file) if ::File.exist?(temp_svg_file)
end

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

Generate trident spec sheet as an SVG image.

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)
  svg = ""
  svg << USPSFlags::Core.headers(scale: scale, title: "USPS Trident Specifications")
  svg << USPSFlags::Core.trident_spec(fly: fly, unit: unit)
  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.

Since:

  • 0.1.5



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

def svg(flag, outfile: nil, scale: nil, field: true)
  flag = flag.upcase.delete("/", "_", "PENNANT")
  if ["CRUISE", "OIC"].include?(flag)
    USPSFlags::Generate::Flag.pennant(type: flag, outfile: outfile, scale: scale)
  elsif flag == "ENSIGN"
    USPSFlags::Generate::Flag.ensign(outfile: outfile, scale: scale)
  elsif flag == "US"
    USPSFlags::Generate::Flag.us(outfile: outfile, scale: scale)
  elsif flag == "WHEEL"
    USPSFlags::Generate::Flag.wheel(outfile: outfile, scale: scale)
  else
    USPSFlags::Generate::Flag.officer(rank: flag, outfile: outfile, scale: scale, field: field)
  end
end

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

Generate zip archives of current static image files.

Raises:

  • (USPSFlags::Errors::ZipGenerationError)

Since:

  • 0.1.5



70
71
72
73
74
# File 'lib/usps_flags/generate.rb', line 70

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