cliz 0.0.5 → 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (4) hide show
  1. checksums.yaml +4 -4
  2. data/bin/cliz +23 -12
  3. data/lib/functions.rb +92 -44
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 965f9c3fcb4161f00e2b1626bd7686ffc01e02bf06c8c8070c178353f0ad752f
4
- data.tar.gz: a4776254658b1af7c2aeeb68f60eb4a5576d98b30bbc00690179724bfe9238d3
3
+ metadata.gz: 298c94e7dc3f9d453ed368ec0bcde14528f2306416c01367dd447a841de0bcd6
4
+ data.tar.gz: 7d1f16b725d776e6a44af89d5aba4be0e8e5958513786780193facca791f7183
5
5
  SHA512:
6
- metadata.gz: 655b7e2c7943d3bf62946906ba25ae5b6ee3e90cf4d7200df9301d2a799b23babbec1900378399f944998d5a8db0e56eaf4103b4d9f03291701cad16c69ae17a
7
- data.tar.gz: d24c1e49b0d531e58bc3e673415e8ec4288964af117391ebe12612a676d70f8651f5f23d0d3681479ccb980de3fcc31b85d93142c4bf77189929c655a2d93de8
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
- config = YAML.load(File.read('config.yml'))
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
- system("sh #{PRE_SCTIPT}") if PRE_SCRIPT
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
- short_suggestion ||= generate_short_suggestion OUTPUT_DIR
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
- system("sh #{DEPLOY_SCRIPT}") if DEPLOY_SCRIPT
36
+ reverse_index config, short_url
26
37
 
27
- puts File.join BASE_DOMAIN, short_suggestion
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
- # Gets a folder path for the shortened url
6
- def get_a_folder_path output_directory, short_suggestion = nil
7
- folder_path = nil
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
- short_suggestion ||= generate_short_suggestion output_directory
10
- folder_path = File.join(output_directory, short_suggestion, '/')
12
+ short_suggestion ||= generate_short_suggestion output_directory
13
+ folder_path = File.join(output_directory, short_suggestion, '/')
11
14
 
12
- if File.directory?(folder_path)
13
- raise ("This short URL is already taken.")
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
- end
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
- def generate_short_suggestion output_directory
20
- counter = 0
21
- random_string = nil
54
+ redirect_content = html.gsub 'https://cliz.in', url
55
+ FileUtils.mkdir_p(folder_path)
22
56
 
23
- loop do
24
- random_string = SecureRandom.alphanumeric[..counter]
25
- folder_path = File.join(output_directory, random_string, '/')
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
- random_string
31
- end
32
-
33
- def create_index_file url, folder_path
34
- html = %Q{
35
- <!DOCTYPE html>
36
- <html>
37
- <head>
38
- <meta content="text/html;charset=utf-8" http-equiv="Content-Type">
39
- <meta content="utf-8" http-equiv="encoding">
40
- <script type="text/javascript">
41
- window.location.replace("https://cliz.in");
42
- </script>
43
- <meta http-equiv="refresh" content="0; url='https://cliz.in'" />
44
- </head>
45
- <body>
46
- <p>Please follow <a href="https://cliz.in">this link</a>.</p>
47
- </body>
48
- </html>
49
- }
50
-
51
- redirect_content = html.gsub 'https://cliz.in', url
52
- FileUtils.mkdir_p(folder_path)
53
-
54
- File.open "#{folder_path}/index.html", 'w' do |f|
55
- f.puts redirect_content
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.5
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-04-24 00:00:00.000000000 Z
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.15
41
+ rubygems_version: 3.2.22
42
42
  signing_key:
43
43
  specification_version: 4
44
44
  summary: Cliz - The static URL shortner