Skip to content

Commit

Permalink
Add core functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
orhantoy committed Jan 20, 2019
1 parent 8c0bb8a commit f962c68
Showing 17 changed files with 678 additions and 33 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -6,6 +6,7 @@
/pkg/
/spec/reports/
/tmp/
/Gemfile.lock

# rspec failure tracking
.rspec_status
161 changes: 157 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
# Edifunct

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/edifunct`. To experiment with that code, run `bin/console` for an interactive prompt.
Fun with EDIFACT :tada:

TODO: Delete this and the text above, and describe your gem
EDIFACT files consist of segments and extracting the segments themselves is not too complex.
But when segments are being grouped in segment groups and nested segment groups, it would require having an additional, manual parsing step after extracting the segments.
This gem makes this easy by parsing the EDIFACT file according to a simple schema which you provide alongside the EDIFACT file.

## Installation

@@ -22,7 +24,158 @@ Or install it yourself as:

## Usage

TODO: Write usage instructions here
An example EDIFACT file:

```
UNB+UNOC:4+5790000110018:14+SEAFT.AFT006+20151012:1354+31'
UNH+45689+IFTSTA:D:10B:UN'
BGM+77+YSTSE-39237+9'
DTM+137:20150820:203'
DTM+2:20150820:102'
DTM+234:20150820:102'
CNI+1+DSVS41599'
CNT+7:9:KGM'
STS++Z1+5+8+38+108'
RFF+BN:123456'
RFF+ZTF:9F'
RFF+CU:8008331140'
RFF+AAS:7481947187'
RFF+SRN:57065930000021747'
RFF+AAM:40157065930100420983'
RFF+ASI:FM807287'
DTM+334:201508201020:203'
FTX+AHN+++Collect remarks1:2:3:4:5'
NAD+AP'
CTA+GR+:DONALD DRIVER'
NAD+CZ+++UAE LOGISTICS AB+BOX 1001:SE-164 21 KISTA+KISTA++164 21+SE'
NAD+CN+++SIERSEMA+KEURWEG 2:.:NL 5145 NX 5145 NX WAALWIJK+5145 NX WAALWIJK++5145 NX+NL'
NAD+DP+++BYGMA KOLDING+GEJLHAVEGÅRD 2 A:DK-6000 KOLDING+KOLDING++6000+DK'
NAD+PW+++NAUTISK TEKNIK APS+FARUMVEJ 107, GANLØSE:V/MARTIN KRISTENSEN:DK-3660 STENLØSE+STENLØSE++3660+DK'
NAD+ST+123456++ALFA LAVAL KOLDING A/S+31 ALBUEN:DK-6000 KOLDING+KOLDING++6000+DK'
NAD+SF+789456++SANISTAL SIA+Tiraines iela 9+Riga++1058+LV'
LOC+Z01+SELAA::6:SELAA LANDSKRONA'
GID+1+1:PK'
LOC+14+LANDSKRONA'
MEA+WT+AAB+KGM:100'
MEA+WT+ADZ+KGM:90'
MEA+VOL++MTQ:2'
MEA+LMT++MTR:4'
MEA+CT+SQ+PLL:5'
DIM+1+MTR:0.2:0.1:0.1'
PCI+17'
GIN+BN+00073000093496312546:00073000090414361624+00073000090414361631:00073000090414361648+00073000090414361655:00073000093496312539+00073000123496312546:00073000053496312546+00073000093496312789:00073000093496312684'
PCI+18'
GIN+AW+373999991234567899:373323995756893927+373323995780867383:373323995756893927+373323995859384889:373323995859360043+373323995859387804:373323995859387811+373323995859387842:373323995859392068'
UNT+18+45689'
UNZ+1+31'
```

A schema can be described entirely in JSON. An example schema for the EDIFACT file above could look like this:

```json
[
{ "type": "segment", "segment_tag": "UNB" },
{ "type": "segment", "segment_tag": "UNH" },
{ "type": "segment", "segment_tag": "BGM" },
{ "type": "segment", "segment_tag": "DTM", "repeat": true },
{
"type": "segment_group",
"group_name": "SG13",
"content": [
{ "type": "segment", "segment_tag": "CNI" },
{ "type": "segment", "segment_tag": "CNT", "repeat": true },
{
"type": "segment_group",
"group_name": "SG14",
"content": [
{ "type": "segment", "segment_tag": "STS" },
{ "type": "segment", "segment_tag": "RFF", "repeat": true },
{ "type": "segment", "segment_tag": "DTM", "repeat": true },
{ "type": "segment", "segment_tag": "FTX", "repeat": true },
{
"type": "segment_group",
"group_name": "SG15",
"content": [
{ "type": "segment", "segment_tag": "NAD" },
{
"type": "segment_group",
"group_name": "SG16",
"content": [
{ "type": "segment", "segment_tag": "CTA" }
]
}
]
},
{ "type": "segment", "segment_tag": "LOC" },
{
"type": "segment_group",
"group_name": "SG23",
"content": [
{ "type": "segment", "segment_tag": "GID" },
{ "type": "segment", "segment_tag": "LOC", "repeat": true },
{
"type": "segment_group",
"group_name": "SG24",
"content": [
{ "type": "segment", "segment_tag": "MEA" }
]
},
{
"type": "segment_group",
"group_name": "SG25",
"content": [
{ "type": "segment", "segment_tag": "DIM" }
]
},
{
"type": "segment_group",
"group_name": "SG26",
"content": [
{ "type": "segment", "segment_tag": "PCI" },
{ "type": "segment", "segment_tag": "GIN", "repeat": true }
]
}
]
}
]
}
]
},
{ "type": "segment", "segment_tag": "UNT" },
{ "type": "segment", "segment_tag": "UNZ" }
]
```

Now we are ready to parse the EDIFACT file:

```ruby
# iftsta_example_as_string: EDIFACT file (String)
# iftsta_schema: Schema (Array<Hash>)

