ruby-short_url 1.1.0 → 1.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 0d1ec6226599a832509efde0026d2e617ac9e8b624afdf4659e9b6b5e22c695d
4
- data.tar.gz: 1ebc6a2ab572cd422f7cd72ec401cedb3355d6bbc79dd5432337b742758ac18e
3
+ metadata.gz: 969037b93fe75fa29b45de3cc6e931886b16a9d9485530f59efb0f64eb80c578
4
+ data.tar.gz: dd827e2a29013e165c08e9c1f010452f9e0b054abd208a2fde16cc7c5e0c4193
5
5
  SHA512:
6
- metadata.gz: d95b0b18faaa8dd3b01e59d4aa26efab02195c75c049298eb253a8f569ca10939a12789e6c84584d8e461d7c06efac6b87c6802cdc56dc9ce7d0ac6a0922171e
7
- data.tar.gz: 345d2afce842c154cee3847b08aa62a0a86775537c494f7078f478b2105cde2b93641c8072bb51110e6d7a1d4fc75ee1f0bd02fa23409e2d81bde33e01dc775f
6
+ metadata.gz: 123a3efa9602bb2478dcca44eb96a8a1b3d82f90ffea47013e37a43b03e8d68df65cf37673134c40331d1a46b7729cee7e7b3e85c3bea204daa78b24abb31c46
7
+ data.tar.gz: 948c137acda8b9865289c633f935e3a59e05b587771cd1fe78d0e1f096208f3e2951a93cb4ee09fd34ed05cbb24f2ae3a87c8e1b15089e695cae847f81214e41
@@ -0,0 +1,18 @@
1
+ name: Coverage
2
+ on: [push]
3
+ jobs:
4
+ build:
5
+ runs-on: ubuntu-latest
6
+ name: codeclimate report
7
+ steps:
8
+ - uses: actions/checkout@v2
9
+ - name: Set up Ruby
10
+ uses: actions/setup-ruby@v1
11
+ with:
12
+ ruby-version: 2.6
13
+ - run: bundle install
14
+ - uses: paambaati/codeclimate-action@v2.5.4
15
+ env:
16
+ CC_TEST_REPORTER_ID: 357da37cbdcc472b4b33ba5f6e8a6fcc14ebcb41bb951b5133c547818ad6fbb3
17
+ with:
18
+ coverageCommand: bundle exec rake
@@ -11,10 +11,4 @@ jobs:
11
11
  with:
12
12
  ruby-version: 2.6
13
13
  - run: bundle install
14
- - uses: actions/cache@v1
15
- with:
16
- path: vendor/bundle
17
- key: ${{ runner.os }}-gems-${{ hashFiles('**/Gemfile.lock') }}
18
- restore-keys: |
19
- ${{ runner.os }}-gems-
20
14
  - run: bundle exec rubocop
@@ -0,0 +1,17 @@
1
+ name: Test
2
+ on: [pull_request]
3
+ jobs:
4
+ build:
5
+ strategy:
6
+ matrix:
7
+ ruby: [2.4, 2.5, 2.6, 2.7]
8
+ runs-on: ubuntu-latest
9
+ name: Test
10
+ steps:
11
+ - uses: actions/checkout@v2
12
+ - name: Set up Ruby
13
+ uses: actions/setup-ruby@v1
14
+ with:
15
+ ruby-version: ${{ matrix.ruby }}
16
+ - run: bundle install
17
+ - run: bundle exec rake
@@ -1,7 +1,7 @@
1
- inherit_from: .rubocop_todo.yml
2
-
3
1
  AllCops:
4
- TargetRubyVersion: 2.3
2
+ TargetRubyVersion: 2.4
3
+ Style/HashEachMethods:
4
+ Enabled: false
5
5
  Style/HashTransformKeys:
6
6
  Enabled: true
7
7
  Style/HashTransformValues:
data/README.md CHANGED
@@ -1,8 +1,8 @@
1
1
  # Ruby::ShortUrl
2
2
 
