Skip to content

Commit

Permalink
More data updates
Browse files Browse the repository at this point in the history
  • Loading branch information
tcd committed Dec 27, 2019
1 parent 4860aea commit 8337f39
Show file tree
Hide file tree
Showing 14 changed files with 208 additions and 107 deletions.
2 changes: 1 addition & 1 deletion data/004010/elements.tsv
Original file line number Diff line number Diff line change
Expand Up @@ -313,7 +313,7 @@ id name type min max description
370 Terms Discount Due Date DT 8 8 Date payment is due if discount is to be earned expressed in format CCYYMMDD
371 Change Reason Code ID 2 2 Code specifying the reason for price or quantity change
372 Lading Liability Code ID 1 1 Code identifying limits of liability
373 Date DT 8 8 Date expressed as CCYYMMDD
373 Date DT 8 8 Date expressed as CCYYMMDD
374 Date/Time Qualifier ID 3 3 Code specifying type of date or time, or both date and time
375 Tariff Service Code ID 2 2 Code specifying the types of services for rating purposes
376 Test Indicator ID 1 1 Code indicating whether data enclosed by this interchange envelope is test or production
Expand Down
2 changes: 1 addition & 1 deletion data/004010/segments.tsv
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
id description
id name
AAA Request Validation
ACD Account Description
ACK Line Item Acknowledgment
Expand Down
2 changes: 1 addition & 1 deletion data/004010/segments/beg.segment.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ elements:
- { ref: BEG02, id: '92', req: M }
- { ref: BEG03, id: '324', req: M }
- { ref: BEG04, id: '328', req: O }
- { ref: BEG05, id: '373', req: M }
- { ref: BEG05, id: '373', req: M, description: The date assigned to the purchase order by the purchaser. }
- { ref: BEG06, id: '367', req: O }
- { ref: BEG07, id: '587', req: O }
- { ref: BEG08, id: '1019', req: O }
Expand Down
23 changes: 0 additions & 23 deletions lib/definitions/transaction_sets/850.rb

This file was deleted.

37 changes: 37 additions & 0 deletions lib/definitions/transaction_sets/manual/850.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
module Eddy
module TransactionSets
# ### Transaction Set Summary:
#
# - Id: 850
# - Name: Purchase Order
# - Functional Group: PO
class TS850 < Eddy::TransactionSet

ID = "850".freeze
NAME = "Purchase Order".freeze
FUNCTIONAL_GROUP = "PO".freeze

# @param store [Eddy::Data::Store]
# @return [void]
def initialize(store)
@beg = Eddy::Segments::BEG.new(store)
@td5 = Eddy::Segments::TD5.new(store)
@n1 = Eddy::Segments::N1.new(store)
@n3 = Eddy::Segments::N3.new(store)
@n4 = Eddy::Segments::N4.new(store)
@po1 = Eddy::Segments::PO1.new(store) # FIXME: This is a loop with a max repeat of 100_000
@ctt = Eddy::Segments::CTT.new(store)
super(
store,
@beg,
@td5,
@n1,
@n3,
@n4,
@po1,
@ctt,
)
end
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,10 @@ def initialize(store)
@ctt = Eddy::Segments::CTT.new(store)
super(
store,
@bak,
@l_n1,
@l_po1,
@ctt,
)
end

Expand Down
13 changes: 7 additions & 6 deletions lib/eddy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ module Eddy; end
require "eddy/config"
require "eddy/data"

Dir.glob(File.join(__dir__, "eddy", "schema", "*.rb")).each { |file| require file }
Dir.glob(File.join(__dir__, "eddy", "schema", "*.rb")).sort.each { |file| require file }

require "eddy/models/element"
require "eddy/models/segment"
Expand All @@ -22,11 +22,12 @@ module Eddy; end

require "eddy/build/build"

# Dir.glob(File.join(__dir__, "definitions", "elements", "manual", "**", "*.rb")).each { |file| require file }
# Dir.glob(File.join(__dir__, "definitions", "segments", "manual", "**", "*.rb")).each { |file| require file }
Dir.glob(File.join(__dir__, "definitions", "transaction_sets", "*.rb")).each { |file| require file }
# Dir.glob(File.join(__dir__, "definitions", "elements", "manual", "**", "*.rb")).sort.each { |file| require file }
# Dir.glob(File.join(__dir__, "definitions", "segments", "manual", "**", "*.rb")).sort.each { |file| require file }
# Dir.glob(File.join(__dir__, "definitions", "transaction_sets", "manual", "**", "*.rb")).sort.each { |file| require file }

