usps_flags 0.2.6 → 0.3.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 230cbb0c6890872f99ec67e5a5db72eb528b84bc
4
- data.tar.gz: 54251932a8c574ebfa8efea790086eeec8cc5459
3
+ metadata.gz: 9fdff1aa8bd6137e6cf924483f5e56febb4cb5cc
4
+ data.tar.gz: 9a497de8ac453be04f0b4ca8620b67048f68a922
5
5
  SHA512:
6
- metadata.gz: 57d576e35c377d8527988d9fd51628712d6a3eaf8db4c9c137f4e657e61fb174f11c1868d407ab2dffce080ccdde5631e586e1f71e6b0fc610984ee8cf42ebaa
7
- data.tar.gz: 80160eaa9c095e3a3af76d0c27550183b081c9599140c43b0a8c378e99359d96f65c1f6c32554238cbdd60e09908d5c57fe5635c81a0da6938c752fc0c54ff3d
6
+ metadata.gz: 8c7cff7cdd70760dae34da50fc1b217a721da2bf02dce238863641303f3c40a5e26601fdcb60e213fff2237ed1b1b9aafcb8db5cbadd88542bda7ab6ef4d3119
7
+ data.tar.gz: 4bfa4672d65cbb79f1c1042c205e0af15dd901856aabd1f7067782c47be356894766f73dca6f72258c7d800182cab624a15542c84416a82ca175b1d75241f59b
checksums.yaml.gz.sig CHANGED
Binary file
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- usps_flags (0.2.5)
4
+ usps_flags (0.2.6)
5
5
  file_utils (~> 1.1, >= 1.1.2)
6
6
  mini_magick (~> 4.8, >= 4.8.0)
7
7
  rubyzip (~> 1.2, >= 1.2.1)
@@ -0,0 +1,19 @@
1
+ module USPSFlags::Errors
2
+ class PNGGenerationError < StandardError
3
+ def initialize(msg = "There was an error generating the PNG file.", svg: "")
4
+ super(msg)
5
+ end
6
+ end
7
+
8
+ class StaticFilesGenerationError < StandardError
9
+ def initialize(msg = "There was an error generating the static files.", cause: nil)
10
+ super(msg)
11
+ end
12
+ end
13
+
14
+ class ZipGenerationError < StandardError
15
+ def initialize(msg = "There was an error generating the zip file.", type: nil, cause: nil)
16
+ super(msg)
17
+ end
18
+ end
19
+ end
@@ -28,11 +28,14 @@ class USPSFlags::Generate
28
28
  # @param [String] svg The SVG data.
29
29
  # @param [String] outfile The path to save the PNG file to. (Required because the file is not accessible if this is left blank.)
30
30
  # @param [Boolean] trim Whether to trim the generated PNG file of excess transparency.
31
- def png(svg, outfile:, trim: false)
32
- temp_svg = ::File.new("temp.svg", "w+")
31
+ def png(svg, outfile: nil, trim: false)
32
+ temp_svg_file = "temp.svg"
33
+ temp_svg = ::File.new(temp_svg_file, "w+")
33
34
  temp_svg.write(svg)
34
35
  temp_svg.flush
35
36
 
37
+ raise USPSFlags::Errors::PNGGenerationError, svg: svg if outfile.nil? || outfile.empty?
38
+
36
39
  MiniMagick::Tool::Convert.new do |convert|
37
40
  convert << "-background" << "none"
38
41
  convert << "-format" << "png"
@@ -41,7 +44,7 @@ class USPSFlags::Generate
41
44
  convert << outfile
42
45
  end
43
46
  ensure
44
- ::File.delete(temp_svg) if ::File.exist?(temp_svg)
47
+ ::File.delete(temp_svg_file) if ::File.exist?(temp_svg_file)
45
48
  end
46
49
 
47
50
  # Generate all static SVG and PNG files, and automaticall generates zip archives for download.
@@ -51,6 +54,8 @@ class USPSFlags::Generate
51
54
  # @param [Boolean] zips Whether to create zip archives for all images created. Does not create a zip for skipped formats.
