faker-isbn 0.0.1 → 0.0.2

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/.rvmrc ADDED
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env bash
2
+
3
+ # This is an RVM Project .rvmrc file, used to automatically load the ruby
4
+ # development environment upon cd'ing into the directory
5
+
6
+ # First we specify our desired <ruby>[@<gemset>], the @gemset name is optional,
7
+ # Only full ruby name is supported here, for short names use:
8
+ # echo "rvm use 1.9.3" > .rvmrc
9
+ environment_id="ruby-1.9.3-p374@faker-isbn"
10
+
11
+ # Uncomment the following lines if you want to verify rvm version per project
12
+ # rvmrc_rvm_version="1.18.5 (stable)" # 1.10.1 seams as a safe start
13
+ # eval "$(echo ${rvm_version}.${rvmrc_rvm_version} | awk -F. '{print "[[ "$1*65536+$2*256+$3" -ge "$4*65536+$5*256+$6" ]]"}' )" || {
14
+ # echo "This .rvmrc file requires at least RVM ${rvmrc_rvm_version}, aborting loading."
15
+ # return 1
16
+ # }
17
+
18
+ # First we attempt to load the desired environment directly from the environment
19
+ # file. This is very fast and efficient compared to running through the entire
20
+ # CLI and selector. If you want feedback on which environment was used then
21
+ # insert the word 'use' after --create as this triggers verbose mode.
22
+ if [[ -d "${rvm_path:-$HOME/.rvm}/environments"
23
+ && -s "${rvm_path:-$HOME/.rvm}/environments/$environment_id" ]]
24
+ then
25
+ \. "${rvm_path:-$HOME/.rvm}/environments/$environment_id"
26
+ [[ -s "${rvm_path:-$HOME/.rvm}/hooks/after_use" ]] &&
27
+ \. "${rvm_path:-$HOME/.rvm}/hooks/after_use" || true
28
+ else
29
+ # If the environment file has not yet been created, use the RVM CLI to select.
30
+ rvm --create "$environment_id" || {
31
+ echo "Failed to create RVM environment '${environment_id}'."
32
+ return 1
33
+ }
34
+ fi
35
+
36
+ # If you use bundler, this might be useful to you:
37
+ # if [[ -s Gemfile ]] && {
38
+ # ! builtin command -v bundle >/dev/null ||
39
+ # builtin command -v bundle | GREP_OPTIONS= \grep $rvm_path/bin/bundle >/dev/null
40
+ # }
41
+ # then
42
+ # printf "%b" "The rubygem 'bundler' is not installed. Installing it now.\n"
43
+ # gem install bundler
44
+ # fi
45
+ # if [[ -s Gemfile ]] && builtin command -v bundle >/dev/null
46
+ # then
47
+ # bundle install | GREP_OPTIONS= \grep -vE '^Using|Your bundle is complete'
48
+ # fi
data/README.md CHANGED
@@ -1,4 +1,6 @@
1
1
  faker-isbn
2
2
  ==========
3
3
 
4
- faker-isbn
4
+ ## Usage
5
+
6
+ `Faker::ISBN.thirteen_digit`
data/faker-isbn.gemspec CHANGED
@@ -6,8 +6,8 @@ Gem::Specification.new do |s|
6
6
  s.name = "faker-isbn"
7
7
  s.version = Faker::ISBN::VERSION
8
8
  s.platform = Gem::Platform::RUBY
9
- s.authors = ["Nick Zalabak"]
10
- s.email = ["techwhizbang@gmail.com"]
9
+ s.authors = ["Nick Zalabak", "Nina Revko"]
10
+ s.email = ["techwhizbang@gmail.com", "9nikka6@gmail.com"]
11
11
  s.homepage = ""
