Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

2097 Added barcodes #2104

Merged
merged 4 commits into from
Aug 13, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ gem 'faker', :git => 'https://github.com/faker-ruby/faker.git', :branch => 'mast
- [Faker::Artist](doc/default/artist.md)
- [Faker::Avatar](doc/default/avatar.md)
- [Faker::Bank](doc/default/bank.md)
- [Faker::Barcode](doc/default/barcode.md)
- [Faker::Beer](doc/default/beer.md)
- [Faker::Blood](doc/default/blood.md)
- [Faker::Boolean](doc/default/boolean.md)
Expand Down
36 changes: 36 additions & 0 deletions doc/default/barcode.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# Faker::Barcode

Generates EAN, UPC, ISBN, ISMN, ISSN format barcode with check digit attached at last

```ruby
# EAN barcodes
Faker::Barcode.ean => "85657526"
Faker::Barcode.ean(8) => "30152700"
Faker::Barcode.ean(13) => "2115190480285"

# EAN barcodes with composite string attached in code
Faker::Barcode.ean_with_composite_symbology => "41007624|JHOC6649"
Faker::Barcode.ean_with_composite_symbology(8) => "38357961|XUYJ3266"
Faker::Barcode.ean_with_composite_symbology(13) => "9530722443911|CKHWQHID"

# UPC_A barcodes
Faker::Barcode.upc_a => "766807541831"

# UPC_A barcode with composite symbology attached
Faker::Barcode.upc_a_with_composite_symbology => "790670155765|JOVG6208"

# UPC_E barcode numbers
Faker::Barcode.upc_e => "03746820"

# UPC_E barcode with composite symbology attached
Faker::Barcode.upc_e_with_composite_symbology => "05149247|BKZX9722"

# ISBN barcode numbers
Faker::Barcode.isbn => "9798363807732"

# ISMN barcode numbers
Faker::Barcode.ismn => "9790527672897"

# ISSN barcode numbers
Faker::Barcode.issn => "9775541703338"
```
154 changes: 154 additions & 0 deletions lib/faker/default/barcode.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
# frozen_string_literal: true

module Faker
class Barcode < Base
class << self
## Returns a EAN 8 or 13 digit format barcode number with check digit
# @returns [String]
#
# @example
# Faker::Barcode.ean => "85657526"
# Faker::Barcode.ean(8) => "30152700"
# Faker::Barcode.ean(13) => "2115190480285"
#
# @faker.version 2.13.0
sudeeptarlekar marked this conversation as resolved.
Show resolved Hide resolved
def ean(length = 8)
generate_barcode("barcode.ean_#{Integer(length).to_s}")
end

## Returns a EAN 8 or 13 digit format barcode number with composite string attached with check digit
# @returns [String]
#
# @example
# Faker::Barcode.ean_with_composite_sumbology => "41007624|JHOC6649"
# Faker::Barcode.ean_with_composite_sumbology(8) => "38357961|XUYJ3266"
# Faker::Barcode.ean_with_composite_sumbology(13) => "9530722443911|CKHWQHID"
#
# @faker.version 2.13.0
def ean_with_composite_symbology(length = 8)
"#{ean(length)}|#{bothify(parse('barcode.composite_symbol'))}"
end

## Returns a UPC_A format barcode number with check digit
# @returns [String]
#
# @example
# Faker::Barcode.upc_a => "766807541831"
#
# @faker.version 2.13.0
def upc_a
generate_barcode('barcode.upc_a')
end

## Returns a UPC_E format barcode number with check digit
# @returns [String]
#
# @example
# Faker::Barcode.upc_e => "03746820"
#
# @faker.version 2.13.0
def upc_e
generate_barcode('barcode.upc_e')
end

## Returns a UPC_A format barcode number with composite string attached with check digit
# @returns [String]
#
# @example
# Faker::Barcode.upc_a_with_composite_symbology => "790670155765|JOVG6208"
#
# @faker.version 2.13.0
def upc_a_with_composite_symbology
"#{upc_a}|#{bothify(parse('barcode.composite_symbol'))}"
end

## Returns a UPC_E format barcode number with composite string attached with check digit
# @returns [String]
#
# @example
# Faker::Barcode.upc_e_with_composite_symbology => "05149247|BKZX9722"
#
# @faker.version 2.13.0
def upc_e_with_composite_symbology
"#{upc_e}|#{bothify(parse('barcode.composite_symbol'))}"
end

## Returns a ISBN format barcode number with check digit
# @returns [String]
#
# @example
# Faker::Barcode.isbn => "9798363807732"
#
# @faker.version 2.13.0
def isbn
generate_barcode('barcode.isbn')
end

## Returns a ISMN format barcode number with check digit
# @returns [String]
#
# @example
# Faker::Barcode.ismn => "9790527672897"
#
# @faker.version 2.13.0
def ismn
generate_barcode('barcode.ismn')
end

## Returns a ISSN format barcode number with check digit
# @returns [String]
#
# @example
# Faker::Barcode.issn => "9775541703338"
#
# @faker.version 2.13.0
def issn
generate_barcode('barcode.issn')
end

private

def generate_barcode(key)
barcode = parse(key)
check_digit = generate_check_digit *sum_even_odd(barcode)
"#{barcode}#{check_digit}"
end

## Returns the sum of even and odd numbers from value passed
#
# @returns [Array]
#
# @example
# Faker::Barcode.send(:sum_even_odd, 12345) => [9, 5]
# Faker::Barcode.send(:sum_even_odd, 87465) => [17, 13]
#
# @faker.version 2.13.0
def sum_even_odd(fake_num)
number = fake_num.to_i
sum_even, sum_odd = 0, 0, index = 1

while number != 0 do
index % 2 == 0 ? sum_even += number % 10 : sum_odd += number % 10

number /= 10
index += 1
end

return sum_odd, sum_even
end

## Generates the check digits from sum passed
#
# @returns [Integer]
#
# @example
# Faker::Barcode.send(:generate_check_digit, 12, 4) => 0
# Faker::Barcode.send(:generate_check_digit, 23, 5) => 6
#
# @faker.version 2.13.0
def generate_check_digit(odd_sum, even_sum)
(10 - (odd_sum * 3 + even_sum) % 10) % 10
end
end
end
end
24 changes: 24 additions & 0 deletions lib/locales/en/barcode.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
en:
faker:
barcode:
ean_8: '#######'
ean_13: '############'
upc_a: '###########'
upc_e:
- '0######'
- '1######'
composite_symbol:
- '########'
- '????????'
- '####????'
- '????####'
- '##??##??'
- '??##??##'
isbn:
- '978#########'
- '9798########'
- '97910#######'
- '97911#######'
- '97912#######'
ismn: '9790########'
issn: '977#########'