iftsta_example = Edifunct.parse(iftsta_example_as_string, schema: iftsta_schema)
iftsta_example.lookup_groups('SG13').each_with_object({}) do |group, consignments|
sg14 = group.lookup_group('SG14')
next unless sg14

rff_cu_record = sg14.lookup_segment('RFF') { |s| s.data_elements[0] && s.data_elements[0][0] == 'CU' }
next unless rff_cu_record

dtm_record = sg14.lookup_segment('DTM')
next unless dtm_record

sts_record = sg14.lookup_segment('STS')
next unless sts_record

consignment_ref = rff_cu_record.data_elements[0][1]
consignments[consignment_ref] = [
{
status: sts_record.data_elements[1][0],
event_time: dtm_record.data_elements[0][1],
}
]
end
# => {"8008331140"=>[{:status=>"Z1", :event_time=>"201508201020"}]}
```

## Development

@@ -32,4 +185,4 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/edifunct.
Bug reports and pull requests are welcome on GitHub at https://github.com/orhantoy/edifunct.
102 changes: 102 additions & 0 deletions README_generate.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
require "bundler/setup"
require "json"
require "edifunct"

puts "# Edifunct"
puts
puts <<-MARKDOWN
Fun with EDIFACT :tada:
EDIFACT files consist of segments and extracting the segments themselves is not too complex.
But when segments are being grouped in segment groups and nested segment groups, it would require having an additional, manual parsing step after extracting the segments.
This gem makes this easy by parsing the EDIFACT file according to a simple schema which you provide alongside the EDIFACT file.
MARKDOWN

puts "\n## Installation"
puts
puts <<-MARKDOWN
Add this line to your application's Gemfile:
```ruby
gem 'edifunct'
```
And then execute:
$ bundle
Or install it yourself as:
$ gem install edifunct
MARKDOWN

fixtures_dir = File.join(__dir__, "spec", "fixtures")
iftsta_example_as_string = File.read(File.join(fixtures_dir, "IFTSTA_example.edi"), encoding: "ISO-8859-1")
iftsta_schema_json = File.read(File.join(fixtures_dir, "IFTSTA_schema.json"))
iftsta_schema = JSON.parse(iftsta_schema_json)

puts "\n## Usage"
puts
puts "An example EDIFACT file:"
puts
puts "```"
print iftsta_example_as_string
puts "```"
puts
puts "A schema can be described entirely in JSON. An example schema for the EDIFACT file above could look like this:"
puts
puts "```json"
print iftsta_schema_json
puts "```"
puts
puts "Now we are ready to parse the EDIFACT file:"
puts
puts "```ruby"
puts "# iftsta_example_as_string: EDIFACT file (String)"
puts "# iftsta_schema: Schema (Array<Hash>)"
puts

prog = <<-PROG
iftsta_example = Edifunct.parse(iftsta_example_as_string, schema: iftsta_schema)
iftsta_example.lookup_groups('SG13').each_with_object({}) do |group, consignments|
sg14 = group.lookup_group('SG14')
next unless sg14
rff_cu_record = sg14.lookup_segment('RFF') { |s| s.data_elements[0] && s.data_elements[0][0] == 'CU' }
next unless rff_cu_record
dtm_record = sg14.lookup_segment('DTM')
next unless dtm_record
sts_record = sg14.lookup_segment('STS')
next unless sts_record
consignment_ref = rff_cu_record.data_elements[0][1]
consignments[consignment_ref] = [
{
status: sts_record.data_elements[1][0],
event_time: dtm_record.data_elements[0][1],
}
]
end
PROG

print prog
consignments = eval prog
puts "# => #{consignments.inspect}"

puts "```"

puts "\n## Development"
puts
puts <<-MARKDOWN
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
MARKDOWN

puts "\n## Contributing"
puts
puts <<-MARKDOWN
Bug reports and pull requests are welcome on GitHub at https://github.com/orhantoy/edifunct.
MARKDOWN
2 changes: 2 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"

7 changes: 0 additions & 7 deletions bin/console
Original file line number Diff line number Diff line change
@@ -3,12 +3,5 @@
require "bundler/setup"
require "edifunct"

# You can add fixtures and/or initialization code here to make experimenting
# with your gem easier. You can also use a different console, if you like.

# (If you use this, don't forget to add pry to your Gemfile!)
# require "pry"
# Pry.start

require "irb"
IRB.start(__FILE__)
2 changes: 0 additions & 2 deletions bin/setup
Original file line number Diff line number Diff line change
@@ -4,5 +4,3 @@ IFS=$'\n\t'
set -vx

bundle install

# Do any other automated setup that you need to do here
20 changes: 4 additions & 16 deletions edifunct.gemspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
@@ -9,22 +10,9 @@ Gem::Specification.new do |spec|
spec.authors = ["Orhan Toy"]
spec.email = ["toyorhan@gmail.com"]

spec.summary = %q{TODO: Write a short summary, because RubyGems requires one.}
spec.description = %q{TODO: Write a longer description or delete this line.}
spec.homepage = "TODO: Put your gem's website or public repo URL here."

# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
# to allow pushing to a single host or delete this section to allow pushing to any host.
if spec.respond_to?(:metadata)
spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"

spec.metadata["homepage_uri"] = spec.homepage
spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
else
raise "RubyGems 2.0 or newer is required to protect against " \
"public gem pushes."
end
spec.summary = 'A schema-based EDIFACT parser'
spec.description = 'Edifunct provides an easy way to structurally parse an EDIFACT file based on a simple schema'
spec.homepage = 'https://github.com/orhantoy/edifunct'

# Specify which files should be added to the gem when it is released.
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
23 changes: 21 additions & 2 deletions lib/edifunct.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,25 @@
# frozen_string_literal: true

require "edifunct/version"
require "edifunct/parser"
require "edifunct/tokenizer"

# Top-level Edifunct namespace with short-hands methods to parse EDIFACT documents.
module Edifunct
class Error < StandardError; end
# Your code goes here...
class << self
def parse(edifact_message, schema:)
parser = Parser.new(edifact_message, schema: schema)
parser.as_root_group
end

def parse_file(file_args, schema:)
edifact_message = File.read(*Array(file_args))
parse(edifact_message, schema: schema)
end

def as_segments(edifact_message)
tokenizer = Tokenizer.for_message(edifact_message)
tokenizer.as_segments(edifact_message)
end
end
end
Loading
Oops, something went wrong.

0 comments on commit f962c68

Please sign in to comment.