cliz 0.0.5 → 0.1.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/bin/cliz +23 -12
- data/lib/functions.rb +92 -44
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 298c94e7dc3f9d453ed368ec0bcde14528f2306416c01367dd447a841de0bcd6
|
4
|
+
data.tar.gz: 7d1f16b725d776e6a44af89d5aba4be0e8e5958513786780193facca791f7183
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7b668249b3ae982ebd7a75803a00649c71dbf8876d08f59052b11863fe5f958681708a04fb0a4911853a00416082e89d18cba3ccfd529c3b9b93fc4dc3add18b
|
7
|
+
data.tar.gz: e7f58dbb7b006c61e7c8549d2f040b45c80c0925419297b0833861d52eaecc81516ba3dbffcf98a41fd277e6989b678ab068bc6259dd9835d5a8a3db66a565eb
|
data/bin/cliz
CHANGED
@@ -3,25 +3,36 @@
|
|
3
3
|
require 'securerandom'
|
4
4
|
require 'fileutils'
|
5
5
|
require 'yaml'
|
6
|
+
require 'fileutils'
|
6
7
|
require_relative '../lib/functions.rb'
|
7
8
|
|
9
|
+
include Cliz
|
8
10
|
|
9
|
-
|
10
|
-
|
11
|
-
OUTPUT_DIR = config['output_dir']
|
12
|
-
BASE_DOMAIN = config['base_domain']
|
13
|
-
DEPLOY_SCRIPT = config['deploy_script']
|
14
|
-
PRE_SCRIPT = config['pre_script']
|
11
|
+
FileUtils.mkdir_p 'reverse-index'
|
15
12
|
|
13
|
+
config = YAML.load(File.read('config.yml'))
|
16
14
|
url = ARGV[0]
|
17
15
|
short_suggestion = ARGV[1]
|
18
16
|
|
19
|
-
|
17
|
+
unless short_suggestion
|
18
|
+
# check for reverse index
|
19
|
+
reverse_index_file = File.join 'reverse-index', "#{Digest::SHA2.hexdigest url}"
|
20
|
+
|
21
|
+
if File.exist? reverse_index_file
|
22
|
+
base_domain = config['base_domain']
|
23
|
+
paths = File.open(reverse_index_file).readlines
|
24
|
+
for path in paths
|
25
|
+
puts File.join base_domain, path
|
26
|
+
end
|
27
|
+
exit
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
|
32
|
+
short_url = short config, url, short_suggestion
|
20
33
|
|
21
|
-
|
22
|
-
folder_path = get_a_folder_path OUTPUT_DIR, short_suggestion
|
23
|
-
create_index_file(url, folder_path)
|
34
|
+
# do reverse index
|
24
35
|
|
25
|
-
|
36
|
+
reverse_index config, short_url
|
26
37
|
|
27
|
-
puts
|
38
|
+
puts short_url
|
data/lib/functions.rb
CHANGED
@@ -1,57 +1,105 @@
|
|
1
1
|
require 'securerandom'
|
2
2
|
require 'fileutils'
|
3
|
+
require 'uri'
|
4
|
+
require 'digest'
|
3
5
|
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
6
|
+
module Cliz
|
7
|
+
##
|
8
|
+
# Gets a folder path for the shortened url
|
9
|
+
def get_a_folder_path output_directory, short_suggestion = nil
|
10
|
+
folder_path = nil
|
8
11
|
|
9
|
-
|
10
|
-
|
12
|
+
short_suggestion ||= generate_short_suggestion output_directory
|
13
|
+
folder_path = File.join(output_directory, short_suggestion, '/')
|
11
14
|
|
12
|
-
|
13
|
-
|
15
|
+
if File.directory?(folder_path)
|
16
|
+
raise ("This short URL is already taken.")
|
17
|
+
end
|
18
|
+
|
19
|
+
folder_path
|
20
|
+
end
|
21
|
+
|
22
|
+
def generate_short_suggestion output_directory
|
23
|
+
counter = 0
|
24
|
+
random_string = nil
|
25
|
+
|
26
|
+
loop do
|
27
|
+
random_string = SecureRandom.alphanumeric[..counter]
|
28
|
+
folder_path = File.join(output_directory, random_string, '/')
|
29
|
+
break unless File.directory?(folder_path)
|
30
|
+
counter += 1
|
31
|
+
end
|
32
|
+
|
33
|
+
random_string
|
14
34
|
end
|
15
35
|
|
16
|
-
folder_path
|
17
|
-
|
36
|
+
def create_index_file url, folder_path
|
37
|
+
html = %Q{
|
38
|
+
<!DOCTYPE html>
|
39
|
+
<html>
|
40
|
+
<head>
|
41
|
+
<meta content="text/html;charset=utf-8" http-equiv="Content-Type">
|
42
|
+
<meta content="utf-8" http-equiv="encoding">
|
43
|
+
<script type="text/javascript">
|
44
|
+
window.location.replace("https://cliz.in");
|
45
|
+
</script>
|
46
|
+
<meta http-equiv="refresh" content="0; url='https://cliz.in'" />
|
47
|
+
</head>
|
48
|
+
<body>
|
49
|
+
<p>Please follow <a href="https://cliz.in">this link</a>.</p>
|
50
|
+
</body>
|
51
|
+
</html>
|
52
|
+
}
|
18
53
|
|
19
|
-
|
20
|
-
|
21
|
-
random_string = nil
|
54
|
+
redirect_content = html.gsub 'https://cliz.in', url
|
55
|
+
FileUtils.mkdir_p(folder_path)
|
22
56
|
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
break unless File.directory?(folder_path)
|
27
|
-
counter += 1
|
57
|
+
File.open "#{folder_path}/index.html", 'w' do |f|
|
58
|
+
f.puts redirect_content
|
59
|
+
end
|
28
60
|
end
|
29
61
|
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
62
|
+
def short config, url, short_suggestion = nil
|
63
|
+
output_dir = config['output_dir']
|
64
|
+
base_domain = config['base_domain']
|
65
|
+
deploy_script = config['deploy_script']
|
66
|
+
pre_script = config['pre_script']
|
67
|
+
|
68
|
+
system("sh #{pre_script}") if pre_script
|
69
|
+
|
70
|
+
short_suggestion ||= generate_short_suggestion output_dir
|
71
|
+
folder_path = get_a_folder_path output_dir, short_suggestion
|
72
|
+
create_index_file(url, folder_path)
|
73
|
+
|
74
|
+
system("sh #{deploy_script}") if deploy_script
|
75
|
+
|
76
|
+
File.join base_domain, short_suggestion
|
77
|
+
end
|
78
|
+
|
79
|
+
##
|
80
|
+
# Gets the path of short_url
|
81
|
+
# Say its https://cliz.in/abc/d, it returns abc/d
|
82
|
+
def path config, short_url
|
83
|
+
base_domain = config['base_domain']
|
84
|
+
short_url.gsub base_domain, ""
|
85
|
+
end
|
86
|
+
|
87
|
+
def long_url config, short_url
|
88
|
+
output_dir = config['output_dir']
|
89
|
+
|
90
|
+
path = path config, short_url
|
91
|
+
file_contents = File.open(File.join(output_dir, path, 'index.html')).readlines.join '\n'
|
92
|
+
URI.extract(file_contents).first
|
93
|
+
end
|
94
|
+
|
95
|
+
def reverse_index config, short_url
|
96
|
+
long_url = long_url config, short_url
|
97
|
+
|
98
|
+
reverse_index_file = File.join 'reverse-index', "#{Digest::SHA2.hexdigest long_url}"
|
99
|
+
|
100
|
+
|
101
|
+
File.open reverse_index_file, "a" do |f|
|
102
|
+
f.puts path(config, short_url)
|
103
|
+
end
|
56
104
|
end
|
57
105
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: cliz
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0
|
4
|
+
version: 0.1.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Karthikeyan A K
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2021-
|
11
|
+
date: 2021-10-28 00:00:00.000000000 Z
|
12
12
|
dependencies: []
|
13
13
|
description: Cliz - The static URL shortner
|
14
14
|
email: mindaslab@protonmail.com
|
@@ -38,7 +38,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
38
38
|
- !ruby/object:Gem::Version
|
39
39
|
version: '0'
|
40
40
|
requirements: []
|
41
|
-
rubygems_version: 3.2.
|
41
|
+
rubygems_version: 3.2.22
|
42
42
|
signing_key:
|
43
43
|
specification_version: 4
|
44
44
|
summary: Cliz - The static URL shortner
|