embratel 0.0.1

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/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ pkg/*
2
+ *.gem
3
+ .bundle
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source :gemcutter
2
+
3
+ # Specify your gem's dependencies in embratel.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,20 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ embratel (0.0.1)
5
+ fastercsv (= 1.5.3)
6
+
7
+ GEM
8
+ remote: http://rubygems.org/
9
+ specs:
10
+ fastercsv (1.5.3)
11
+ rake (0.8.7)
12
+
13
+ PLATFORMS
14
+ ruby
15
+
16
+ DEPENDENCIES
17
+ bundler (>= 1.0.0)
18
+ embratel!
19
+ fastercsv (= 1.5.3)
20
+ rake (= 0.8.7)
data/MIT-LICENSE ADDED
@@ -0,0 +1,20 @@
1
+ Copyright (c) 2010 Murilo Soares Pereira
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining
4
+ a copy of this software and associated documentation files (the
5
+ "Software"), to deal in the Software without restriction, including
6
+ without limitation the rights to use, copy, modify, merge, publish,
7
+ distribute, sublicense, and/or sell copies of the Software, and to
8
+ permit persons to whom the Software is furnished to do so, subject to
9
+ the following conditions:
10
+
11
+ The above copyright notice and this permission notice shall be
12
+ included in all copies or substantial portions of the Software.
13
+
14
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
15
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
16
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
17
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
18
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
19
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
20
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.org ADDED
@@ -0,0 +1,19 @@
1
+ * embratel
2
+ ** Instalando
3
+ #+BEGIN_SRC
4
+ gem install embratel
5
+ #+END_SRC
6
+ ** Testando
7
+ Para rodar os testes você precisa ter o
8
+ [[http://github.com/carlhuda/bundler][bundler]] instalado.
9
+ #+BEGIN_SRC
10
+ gem install bundler
11
+ bundle install
12
+ rake test
13
+ #+END_SRC
14
+
15
+ ** Autor
16
+ [[mailto:murilo.soares3@gmail.com][Murilo Soares Pereira]].
17
+
18
+ ** Licença
19
+ Distribuído sob a [[http://github.com/murilasso/embratel/blob/master/MIT-LICENSE][licença MIT]].
data/Rakefile ADDED
@@ -0,0 +1,11 @@
1
+ require 'bundler'
2
+ Bundler::GemHelper.install_tasks
3
+
4
+ require 'rake/testtask'
5
+ Rake::TestTask.new(:test) do |test|
6
+ test.libs << 'lib' << 'test'
7
+ test.pattern = 'test/**/*_test.rb'
8
+ test.verbose = true
9
+ end
10
+
11
+ task :default => :test
data/embratel.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # -*- encoding: utf-8 -*-
2
+ require File.expand_path("../lib/embratel/version", __FILE__)
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = "embratel"
6
+ s.version = Embratel::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.authors = ["Murilo Soares Pereira"]
9
+ s.email = ["murilo.soares3@gmail.com"]
10
+ s.homepage = "http://github.com/murilasso/embratel"
11
+ s.summary = "Gem for extracting information from Embratel's phone bill files."
12
+ s.description = ""
13
+
14
+ s.required_rubygems_version = ">= 1.3.6"
15
+ s.rubyforge_project = "embratel"
16
+
17
+ s.add_development_dependency "bundler", ">= 1.0.0"
18
+ s.add_development_dependency "rake", "0.8.7"
19
+
20
+ s.add_dependency "fastercsv", "1.5.3"
21
+
22
+ s.files = `git ls-files`.split("\n")
23
+ s.require_path = 'lib'
24
+ end
@@ -0,0 +1,38 @@
1
+ module Embratel
2
+ class Call
3
+ NUMBER_CALLED_REGEXP = /^\d{10}$/
4
+ COST_REGEXP = /^\d*(\.\d+)?$/
5
+ FIELDS = %w[
6
+ id
7
+ caller
8
+ description
9
+ date
10
+ number_called
11
+ caller_local
12
+ called_local
13
+ start_time
14
+ end_time
15
+ imp
16
+ country
17
+ quantity
18
+ unit
19
+ cost
20
+ ]
21
+
22
+ FIELDS.each { |field| attr_accessor field.to_sym }
23
+
24
+ def initialize(csv_row)
25
+ @csv_row_size = csv_row.size
26
+
27
+ FIELDS.each_with_index do |field, index|
28
+ instance_variable_set("@#{field}".to_sym, csv_row[index].to_s.strip)
29
+ end
30
+ end
31
+
32
+ def valid?
33
+ @csv_row_size == 14 &&
34
+ number_called =~ NUMBER_CALLED_REGEXP &&
35
+ cost =~ COST_REGEXP
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,37 @@
1
+ require 'fastercsv'
2
+
3
+ module Embratel
4
+ class PhoneBill
5
+ def initialize(path)
6
+ begin
7
+ @csv = FasterCSV.read(path, { :skip_blanks => true })
8
+ rescue Errno::ENOENT
9
+ raise
10
+ rescue Errno::EISDIR
11
+ raise
12
+ rescue FasterCSV::MalformedCSVError
13
+ raise
14
+ else
15
+ raise InvalidPhoneBillFileError if invalid_rows?
16
+ end
17
+ end
18
+
19
+ def calls
20
+ @calls ||= @csv.inject([]) do |calls, row|
21
+ call = Call.new(row)
22
+ call.valid? ? (calls << call) : calls
23
+ end
24
+ end
25
+
26
+ def total
27
+ @total ||= calls.inject(0) { |sum, call| sum += call.cost.to_f }
28
+ end
29
+
30
+ private
31
+ def invalid_rows?
32
+ csv = @csv.dup
33
+ 3.times { csv.shift }
34
+ csv.any? { |row| !Call.new(row).valid? }
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,3 @@
1
+ module Embratel
2
+ VERSION = "0.0.1"
3
+ end
data/lib/embratel.rb ADDED
@@ -0,0 +1,20 @@
1
+ require 'rubygems'
2
+
3
+ begin
4
+ require 'bundler'
5
+ rescue LoadError
6
+ STDERR.puts "You need to install bundler"
7
+ else
8
+ begin
9
+ Bundler.setup
10
+ rescue Bundler::BundlerError
11
+ STDERR.puts $!
12
+ end
13
+ end
14
+
15
+ require 'embratel/phone_bill'
16
+ require 'embratel/call'
17
+
18
+ module Embratel
19
+ class InvalidPhoneBillFileError < StandardError; end
20
+ end
@@ -0,0 +1,112 @@
1
+ require 'test_helper'
2
+
3
+ class CallTest < Test::Unit::TestCase
4
+ def row_with_a_missing_field
5
+ [
6
+ '1',
7
+ '1634125644-FRANQUIA 01',
8
+ '04 - LIGACOES DDD PARA CELULARES',
9
+ '11/08/10 A 99/99/99',
10
+ 'SCL -SP',
11
+ 'CAS -SP',
12
+ '02:56:29 AM',
13
+ '',
14
+ 'E',
15
+ '',
16
+ '500',
17
+ 'MIN',
18
+ '0.73'
19
+ ]
20
+ end
21
+
22
+ def row_with_invalid_number_called
23
+ [
24
+ '1',
25
+ '1634125644-FRANQUIA 01',
26
+ '04 - LIGACOES DDD PARA CELULARES',
27
+ '11/08/10 A 99/99/99',
28
+ '19936928871',
29
+ 'SCL -SP',
30
+ 'CAS -SP',
31
+ '02:56:29 AM',
32
+ '',
33
+ 'E',
34
+ '',
35
+ '500',
36
+ 'MIN',
37
+ '0.73'
38
+ ]
39
+ end
40
+
41
+ def row_with_invalid_cost
42
+ [
43
+ '1',
44
+ '1634125644-FRANQUIA 01',
45
+ '04 - LIGACOES DDD PARA CELULARES',
46
+ '11/08/10 A 99/99/99',
47
+ '19936928871',
48
+ 'SCL -SP',
49
+ 'CAS -SP',
50
+ '02:56:29 AM',
51
+ '',
52
+ 'E',
53
+ '',
54
+ '500',
55
+ 'MIN',
56
+ '.73'
57
+ ]
58
+ end
59
+
60
+ def valid_row
61
+ [
62
+ '1',
63
+ '1634125644-FRANQUIA 01',
64
+ '04 - LIGACOES DDD PARA CELULARES',
65
+ '11/08/10 A 99/99/99',
66
+ '1993692887',
67
+ 'SCL -SP',
68
+ 'CAS -SP',
69
+ '02:56:29 AM',
70
+ '',
71
+ 'E',
72
+ '',
73
+ '500',
74
+ 'MIN',
75
+ '0.73'
76
+ ]
77
+ end
78
+
79
+ def test_call_instantiated_with_a_row_with_a_missing_field
80
+ call = Embratel::Call.new(row_with_a_missing_field)
81
+ assert(!call.valid?)
82
+ end
83
+
84
+ def test_call_instantiated_with_a_row_with_invalid_number_called
85
+ call = Embratel::Call.new(row_with_invalid_number_called)
86
+ assert(!call.valid?)
87
+ end
88
+
89
+ def test_call_instantiated_with_a_row_with_invalid_cost
90
+ call = Embratel::Call.new(row_with_invalid_cost)
91
+ assert(!call.valid?)
92
+ end
93
+
94
+ def test_call_instantiated_with_a_valid_row
95
+ call = Embratel::Call.new(valid_row)
96
+ assert(call.valid?)
97
+ assert_equal(call.id, '1')
98
+ assert_equal(call.caller, '1634125644-FRANQUIA 01')
99
+ assert_equal(call.description, '04 - LIGACOES DDD PARA CELULARES')
100
+ assert_equal(call.date, '11/08/10 A 99/99/99')
101
+ assert_equal(call.number_called, '1993692887')
102
+ assert_equal(call.caller_local, 'SCL -SP')
103
+ assert_equal(call.called_local, 'CAS -SP')
104
+ assert_equal(call.start_time, '02:56:29 AM')
105
+ assert_equal(call.end_time, '')
106
+ assert_equal(call.imp, 'E')
107
+ assert_equal(call.country, '')
108
+ assert_equal(call.quantity, '500')
109
+ assert_equal(call.unit, 'MIN')
110
+ assert_equal(call.cost, '0.73')
111
+ end
112
+ end
@@ -0,0 +1,60 @@
1
+ require 'test_helper'
2
+
3
+ class Embratel::PhoneBillTest < Test::Unit::TestCase
4
+ FIXTURES_PATH = File.join(File.dirname(__FILE__), '..', 'fixtures')
5
+
6
+ def non_existing_file_path
7
+ "#{FIXTURES_PATH}/non_existing_file.csv"
8
+ end
9
+
10
+ def directory_path
11
+ "#{FIXTURES_PATH}"
12
+ end
13
+
14
+ def non_phone_bill_file_path
15
+ "#{FIXTURES_PATH}/text_file.txt"
16
+ end
17
+
18
+ def invalid_csv_phone_bill_file_path
19
+ "#{FIXTURES_PATH}/invalid_phone_bill_file.csv"
20
+ end
21
+
22
+ def valid_csv_phone_bill_file_path
23
+ "#{FIXTURES_PATH}/valid_phone_bill_file.csv"
24
+ end
25
+
26
+ def test_phone_bill_instantiation_with_a_non_existing_file_path
27
+ assert_raise(Errno::ENOENT) { Embratel::PhoneBill.new(non_existing_file_path) }
28
+ end
29
+
30
+ def test_phone_bill_instantiation_with_a_directory_path
31
+ assert_raise(Errno::EISDIR) { Embratel::PhoneBill.new(directory_path) }
32
+ end
33
+
34
+ def test_phone_bill_instantiation_with_a_non_phone_bill_file_path
35
+ assert_raise(Embratel::InvalidPhoneBillFileError) do
36
+ Embratel::PhoneBill.new(non_phone_bill_file_path)
37
+ end
38
+ end
39
+
40
+ def test_phone_bill_instantiation_with_an_invalid_csv_phone_bill_file_path
41
+ assert_raise(Embratel::InvalidPhoneBillFileError) do
42
+ Embratel::PhoneBill.new(invalid_csv_phone_bill_file_path)
43
+ end
44
+ end
45
+
46
+ def test_phone_bill_instantiation_with_a_valid_csv_phone_bill_file_path
47
+ phone_bill = Embratel::PhoneBill.new(valid_csv_phone_bill_file_path)
48
+ assert(!phone_bill.send(:invalid_rows?))
49
+ end
50
+
51
+ def test_calls_method
52
+ phone_bill = Embratel::PhoneBill.new(valid_csv_phone_bill_file_path)
53
+ assert_equal(3, phone_bill.calls.size)
54
+ end
55
+
56
+ def test_total_method
57
+ phone_bill = Embratel::PhoneBill.new(valid_csv_phone_bill_file_path)
58
+ assert_equal(10.5, phone_bill.total)
59
+ end
60
+ end
@@ -0,0 +1,6 @@
1
+ ,,,,,,,,,,,,,
2
+ ,,,,,,,,,,,,,
3
+ "Seq ","Origem ","Descri��o ","Periodo/Data ","Terminal_Destino ","Local Origem","Local Destino ","Hora Inicio ","Hora Fim ","Imp ","Pais ","Qtde ","Unid ","Valor (R$) "
4
+ 1,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:56:29 AM," ","E "," ",500,"MIN ",0.73
5
+ 2,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:59:03 AM," ","E "," ",900,"MIN ",1.3
6
+ 3,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES "
@@ -0,0 +1,6 @@
1
+ Lorem ipsum dolor sit amet, consectetur adipisicing elit, sed do eiusmod tempor
2
+ incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis
3
+ nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat.
4
+ Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu
5
+ fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in
6
+ culpa qui officia deserunt mollit anim id est laborum.
@@ -0,0 +1,6 @@
1
+ ,,,,,,,,,,,,,
2
+ ,,,,,,,,,,,,,
3
+ "Seq ","Origem ","Descri��o ","Periodo/Data ","Terminal_Destino ","Local Origem","Local Destino ","Hora Inicio ","Hora Fim ","Imp ","Pais ","Qtde ","Unid ","Valor (R$) "
4
+ 1,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:56:29 AM," ","E "," ",500,"MIN ",0.73
5
+ 2,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","11/08/10 A 99/99/99 ",1993692887,"SCL -SP ","CAS -SP ",02:59:03 AM," ","E "," ",900,"MIN ",1.3
6
+ 3,"1634125644-FRANQUIA 01 ","04 - LIGACOES DDD PARA CELULARES ","13/08/10 A 99/99/99 ",1992563321,"SCL -SP ","CAS -SP ",09:07:55 PM," ","E "," ",5800,"MIN ",8.47
@@ -0,0 +1,7 @@
1
+ require 'test/unit'
2
+
3
+ $:.unshift File.join(File.dirname(__FILE__), '..', 'lib')
4
+ require 'embratel'
5
+
6
+ class Test::Unit::TestCase
7
+ end
metadata ADDED
@@ -0,0 +1,133 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: embratel
3
+ version: !ruby/object:Gem::Version
4
+ hash: 29
5
+ prerelease: false
6
+ segments:
7
+ - 0
8
+ - 0
9
+ - 1
10
+ version: 0.0.1
11
+ platform: ruby
12
+ authors:
13
+ - Murilo Soares Pereira
14
+ autorequire:
15
+ bindir: bin
16
+ cert_chain: []
17
+
18
+ date: 2010-10-16 00:00:00 -03:00
19
+ default_executable:
20
+ dependencies:
21
+ - !ruby/object:Gem::Dependency
22
+ name: bundler
23
+ prerelease: false
24
+ requirement: &id001 !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ">="
28
+ - !ruby/object:Gem::Version
29
+ hash: 23
30
+ segments:
31
+ - 1
32
+ - 0
33
+ - 0
34
+ version: 1.0.0
35
+ type: :development
36
+ version_requirements: *id001
37
+ - !ruby/object:Gem::Dependency
38
+ name: rake
39
+ prerelease: false
40
+ requirement: &id002 !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - "="
44
+ - !ruby/object:Gem::Version
45
+ hash: 49
46
+ segments:
47
+ - 0
48
+ - 8
49
+ - 7
50
+ version: 0.8.7
51
+ type: :development
52
+ version_requirements: *id002
53
+ - !ruby/object:Gem::Dependency
54
+ name: fastercsv
55
+ prerelease: false
56
+ requirement: &id003 !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - "="
60
+ - !ruby/object:Gem::Version
61
+ hash: 5
62
+ segments:
63
+ - 1
64
+ - 5
65
+ - 3
66
+ version: 1.5.3
67
+ type: :runtime
68
+ version_requirements: *id003
69
+ description: ""
70
+ email:
71
+ - murilo.soares3@gmail.com
72
+ executables: []
73
+
74
+ extensions: []
75
+
76
+ extra_rdoc_files: []
77
+
78
+ files:
79
+ - .gitignore
80
+ - Gemfile
81
+ - Gemfile.lock
82
+ - MIT-LICENSE
83
+ - README.org
84
+ - Rakefile
85
+ - embratel.gemspec
86
+ - lib/embratel.rb
87
+ - lib/embratel/call.rb
88
+ - lib/embratel/phone_bill.rb
89
+ - lib/embratel/version.rb
90
+ - test/embratel/call_test.rb
91
+ - test/embratel/phone_bill_test.rb
92
+ - test/fixtures/invalid_phone_bill_file.csv
93
+ - test/fixtures/text_file.txt
94
+ - test/fixtures/valid_phone_bill_file.csv
95
+ - test/test_helper.rb
96
+ has_rdoc: true
97
+ homepage: http://github.com/murilasso/embratel
98
+ licenses: []
99
+
100
+ post_install_message:
101
+ rdoc_options: []
102
+
103
+ require_paths:
104
+ - lib
105
+ required_ruby_version: !ruby/object:Gem::Requirement
106
+ none: false
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ hash: 3
111
+ segments:
112
+ - 0
113
+ version: "0"
114
+ required_rubygems_version: !ruby/object:Gem::Requirement
115
+ none: false
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ hash: 23
120
+ segments:
121
+ - 1
122
+ - 3
123
+ - 6
124
+ version: 1.3.6
125
+ requirements: []
126
+
127
+ rubyforge_project: embratel
128
+ rubygems_version: 1.3.7
129
+ signing_key:
130
+ specification_version: 3
131
+ summary: Gem for extracting information from Embratel's phone bill files.
132
+ test_files: []
133
+