Dir.glob(File.join(__dir__, "definitions", "elements", "**", "*.rb")).each { |file| require file }
Dir.glob(File.join(__dir__, "definitions", "segments", "**", "*.rb")).each { |file| require file }
Dir.glob(File.join(__dir__, "definitions", "elements", "**", "*.rb")).sort.each { |file| require file }
Dir.glob(File.join(__dir__, "definitions", "segments", "**", "*.rb")).sort.each { |file| require file }
Dir.glob(File.join(__dir__, "definitions", "transaction_sets", "**", "*.rb")).sort.each { |file| require file }

require "eddy/cli/cli"
21 changes: 21 additions & 0 deletions lib/eddy/schema/segment_summary.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,27 @@ def self.create(params = {})
return s
end

# @param path [String] Path to a JSON or YAML file containing a valid Segment definition.
# @return [self]
def self.from_file(path)
raise Eddy::Errors::Error, "Invalid segment definition" unless Eddy::Schema.valid_segment_data?(path)
data = Eddy::Util.read_json_or_yaml(path)
return Eddy::Schema::SegmentSummary.create(data)
end

# @param id [String]
# @return [self]
def self.default_for_id(id)
id.downcase!
if ["ge", "gs", "iea", "isa", "se", "st"].include?(id)
path = File.join(Eddy::Util.data_dir, "004010", "segments", "envelope", "#{id}.segment.yml")
return self.from_file(path)
end
path = File.join(Eddy::Util.data_dir, "004010", "segments", "#{id}.segment.yml")
raise Eddy::Errors::Error, "No segment found with id #{id}" unless File.file?(path)
return self.from_file(path)
end

# TODO: Only use defaults if not enough info is provided.
# TODO: Define *enough info*.
#
Expand Down
63 changes: 59 additions & 4 deletions lib/eddy/util/edi_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,40 +3,76 @@ module Util

# Return raw data from `data/004010/elements.tsv`.
#
#
# @example Example return value
# [
# {:id=>"1", :name=>"Route Code", :type=>"AN", :min=>"1", :max=>"13", :description=>"Mutually defined route code"},
# {:id=>"2", :name=>"Number of Accepted Transaction Sets", :type=>"N0", :min=>"1", :max=>"6", :description=>"Number of accepted Transaction Sets in a Functional Group"},
# {:id=>"3", :name=>"Free Form Message", :type=>"AN", :min=>"1", :max=>"60", :description=>"Free-form text"},
# ...,
# ]
#
# @return [Array<Hash>]
def self.raw_element_data()
return Eddy::Util.parse_tsv(File.join(Eddy::Util.data_dir, "004010", "elements.tsv"))
end

# Returns a hash where the keys are Element ids and the values are unprocessed Element names.
#
# @example Example return value
# {
# "1" => "Route Code",
# "2" => "Number of Accepted Transaction Sets",
# "3" => "Free Form Message",
# "4" => "Air Carrier Code",
# "5" => "Airport Code",
# "7" => "Bank Account Number",
# "8" => "Bank Client Code",
# "9" => "Late Reason Code",
# "11" => "Billing Code",
# ...,
# }
#
# @return [Hash<String, String>]
def self.element_ids()
file = File.join(Eddy::Util.data_dir, "004010", "elements-short.tsv")
data = {}
CSV.foreach(file, { col_sep: "\t", quote_char: "\x00", headers: false }) do |row|
next if row == ["id", "description"]
next if row == ["id", "name"]
data[row[0]] = row[1]
end
return data
end

# Returns a hash where the keys are Segment ids and the values are unprocessed Segment names.
#
# @example Example return value
# {
# "AAA" => "Request Validation",
# "ACD" => "Account Description",
# "ACK" => "Line Item Acknowledgment",
# "ACS" => "Ancillary Charges",
# "ACT" => "Account Identification",
# "AD1" => "Adjustment Amount",
# "ADI" => "Animal Disposition",
# ...,
# }
#
# @return [Hash<String, String>]
def self.segment_ids()
file = File.join(Eddy::Util.data_dir, "004010", "segments.tsv")
data = {}
CSV.foreach(file, { col_sep: "\t", quote_char: "\x00" }) do |row|
next if row == ["id", "description"]
next if row == ["id", "name"]
data[row[0]] = row[1]
end
return data
end

