economic-ocr 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +17 -0
- data/.rvmrc +1 -0
- data/.travis.yml +11 -0
- data/Gemfile +4 -0
- data/LICENSE +22 -0
- data/README.md +29 -0
- data/Rakefile +9 -0
- data/economic-ocr.gemspec +22 -0
- data/lib/economic-ocr.rb +42 -0
- data/lib/economic-ocr/version.rb +5 -0
- data/spec/economic-ocr_spec.rb +50 -0
- metadata +68 -0
data/.gitignore
ADDED
data/.rvmrc
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
rvm ruby-1.9.3-p0-patched@economic-ocr --create
|
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2013 Albert
|
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.
|
data/README.md
ADDED
@@ -0,0 +1,29 @@
|
|
1
|
+
# Economic::Ocr
|
2
|
+
|
3
|
+
TODO: Write a gem description
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
gem 'economic-ocr'
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install economic-ocr
|
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
|
data/Rakefile
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
require File.expand_path('../lib/economic-ocr/version', __FILE__)
|
3
|
+
|
4
|
+
Gem::Specification.new do |gem|
|
5
|
+
gem.authors = ["Barsoom"]
|
6
|
+
gem.email = ["albert@barsoom.se"]
|
7
|
+
gem.summary = "Generates OCR numbers with an extra e-conomic flavor"
|
8
|
+
gem.homepage = "https://github.com/barsoom/economic-ocr"
|
9
|
+
|
10
|
+
gem.files = `git ls-files`.split($\)
|
11
|
+
gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
|
12
|
+
gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
|
13
|
+
gem.name = "economic-ocr"
|
14
|
+
gem.require_paths = ["lib"]
|
15
|
+
gem.version = Economic::Ocr::VERSION
|
16
|
+
# For Travis CI.
|
17
|
+
gem.add_development_dependency "rake"
|
18
|
+
#
|
19
|
+
if RUBY_VERSION < "1.9.3"
|
20
|
+
gem.add_development_dependency "minitest"
|
21
|
+
end
|
22
|
+
end
|
data/lib/economic-ocr.rb
ADDED
@@ -0,0 +1,42 @@
|
|
1
|
+
require "economic-ocr/version"
|
2
|
+
|
3
|
+
module Economic
|
4
|
+
class InvalidOcr < StandardError; end
|
5
|
+
class Ocr
|
6
|
+
def self.calculate(number)
|
7
|
+
return unless number
|
8
|
+
value = "#{ number }0" # E-conomic wants us to add a 0 for some reason.
|
9
|
+
value += ((value.length + 2) % 10).to_s # 2 for length and check.
|
10
|
+
(value + luhn_checksum(value).to_s).to_i
|
11
|
+
end
|
12
|
+
|
13
|
+
def self.id_from_ocr(ocr)
|
14
|
+
return unless ocr
|
15
|
+
ocr.to_s[0..-4].to_i
|
16
|
+
end
|
17
|
+
|
18
|
+
def self.id_from_ocr!(ocr)
|
19
|
+
ocr = ocr.to_i
|
20
|
+
number = id_from_ocr(ocr)
|
21
|
+
|
22
|
+
if calculate(number) == ocr
|
23
|
+
number
|
24
|
+
else
|
25
|
+
raise InvalidOcr, "Invalid OCR: #{ocr}"
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
private
|
30
|
+
|
31
|
+
def self.luhn_checksum(number)
|
32
|
+
digits = number.to_s.reverse.scan(/\d/).map { |x| x.to_i }
|
33
|
+
digits = digits.each_with_index.map { |d, i|
|
34
|
+
d *= 2 if i.even?
|
35
|
+
d > 9 ? d - 9 : d
|
36
|
+
}
|
37
|
+
sum = digits.inject(0) { |m, x| m + x }
|
38
|
+
mod = 10 - sum % 10
|
39
|
+
mod == 10 ? 0 : mod
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
require "minitest/autorun"
|
2
|
+
require "minitest/pride"
|
3
|
+
require "economic-ocr"
|
4
|
+
|
5
|
+
describe Economic::Ocr, "self.calculate" do
|
6
|
+
|
7
|
+
it "is the ID + 0 + length + checksum" do
|
8
|
+
Economic::Ocr.calculate(77).must_equal 77057
|
9
|
+
Economic::Ocr.calculate(78).must_equal 78055
|
10
|
+
Economic::Ocr.calculate(123).must_equal 123067
|
11
|
+
Economic::Ocr.calculate(1234).must_equal 1234079
|
12
|
+
Economic::Ocr.calculate(1234567890).must_equal 1234567890037
|
13
|
+
end
|
14
|
+
|
15
|
+
it "returns nil when given nil" do
|
16
|
+
Economic::Ocr.calculate(nil).must_equal nil
|
17
|
+
end
|
18
|
+
|
19
|
+
end
|
20
|
+
|
21
|
+
describe Economic::Ocr, "self.id_from_ocr" do
|
22
|
+
|
23
|
+
it "returns the id" do
|
24
|
+
Economic::Ocr.id_from_ocr(77057).must_equal 77
|
25
|
+
Economic::Ocr.id_from_ocr(78055).must_equal 78
|
26
|
+
Economic::Ocr.id_from_ocr(123067).must_equal 123
|
27
|
+
Economic::Ocr.id_from_ocr(1234079).must_equal 1234
|
28
|
+
Economic::Ocr.id_from_ocr(1234567890037).must_equal 1234567890
|
29
|
+
end
|
30
|
+
|
31
|
+
it "returns nil when given nil" do
|
32
|
+
Economic::Ocr.id_from_ocr(nil).must_equal nil
|
33
|
+
end
|
34
|
+
|
35
|
+
end
|
36
|
+
|
37
|
+
describe Economic::Ocr, "self.id_from_ocr!" do
|
38
|
+
it "returns an id when valid" do
|
39
|
+
Economic::Ocr.id_from_ocr!(1234567890037).must_equal 1234567890
|
40
|
+
end
|
41
|
+
|
42
|
+
it "handles strings" do
|
43
|
+
Economic::Ocr.id_from_ocr!("1234567890037").must_equal 1234567890
|
44
|
+
end
|
45
|
+
|
46
|
+
it "raises an error when not valid" do
|
47
|
+
-> { Economic::Ocr.id_from_ocr!(123) }.must_raise Economic::InvalidOcr
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
metadata
ADDED
@@ -0,0 +1,68 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: economic-ocr
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Barsoom
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2013-07-09 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: rake
|
16
|
+
requirement: &70146045724880 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :development
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70146045724880
|
25
|
+
description:
|
26
|
+
email:
|
27
|
+
- albert@barsoom.se
|
28
|
+
executables: []
|
29
|
+
extensions: []
|
30
|
+
extra_rdoc_files: []
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- .rvmrc
|
34
|
+
- .travis.yml
|
35
|
+
- Gemfile
|
36
|
+
- LICENSE
|
37
|
+
- README.md
|
38
|
+
- Rakefile
|
39
|
+
- economic-ocr.gemspec
|
40
|
+
- lib/economic-ocr.rb
|
41
|
+
- lib/economic-ocr/version.rb
|
42
|
+
- spec/economic-ocr_spec.rb
|
43
|
+
homepage: https://github.com/barsoom/economic-ocr
|
44
|
+
licenses: []
|
45
|
+
post_install_message:
|
46
|
+
rdoc_options: []
|
47
|
+
require_paths:
|
48
|
+
- lib
|
49
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
56
|
+
none: false
|
57
|
+
requirements:
|
58
|
+
- - ! '>='
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '0'
|
61
|
+
requirements: []
|
62
|
+
rubyforge_project:
|
63
|
+
rubygems_version: 1.8.10
|
64
|
+
signing_key:
|
65
|
+
specification_version: 3
|
66
|
+
summary: Generates OCR numbers with an extra e-conomic flavor
|
67
|
+
test_files:
|
68
|
+
- spec/economic-ocr_spec.rb
|