cnab 0.0.2 → 0.1.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.
- data/README.md +1 -1
- data/cnab.gemspec +1 -0
- data/lib/cnab.rb +28 -1
- data/lib/cnab/detalhe.rb +12 -1
- data/lib/cnab/exceptions.rb +6 -0
- data/lib/cnab/exceptions/no_file_given.rb +9 -0
- data/lib/cnab/exceptions/segment_not_implemented.rb +9 -0
- data/lib/cnab/retorno.rb +13 -0
- data/lib/cnab/version.rb +1 -1
- data/spec/cnab/detalhe_spec.rb +23 -0
- data/spec/cnab/retorno_spec.rb +61 -0
- data/spec/cnab_spec.rb +56 -0
- metadata +28 -8
data/README.md
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# Cnab
|
2
2
|
|
3
|
-
[](http://badge.fury.io/rb/cnab) [](https://travis-ci.org/zertico/cnab) [](http://badge.fury.io/rb/cnab) [](https://travis-ci.org/zertico/cnab) [](https://coveralls.io/r/zertico/cnab) [](https://codeclimate.com/github/zertico/cnab)
|
4
4
|
|
5
5
|
Gem used to parse return from Brazil banks in format of cnab files.
|
6
6
|
|
data/cnab.gemspec
CHANGED
data/lib/cnab.rb
CHANGED
@@ -6,6 +6,33 @@ module Cnab
|
|
6
6
|
autoload :TrailerArquivo, 'cnab/trailer_arquivo'
|
7
7
|
autoload :TrailerLote, 'cnab/trailer_lote'
|
8
8
|
autoload :Detalhe, 'cnab/detalhe'
|
9
|
+
autoload :Retorno, 'cnab/retorno'
|
9
10
|
|
10
11
|
autoload :Helper, 'cnab/helper'
|
11
|
-
|
12
|
+
autoload :Exceptions, 'cnab/exceptions'
|
13
|
+
|
14
|
+
def self.parse(file = nil)
|
15
|
+
raise Exceptions::NoFileGiven if file.nil?
|
16
|
+
|
17
|
+
File.open(file, 'rb') do |f|
|
18
|
+
header_arquivo = HeaderArquivo.new(f.gets)
|
19
|
+
header_lote = HeaderLote::Cobranca.new(f.gets)
|
20
|
+
|
21
|
+
detalhes = []
|
22
|
+
while(line = f.gets)
|
23
|
+
if line[7] == "5"
|
24
|
+
trailer_lote = TrailerLote::Cobranca.new(line)
|
25
|
+
break
|
26
|
+
end
|
27
|
+
detalhes << Detalhe.parse(line)
|
28
|
+
end
|
29
|
+
|
30
|
+
trailer_arquivo = TrailerArquivo.new(f.gets)
|
31
|
+
Retorno.new({ :header_arquivo => header_arquivo,
|
32
|
+
:header_lote => header_lote,
|
33
|
+
:detalhes => detalhes,
|
34
|
+
:trailer_lote => trailer_lote,
|
35
|
+
:trailer_arquivo => trailer_arquivo })
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
data/lib/cnab/detalhe.rb
CHANGED
@@ -2,5 +2,16 @@ module Cnab
|
|
2
2
|
module Detalhe
|
3
3
|
autoload :SegmentoT, 'cnab/detalhe/segmento_t'
|
4
4
|
autoload :SegmentoU, 'cnab/detalhe/segmento_u'
|
5
|
+
|
6
|
+
def self.parse(line)
|
7
|
+
case line[13]
|
8
|
+
when "T"
|
9
|
+
SegmentoT.new(line)
|
10
|
+
when "U"
|
11
|
+
SegmentoU.new(line)
|
12
|
+
else
|
13
|
+
raise Exceptions::SegmentNotImplemented
|
14
|
+
end
|
15
|
+
end
|
5
16
|
end
|
6
|
-
end
|
17
|
+
end
|
data/lib/cnab/retorno.rb
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
module Cnab
|
2
|
+
class Retorno
|
3
|
+
include Helper
|
4
|
+
|
5
|
+
def initialize(args = {})
|
6
|
+
@header_arquivo = args[:header_arquivo]
|
7
|
+
@header_lote = args[:header_lote]
|
8
|
+
@trailer_arquivo = args[:trailer_arquivo]
|
9
|
+
@trailer_lote = args[:trailer_lote]
|
10
|
+
@detalhes = args[:detalhes] || []
|
11
|
+
end
|
12
|
+
end
|
13
|
+
end
|
data/lib/cnab/version.rb
CHANGED
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cnab::Detalhe do
|
4
|
+
describe "#parse" do
|
5
|
+
context "with a line of segmento U" do
|
6
|
+
it "should return a instance of SegmentoU" do
|
7
|
+
Cnab::Detalhe.parse("1234567890123U").should be_an_instance_of(Cnab::Detalhe::SegmentoU)
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
context "with a line of segmento T" do
|
12
|
+
it "should return a instance of SegmentoT" do
|
13
|
+
Cnab::Detalhe.parse("1234567890123T").should be_an_instance_of(Cnab::Detalhe::SegmentoT)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
context "with a line of any other segmento" do
|
18
|
+
it "should raise an error" do
|
19
|
+
lambda { Cnab::Detalhe.parse("1234567890123V") }.should raise_error(Cnab::Exceptions::SegmentNotImplemented)
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -0,0 +1,61 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Cnab::Retorno do
|
4
|
+
describe "#initialize" do
|
5
|
+
context "without params" do
|
6
|
+
before :each do
|
7
|
+
@retorno = Cnab::Retorno.new
|
8
|
+
end
|
9
|
+
|
10
|
+
it "should set #header_arquivo as nil" do
|
11
|
+
@retorno.header_arquivo.should be_nil
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should set #header_lote as nil" do
|
15
|
+
@retorno.header_lote.should be_nil
|
16
|
+
end
|
17
|
+
|
18
|
+
it "should set #trailer_arquivo as nil" do
|
19
|
+
@retorno.trailer_arquivo.should be_nil
|
20
|
+
end
|
21
|
+
|
22
|
+
it "should set #trailer_lote as nil" do
|
23
|
+
@retorno.trailer_lote.should be_nil
|
24
|
+
end
|
25
|
+
|
26
|
+
it "should set #detalhes as an empty array" do
|
27
|
+
@retorno.detalhes.should == []
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
context "with params" do
|
32
|
+
before :each do
|
33
|
+
@retorno = Cnab::Retorno.new({ :header_arquivo => "header_arquivo",
|
34
|
+
:header_lote => "header_lote",
|
35
|
+
:trailer_arquivo => "trailer_arquivo",
|
36
|
+
:trailer_lote => "trailer_lote",
|
37
|
+
:detalhes => ["detalhe1"] })
|
38
|
+
end
|
39
|
+
|
40
|
+
it "should set #header_arquivo" do
|
41
|
+
@retorno.header_arquivo.should == "header_arquivo"
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should set #header_lote" do
|
45
|
+
@retorno.header_lote.should == "header_lote"
|
46
|
+
end
|
47
|
+
|
48
|
+
it "should set #trailer_arquivo" do
|
49
|
+
@retorno.trailer_arquivo.should == "trailer_arquivo"
|
50
|
+
end
|
51
|
+
|
52
|
+
it "should set #trailer_lote" do
|
53
|
+
@retorno.trailer_lote.should == "trailer_lote"
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should set #detalhes" do
|
57
|
+
@retorno.detalhes.should == ["detalhe1"]
|
58
|
+
end
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
data/spec/cnab_spec.rb
ADDED
@@ -0,0 +1,56 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
require 'fakefs'
|
3
|
+
|
4
|
+
describe Cnab do
|
5
|
+
describe "#parse" do
|
6
|
+
context "withoud file" do
|
7
|
+
it "should raise an exception" do
|
8
|
+
lambda { Cnab.parse }.should raise_error Cnab::Exceptions::NoFileGiven
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
context "with a file" do
|
13
|
+
before :each do
|
14
|
+
@line = "#{LINE}\n"
|
15
|
+
@line[13] = "T"
|
16
|
+
|
17
|
+
File.open("cnab.txt", 'w') do |f|
|
18
|
+
f.write(@line)
|
19
|
+
f.write(@line)
|
20
|
+
f.write(@line)
|
21
|
+
@line[7] = "5"
|
22
|
+
f.write(@line)
|
23
|
+
f.write(@line)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
27
|
+
it "should return a Retorno instance" do
|
28
|
+
Cnab.parse("cnab.txt").should be_an_instance_of(Cnab::Retorno)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should return a HeaderArquivo instance" do
|
32
|
+
Cnab.parse("cnab.txt").header_arquivo.should be_an_instance_of(Cnab::HeaderArquivo)
|
33
|
+
end
|
34
|
+
|
35
|
+
it "should return a HeaderLote instance" do
|
36
|
+
Cnab.parse("cnab.txt").header_lote.should be_an_instance_of(Cnab::HeaderLote::Cobranca)
|
37
|
+
end
|
38
|
+
|
39
|
+
it "should return an array of detalhes" do
|
40
|
+
Cnab.parse("cnab.txt").detalhes.should be_an_instance_of(Array)
|
41
|
+
end
|
42
|
+
|
43
|
+
it "should return an SegmentoT instance inside detalhes array" do
|
44
|
+
Cnab.parse("cnab.txt").detalhes.first.should be_an_instance_of(Cnab::Detalhe::SegmentoT)
|
45
|
+
end
|
46
|
+
|
47
|
+
it "should return a TrailerLote instance" do
|
48
|
+
Cnab.parse("cnab.txt").trailer_lote.should be_an_instance_of(Cnab::TrailerLote::Cobranca)
|
49
|
+
end
|
50
|
+
|
51
|
+
it "should return a TrailerArquivo instance" do
|
52
|
+
Cnab.parse("cnab.txt").trailer_arquivo.should be_an_instance_of(Cnab::TrailerArquivo)
|
53
|
+
end
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cnab
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-03-
|
13
|
+
date: 2013-03-26 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -44,6 +44,22 @@ dependencies:
|
|
44
44
|
- - ! '>='
|
45
45
|
- !ruby/object:Gem::Version
|
46
46
|
version: '0'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: fakefs
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: !ruby/object:Gem::Requirement
|
58
|
+
none: false
|
59
|
+
requirements:
|
60
|
+
- - ! '>='
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: '0'
|
47
63
|
- !ruby/object:Gem::Dependency
|
48
64
|
name: coveralls
|
49
65
|
requirement: !ruby/object:Gem::Requirement
|
@@ -81,19 +97,26 @@ files:
|
|
81
97
|
- lib/cnab/detalhe.rb
|
82
98
|
- lib/cnab/detalhe/segmento_t.rb
|
83
99
|
- lib/cnab/detalhe/segmento_u.rb
|
100
|
+
- lib/cnab/exceptions.rb
|
101
|
+
- lib/cnab/exceptions/no_file_given.rb
|
102
|
+
- lib/cnab/exceptions/segment_not_implemented.rb
|
84
103
|
- lib/cnab/header_arquivo.rb
|
85
104
|
- lib/cnab/header_lote.rb
|
86
105
|
- lib/cnab/header_lote/cobranca.rb
|
87
106
|
- lib/cnab/helper.rb
|
107
|
+
- lib/cnab/retorno.rb
|
88
108
|
- lib/cnab/trailer_arquivo.rb
|
89
109
|
- lib/cnab/trailer_lote.rb
|
90
110
|
- lib/cnab/trailer_lote/cobranca.rb
|
91
111
|
- lib/cnab/version.rb
|
92
112
|
- spec/cnab/detalhe/segmento_t_spec.rb
|
93
113
|
- spec/cnab/detalhe/segmento_u_spec.rb
|
114
|
+
- spec/cnab/detalhe_spec.rb
|
94
115
|
- spec/cnab/header_arquivo_spec.rb
|
95
116
|
- spec/cnab/header_lote/cobranca_spec.rb
|
117
|
+
- spec/cnab/retorno_spec.rb
|
96
118
|
- spec/cnab/trailer_arquivo_spec.rb
|
119
|
+
- spec/cnab_spec.rb
|
97
120
|
- spec/spec_helper.rb
|
98
121
|
homepage:
|
99
122
|
licenses: []
|
@@ -107,18 +130,12 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
107
130
|
- - ! '>='
|
108
131
|
- !ruby/object:Gem::Version
|
109
132
|
version: '0'
|
110
|
-
segments:
|
111
|
-
- 0
|
112
|
-
hash: -4151617072035159123
|
113
133
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
114
134
|
none: false
|
115
135
|
requirements:
|
116
136
|
- - ! '>='
|
117
137
|
- !ruby/object:Gem::Version
|
118
138
|
version: '0'
|
119
|
-
segments:
|
120
|
-
- 0
|
121
|
-
hash: -4151617072035159123
|
122
139
|
requirements: []
|
123
140
|
rubyforge_project:
|
124
141
|
rubygems_version: 1.8.25
|
@@ -128,7 +145,10 @@ summary: It parse CNAB files
|
|
128
145
|
test_files:
|
129
146
|
- spec/cnab/detalhe/segmento_t_spec.rb
|
130
147
|
- spec/cnab/detalhe/segmento_u_spec.rb
|
148
|
+
- spec/cnab/detalhe_spec.rb
|
131
149
|
- spec/cnab/header_arquivo_spec.rb
|
132
150
|
- spec/cnab/header_lote/cobranca_spec.rb
|
151
|
+
- spec/cnab/retorno_spec.rb
|
133
152
|
- spec/cnab/trailer_arquivo_spec.rb
|
153
|
+
- spec/cnab_spec.rb
|
134
154
|
- spec/spec_helper.rb
|