ruby-short_url 1.1.0 → 1.2.0
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.
- checksums.yaml +4 -4
- data/.github/workflows/coverage.yml +18 -0
- data/.github/workflows/rubocop.yml +0 -6
- data/.github/workflows/test.yml +17 -0
- data/.rubocop.yml +3 -3
- data/README.md +11 -3
- data/lib/ruby/short_url/encoder.rb +27 -28
- data/lib/ruby/short_url/version.rb +1 -1
- data/ruby-short_url.gemspec +1 -1
- metadata +5 -5
- data/.rubocop_todo.yml +0 -14
- data/.travis.yml +0 -13
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 969037b93fe75fa29b45de3cc6e931886b16a9d9485530f59efb0f64eb80c578
|
4
|
+
data.tar.gz: dd827e2a29013e165c08e9c1f010452f9e0b054abd208a2fde16cc7c5e0c4193
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/.rubocop.yml
CHANGED
data/README.md
CHANGED
@@ -1,8 +1,8 @@
|
|
1
1
|
# Ruby::ShortUrl
|
2
2
|
|
3
|
-
[](https://travis-ci.org/toshimaru/ruby-short_url)
|
4
|
-

|
5
3
|
[](http://badge.fury.io/rb/ruby-short_url)
|
4
|
+

|
5
|
+

|
6
6
|
[](https://codeclimate.com/github/toshimaru/ruby-short_url/coverage)
|
7
7
|
[](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
|
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
|
17
|
+
@mapping = (0...block_size).to_a.reverse!
|
19
18
|
end
|
20
19
|
|
21
|
-
def encode_url(
|
22
|
-
enbase(encode(
|
20
|
+
def encode_url(int, min_length: MIN_LENGTH)
|
21
|
+
enbase(encode(int), min_length)
|
23
22
|
end
|
24
23
|
|
25
|
-
def decode_url(
|
26
|
-
decode(debase(
|
24
|
+
def decode_url(str)
|
25
|
+
decode(debase(str))
|
27
26
|
end
|
28
27
|
|
29
28
|
private
|
30
29
|
|
31
|
-
def encode(
|
32
|
-
(
|
30
|
+
def encode(int)
|
31
|
+
(int & ~@mask) | _encode(int & @mask)
|
33
32
|
end
|
34
33
|
|
35
|
-
def decode(
|
36
|
-
(
|
34
|
+
def decode(int)
|
35
|
+
(int & ~@mask) | _decode(int & @mask)
|
37
36
|
end
|
38
37
|
|
39
|
-
def enbase(
|
40
|
-
result = _enbase(
|
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(
|
48
|
-
|
49
|
-
|
50
|
-
|
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(
|
55
|
-
@mapping.
|
56
|
-
_calc_result(result,
|
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(
|
61
|
-
@mapping.
|
62
|
-
_calc_result(result,
|
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,
|
67
|
-
return result |= (1 <<
|
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(
|
73
|
-
|
74
|
-
return @alphabet[
|
71
|
+
def _enbase(int)
|
72
|
+
len = @alphabet.length
|
73
|
+
return @alphabet[int] if int < len
|
75
74
|
|
76
|
-
_enbase(
|
75
|
+
_enbase(int / len) + @alphabet[int % len]
|
77
76
|
end
|
78
77
|
end
|
79
78
|
end
|
data/ruby-short_url.gemspec
CHANGED
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.
|
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-
|
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.
|
46
|
+
version: 2.4.0
|
47
47
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
48
48
|
requirements:
|
49
49
|
- - ">="
|
data/.rubocop_todo.yml
DELETED
@@ -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'
|
data/.travis.yml
DELETED
@@ -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
|