Class: FTF::Generator
- Inherits:
-
Object
- Object
- FTF::Generator
- Defined in:
- lib/ftf/generator.rb
Overview
Generate
Class Method Summary collapse
Instance Method Summary collapse
- #filename ⇒ Object
-
#generate! ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
-
#holder(holder) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
-
#initialize(data) ⇒ Generator
constructor
A new instance of Generator.
- #parse(data) ⇒ Object
- #product_footer(product) ⇒ Object
-
#relationship(relationship) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
-
#validate! ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity.
- #write_record(record) ⇒ Object
Constructor Details
#initialize(data) ⇒ Generator
Returns a new instance of Generator.
9 10 11 12 13 |
# File 'lib/ftf/generator.rb', line 9 def initialize(data) @report = Mappings::Report.new(data) @record_index = 1 validate! end |
Class Method Details
.generate!(data) ⇒ Object
114 115 116 |
# File 'lib/ftf/generator.rb', line 114 def self.generate!(data) new(data).generate! end |
Instance Method Details
#filename ⇒ Object
110 111 112 |
# File 'lib/ftf/generator.rb', line 110 def filename "FTF_#{@report.fiscal_id}_#{@report.period}_#{@report.index}.txt" end |
#generate! ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/ftf/generator.rb', line 41 def generate! # rubocop:disable Metrics/AbcSize,Metrics/MethodLength @output = File.new(filename, "w", external_encoding: Encoding::ISO8859_1) write_record(@report.render_header(@record_index)) @report.products.each do |product| write_record(product.render(@record_index)) product.holders.each do |holder| write_record(holder(holder)) write_record(relationship(holder.relationship)) end write_record((product)) end write_record(@report.(@record_index)) @output.close end |
#holder(holder) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 |
# File 'lib/ftf/generator.rb', line 64 def holder(holder) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength fields = { record_type: "20", action: holder.action, fiscal_id: @report.fiscal_id, record_index: @record_index.to_s.rjust(10, "0"), name: holder.name.to_s.ljust(178, " "), id_type: holder.id_type.to_s.rjust(2, "0"), id: holder.id.to_s.ljust(30, " "), expedition_country: holder.country.to_s.ljust(2, " "), created_at: holder.created_at.ljust(8, " "), nationality_country: holder.country.to_s.ljust(2, " "), residence_country: holder.country.to_s.ljust(2, " "), reserved_field: " " * 4 } fields.values.join end |
#parse(data) ⇒ Object
30 31 32 33 34 35 36 37 38 39 |
# File 'lib/ftf/generator.rb', line 30 def parse(data) case data when Hash data when String JSON.parse(data, symbolize_names: true) else raise "Please provide a JSON string to parse or a symbol-keyed hash" end end |
#product_footer(product) ⇒ Object
97 98 99 100 101 102 103 104 105 106 107 108 |
# File 'lib/ftf/generator.rb', line 97 def (product) fields = { record_type: "19", fiscal_id: @report.fiscal_id, record_index: @record_index.to_s.rjust(10, "0"), type: product.type.to_s.rjust(2, "0"), id_type: product.id_type.to_s.rjust(2, "0"), id: product.id.to_s.ljust(38, " "), reserved_field: " " * 187 } fields.values.join end |
#relationship(relationship) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
# File 'lib/ftf/generator.rb', line 82 def relationship(relationship) # rubocop:disable Metrics/AbcSize,Metrics/MethodLength fields = { record_type: "30", action: relationship.action, fiscal_id: @report.fiscal_id, record_index: @record_index.to_s.rjust(10, "0"), type: relationship.type.to_s.ljust(3, " "), created_at: relationship.created_at.ljust(8, " "), canceled_at: relationship.canceled_at.ljust(8, " "), previous_type: relationship.previous_type.to_s.ljust(3, " "), reserved_field: " " * 206 } fields.values.join end |
#validate! ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/ftf/generator.rb', line 15 def validate! # rubocop:disable Metrics/AbcSize,Metrics/CyclomaticComplexity,Metrics/MethodLength,Metrics/PerceivedComplexity if !@report.request_id.blank? && @report.file_type != ("C") raise "Files generated for a request id must be complementary (type C)" end if @report.products.empty? && @report.file_type != ("N") raise "Files generated without product records must be negative (type N)" end if !@report.products.empty? && !%w[M C].include?(@report.file_type) raise "Files generated with product records must be monthly or complementary (type M or C)" end raise "Please include your fiscal_id in the data" if @report.fiscal_id.blank? raise "Please include your period in the data" if @report.period.blank? raise "Please include your file_type in the data" if @report.file_type.blank? end |
#write_record(record) ⇒ Object
59 60 61 62 |
# File 'lib/ftf/generator.rb', line 59 def write_record(record) @output.puts(record) @record_index += 1 end |