52
55
  # @param [Boolean] reset Whether to delete all previous files before generating new files.
53
56
  def all(svg: true, png: true, zips: true, reset: true)
57
+ raise USPSFlags::Errors::StaticFilesGenerationError, "At least one argument switch must be true out of [svg, png, zips]." unless svg || png || zips
58
+
54
59
  remove_static_files if reset
55
60
 
56
61
  puts "\nSVGs generate a single file.",
@@ -72,6 +77,9 @@ class USPSFlags::Generate
72
77
  USPSFlags::Helpers.log "\nTotal run time: #{Time.now - overall_start_time} s\n\n"
73
78
 
74
79
  nil
80
+ rescue => e
81
+ raise e if e.is_a?(USPSFlags::Errors::StaticFilesGenerationError)
82
+ raise USPSFlags::Errors::StaticFilesGenerationError, cause: e
75
83
  end
76
84
 
77
85
  # Generate zip archives of current static image files.
@@ -79,17 +87,21 @@ class USPSFlags::Generate
79
87
  # @param [Boolean] svg Generate zip archive of SVG images.
80
88
  # @param [Boolean] png Generate zip archive of PNG images.
81
89
  def zips(svg: true, png: true)
90
+ raise USPSFlags::Errors::ZipGenerationError, "At least one argument switch must be true out of [svg, png]." unless svg || png
82
91
  begin
83
92
  generate_zip("svg") if svg
84
93
  rescue Errno::EACCES => e
85
- puts "Error: Failed to generate SVG Zip -> #{e.message}"
94
+ raise USPSFlags::Errors::ZipGenerationError, type: :svg, cause: e
86
95
  end
87
96
 
88
97
  begin
89
98
  generate_zip("png") if png
90
99
  rescue Errno::EACCES => e
91
- puts "Error: Failed to generate SVG Zip -> #{e.message}"
100
+ raise USPSFlags::Errors::ZipGenerationError, type: :png, cause: e
92
101
  end
102
+ rescue => e
103
+ raise e if e.is_a?(USPSFlags::Errors::ZipGenerationError)
104
+ raise USPSFlags::Errors::ZipGenerationError, cause: e
93
105
  end
94
106
 
95
107
  # Generate trident spec sheet as an SVG image.
@@ -111,7 +123,10 @@ class USPSFlags::Generate
111
123
 
112
124
  private
113
125
  def remove_static_files
114
- ["SVG", "PNG", "ZIP"].each { |dir| ::FileUtils.rm_rf(Dir.glob("#{USPSFlags::Config.flags_dir}/#{dir}/*")) }
126
+ ["SVG", "PNG", "ZIP"].each do |dir|
127
+ dir_path = "#{USPSFlags::Config.flags_dir}/#{dir}"
128
+ ::FileUtils.rm_rf(::Dir.glob("#{dir_path}/*")) if ::Dir.exist?(dir_path)
129
+ end
115
130
  ["SVG/insignia", "PNG/insignia"].each { |dir| ::FileUtils.mkdir_p("#{USPSFlags::Config.flags_dir}/#{dir}") }
116
131
  USPSFlags::Helpers.log "\n - Cleared previous files.\n"
117
132
  end
@@ -178,17 +193,13 @@ class USPSFlags::Generate
178
193
  svg flag, field: false, outfile: @svg_ins_file, scale: 1
179
194
  USPSFlags::Helpers.log "I"
180
195
  end
181
- rescue => e
182
- USPSFlags::Helpers.log "x -> #{e.message}"
183
196
  end
184
197
 
185
198
  def generate_static_png(flag)
186
199
  USPSFlags::Helpers.log " | "
187
200
  generate_fullsize_png
188
201
  generate_fullsize_png_insignia(flag)
189
- generate_reduced_size_pngs
190
- rescue => e
191
- USPSFlags::Helpers.log "x -> #{e.message}"
202
+ generate_reduced_size_pngs(flag)
192
203
  end
193
204
 
194
205
  def generate_fullsize_png
