coupon_code 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +14 -0
- data/.travis.yml +10 -0
- data/Gemfile +6 -0
- data/LICENSE.txt +22 -0
- data/README-ko.md +61 -0
- data/README.md +66 -0
- data/Rakefile +13 -0
- data/bin/coupon-code +6 -0
- data/coupon_code.gemspec +24 -0
- data/lib/coupon_code.rb +50 -0
- data/lib/coupon_code/version.rb +3 -0
- data/test/coupon_code_test.rb +98 -0
- data/test/test_helper.rb +7 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 135fb183f47db7cf01f192063d01aa2b215da141
|
4
|
+
data.tar.gz: 336135ed4bb5531220cb533fc2081c24f1815268
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4654f9a3b3b68f39509b038e5c877ca30e03c3c37fe2f38c56585c46254b725c0259ff62e2457010b4a82ad2e1d62fefe9a1358a688d29ab67d11b493fa0d036
|
7
|
+
data.tar.gz: bb8f29e3932e5e2c2f97cab42e4af463bda8fd4382e2d5bf996d905185cc60eb6b445dcac3fa3cf79b0995cdcfc6f757109c19c1088f733103e967cc3c8d9aee
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Copyright (c) 2014 Sanghyun Park
|
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-ko.md
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
# CouponCode
|
2
|
+
|
3
|
+
이 루비 젬(Ruby Gem)은 쿠폰 코드를 만들고 검증하는 기능을 제공합니다.
|
4
|
+
|
5
|
+
이 젬(Gem)은 [Grant][grant]가 작성한 [Algorithm::CouponCode][couponcode]를 루비(Ruby) 언어로 옮겨 구현한 것입니다.
|
6
|
+
하지만 현 시점에는 원작이 가진 기능 중 기본적인 코드 생성/검증 기능만 구현되어 있다는 점을 주지해 주십시오. 평문을 입력해 코드를 생성하는 기능이나
|
7
|
+
자동교정, jQuery 플러그인 등은 포함되어있지 않습니다.
|
8
|
+
|
9
|
+
이 루비 젬은 [https://stripes.co.kr](https://stripes.co.kr)를 위해 개발됐습니다.
|
10
|
+
|
11
|
+
## 설치
|
12
|
+
|
13
|
+
애플리케이션의 Gemfile에 아래의 코드 한 줄을 추가하십시오:
|
14
|
+
|
15
|
+
gem 'coupon_code'
|
16
|
+
|
17
|
+
그리고 다음 명령을 실행하십시오:
|
18
|
+
|
19
|
+
$ bundle
|
20
|
+
|
21
|
+
또는 아래와 같이 직접 설치해 사용하십시오:
|
22
|
+
|
23
|
+
$ gem install coupon_code
|
24
|
+
|
25
|
+
## 사용방법
|
26
|
+
|
27
|
+
>> require 'coupon_code'
|
28
|
+
>> code = CouponCode.generate
|
29
|
+
=> "1K7Q-CTFM-LMTC"
|
30
|
+
>> CouponCode.validate(code)
|
31
|
+
=> "1K7Q-CTFM-LMTC"
|
32
|
+
>> CouponCode.validate('1K7Q-CTFM-LMTO') # Invalid code
|
33
|
+
=> nil
|
34
|
+
|
35
|
+
## 테스팅
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
$ bundle exec rake test
|
39
|
+
```
|
40
|
+
## Thanks to
|
41
|
+
|
42
|
+
- 펄로 작성된 [Grant McLean][grant]의 [Algorithm::CouponCode][couponcode]
|
43
|
+
- 자바스크립트로 작성된 [Andrew Chilton][chilts]의 [NodeJS implementation][node-couponcode]
|
44
|
+
|
45
|
+
## 라이센스
|
46
|
+
|
47
|
+
MIT. 자세한 내용은 [LICENSE][license]를 읽어주십시오.
|
48
|
+
|
49
|
+
## 기여하기
|
50
|
+
|
51
|
+
1. 이 저장소를 포크합니다.
|
52
|
+
2. 새 기능을 위한 브랜치를 만듭니다. (`git checkout -b my-new-feature`)
|
53
|
+
3. 작업한 변경 내역을 커밋합니다. (`git commit -am 'Add some feature'`)
|
54
|
+
4. 깃헙 저장소의 브랜치에 푸시합니다. (`git push origin my-new-feature`)
|
55
|
+
5. 풀 리퀘스트(Pull Request)를 해주십시오.
|
56
|
+
|
57
|
+
[grant]: https://github.com/grantm/
|
58
|
+
[couponcode]: https://github.com/grantm/Algorithm-CouponCode
|
59
|
+
[chilts]: https://github.com/chilts
|
60
|
+
[node-couponcode]: https://github.com/chilts/node-coupon-code
|
61
|
+
[license]: https://raw.github.com/appsattic/node-coupon-code/master/LICENSE
|
data/README.md
ADDED
@@ -0,0 +1,66 @@
|
|
1
|
+
[![Build Status](https://travis-ci.org/baxang/coupon-code.svg)](https://travis-ci.org/baxang/coupon-code)
|
2
|
+
[![Code Climate](https://codeclimate.com/github/baxang/coupon-code/badges/gpa.svg)](https://codeclimate.com/github/baxang/coupon-code)
|
3
|
+
|
4
|
+
# CouponCode
|
5
|
+
|
6
|
+
CouponCode gem generates and validates coupon codes.
|
7
|
+
|
8
|
+
It is a Ruby implementation of [Grant][grant]'s [Algorithm::CouponCode][couponcode],
|
9
|
+
but please note that only basic generation/validation functionality implemented at the moment and
|
10
|
+
some of the features the CPAN module provides such as feeding a plaintext, auto-correct, and jQuery plugin are
|
11
|
+
not yet implemented.
|
12
|
+
|
13
|
+
This gem is developed for https://stripes.co.kr
|
14
|
+
|
15
|
+
## Installation
|
16
|
+
|
17
|
+
Add this line to your application's Gemfile:
|
18
|
+
|
19
|
+
gem 'coupon_code'
|
20
|
+
|
21
|
+
And then execute:
|
22
|
+
|
23
|
+
$ bundle
|
24
|
+
|
25
|
+
Or install it yourself as:
|
26
|
+
|
27
|
+
$ gem install coupon_code
|
28
|
+
|
29
|
+
## Synopsis
|
30
|
+
|
31
|
+
>> require 'coupon_code'
|
32
|
+
>> code = CouponCode.generate
|
33
|
+
=> "1K7Q-CTFM-LMTC"
|
34
|
+
>> CouponCode.validate(code)
|
35
|
+
=> "1K7Q-CTFM-LMTC"
|
36
|
+
>> CouponCode.validate('1K7Q-CTFM-LMTO') # Invalid code
|
37
|
+
=> nil
|
38
|
+
|
39
|
+
## Testing
|
40
|
+
|
41
|
+
```ruby
|
42
|
+
$ bundle exec rake test
|
43
|
+
```
|
44
|
+
|
45
|
+
## Thanks to
|
46
|
+
|
47
|
+
- [Grant McLean][grant]'s [Algorithm::CouponCode][couponcode] in Perl
|
48
|
+
- [Andrew Chilton][chilts]'s [NodeJS implementation][node-couponcode] in JavaScript
|
49
|
+
|
50
|
+
## License
|
51
|
+
|
52
|
+
MIT. See [LICENSE][license] for more details.
|
53
|
+
|
54
|
+
## Contributing
|
55
|
+
|
56
|
+
1. Fork it ( https://github.com/baxang/coupon-code/fork )
|
57
|
+
2. Create your feature branch (`git checkout -b my-new-feature`)
|
58
|
+
3. Commit your changes (`git commit -am 'Add some feature'`)
|
59
|
+
4. Push to the branch (`git push origin my-new-feature`)
|
60
|
+
5. Create a new Pull Request
|
61
|
+
|
62
|
+
[grant]: https://github.com/grantm/
|
63
|
+
[couponcode]: https://github.com/grantm/Algorithm-CouponCode
|
64
|
+
[chilts]: https://github.com/chilts
|
65
|
+
[node-couponcode]: https://github.com/chilts/node-coupon-code
|
66
|
+
[license]: https://raw.github.com/appsattic/node-coupon-code/master/LICENSE
|
data/Rakefile
ADDED
data/bin/coupon-code
ADDED
data/coupon_code.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 'coupon_code/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'coupon_code'
|
8
|
+
spec.version = CouponCode::VERSION
|
9
|
+
spec.authors = ['Sanghyun Park']
|
10
|
+
spec.email = ['sh@baxang.com']
|
11
|
+
spec.summary = %q{Generate and validate coupon codes.}
|
12
|
+
spec.description = %q{A Ruby implementation of Perl's Algorithm::CouponCode CPAN module.}
|
13
|
+
spec.homepage = 'https://github.com/baxang/coupon-code'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0")
|
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.7'
|
22
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
23
|
+
spec.add_development_dependency 'minitest', '~> 5.5'
|
24
|
+
end
|
data/lib/coupon_code.rb
ADDED
@@ -0,0 +1,50 @@
|
|
1
|
+
require "coupon_code/version"
|
2
|
+
require 'securerandom'
|
3
|
+
require 'digest/sha1'
|
4
|
+
|
5
|
+
module CouponCode
|
6
|
+
SYMBOL = '0123456789ABCDEFGHJKLMNPQRTUVWXY'
|
7
|
+
PARTS = 3
|
8
|
+
LENGTH = 4
|
9
|
+
|
10
|
+
def self.generate(options = { parts: PARTS })
|
11
|
+
num_parts = options.delete(:parts)
|
12
|
+
|
13
|
+
parts = []
|
14
|
+
(1..num_parts).each do |i|
|
15
|
+
part = ''
|
16
|
+
(1...LENGTH).each do |j|
|
17
|
+
part << randome_symbol
|
18
|
+
end
|
19
|
+
part << checkdigit_alg_1(part, i)
|
20
|
+
parts << part
|
21
|
+
end
|
22
|
+
parts.join('-')
|
23
|
+
end
|
24
|
+
|
25
|
+
def self.validate(orig, num_parts = PARTS)
|
26
|
+
code = orig.upcase
|
27
|
+
code.gsub!(/[^0-9A-Z]+/, '')
|
28
|
+
parts = code.scan(/[0-9A-Z]{#{LENGTH}}/)
|
29
|
+
return if parts.length != num_parts
|
30
|
+
parts.each_with_index do |part, i|
|
31
|
+
data = part[0...(LENGTH - 1)]
|
32
|
+
check = part[-1]
|
33
|
+
return if check != checkdigit_alg_1(data, i + 1)
|
34
|
+
end
|
35
|
+
parts.join('-')
|
36
|
+
end
|
37
|
+
|
38
|
+
def self.checkdigit_alg_1(orig, check)
|
39
|
+
|
40
|
+
orig.split('').each_with_index do |c, i|
|
41
|
+
k = SYMBOL.index(c)
|
42
|
+
check = check * 19 + k
|
43
|
+
end
|
44
|
+
SYMBOL[ check % 31 ]
|
45
|
+
end
|
46
|
+
|
47
|
+
def self.randome_symbol
|
48
|
+
SYMBOL[ rand(SYMBOL.length) ]
|
49
|
+
end
|
50
|
+
end
|
@@ -0,0 +1,98 @@
|
|
1
|
+
require 'test_helper'
|
2
|
+
|
3
|
+
describe CouponCode do
|
4
|
+
describe 'Generator' do
|
5
|
+
def generate(*args)
|
6
|
+
CouponCode.generate(*args)
|
7
|
+
end
|
8
|
+
|
9
|
+
it 'should be loaded.' do
|
10
|
+
CouponCode.must_respond_to(:generate)
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'should generate a code' do
|
14
|
+
generate.wont_be_empty
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'should only contain uppercase letters, digits, and dashes.' do
|
18
|
+
generate.must_match(/^[0-9A-Z-]+$/)
|
19
|
+
end
|
20
|
+
|
21
|
+
it 'should look like XXXX-XXXX-XXXX.' do
|
22
|
+
generate.must_match(/^\w{4}-\w{4}-\w{4}$/)
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'should generate different codes.' do
|
26
|
+
code1 = generate
|
27
|
+
code2 = generate
|
28
|
+
code1.wont_equal(code2)
|
29
|
+
end
|
30
|
+
|
31
|
+
it 'should generate an arbitrary number of parts.' do
|
32
|
+
generate(parts: 2).must_match(/^\w{4}-\w{4}$/)
|
33
|
+
generate(parts: 5).must_match(/^\w{4}-\w{4}-\w{4}-\w{4}-\w{4}$/)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
|
37
|
+
describe 'Validator' do
|
38
|
+
it 'should be loaded.' do
|
39
|
+
CouponCode.must_respond_to(:validate)
|
40
|
+
end
|
41
|
+
|
42
|
+
it 'should fail to validate invalid code.' do
|
43
|
+
CouponCode.validate('').must_equal(nil)
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'should accept a valid code.' do
|
47
|
+
CouponCode.validate('1K7Q-CTFM-LMTC').wont_be_nil
|
48
|
+
end
|
49
|
+
|
50
|
+
it 'should reject a short code.' do
|
51
|
+
CouponCode.validate('1K7Q-CTFM').must_be_nil
|
52
|
+
end
|
53
|
+
|
54
|
+
it 'should accept a short code with correct parts.' do
|
55
|
+
CouponCode.validate('1K7Q-CTFM', 2).wont_be_nil
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should reject a short code with wrong parts.' do
|
59
|
+
CouponCode.validate('CTFM-1K7Q', 2).must_be_nil
|
60
|
+
end
|
61
|
+
|
62
|
+
it 'should fix and validate a lowercase code.' do
|
63
|
+
code = '1k7q-ctfm-lmtc'
|
64
|
+
CouponCode.validate(code.downcase).must_equal(code.upcase)
|
65
|
+
end
|
66
|
+
|
67
|
+
it 'should validate alternative separators.' do
|
68
|
+
code = '1k7q/ctfm/lmtc'
|
69
|
+
CouponCode.validate(code).must_equal('1K7Q-CTFM-LMTC')
|
70
|
+
|
71
|
+
code = '1k7q ctfm lmtc'
|
72
|
+
CouponCode.validate(code).must_equal('1K7Q-CTFM-LMTC')
|
73
|
+
|
74
|
+
code = '1k7qctfmlmtc'
|
75
|
+
CouponCode.validate(code).must_equal('1K7Q-CTFM-LMTC')
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'should valid code-pretest.' do
|
79
|
+
CouponCode.validate('1K7Q', 1).wont_be_nil
|
80
|
+
CouponCode.validate('1K7C', 1).must_be_nil
|
81
|
+
|
82
|
+
CouponCode.validate('1K7Q-CTFM', 2).wont_be_nil
|
83
|
+
CouponCode.validate('1K7Q-CTFW', 2).must_be_nil
|
84
|
+
|
85
|
+
CouponCode.validate('1K7Q-CTFM-LMTC', 3).wont_be_nil
|
86
|
+
CouponCode.validate('1K7Q-CTFM-LMT1', 3).must_be_nil
|
87
|
+
|
88
|
+
CouponCode.validate('7YQH-1FU7-E1HX-0BG9', 4).wont_be_nil
|
89
|
+
CouponCode.validate('7YQH-1FU7-E1HX-0BGP', 4).must_be_nil
|
90
|
+
|
91
|
+
CouponCode.validate('YENH-UPJK-PTE0-20U6-QYME', 5).wont_be_nil
|
92
|
+
CouponCode.validate('YENH-UPJK-PTE0-20U6-QYMT', 5).must_be_nil
|
93
|
+
|
94
|
+
CouponCode.validate('YENH-UPJK-PTE0-20U6-QYME-RBK1', 6).wont_be_nil
|
95
|
+
CouponCode.validate('YENH-UPJK-PTE0-20U6-QYME-RBK2', 6).must_be_nil
|
96
|
+
end
|
97
|
+
end
|
98
|
+
end
|
data/test/test_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: coupon_code
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.0.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Sanghyun Park
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2014-12-18 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.7'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.7'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.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: '5.5'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '5.5'
|
55
|
+
description: A Ruby implementation of Perl's Algorithm::CouponCode CPAN module.
|
56
|
+
email:
|
57
|
+
- sh@baxang.com
|
58
|
+
executables:
|
59
|
+
- coupon-code
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- Gemfile
|
66
|
+
- LICENSE.txt
|
67
|
+
- README-ko.md
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/coupon-code
|
71
|
+
- coupon_code.gemspec
|
72
|
+
- lib/coupon_code.rb
|
73
|
+
- lib/coupon_code/version.rb
|
74
|
+
- test/coupon_code_test.rb
|
75
|
+
- test/test_helper.rb
|
76
|
+
homepage: https://github.com/baxang/coupon-code
|
77
|
+
licenses:
|
78
|
+
- MIT
|
79
|
+
metadata: {}
|
80
|
+
post_install_message:
|
81
|
+
rdoc_options: []
|
82
|
+
require_paths:
|
83
|
+
- lib
|
84
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
85
|
+
requirements:
|
86
|
+
- - ">="
|
87
|
+
- !ruby/object:Gem::Version
|
88
|
+
version: '0'
|
89
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
90
|
+
requirements:
|
91
|
+
- - ">="
|
92
|
+
- !ruby/object:Gem::Version
|
93
|
+
version: '0'
|
94
|
+
requirements: []
|
95
|
+
rubyforge_project:
|
96
|
+
rubygems_version: 2.2.2
|
97
|
+
signing_key:
|
98
|
+
specification_version: 4
|
99
|
+
summary: Generate and validate coupon codes.
|
100
|
+
test_files:
|
101
|
+
- test/coupon_code_test.rb
|
102
|
+
- test/test_helper.rb
|