Class: USPSFlags::Generate
- Inherits:
-
Object
- Object
- USPSFlags::Generate
- Defined in:
- lib/usps_flags/generate.rb
Overview
Controller class for generating files.
Class Method Summary collapse
-
.all(svg: true, png: true, zips: true, reset: true) ⇒ Object
Generate all static SVG and PNG files, and automaticall generates zip archives for download.
-
.png(svg, outfile:, trim: false) ⇒ Object
Convert SVG data into a PNG file.
-
.spec(outfile: nil, fly: nil, unit: nil, scale: nil) ⇒ String
Generate trident spec sheet as an SVG image.
-
.svg(flag, outfile: nil, scale: nil, field: true) ⇒ String
The primary controller method.
-
.zips(svg: true, png: true) ⇒ Object
Generate zip archives of current static image files.
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.
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 |
# File 'lib/usps_flags/generate.rb', line 53 def all(svg: true, png: true, zips: true, reset: true) remove_static_files if reset puts "\nSVGs generate a single file.", "PNGs generate full-res, 1500w, 1000w, 500w, and thumbnail files.", "Corresponding rank insignia (including smaller sizes) are also generated, as appropriate." USPSFlags::Helpers.log "\nGeneration location: #{USPSFlags::Config.flags_dir}\n" USPSFlags::Helpers.log "\n#{Time.now.strftime('%Y%m%d.%H%M%S%z')} – Generating static files...\n\n" USPSFlags::Helpers.log "Flag | SVG | PNG | Run time\n".rjust(USPSFlags::Helpers.max_flag_name_length+31), "\n".rjust(USPSFlags::Helpers.max_flag_name_length+32, "-") overall_start_time = Time.now USPSFlags::Helpers.valid_flags(:all).each do |flag| generate_static_images_for(flag, svg: svg, png: png) end zips(svg: svg, png: png) if zips USPSFlags::Helpers.log "\nTotal run time: #{Time.now - overall_start_time} s\n\n" nil end |
.png(svg, outfile:, trim: false) ⇒ Object
Convert SVG data into a PNG file.
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/usps_flags/generate.rb', line 31 def png(svg, outfile:, trim: false) temp_svg = ::File.new("temp.svg", "w+") temp_svg.write(svg) temp_svg.flush 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) if ::File.exist?(temp_svg) end |
.spec(outfile: nil, fly: nil, unit: nil, scale: nil) ⇒ String
Generate trident spec sheet as an SVG image.
112 113 114 115 116 117 118 119 120 |
# File 'lib/usps_flags/generate.rb', line 112 def spec(outfile: nil, fly: nil, unit: nil, scale: nil) fly = fly.nil? ? USPSFlags::Config::BASE_FLY : fly svg = "" svg << USPSFlags::Core.headers(scale: scale, title: "USPS Trident Specifications") svg << USPSFlags::Core.trident_spec(fly: fly, unit: unit) svg << USPSFlags::Core. 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.
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.
81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 |
# File 'lib/usps_flags/generate.rb', line 81 def zips(svg: true, png: true) ["svg", "png"].each do |format| begin if binding.local_variable_get(format) zip = "#{USPSFlags::Config.flags_dir}/ZIP/USPS_Flags.#{format}.zip" ::File.delete(zip) if ::File.exist?(zip) Zip::File.open(zip, Zip::File::CREATE) do |z| Dir.glob("#{USPSFlags::Config.flags_dir}/#{format.upcase}/**/*").each do |f| if f.split("/").last(2).first == "insignia" filename = "insignia/#{f.split("/").last}" z.add(filename, f) else z.add(f.split("/").last, f) end end end puts "Generated #{format.upcase} Zip" end rescue Errno::EACCES => e puts "Error: Failed to generate #{format.upcase} Zip -> #{e.}" end end end |