@@ -205,7 +216,7 @@ class USPSFlags::Generate
205
216
  end
206
217
  end
207
218
 
208
- def generate_reduced_size_pngs
219
+ def generate_reduced_size_pngs(flag)
209
220
  USPSFlags::Helpers.png_sizes.keys.each do |size|
210
221
  USPSFlags::Helpers.log(".") and next if ::File.exist?("#{USPSFlags::Config.flags_dir}/PNG/#{flag}.#{size}.png")
211
222
  size, size_key = USPSFlags::Helpers.size_and_key(size: size, flag: flag)
data/lib/usps_flags.rb CHANGED
@@ -12,6 +12,7 @@ class USPSFlags
12
12
  require 'usps_flags/helpers'
13
13
  require 'usps_flags/core'
14
14
  require 'usps_flags/generate'
15
+ require 'usps_flags/errors'
15
16
 
16
17
  # Dir['./lib/usps_flags/core/**'].map { |d| d.split("/").last.split(".rb").first }
17
18
  %w[anchor binoculars ensign field footer headers lighthouse pennant star trident tridents trident_spec trumpet us wheel].each do |d|
@@ -104,7 +105,7 @@ class USPSFlags
104
105
  #
105
106
  # @return [String] Returns the SVG file output path.
106
107
  def png
107
- raise "Error: png_file must be set." if self.png_file.nil?
108
+ raise USPSFlags::Errors::PNGGenerationError, "A path must be set with png_file." if self.png_file.nil?
108
109
  svg_file_storage = self.svg_file
109
110
  self.svg_file ""
110
111
  USPSFlags::Generate.png(self.svg, outfile: self.png_file, trim: self.trim)
data/spec/spec_helper.rb CHANGED
@@ -7,6 +7,23 @@ CodeClimate::TestReporter.start
7
7
 
8
8
  require 'usps_flags'
9
9
 
10
+ # The spec for USPSFlags and USPSFlags::Generate contain some examples that check for
11
+ # USPSFlags::Errors::PNGGenerationError
12
+ # USPSFlags::Errors::StaticFilesGenerationError
13
+ # USPSFlags::Errors::ZipGenerationError
14
+ RSpec::Expectations.configuration.on_potential_false_positives = :nothing
15
+
10
16
  RSpec.configure do |config|
11
- # some (optional) config here
17
+ config.before(:suite) do
18
+ $tmp_flags_dir = "tmp/flags"
19
+ $tmp_alt_flags_dir = "tmp/alt_flags"
20
+
21
+ USPSFlags::Config.new do |config|
22
+ config.flags_dir = $tmp_flags_dir
23
+ end
24
+ end
25
+
26
+ config.after(:suite) do
27
+ ::FileUtils.rm_rf("tmp") if ::Dir.exist?("tmp")
28
+ end
12
29
  end
@@ -3,14 +3,13 @@ require 'spec_helper'
3
3
  describe USPSFlags::Config do
4
4
  describe "flags_dir" do
5
5
  it "should return the current flags directory" do
6
- default_flags_dir = File.dirname(__dir__).gsub("/spec", "/lib") + "/output"
7
- expect(USPSFlags::Config.flags_dir).to eql(default_flags_dir)
6
+ expect(USPSFlags::Config.flags_dir).to eql($tmp_flags_dir)
8
7
  end
9
8
  end
10
9
 
11
10
  describe "log_path" do
12
11
  it "should return the current flags directory" do
13
- default_log_path = File.dirname(__dir__).gsub("/spec", "/lib") + "/output/log"
12
+ default_log_path = $tmp_flags_dir + "/log"
14
13
  expect(USPSFlags::Config.log_path).to eql(default_log_path)
15
14
  end
16
15
  end
@@ -35,12 +34,11 @@ describe USPSFlags::Config do
35
34
 
36
35
  describe "configuration constructor" do
37
36
  it "should return a properly constructed configuration" do
38
- test_flags_dir = "./path/to/flags/for/spec"
39
37
  @config = USPSFlags::Config.new do |config|
