cliz 0.0.7 → 0.1.2

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 +17 -3
  3. data/lib/functions.rb +116 -56
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 3244d06cf94a457489f61c5f74aa87e38809ddf73ae1d4afff4cd1223e3bf20a
4
- data.tar.gz: 555842ee24707775b95c7cdedac5e1f0a385454951987b6e10317baec436e8c5
3
+ metadata.gz: f7e969650b2cf9c73bcc4ccd6828ae1c017e3b28636c53ab5df834f22b3578be
4
+ data.tar.gz: c65d9ec553353788a99a7885582ba078cfde12a8f073a341ed554facdf271ab2
5
5
  SHA512:
6
- metadata.gz: 04da2ae52ffb1b8756e349f8e177751d5040bb4309f8ff00c19deb2d8229fce35f704a0be5335da0ae382c208b15eb314ab3d08b8ca625b64cc41a58889d9a50
7
- data.tar.gz: 8baec4b0aa47d518723a56be58113c1f5436e0ae75638592c10d045babeccfac395117d0becdd5630b2e6fc5c4fdfd100b229ef63f6c976b70930b72ed7bd703
6
+ metadata.gz: 42bf0f66ac2f39af3cd067f817251d84f68fa93cc79cb53b8bff39500208e3d817cc6bc10ca41acef0046b61e2b75075b7c66e5d2812c0517c0ec5b6bb56babe
7
+ data.tar.gz: 3055bdfb251fd53754a2d0e1c0ca40f3ca07e6d5b12cafe7bf11fb585978aa510177d74477f350ba1f874d8ad69e2b61d48112b9d7691c90e9b9436c48c7130e
data/bin/cliz CHANGED
@@ -1,13 +1,27 @@
1
1
  #!/usr/bin/env ruby
2
2
 
3
- require 'securerandom'
4
3
  require 'fileutils'
5
4
  require 'yaml'
6
5
  require_relative '../lib/functions.rb'
7
6
 
7
+ include Cliz
8
8
 
9
9
  config = YAML.load(File.read('config.yml'))
10
+ output_dir = config['output_dir']
11
+
12
+ # Is this the right place for this?
13
+ # Could we have a initialization function?
14
+ FileUtils.mkdir_p File.join output_dir, 'reverse-index'
15
+
10
16
  url = ARGV[0]
11
17
  short_suggestion = ARGV[1]
12
- short_url = short config, url, short_suggestion
13
- puts short_url
18
+ short_urls = nil
19
+
20
+ if short_suggestion
21
+ short_urls = [short(config, url, short_suggestion)]
22
+ else
23
+ short_urls = previously_shortened_urls(config, url)
24
+ short_urls ||= [short(config, url)]
25
+ end
26
+
27
+ print_urls(short_urls)
data/lib/functions.rb CHANGED
@@ -1,74 +1,134 @@
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
34
+ end
35
+
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
+ }
53
+
54
+ redirect_content = html.gsub 'https://cliz.in', url
55
+ FileUtils.mkdir_p(folder_path)
56
+
57
+ File.open "#{folder_path}/index.html", 'w' do |f|
58
+ f.puts redirect_content
59
+ end
14
60
  end
15
61
 
16
- folder_path
17
- end
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']
18
67
 
19
- def generate_short_suggestion output_directory
20
- counter = 0
21
- random_string = nil
68
+ system("sh #{pre_script}") if pre_script
22
69
 
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
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
+ short_url = File.join base_domain, short_suggestion
75
+ reverse_index config, short_url
76
+
77
+ system("sh #{deploy_script}") if deploy_script
78
+
79
+ short_url
28
80
  end
29
81
 
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
82
+ ##
83
+ # Gets the path of short_url
84
+ # Say its https://cliz.in/abc/d, it returns abc/d
85
+ def path config, short_url
86
+ base_domain = config['base_domain']
87
+ short_url.gsub base_domain, ""
56
88
  end
57
- end
58
89
 
59
- def short config, url, short_suggestion = nil
60
- output_dir = config['output_dir']
61
- base_domain = config['base_domain']
62
- deploy_script = config['deploy_script']
63
- pre_script = config['pre_script']
90
+ def long_url config, short_url
91
+ output_dir = config['output_dir']
64
92
 
65
- system("sh #{pre_script}") if pre_script
93
+ path = path config, short_url
94
+ file_contents = File.open(File.join(output_dir, path, 'index.html')).readlines.join '\n'
95
+ URI.extract(file_contents).first
96
+ end
66
97
 
67
- short_suggestion ||= generate_short_suggestion output_dir
68
- folder_path = get_a_folder_path output_dir, short_suggestion
69
- create_index_file(url, folder_path)
98
+ def reverse_index config, short_url
99
+ output_dir = config['output_dir']
70
100
 
71
- system("sh #{deploy_script}") if deploy_script
101
+ long_url = long_url config, short_url
72
102
 
73
- File.join base_domain, short_suggestion
103
+ reverse_index_file = File.join output_dir, 'reverse-index', "#{Digest::SHA2.hexdigest long_url}"
104
+
105
+
106
+ File.open reverse_index_file, "a" do |f|
107
+ f.puts path(config, short_url)
108
+ end
109
+ end
110
+
111
+ def previously_shortened_urls(config, url)
112
+ output_dir = config['output_dir']
113
+
114
+ reverse_index_file = File.join output_dir, 'reverse-index', "#{Digest::SHA2.hexdigest url}"
115
+
116
+ if File.exist? reverse_index_file
117
+ urls = []
118
+ base_domain = config['base_domain']
119
+ paths = File.open(reverse_index_file).readlines
120
+
121
+ for path in paths
122
+ urls << File.join(base_domain, path)
123
+ end
124
+ end
125
+
126
+ urls
127
+ end
128
+
129
+ def print_urls(urls)
130
+ for url in urls
131
+ puts url
132
+ end
133
+ end
74
134
  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.7
4
+ version: 0.1.2
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-25 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