# Return the full name of an Element with the given id.
#
# For fetching more data, see {element_data_by_id}.
# @example
# Eddy::Util.element_name_by_id("93") #=> "Name"
#
# @param id [String] ID of the Element to look up.
# @return [Hash]
Expand All @@ -48,6 +84,9 @@ def self.element_name_by_id(id)

# Return the full name of a Segment with the given id.
#
# @example
# Eddy::Util.segment_name_by_id("N2") #=> "Additional Name Information"
#
# @param id [String] ID of the Segment to look up.
# @return [Hash]
def self.segment_name_by_id(id)
Expand All @@ -58,6 +97,9 @@ def self.segment_name_by_id(id)

# List the names of Elements for which Ruby classes have already been built.
#
# @example
# Eddy::Util.list_element_classes() #=> ["166", "326", "349", "234", ...]
#
# @return [Array<String>]
def self.list_element_classes()
dir = File.join(Eddy::Util.root_dir, "lib", "definitions", "elements", "**", "*.rb")
Expand All @@ -67,6 +109,9 @@ def self.list_element_classes()

# List the names of Segments for which Ruby classes have already been built.
#
# @example
# Eddy::Util.list_segment_classes() #=> ["TD5", "N4", "TD1", "BIG", ...]
#
# @return [Array<String>]
def self.list_segment_classes()
dir = File.join(Eddy::Util.root_dir, "lib", "definitions", "segments", "**", "*.rb")
Expand All @@ -76,15 +121,25 @@ def self.list_segment_classes()

# List Segment definition files in `data/004010/segments`.
#
# @example Example return value
# [
# "~/.rbenv/versions/2.6.5/lib/gems/eddy-0.0.0/data/004010/segments/ack.segment.yml",
# "~/.rbenv/versions/2.6.5/lib/gems/eddy-0.0.0/data/004010/segments/bak.segment.yml",
# ...,
# ]
#
# @return [Array<String>]
def self.list_segment_definitions()
dir = File.join(Eddy::Util.data_dir, "004010", "segments")
files = Dir.entries(dir).select { |f| File.file?(File.join(dir, f)) }
return files.map { |f| File.join(dir, f) }
return files.map { |f| File.join(dir, f) }.sort
end

# List names of Elements with Ruby files currently in `Eddy.config.build_dir/elements`.
#
# @example
# Eddy::Util.list_built_elements() #=> []
#
# @return [Array<String>]
def self.list_built_elements()
dir = File.join(Eddy.config.build_dir, "elements", "**", "*.rb")
Expand Down
8 changes: 8 additions & 0 deletions test/eddy/schema/segment_summary_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,14 @@ def test_valid_segment_data?
skip()
end

def test_from_file
skip()
end

def test_default_for_id
skip()
end

def test_doc_comment
skip("There be mystery characters in these strings...")
want = <<~STR
Expand Down
60 changes: 60 additions & 0 deletions test/eddy/util/edi_data_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require "test_helper"

module UtilTest
class EDIDataTest < Minitest::Test

def test_raw_element_data
data = Eddy::Util.raw_element_data()
skip()
end

def test_element_ids
data = Eddy::Util.element_ids()
skip()
end

def test_segment_ids
data = Eddy::Util.segment_ids()
skip()
end

def test_element_name_by_id
name = Eddy::Util.element_name_by_id("93")
assert_equal("Name", name)
end

def test_element_name_by_invalid_id
skip()
end

def test_segment_name_by_id
name = Eddy::Util.segment_name_by_id("N2")
assert_equal("Additional Name Information", name)
end

def test_segment_name_by_invalid_id
skip()
end

def test_list_element_classes
data = Eddy::Util.list_element_classes()
skip()
end

def test_list_segment_classes
data = Eddy::Util.list_segment_classes()
skip()
end

def test_list_segment_definitions
data = Eddy::Util.list_segment_definitions()
skip()
end

def test_list_built_elements
data = Eddy::Util.list_built_elements()
skip()
end

end
end
4 changes: 0 additions & 4 deletions test/eddy/util/main_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,5 @@ def test_timestamp
skip()
end

def test_find_element_by_id
skip("Write `find_element_by_id` method")
end

end
end
Loading

0 comments on commit 8337f39

Please sign in to comment.