40
- config.flags_dir = test_flags_dir
38
+ config.flags_dir = $tmp_flags_dir
41
39
  end
42
40
 
43
- expect(@config.flags_dir).to eql(test_flags_dir)
41
+ expect(@config.flags_dir).to eql($tmp_flags_dir)
44
42
  expect(@config.reset).to eql(false)
45
43
  expect(@config.use_larger_tridents).to eql(true)
46
44
  expect(@config.log_fail_quietly).to eql(true)
@@ -1,32 +1,84 @@
1
1
  require 'spec_helper'
2
2
 
3
3
  describe USPSFlags::Generate do
4
- it "should generate a flag with the correct size" do
5
- expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include(
6
- "width=\"1024pt\" height=\"682pt\" viewBox=\"0 0 3072 2048\""
7
- )
8
- end
4
+ describe "general features" do
5
+ it "should generate a flag with the correct size" do
6
+ expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include(
7
+ "width=\"1024pt\" height=\"682pt\" viewBox=\"0 0 3072 2048\""
8
+ )
9
+ end
9
10
 
10
- it "should generate a flag with the correct field" do
11
- expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include(
12
- <<~SVG
13
- <path d="M 0 0
14
- l 3072 0
15
- l 0 2048
16
- l -3072 0
17
- l 0 -2048
18
- " fill="#BF0D3E" />
19
- SVG
20
- )
21
- end
11
+ it "should generate a flag with the correct field" do
12
+ expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include(
13
+ <<~SVG
14
+ <path d="M 0 0
15
+ l 3072 0
16
+ l 0 2048
17
+ l -3072 0
18
+ l 0 -2048
19
+ " fill="#BF0D3E" />
20
+ SVG
21
+ )
22
+ end
22
23
 
23
- it "should generate a flag with the correct starting position" do
24
- expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include("<path d=\"M 1536 512")
24
+ it "should generate a flag with the correct starting position" do
25
+ expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include("<path d=\"M 1536 512")
26
+ end
27
+
28
+ it "should generate a flag with the correct trident transformations" do
29
+ expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include("<g transform=\"translate(-512)\">")
30
+ expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include("<g transform=\"translate(512)\">")
31
+ end
25
32
  end
26
33
 
27
- it "should generate a flag with the correct trident transformations" do
28
- expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include("<g transform=\"translate(-512)\">")
29
- expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include("<g transform=\"translate(512)\">")
34
+ describe "line flags" do
35
+ it "should generate CC" do
36
+ expect(USPSFlags::Generate.svg("CC", outfile: "")).to include("<title>CC</title>")
37
+ end
38
+
39
+ it "should generate VC" do
40
+ expect(USPSFlags::Generate.svg("VC", outfile: "")).to include("<title>VC</title>")
41
+ end
42
+
43
+ it "should generate RC" do
44
+ expect(USPSFlags::Generate.svg("RC", outfile: "")).to include("<title>RC</title>")
45
+ end
46
+
47
+ it "should generate StfC" do
48
+ expect(USPSFlags::Generate.svg("StfC", outfile: "")).to include("<title>STFC</title>")
49
+ end
50
+
51
+ it "should generate DC" do
52
+ expect(USPSFlags::Generate.svg("DC", outfile: "")).to include("<title>DC</title>")
53
+ end
54
+
55
+ it "should generate DLtC" do
56
+ expect(USPSFlags::Generate.svg("DLtC", outfile: "")).to include("<title>DLTC</title>")
57
+ end
58
+
59
+ it "should generate D1Lt" do
60
+ expect(USPSFlags::Generate.svg("D1Lt", outfile: "")).to include("<title>D1LT</title>")
61
+ end
62
+
63
+ it "should generate DLt" do
64
+ expect(USPSFlags::Generate.svg("DLt", outfile: "")).to include("<title>DLT</title>")
65
+ end
66
+
67
+ it "should generate Cdr" do
68
+ expect(USPSFlags::Generate.svg("Cdr", outfile: "")).to include("<title>CDR</title>")
69
+ end
70
+
71
+ it "should generate LtC" do
72
+ expect(USPSFlags::Generate.svg("LtC", outfile: "")).to include("<title>LTC</title>")
73
+ end
74
+
75
+ it "should generate 1Lt" do
76
+ expect(USPSFlags::Generate.svg("1Lt", outfile: "")).to include("<title>1LT</title>")
77
+ end
78
+
79
+ it "should generate Lt" do
80
+ expect(USPSFlags::Generate.svg("Lt", outfile: "")).to include("<title>LT</title>")
81
+ end
30
82
  end
