barkick 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 0de5ff7b22436b0766678873a92edb9adc4ae649
4
+ data.tar.gz: be6893604f133f0dd815465245c203f15d805ea3
5
+ SHA512:
6
+ metadata.gz: eb2da0c789036400c5624fe40d75c13cf83a409e260eac0fee835acba059bb4e93e27b4ef388e14520048f1aa60edac361c68111fd6cbf0023cbef2ffdca44cf
7
+ data.tar.gz: 8de33157d9f77b077dac4d8935ecef7ddab663f1ce13fa6bafffadfba15454c8d885200151f5e3be9802dc3ba1c81a5a494215eca5c0df026f493b2f556712ee
data/.gitignore ADDED
@@ -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/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in barkick.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Andrew Kane
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,56 @@
1
+ # Barkick
2
+
3
+ Barcodes made easy
4
+
5
+ Works with:
6
+
7
+ - [UPC](http://en.wikipedia.org/wiki/Universal_Product_Code)
8
+ - [EAN](http://en.wikipedia.org/wiki/International_Article_Number_%28EAN%29)
9
+ - [GTIN](http://en.wikipedia.org/wiki/Global_Trade_Item_Number)
10
+ - [ISBN](http://en.wikipedia.org/wiki/International_Standard_Book_Number)
11
+
12
+ For PLU codes, check out the [plu gem](https://github.com/ankane/plu)
13
+
14
+ ## How To Use
15
+
16
+ ```ruby
17
+ gtin = GTIN.new("016000275263")
18
+ gtin.valid? # true
19
+ gtin.gtin14 # "00016000275263"
20
+ gtin.ean13 # "0016000275263"
21
+ gtin.upc # "016000275263"
22
+ gtin.prefix # "001"
23
+ gtin.prefix_name # "GS1 US"
24
+ ```
25
+
26
+ Variable items
27
+
28
+ ```ruby
29
+ gtin = GTIN.new("299265108631")
30
+ gtin.variable? # true
31
+ gtin.restricted? # true
32
+ gtin.price # 8.63
33
+ gtin.base_gtin14 # "00299265000000"
34
+ ```
35
+
36
+ ## Installation
37
+
38
+ Add this line to your Gemfile:
39
+
40
+ ```ruby
41
+ gem "barkick"
42
+ ```
43
+
44
+ And run:
45
+
46
+ ```sh
47
+ bundle
48
+ ```
49
+
50
+ ## Contributing
51
+
52
+ 1. Fork it
53
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
54
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
55
+ 4. Push to the branch (`git push origin my-new-feature`)
56
+ 5. Create new Pull Request
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ task :default => :test
5
+ Rake::TestTask.new do |t|
6
+ t.libs << "test"
7
+ t.pattern = "test/**/*_test.rb"
8
+ end
data/barkick.gemspec ADDED
@@ -0,0 +1,24 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'barkick/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "barkick"
8
+ spec.version = Barkick::VERSION
9
+ spec.authors = ["Andrew Kane"]
10
+ spec.email = ["andrew@chartkick.com"]
11
+ spec.description = %q{Barcodes made easy}
12
+ spec.summary = %q{Barcodes made easy}
13
+ spec.homepage = "https://github.com/ankane/barkick"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ spec.add_development_dependency "minitest"
24
+ end
data/lib/barkick.rb ADDED
@@ -0,0 +1,2 @@
1
+ require "barkick/version"
2
+ require "gtin"
@@ -0,0 +1,3 @@
1
+ module Barkick
2
+ VERSION = "0.0.1"
3
+ end
data/lib/gtin.rb ADDED
@@ -0,0 +1,106 @@
1
+ class GTIN
2
+
3
+ def initialize(number)
4
+ @number = number.to_s
5
+ end
6
+
7
+ def gtin14
8
+ if [8, 12, 13, 14].include?(@number.length)
9
+ @number.rjust(14, "0")
10
+ else
11
+ nil
12
+ end
13
+ end
14
+
15
+ def gtin13
16
+ gtin14[0] == "0" ? gtin14[1..-1] : nil
17
+ end
18
+ alias_method :ean13, :gtin13
19
+
20
+ def gtin12
21
+ gtin14[0..1] == "00" ? gtin14[2..-1] : nil
22
+ end
23
+ alias_method :upc, :gtin12
24
+
25
+ def check_digit
26
+ gtin14[-1]
27
+ end
28
+
29
+ def valid?
30
+ if gtin14
31
+ # http://www.gs1.org/barcodes/support/check_digit_calculator#how
32
+ digits = gtin14.split("").map(&:to_i)
33
+ # digit at position 0 is odd (first digit) for the purpose of this calculation
34
+ odd_digits, even_digits = digits.partition.each_with_index{|digit, i| i.even? }
35
+ (10 - (sum(odd_digits) * 3 + sum(even_digits)) % 10) % 10 != check_digit
36
+ else
37
+ false
38
+ end
39
+ end
40
+
41
+ def base_gtin14
42
+ base = gtin14
43
+ if restricted?
44
+ base[-6..-1] = "000000"
45
+ end
46
+ base
47
+ end
48
+
49
+ # prefix
50
+
51
+ def prefix
52
+ gtin14[1..3]
53
+ end
54
+
55
+ # TODO finish prefix list
56
+ # http://www.gs1.org/barcodes/support/prefix_list
57
+ def prefix_name
58
+ case prefix.to_i
59
+ when 0..19, 30..39, 60..139
60
+ "GS1 US"
61
+ when 20..29, 40..49, 200..299
62
+ "Restricted distribution"
63
+ when 50..59
64
+ "Coupons"
65
+ when 300..379
66
+ "GS1 France"
67
+ when 380
68
+ "GS1 Bulgaria"
69
+ when 383
70
+ "GS1 Slovenija"
71
+ when 978..979
72
+ "Bookland"
73
+ else
74
+ nil
75
+ end
76
+ end
77
+
78
+ def book?
79
+ prefix_name == "Bookland"
80
+ end
81
+
82
+ def restricted?
83
+ prefix_name == "Restricted distribution"
84
+ end
85
+
86
+ # variable weight
87
+
88
+ def variable?
89
+ (20..29).cover?(prefix.to_i)
90
+ end
91
+
92
+ def price
93
+ if variable?
94
+ gtin14[-5..-2].to_f / 100
95
+ else
96
+ nil
97
+ end
98
+ end
99
+
100
+ private
101
+
102
+ def sum(arr)
103
+ arr.inject{|sum,x| sum + x }
104
+ end
105
+
106
+ end
data/test/gtin_test.rb ADDED
@@ -0,0 +1,30 @@
1
+ require_relative "test_helper"
2
+
3
+ class TestGTIN < Minitest::Test
4
+
5
+ def test_gtin
6
+ gtin = GTIN.new("016000275263")
7
+ assert gtin.valid?
8
+ assert_equal "00016000275263", gtin.gtin14
9
+ assert_equal "0016000275263", gtin.gtin13
10
+ assert_equal "016000275263", gtin.gtin12
11
+ assert_equal "0016000275263", gtin.ean13
12
+ assert_equal "016000275263", gtin.upc
13
+ assert_equal "001", gtin.prefix
14
+ assert_equal "GS1 US", gtin.prefix_name
15
+ end
16
+
17
+ def test_invalid
18
+ assert !GTIN.new("1").valid?
19
+ end
20
+
21
+ def test_variable
22
+ gtin = GTIN.new("299265108631")
23
+ assert gtin.valid?
24
+ assert gtin.variable?
25
+ assert gtin.restricted?
26
+ assert_equal 8.63, gtin.price
27
+ assert_equal "00299265000000", gtin.base_gtin14
28
+ end
29
+
30
+ end
@@ -0,0 +1,4 @@
1
+ require "bundler/setup"
2
+ Bundler.require(:default)
3
+ require "minitest/autorun"
4
+ require "minitest/pride"
metadata ADDED
@@ -0,0 +1,99 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: barkick
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Andrew Kane
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2013-09-20 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ~>
18
+ - !ruby/object:Gem::Version
19
+ version: '1.3'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ~>
25
+ - !ruby/object:Gem::Version
26
+ version: '1.3'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - '>='
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - '>='
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - '>='
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - '>='
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Barcodes made easy
56
+ email:
57
+ - andrew@chartkick.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - .gitignore
63
+ - Gemfile
64
+ - LICENSE.txt
65
+ - README.md
66
+ - Rakefile
67
+ - barkick.gemspec
68
+ - lib/barkick.rb
69
+ - lib/barkick/version.rb
70
+ - lib/gtin.rb
71
+ - test/gtin_test.rb
72
+ - test/test_helper.rb
73
+ homepage: https://github.com/ankane/barkick
74
+ licenses:
75
+ - MIT
76
+ metadata: {}
77
+ post_install_message:
78
+ rdoc_options: []
79
+ require_paths:
80
+ - lib
81
+ required_ruby_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - '>='
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ required_rubygems_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - '>='
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ requirements: []
92
+ rubyforge_project:
93
+ rubygems_version: 2.0.0
94
+ signing_key:
95
+ specification_version: 4
96
+ summary: Barcodes made easy
97
+ test_files:
98
+ - test/gtin_test.rb
99
+ - test/test_helper.rb