ruby-short_url 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: add30ce0a9800d2a6dd931d2989b781b1b688a31
4
+ data.tar.gz: 5a3848ee4884d37a00a843a790e5484dbc448f7b
5
+ SHA512:
6
+ metadata.gz: 3c044d20eb69742ea7ceb5945d3c7a6896c4734f7d621bfa4799060b47b6061ee19c5b07073a59fd8ae9244d98de9532b22ec3b992bf0e1ec1c01275bde3aad3
7
+ data.tar.gz: 441ce0ad31c1b1bfeb8c8bd48d1650dd2732bc5db2201d096dde8d8aae64d56981346c05e4ec8392a34a8ec0c5505d9cfe7dcda1bdc98f93439bde80feceee9e
data/.gitignore ADDED
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ - 2.1.0
5
+ - 2.0.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in ruby-short_url.gemspec
4
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,22 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 toshi
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
22
+
data/README.md ADDED
@@ -0,0 +1,49 @@
1
+ # Ruby::ShortUrl
2
+
3
+ Ruby implementation for generating Tiny URL. Ruby implementation of [python-short_url](https://github.com/Alir3z4/python-short_url)
4
+
5
+ > 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.
6
+
7
+ More detail is [here](https://github.com/Alir3z4/python-short_url#short-url-generator).
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'ruby-short_url'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install ruby-short_url
24
+
25
+ ## Usage
26
+
27
+ ```rb
28
+ # shorten id
29
+ Ruby::ShortUrl::Encoder.new.encode_url(123456) # => "00crI"
30
+
31
+ # decode encoded
32
+ Ruby::ShortUrl::Encoder.new.decode_url("00crI") # => 123456
33
+ ```
34
+
35
+ TODO: Customize alphabet/block_size/min_size.
36
+
37
+ ## Development
38
+
39
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
40
+
41
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release` to create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
42
+
43
+ ## Contributing
44
+
45
+ 1. Fork it ( https://github.com/[my-github-username]/ruby-short_url/fork )
46
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
47
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
48
+ 4. Push to the branch (`git push origin my-new-feature`)
49
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "ruby/short_url"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,80 @@
1
+ module Ruby
2
+ module ShortUrl
3
+ # Original imprementation is here: https://github.com/Alir3z4/python-short_url
4
+ class Encoder
5
+ DEFAULT_ALPHABET = ((0..9).to_a + ('A'..'Z').to_a + ('a'..'z').to_a).join
6
+ DEFAULT_BLOCK_SIZE = 24
7
+ MIN_LENGTH = 5
8
+
9
+ def initialize(alphabet: DEFAULT_ALPHABET, block_size: DEFAULT_BLOCK_SIZE)
10
+ @alphabet = alphabet
11
+ @block_size = block_size
12
+ @mask = (1 << block_size) - 1
13
+ @mapping = (0..block_size - 1).to_a
14
+ end
15
+
16
+ def encode_url(n, min_length: MIN_LENGTH)
17
+ enbase(encode(n), min_length)
18
+ end
19
+
20
+ def decode_url(n)
21
+ decode(debase(n))
22
+ end
23
+
24
+ private
25
+
26
+ def encode(n)
27
+ (n & ~@mask) | _encode(n & @mask)
28
+ end
29
+
30
+ def decode(n)
31
+ (n & ~@mask) | _decode(n & @mask)
32
+ end
33
+
34
+ def enbase(x, min_length=MIN_LENGTH)
35
+ result = _enbase(x)
36
+ padding_length = min_length - result.length
37
+ padding_length = 0 if padding_length < 0
38
+ padding = @alphabet[0] * padding_length
39
+ padding.to_s + result.to_s
40
+ end
41
+
42
+ def debase(x)
43
+ n = @alphabet.length
44
+ result = 0
45
+ x.split('').reverse.each_with_index do |c, i|
46
+ unless @alphabet.index(c).nil?
47
+ result += @alphabet.index(c) * (n ** i)
48
+ end
49
+ end
50
+ result
51
+ end
52
+
53
+ def _encode(n)
54
+ result = 0
55
+ @mapping.reverse.each_with_index do |b, i|
56
+ if (n & (1 << i)) != 0
57
+ result |= (1 << b)
58
+ end
59
+ end
60
+ result
61
+ end
62
+
63
+ def _decode(n)
64
+ result = 0
65
+ @mapping.reverse.each_with_index do |b, i|
66
+ if n & (1 << b) != 0
67
+ result |= (1 << i)
68
+ end
69
+ end
70
+ result
71
+ end
72
+
73
+ def _enbase(x)
74
+ n = @alphabet.length
75
+ return @alphabet[x] if x < n
76
+ _enbase(x / n) + @alphabet[x % n]
77
+ end
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,5 @@
1
+ module Ruby
2
+ module ShortUrl
3
+ VERSION = "0.1.0"
4
+ end
5
+ end
@@ -0,0 +1,7 @@
1
+ require "ruby/short_url/version"
2
+ require "ruby/short_url/encoder"
3
+
4
+ module Ruby
5
+ module ShortUrl
6
+ end
7
+ end
@@ -0,0 +1,21 @@
1
+ lib = File.expand_path('../lib', __FILE__)
2
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
3
+ require 'ruby/short_url/version'
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "ruby-short_url"
7
+ spec.version = Ruby::ShortUrl::VERSION
8
+ spec.authors = ["toshi"]
9
+ spec.email = ["me@toshimaru.net"]
10
+
11
+ spec.summary = %q{Ruby implementation for generating Tiny URL. Ruby implementation of python-short_url.}
12
+ spec.homepage = "https://github.com/toshimaru/ruby-short_url"
13
+
14
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
15
+ spec.bindir = "exe"
16
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
17
+ spec.require_paths = ["lib"]
18
+
19
+ spec.add_development_dependency "bundler", "~> 1.9"
20
+ spec.add_development_dependency "rake", "~> 10.0"
21
+ end
metadata ADDED
@@ -0,0 +1,83 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: ruby-short_url
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - toshi
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-05-11 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.9'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.9'
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
+ description:
42
+ email:
43
+ - me@toshimaru.net
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - ".travis.yml"
50
+ - Gemfile
51
+ - LICENSE
52
+ - README.md
53
+ - Rakefile
54
+ - bin/console
55
+ - bin/setup
56
+ - lib/ruby/short_url.rb
57
+ - lib/ruby/short_url/encoder.rb
58
+ - lib/ruby/short_url/version.rb
59
+ - ruby-short_url.gemspec
60
+ homepage: https://github.com/toshimaru/ruby-short_url
61
+ licenses: []
62
+ metadata: {}
63
+ post_install_message:
64
+ rdoc_options: []
65
+ require_paths:
66
+ - lib
67
+ required_ruby_version: !ruby/object:Gem::Requirement
68
+ requirements:
69
+ - - ">="
70
+ - !ruby/object:Gem::Version
71
+ version: '0'
72
+ required_rubygems_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ requirements: []
78
+ rubyforge_project:
79
+ rubygems_version: 2.2.3
80
+ signing_key:
81
+ specification_version: 4
82
+ summary: Ruby implementation for generating Tiny URL. Ruby implementation of python-short_url.
83
+ test_files: []