31
83
 
32
84
  describe "special flags" do
@@ -58,4 +110,66 @@ describe USPSFlags::Generate do
58
110
  expect(USPSFlags::Generate.svg("NFLt", outfile: "")).to include("<title>NFLT</title>")
59
111
  end
60
112
  end
113
+
114
+ describe "pennants" do
115
+ it "should generate the cruise pennant" do
116
+ expect(USPSFlags::Generate.svg("Cruise", outfile: "")).to include("<title>Cruise Pennant</title>")
117
+ end
118
+
119
+ it "should generate the officer-in-charge pennant" do
120
+ expect(USPSFlags::Generate.svg("OIC", outfile: "")).to include("<title>Officer-in-Charge Pennant</title>")
121
+ end
122
+ end
123
+
124
+ describe "other flags" do
125
+ it "should generate US" do
126
+ expect(USPSFlags::Generate.svg("US", outfile: "")).to include("<title>US Ensign</title>")
127
+ end
128
+
129
+ it "should generate USPS Ensign" do
130
+ expect(USPSFlags::Generate.svg("Ensign", outfile: "")).to include("<title>USPS Ensign</title>")
131
+ end
132
+
133
+ it "should generate the USPS Wheel logo" do
134
+ expect(USPSFlags::Generate.svg("Wheel", outfile: "")).to include("<title>USPS Ensign Wheel</title>")
135
+ end
136
+ end
137
+
138
+ describe "trident specifications" do
139
+ it "should generate the trident specification sheet" do
140
+ expect(USPSFlags::Generate.spec(outfile: "")).to include("<title>USPS Trident Specifications</title>")
141
+ end
142
+ end
143
+
144
+ describe "png" do
145
+ it "should raise PNGGenerationError without an outfile" do
146
+ expect { USPSFlags::Generate.png(USPSFlags::Generate.svg("LtC", outfile: ""), outfile: "") }.to raise_error(USPSFlags::Errors::PNGGenerationError)
147
+ end
148
+
149
+ it "should not raise PNGGenerationError when correctly configured" do
150
+ expect { USPSFlags::Generate.png(USPSFlags::Generate.svg("LtC", outfile: ""), outfile: "lib/output/PNG/LTC.png") }.to_not raise_error(USPSFlags::Errors::PNGGenerationError)
151
+ end
152
+ end
153
+
154
+ describe "static files" do
155
+ it "should raise USPSFlags::Errors::StaticFilesGenerationError when not given any true arguments" do
156
+ expect { USPSFlags::Generate.all(svg: false, png: false, zips: false) }.to raise_error(
157
+ USPSFlags::Errors::StaticFilesGenerationError, "At least one argument switch must be true out of [svg, png, zips]."
158
+ )
159
+ end
160
+
161
+ it "should not raise StaticFilesGenerationError while generating all static files" do
162
+ expect { USPSFlags::Generate.all }.to_not raise_error(USPSFlags::Errors::StaticFilesGenerationError)
163
+ end
164
+
165
+ it "should raise USPSFlags::Errors::ZipGenerationError when not given any true arguments" do
166
+ expect { USPSFlags::Generate.zips(svg: false, png: false) }.to raise_error(
167
+ USPSFlags::Errors::ZipGenerationError, "At least one argument switch must be true out of [svg, png]."
168
+ )
169
+ end
170
+
171
+ it "should not raise ZipGenerationError while generating zip files" do
172
+ expect { USPSFlags::Generate.zips }.to_not raise_error(USPSFlags::Errors::ZipGenerationError)
173
+ end
174
+ end
61
175
  end