12
12
  s.summary = %q{Generates fake 13 digit ISBN's for your test suite/database}
13
13
  s.description = %q{A faker extension that generates 13 digit ISBN's for your test suite/database}
@@ -18,5 +18,6 @@ Gem::Specification.new do |s|
18
18
  s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
19
19
  s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
20
20
  s.require_paths = ["lib"]
21
- s.add_dependency(%q<bookland>, ["~> 2.0.0"])
21
+ s.add_development_dependency(%q<bookland>, ["~> 2.0.0"])
22
+ s.add_development_dependency(%q<rspec>, ["~> 2.12.0"])
22
23
  end
@@ -1,5 +1,5 @@
1
1
  module Faker
2
2
  module ISBN
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  end
data/lib/faker-isbn.rb CHANGED
@@ -1,31 +1,36 @@
1
1
  require 'rubygems'
2
- require 'bookland'
3
2
 
4
3
  module Faker
5
4
  class ISBN
6
- @thirteen_digit_multipliers = [1, 3]
7
- @thirteen_digit_modulo = 10
8
5
 
9
- def self.thirteen_digit
10
- isbn_13 = ""
11
- until Bookland::ISBN.new(isbn_13).valid? do
12
- isbn_digit_array = [9, 7, 8]
13
- (1..9).each { isbn_digit_array << rand(10) }
14
- isbn_digit_array << calculate_thirteenth_check_digit(isbn_digit_array)
15
- isbn_13 = isbn_digit_array.join
16
- end
6
+ def self.thirteen_digit(rand_seed=rand(1000))
7
+ isbn_13 = (978000000000+rand_seed).to_s
8
+
9
+ isbn_array = Array.new
10
+ isbn_13.split(//).each { |digit| isbn_array << digit.to_i }
11
+ dot_product = [isbn_array, [1, 3, 1, 3, 1, 3, 1, 3, 1, 3, 1, 3]].transpose.inject(0) { |result, pair| result += pair[0]*pair[1]; result }
12
+ check_digit = 10 - (dot_product % 10)
13
+ check_digit = 0 if check_digit == 10
14
+ isbn_13 << check_digit.to_s
17
15
  isbn_13
18
16
  end
19
17
 
20
- private
18
+ def self.ten_digit
19
+ nine_digits = "0#{rand(10000000..99999999)}"
20
+ multipliers = (2..10).to_a.reverse
21
+ sum_of_products = [nine_digits.chars.map(&:to_i), multipliers].transpose.inject(0) { |memo, obj| memo += obj[0] * obj[1]; memo }
22
+ remainder = sum_of_products % 11
21
23
 
22
- def self.calculate_thirteenth_check_digit(twelve_digit_array)
23
- sum = 0
24
- twelve_digit_array.each_with_index do |digit, index|
25
- multiplier = (index % 2 == 0) ? @thirteen_digit_multipliers[0] : @thirteen_digit_multipliers[1]
26
- sum += digit * multiplier
24
+ if (remainder == 0)
25
+ tenth_digit = 0
26
+ else
27
+ tenth_digit = 11 - remainder
28
+ tenth_digit = "X" if tenth_digit == 10
27
29
  end
28
- @thirteen_digit_modulo - (sum % @thirteen_digit_modulo)
30
+
31
+ ten_digit = nine_digits << tenth_digit.to_s
32
+ ten_digit
29
33
  end
34
+
30
35
  end
31
36
  end
data/spec/isbn_spec.rb CHANGED
@@ -5,7 +5,11 @@ describe Faker::ISBN do
5
5
  describe "#thirteen_digit" do
6
6
 
7
7
  it 'generates a valid 13 digit ISBN' do
8
- 20.times { Bookland::ISBN.new(Faker::ISBN.thirteen_digit).valid?.should be_true }
8
+ 20.times { Bookland::ISBN.valid?(Faker::ISBN.thirteen_digit).should be_true }
9
+ end
10
+
11
+ it 'generates a valid 10 digit ISBN' do
12
+ 20.times { Bookland::ISBN10.valid?(Faker::ISBN.ten_digit).should be_true }
9
13
  end
10
14
 
11
15
  end
data/spec/spec_helper.rb CHANGED
@@ -1,4 +1,5 @@
1
1
  require 'rubygems'
2
2
  require 'rspec'
3
+ require 'bookland'
3
4
 
4
5
  require File.expand_path(File.dirname(__FILE__) + "/../lib/faker-isbn")
metadata CHANGED
@@ -1,35 +1,59 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: faker-isbn
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.1
4
+ version: 0.0.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
8
8
  - Nick Zalabak
9
+ - Nina Revko
9
10
  autorequire:
10
11
  bindir: bin
11
12
  cert_chain: []
12
- date: 2013-01-14 00:00:00.000000000 Z
13
+ date: 2013-02-14 00:00:00.000000000 Z
13
14
  dependencies:
14
15
  - !ruby/object:Gem::Dependency
15
16
  name: bookland
16
- requirement: &70121070842280 !ruby/object:Gem::Requirement
17
+ requirement: !ruby/object:Gem::Requirement
17
18
  none: false
18
19
  requirements:
19
20
  - - ~>
20
21
  - !ruby/object:Gem::Version
21
22
  version: 2.0.0
22
- type: :runtime
23
+ type: :development
23
24
  prerelease: false
24
- version_requirements: *70121070842280
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ none: false
27
+ requirements:
28
+ - - ~>
29
+ - !ruby/object:Gem::Version
30
+ version: 2.0.0
31
+ - !ruby/object:Gem::Dependency
32
+ name: rspec
33
+ requirement: !ruby/object:Gem::Requirement
34
+ none: false
35
+ requirements:
36
+ - - ~>
37
+ - !ruby/object:Gem::Version
38
+ version: 2.12.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: 2.12.0
25
47
  description: A faker extension that generates 13 digit ISBN's for your test suite/database
26
48
  email:
27
49
  - techwhizbang@gmail.com
50
+ - 9nikka6@gmail.com
28
51
  executables: []
29
52
  extensions: []
30
53
  extra_rdoc_files: []
31
54
  files:
32
55
  - .gitignore
56
+ - .rvmrc
33
57
  - Gemfile
34
58
  - README.md
35
59
  - Rakefile
@@ -58,7 +82,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
58
82
  version: '0'
59
83
  requirements: []
60
84
  rubyforge_project: faker-isbn
61
- rubygems_version: 1.8.10
85
+ rubygems_version: 1.8.25
62
86
  signing_key:
63
87
  specification_version: 3
64
88
  summary: Generates fake 13 digit ISBN's for your test suite/database