3
- [![Build Status](https://travis-ci.org/toshimaru/ruby-short_url.svg?branch=master)](https://travis-ci.org/toshimaru/ruby-short_url)
4
- ![RuboCop](https://github.com/toshimaru/ruby-short_url/workflows/RuboCop/badge.svg)
5
3
  [![Gem Version](https://badge.fury.io/rb/ruby-short_url.svg)](http://badge.fury.io/rb/ruby-short_url)
4
+ ![Test](https://github.com/toshimaru/ruby-short_url/workflows/Test/badge.svg)
5
+ ![RuboCop](https://github.com/toshimaru/ruby-short_url/workflows/RuboCop/badge.svg)
6
6
  [![Test Coverage](https://codeclimate.com/github/toshimaru/ruby-short_url/badges/coverage.svg)](https://codeclimate.com/github/toshimaru/ruby-short_url/coverage)
7
7
  [![Code Climate](https://codeclimate.com/github/toshimaru/ruby-short_url/badges/gpa.svg)](https://codeclimate.com/github/toshimaru/ruby-short_url)
8
8
 
@@ -11,6 +11,10 @@
11
11
  Ruby implementation for generating Tiny URL. The implementation is based on [python-short_url](https://github.com/Alir3z4/python-short_url).
12
12
 
13
13
  > A bit-shuffling approach is used to avoid generating consecutive, predictable URLs. However, the algorithm is deterministic and will guarantee that no collisions will occur.
14
+ >
15
+ > The URL alphabet is fully customizable and may contain any number of characters.
16
+
17
+ > The intended use is that incrementing, consecutive integers will be used as keys to generate the short URLs. For example, when creating a new URL, the unique integer ID assigned by a database could be used to generate the URL by using this module. Or a simple counter may be used. As long as the same integer is not used twice, the same short URL will not be generated twice.
14
18
 
15
19
  More detail is [here](https://github.com/Alir3z4/python-short_url#short-url-generator).
16
20
 
@@ -40,7 +44,7 @@ Ruby::ShortUrl::Encoder.new.encode_url(123456) # => "00crI"
40
44
  Ruby::ShortUrl::Encoder.new.decode_url("00crI") # => 123456
41
45
  ```
42
46
 
43
- ### Create your own short URL
47
+ ### Create your own URL Shortner
44
48
 
45
49
  ```rb
46
50
  class CustomEncoder < Ruby::ShortUrl::Encoder
@@ -61,6 +65,8 @@ end
61
65
  # => "00011"
62
66
  # > custom_encoder.encode_url(3)
63
67
  # => "00033"
68
+ # > custom_encoder.encode_url(4)
69
+ # => "0000a"
64
70
  #
65
71
  # > custom_encoder.decode_url("00022")
66
72
  # => 1
@@ -68,6 +74,8 @@ end
68
74
  # => 2
69
75
  # > custom_encoder.decode_url("00033")
70
76
  # => 3
77
+ # > custom_encoder.decode_url("0000a")
78
+ # => 4
71
79
  ```
72
80
 
73
81
  ## Development
@@ -13,67 +13,66 @@ module Ruby
13
13
  raise ArgumentError, 'block_size must be greater than 0.' unless block_size > 0
14
14
 
15
15
  @alphabet = alphabet
16
- @block_size = block_size
17
16
  @mask = (1 << block_size) - 1
18
- @mapping = (0..block_size - 1).to_a
17
+ @mapping = (0...block_size).to_a.reverse!
19
18
  end
20
19
 
21
- def encode_url(n, min_length: MIN_LENGTH)
22
- enbase(encode(n), min_length)
20
+ def encode_url(int, min_length: MIN_LENGTH)
21
+ enbase(encode(int), min_length)
23
22
  end
24
23
 
25
- def decode_url(n)
26
- decode(debase(n))
24
+ def decode_url(str)
25
+ decode(debase(str))
27
26
  end
28
27
 
29
28
  private
30
29
 
31
- def encode(n)
32
- (n & ~@mask) | _encode(n & @mask)
30
+ def encode(int)
31
+ (int & ~@mask) | _encode(int & @mask)
33
32
  end
34
33
 
35
- def decode(n)
36
- (n & ~@mask) | _decode(n & @mask)
34
+ def decode(int)
35
+ (int & ~@mask) | _decode(int & @mask)
37
36
  end
38
37
 
39
- def enbase(x, min_length)
40
- result = _enbase(x)
38
+ def enbase(int, min_length)
39
+ result = _enbase(int)
41
40
  padding_length = min_length - result.length
42
41
  padding_length = 0 if padding_length < 0
43
42
  padding = @alphabet[0] * padding_length
44
43
  padding.to_s + result.to_s
45
44
  end
46
45
 
47
- def debase(x)
48
- n = @alphabet.length
49
- x.split('').reverse.each_with_index.inject(0) do |result, (c, i)|
50
- result + @alphabet.index(c) * (n**i)
46
+ def debase(str)
47
+ len = @alphabet.length
48
+ str.split('').reverse.each_with_index.sum do |char, idx|
49
+ @alphabet.index(char) * (len**idx)
51
50
  end
52
51
  end
53
52
 
54
- def _encode(n)
55
- @mapping.reverse.each_with_index.inject(0) do |result, (b, i)|
56
- _calc_result(result, n, b, i)
53
+ def _encode(int)
54
+ @mapping.each_with_index.inject(0) do |result, (val, idx)|
55
+ _calc_result(result, int, val, idx)
57
56
  end
58
57
  end
59
58
 
60
- def _decode(n)
61
- @mapping.reverse.each_with_index.inject(0) do |result, (b, i)|
62
- _calc_result(result, n, i, b)
59
+ def _decode(int)
60
+ @mapping.each_with_index.inject(0) do |result, (val, idx)|
61
+ _calc_result(result, int, idx, val)
63
62
  end
64
63
  end
65
64
 
66
- def _calc_result(result, n, x, y)
67
- return result |= (1 << x) if n & (1 << y) != 0
65
+ def _calc_result(result, int, int1, int2)
66
+ return result |= (1 << int1) if int & (1 << int2) != 0
68
67
 
69
68
  result
70
69
  end
71
70
 
72
- def _enbase(x)
73
- n = @alphabet.length
74
- return @alphabet[x] if x < n
71
+ def _enbase(int)
72
+ len = @alphabet.length
73
+ return @alphabet[int] if int < len
75
74
 
76
- _enbase(x / n) + @alphabet[x % n]
75
+ _enbase(int / len) + @alphabet[int % len]
77
76
  end
78
77
  end
79
78
  end
@@ -2,6 +2,6 @@
2
2
 
3
3
  module Ruby
4
4
  module ShortUrl
5
- VERSION = '1.1.0'
5
+ VERSION = '1.2.0'
6
6
  end
7
7
  end
@@ -18,5 +18,5 @@ Gem::Specification.new do |spec|
18
18
  spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
19
  spec.require_paths = ['lib']
20
20
 
21
- spec.required_ruby_version = '>= 2.3.0'
21
+ spec.required_ruby_version = '>= 2.4.0'
22
22
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ruby-short_url
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.1.0
4
+ version: 1.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - toshimaru
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2020-03-09 00:00:00.000000000 Z
11
+ date: 2020-03-11 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description:
14
14
  email:
@@ -17,11 +17,11 @@ executables: []
17
17
  extensions: []
18
18
  extra_rdoc_files: []
19
19
  files:
20
+ - ".github/workflows/coverage.yml"
20
21
  - ".github/workflows/rubocop.yml"
22
+ - ".github/workflows/test.yml"
21
23
  - ".gitignore"
22
24
  - ".rubocop.yml"
23
- - ".rubocop_todo.yml"
24
- - ".travis.yml"
25
25
  - Gemfile
26
26
  - LICENSE
27
27
  - README.md
@@ -43,7 +43,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
43
43
  requirements:
44
44
  - - ">="
45
45
  - !ruby/object:Gem::Version
46
- version: 2.3.0
46
+ version: 2.4.0
47
47
  required_rubygems_version: !ruby/object:Gem::Requirement
48
48
  requirements:
49
49
  - - ">="
@@ -1,14 +0,0 @@
1
- # This configuration was generated by
2
- # `rubocop --auto-gen-config`
3
- # on 2020-03-09 23:12:09 +0900 using RuboCop version 0.80.1.
4
- # The point is for the user to remove these configuration records
5
- # one by one as the offenses are removed from the code base.
6
- # Note that changes in the inspected code, or installation of new
7
- # versions of RuboCop, may require this file to be generated again.
8
-
9
- # Offense count: 12
10
- # Configuration parameters: MinNameLength, AllowNamesEndingInNumbers, AllowedNames, ForbiddenNames.
11
- # AllowedNames: io, id, to, by, on, in, at, ip, db, os, pp
12
- Naming/MethodParameterName:
13
- Exclude:
14
- - 'lib/ruby/short_url/encoder.rb'
@@ -1,13 +0,0 @@
1
- language: ruby
2
- cache: bundler
3
- rvm:
4
- - 2.3.8
5
- - 2.4.8
6
- - 2.5.7
7
- - 2.6.5
8
- - 2.7.0
9
- after_success:
10
- - bundle exec codeclimate-test-reporter
11
- addons:
12
- code_climate:
13
- repo_token: 357da37cbdcc472b4b33ba5f6e8a6fcc14ebcb41bb951b5133c547818ad6fbb3