@@ -12,5 +12,13 @@ describe USPSFlags::Helpers do
12
12
  DFLT D1LT DLTC DC PSTFC PRC PVC PCC NAIDE NFLT STFC RC VC CC
13
13
  ].sort)
14
14
  end
15
+
16
+ it "should correctly generate the grid helper" do
17
+ expect(USPSFlags::Helpers::Builders.grid).to include("<circle cx=\"0\" cy=\"0\" r=\"#{USPSFlags::Config::BASE_FLY/60}\" fill=\"#000000\" fill-opacity=\"0.4\" />")
18
+ end
19
+
20
+ it "should correctly generate the locator helper" do
21
+ expect(USPSFlags::Helpers::Builders.locator).to include("<rect x=\"0\" y=\"0\" width=\"#{USPSFlags::Config::BASE_FLY/30}\" height=\"#{USPSFlags::Config::BASE_FLY/30}\" fill=\"#333333\" fill-opacity=\"0.6\" />")
22
+ end
15
23
  end
16
24
  end
@@ -114,14 +114,14 @@ describe USPSFlags do
114
114
  expect(@flag.trim).to eql(true)
115
115
  end
116
116
 
117
- it "should update svg_file" do
118
- @flag.svg_file = "/path/to/svg/output.svg"
119
- expect(@flag.svg_file).to eql("/path/to/svg/output.svg")
117
+ it "should correctly update svg_file" do
118
+ @flag.svg_file = "#{$tmp_alt_flags_dir}/SVG/output.svg"
119
+ expect(@flag.svg_file).to eql("#{$tmp_alt_flags_dir}/SVG/output.svg")
120
120
  end
121
121
 
122
- it "should update png_file" do
123
- @flag.png_file = "/path/to/png/output.png"
124
- expect(@flag.png_file).to eql("/path/to/png/output.png")
122
+ it "should correctly update png_file" do
123
+ @flag.png_file = "#{$tmp_alt_flags_dir}/PNG/output.png"
124
+ expect(@flag.png_file).to eql("#{$tmp_alt_flags_dir}/PNG/output.png")
125
125
  end
126
126
 
127
127
  describe "as configured" do
@@ -138,6 +138,17 @@ describe USPSFlags do
138
138
  it "should construct and generate a flag with a valid body" do
139
139
  expect(@flag.svg).to include(@valid_body)
140
140
  end
141
+
142
+ describe "png" do
143
+ it "should raise PNGGenerationError without png_file set" do
144
+ expect { @flag.png }.to raise_error(USPSFlags::Errors::PNGGenerationError, "A path must be set with png_file.")
145
+ end
146
+
147
+ it "should not raise PNGGenerationError with png_file set" do
148
+ @flag.png_file = "#{$tmp_alt_flags_dir}/PNG/LtC.png"
149
+ expect { @flag.png }.to_not raise_error(USPSFlags::Errors::PNGGenerationError)
150
+ end
151
+ end
141
152
  end
142
153
  end
143
154
  end
data/usps_flags.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'usps_flags'
3
- s.version = '0.2.6'
3
+ s.version = '0.3.0'
4
4
  s.date = '2017-11-06'
5
5
  s.summary = 'Flag generator for United States Power Squadrons'
6
6
  s.description = 'A flag image (PNG, SVG) generator for United States Power Squadrons.'
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: usps_flags
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.6
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander
@@ -213,6 +213,7 @@ files:
213
213
  - lib/usps_flags/core/trumpet.rb
214
214
  - lib/usps_flags/core/us.rb
215
215
  - lib/usps_flags/core/wheel.rb
216
+ - lib/usps_flags/errors.rb
216
217
  - lib/usps_flags/generate.rb
217
218
  - lib/usps_flags/generate/flag.rb
218
219
  - lib/usps_flags/helpers.rb
metadata.gz.sig CHANGED
Binary file