ifmb 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,17 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ifmb_generator.gemspec
4
+ gemspec
@@ -0,0 +1,16 @@
1
+ # A sample Guardfile
2
+ # More info at https://github.com/guard/guard#readme
3
+
4
+ guard 'rspec' do
5
+ watch(%r{^spec/.+_spec\.rb$})
6
+ watch(%r{^lib/(.+)\.rb$}) { 'spec' }
7
+ watch('spec/spec_helper.rb') { "spec" }
8
+
9
+ # Capybara request specs
10
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/requests/#{m[1]}_spec.rb" }
11
+
12
+ # Turnip features and steps
13
+ watch(%r{^spec/acceptance/(.+)\.feature$})
14
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
15
+ end
16
+
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2012 TODO: Write your name
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
@@ -0,0 +1,29 @@
1
+ # Ifmb
2
+
3
+ TODO: Write a gem description
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ gem 'ifmb_generator'
10
+
11
+ And then execute:
12
+
13
+ $ bundle
14
+
15
+ Or install it yourself as:
16
+
17
+ $ gem install ifmb
18
+
19
+ ## Usage
20
+
21
+ TODO: Write usage instructions here
22
+
23
+ ## Contributing
24
+
25
+ 1. Fork it
26
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
27
+ 3. Commit your changes (`git commit -am 'Added some feature'`)
28
+ 4. Push to the branch (`git push origin my-new-feature`)
29
+ 5. Create new Pull Request
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env rake
2
+ require "bundler/gem_tasks"
@@ -0,0 +1,23 @@
1
+ #encoding: utf-8
2
+
3
+ # -*- encoding: utf-8 -*-
4
+ require File.expand_path('../lib/ifmb/version', __FILE__)
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.authors = ["Ismael Abreu", "Luís Ramalho"]
8
+ gem.email = ["info@sinemys.pt"]
9
+ gem.description = %q{Generate references for IFMB payments (portuguese ATM payment service from IFTHEN)}
10
+ gem.summary = %q{Payment references generation for IFMB}
11
+ gem.homepage = "http://github.com/sinemys/ifmb"
12
+
13
+ gem.add_development_dependency('rspec')
14
+ gem.add_development_dependency('guard-rspec')
15
+ gem.add_development_dependency('rb-fsevent')
16
+
17
+ gem.files = `git ls-files`.split($\)
18
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
19
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
20
+ gem.name = "ifmb"
21
+ gem.require_paths = ["lib"]
22
+ gem.version = Ifmb::VERSION
23
+ end
@@ -0,0 +1,9 @@
1
+ require "Ifmb/version"
2
+ require 'Ifmb/html_formatter'
3
+ require 'ifmb/generator'
4
+
5
+ module Ifmb
6
+
7
+ Reference = Struct.new :entidade, :sub_endidade, :order_id, :order_value, :referencia
8
+
9
+ end
@@ -0,0 +1,122 @@
1
+ module Ifmb
2
+
3
+ class Generator
4
+ attr_reader :entidade, :sub_entidade, :order_id, :order_value
5
+
6
+ def initialize(args = {})
7
+ args = args.select do |k,v|
8
+ [:entidade, :sub_entidade, :order_id, :order_value].include? k
9
+ end
10
+
11
+ args.each do |attr, value|
12
+ send "#{attr}=", value
13
+ # instance_variable_set "@#{attr}", value
14
+ end
15
+
16
+ @erros = Hash.new { |hash, key| hash[key] = [] }
17
+ end
18
+
19
+
20
+ [:entidade, :sub_entidade, :order_id, :order_value].each do |attr|
21
+ define_method "#{attr}=" do |value|
22
+ # refresh validation
23
+ instance_variable_set "@#{attr}", value
24
+ end
25
+ end
26
+
27
+
28
+ def valid?
29
+ if (entidade.to_s.length < 5)
30
+ @errors[:entidade] << 'nao e valida! Tem que ter 5 digitos'
31
+ return false
32
+ elsif (entidade.to_s.length > 5)
33
+ @errors[:entidade] << 'nao e valida! Tem que ter 5 digitos'
34
+ return false
35
+ end
36
+
37
+ if (sub_entidade.to_s.length == 0)
38
+ p 'sub entidade invalida'
39
+ return false
40
+ elsif (sub_entidade.to_s.length == 1)
41
+ sub_entidade = '00' + sub_entidade.to_s
42
+ elsif (sub_entidade.to_s.length == 2)
43
+ sub_entidade = '0' + sub_entidade.to_s
44
+ elsif (sub_entidade.to_s.length > 3)
45
+ p 'sub entidade invalida +'
46
+ return false
47
+ end
48
+ true
49
+ end
50
+
51
+ def referencia
52
+ @referencia ||= generate
53
+ end
54
+
55
+ def in_html
56
+ @html_format ||= render_html
57
+ end
58
+
59
+ def generate
60
+ raise ArgumentError.new "arguments are invalid! #{@errors}" unless valid?
61
+
62
+
63
+
64
+ check_val = 0
65
+ # order_id = "0000" + order_id.to_s
66
+
67
+ puts order_value.inspect
68
+
69
+ # order_id = order_id.slice((order_id.length - 4)..-1).to_s
70
+
71
+
72
+ if ((order_value.to_f * 100).round < 1)
73
+ p 'impossivel de realizar pagamentos de 1 euro'
74
+ exit
75
+ end
76
+
77
+ if ((order_value.to_f * 100).round >= 1_000_000)
78
+ # this is not implemented
79
+ # related with the while loop
80
+ p 'Aviso: Pagamento Fraccionado por exceder o valor limite para pagamentos por multibanco'
81
+ exit
82
+ end
83
+
84
+ # this is not implemented
85
+ # while (order_value.to_i >= 1_000_000)
86
+ # order_id += order_id
87
+ # generate_mb_ref(entidade, sub_entidade, order_id, 999999.99)
88
+ # order_value = order_value.to_f - 999999.99
89
+ # end
90
+ puts order_id
91
+
92
+ check_str = sprintf('%05u%03u%04u%08u', entidade, sub_entidade, order_id.to_s, (order_value.to_f * 100).round)
93
+
94
+ chk_array = [3, 30, 9, 90, 27, 76, 81, 34, 49, 5, 50, 15, 53, 45, 62, 38, 89, 17, 73, 51]
95
+
96
+ 20.times { |i|
97
+ check_int = check_str[19 - i, 1]
98
+ check_val += (check_int.to_i%10)*chk_array[i]
99
+ }
100
+
101
+ check_val %= 97
102
+
103
+ check_digits = sprintf('%02u', 98-check_val);
104
+
105
+ p "ENTIDADE : #{entidade}"
106
+ p "REFERENCIA: #{sub_entidade} #{check_str[8,3]} #{check_str[11,1]} #{check_digits}"
107
+ p "VALOR : #{order_value}"
108
+ @referencia = "#{sub_entidade}#{check_str[8,3]}#{check_str[11,1]}#{check_digits}".to_i
109
+ end
110
+
111
+ private
112
+
113
+ def render_html
114
+ HtmlFormatter.new(result).output
115
+ end
116
+
117
+ def result
118
+ @result ||= Reference.new entidade, sub_entidade, order_id, order_value, referencia
119
+ end
120
+
121
+ end
122
+ end
@@ -0,0 +1,43 @@
1
+ module Ifmb
2
+ class HtmlFormatter
3
+
4
+ def initialize(reference)
5
+ @reference = reference
6
+ end
7
+
8
+ def output
9
+ formatted_reference
10
+ end
11
+
12
+ private
13
+
14
+ def formatted_reference
15
+ '<div style="background: #F7F7F7; border: 1px solid #DDDDDD; padding: 10px; margin-bottom: 10px;">
16
+ <table cellpadding="3" width="300px" cellspacing="0" style="margin-top: 10px;border: 1px solid #45829F; background-color: #FFFFFF;" align="center">
17
+ <tr>
18
+ <td style="font-size: x-small; border-top: 0px; border-left: 0px; border-right: 0px; border-bottom: 1px solid #45829F; background-color: #45829F; color: White" colspan="3"><div align="center">Pagamento por Multibanco ou Homebanking</div></td>
19
+ </tr>
20
+ <tr>
21
+ <td rowspan="3"><div align="center"><img src="http://img412.imageshack.us/img412/9672/30239592.jpg" alt="" width="52" height="60"/></div></td>
22
+ <td style="font-size: x-small; font-weight:bold; text-align:left">Entidade:</td>
23
+ <td style="font-size: x-small; text-align:left">'+@reference.entidade.to_s+'</td>
24
+ </tr>
25
+ <tr>
26
+ <td style="font-size: x-small; font-weight:bold; text-align:left">Refer&ecirc;ncia:</td>
27
+ <td style="font-size: x-small; text-align:left">'+@reference.referencia.to_s+'</td>
28
+ </tr>
29
+ <tr>
30
+ <td style="font-size: x-small; font-weight:bold; text-align:left">Valor:</td>
31
+ <td style="font-size: x-small; text-align:left">'+@reference.order_value.to_s+'</td>
32
+ </tr>
33
+ <tr>
34
+ <td style="font-size: xx-small;border-top: 1px solid #45829F; border-left: 0px; border-right: 0px; border-bottom: 0px; background-color: #45829F; color: White" colspan="3">O tal&atilde;o emitido pela caixa autom&aacute;tica faz prova de pagamento. Conserve-o.</td>
35
+ </tr>
36
+ </table>
37
+ </div>'
38
+ end
39
+
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,3 @@
1
+ module Ifmb
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1,49 @@
1
+ require 'spec_helper'
2
+ require 'Ifmb'
3
+
4
+ describe Ifmb::Generator do
5
+
6
+ G = Ifmb::Generator
7
+ before(:each) do
8
+ @g1 = G.new entidade: 18282, sub_entidade: 123, order_id: 162, order_value: 234.23
9
+ @g2 = G.new entidade: 11604, sub_entidade: 999, order_id: 1234, order_value: 25.86
10
+ @g3 = G.new entidade: 18282, sub_entidade: 700, order_id: 700, order_value: 234.23
11
+ end
12
+
13
+ it "has attributes" do
14
+ @g1.entidade.should == 18282
15
+ @g1.sub_entidade.should == 123
16
+ @g1.order_id.should == 162
17
+ @g1.order_value.should == 234.23
18
+ end
19
+
20
+ context "without any starter value" do
21
+ it "retrives the reference for the given values in initialize" do
22
+ g = G.new entidade: 18282, sub_entidade: 123, order_id: 162, order_value: 234.23
23
+ g.generate.should == 123016251
24
+ g.referencia.should == 123016251
25
+ end
26
+ end
27
+
28
+ describe "Generator#generate" do
29
+ it "retrives a valid reference" do
30
+ @g2.order_value.should_not be_nil
31
+ @g2.generate.should == 999123490
32
+ end
33
+
34
+ it 'works with octal like numbers' do
35
+ @g3.generate.should == 700070047
36
+ end
37
+ end
38
+
39
+ describe "Generator#in_html" do
40
+ it "should output html" do
41
+ @g3.generate
42
+ @g3.in_html.should match /div/
43
+ @g3.in_html.should match /#{@g3.entidade}/
44
+ @g3.in_html.should match /#{@g3.order_value}/
45
+ @g3.in_html.should match /#{@g3.referencia}/
46
+ end
47
+ end
48
+
49
+ end
@@ -0,0 +1,48 @@
1
+ require 'spec_helper'
2
+
3
+ describe Ifmb::HtmlFormatter do
4
+
5
+ H = Ifmb::HtmlFormatter
6
+ before :each do
7
+ @reference = Ifmb::Reference.new 11604, 999, 1234, 25.86, 999123490
8
+ end
9
+
10
+ it "receives a Reference object" do
11
+ formatter = H.new @reference
12
+ formatter.should be_instance_of H
13
+ end
14
+
15
+
16
+ it "formats a given reference information" do
17
+ formatter = H.new @reference
18
+ formatter.output.should == formatted_reference(@reference)
19
+ end
20
+
21
+
22
+ def formatted_reference reference
23
+ '<div style="background: #F7F7F7; border: 1px solid #DDDDDD; padding: 10px; margin-bottom: 10px;">
24
+ <table cellpadding="3" width="300px" cellspacing="0" style="margin-top: 10px;border: 1px solid #45829F; background-color: #FFFFFF;" align="center">
25
+ <tr>
26
+ <td style="font-size: x-small; border-top: 0px; border-left: 0px; border-right: 0px; border-bottom: 1px solid #45829F; background-color: #45829F; color: White" colspan="3"><div align="center">Pagamento por Multibanco ou Homebanking</div></td>
27
+ </tr>
28
+ <tr>
29
+ <td rowspan="3"><div align="center"><img src="http://img412.imageshack.us/img412/9672/30239592.jpg" alt="" width="52" height="60"/></div></td>
30
+ <td style="font-size: x-small; font-weight:bold; text-align:left">Entidade:</td>
31
+ <td style="font-size: x-small; text-align:left">'+reference.entidade.to_s+'</td>
32
+ </tr>
33
+ <tr>
34
+ <td style="font-size: x-small; font-weight:bold; text-align:left">Refer&ecirc;ncia:</td>
35
+ <td style="font-size: x-small; text-align:left">'+reference.referencia.to_s+'</td>
36
+ </tr>
37
+ <tr>
38
+ <td style="font-size: x-small; font-weight:bold; text-align:left">Valor:</td>
39
+ <td style="font-size: x-small; text-align:left">'+reference.order_value.to_s+'</td>
40
+ </tr>
41
+ <tr>
42
+ <td style="font-size: xx-small;border-top: 1px solid #45829F; border-left: 0px; border-right: 0px; border-bottom: 0px; background-color: #45829F; color: White" colspan="3">O tal&atilde;o emitido pela caixa autom&aacute;tica faz prova de pagamento. Conserve-o.</td>
43
+ </tr>
44
+ </table>
45
+ </div>'
46
+ end
47
+
48
+ end
@@ -0,0 +1,5 @@
1
+ require 'rspec'
2
+ require 'ifmb'
3
+ RSpec.configure do |c|
4
+ c.mock_with :rspec
5
+ end
@@ -0,0 +1,5 @@
1
+ require 'ifmb'
2
+
3
+ generator = Ifmb::Generator.new entidade: 18282, sub_entidade: 123, order_id: 162, order_value: 234.23
4
+ generator.generate
5
+ generator.in_html
metadata ADDED
@@ -0,0 +1,115 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ifmb
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Ismael Abreu
9
+ - Luís Ramalho
10
+ autorequire:
11
+ bindir: bin
12
+ cert_chain: []
13
+ date: 2012-11-21 00:00:00.000000000 Z
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: rspec
17
+ requirement: !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ! '>='
29
+ - !ruby/object:Gem::Version
30
+ version: '0'
31
+ - !ruby/object:Gem::Dependency
32
+ name: guard-rspec
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ! '>='
37
+ - !ruby/object:Gem::Version
38
+ version: '0'
39
+ type: :development
40
+ prerelease: false
41
+ version_requirements: !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ! '>='
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ - !ruby/object:Gem::Dependency
48
+ name: rb-fsevent
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'
63
+ description: Generate references for IFMB payments (portuguese ATM payment service
64
+ from IFTHEN)
65
+ email:
66
+ - info@sinemys.pt
67
+ executables: []
68
+ extensions: []
69
+ extra_rdoc_files: []
70
+ files:
71
+ - .gitignore
72
+ - .rspec
73
+ - Gemfile
74
+ - Guardfile
75
+ - LICENSE
76
+ - README.md
77
+ - Rakefile
78
+ - ifmb.gemspec
79
+ - lib/ifmb.rb
80
+ - lib/ifmb/generator.rb
81
+ - lib/ifmb/html_formatter.rb
82
+ - lib/ifmb/version.rb
83
+ - spec/generator_spec.rb
84
+ - spec/html_formatter_spec.rb
85
+ - spec/spec_helper.rb
86
+ - teste_ifmb.rb
87
+ homepage: http://github.com/sinemys/ifmb
88
+ licenses: []
89
+ post_install_message:
90
+ rdoc_options: []
91
+ require_paths:
92
+ - lib
93
+ required_ruby_version: !ruby/object:Gem::Requirement
94
+ none: false
95
+ requirements:
96
+ - - ! '>='
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ required_rubygems_version: !ruby/object:Gem::Requirement
100
+ none: false
101
+ requirements:
102
+ - - ! '>='
103
+ - !ruby/object:Gem::Version
104
+ version: '0'
105
+ requirements: []
106
+ rubyforge_project:
107
+ rubygems_version: 1.8.23
108
+ signing_key:
109
+ specification_version: 3
110
+ summary: Payment references generation for IFMB
111
+ test_files:
112
+ - spec/generator_spec.rb
113
+ - spec/html_formatter_spec.rb
114
+ - spec/spec_helper.rb
115
+ has_rdoc: