cliz 0.0.6 → 0.1.1

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 +25 -10
  3. data/lib/functions.rb +94 -44
  4. metadata +3 -3
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: ff3f968702225f2eca11654dc309d70faa3ffba83da660aa1c6b570d759a2719
4
- data.tar.gz: 4d7c5ec22057ba4985b3076def8901abf35eb023c14d4d5bf8ef7031f70cf790
3
+ metadata.gz: de4a5cd7ac9ce9598f60e1674053920a3362d5cf93a9fc19c1b879845fb06567
4
+ data.tar.gz: 23e5e440771e05f9e15a8b8a508de4747fccf613dc1af6e459cce7c9bad85c5c
5
5
  SHA512:
6
- metadata.gz: e40b13d6b3d477e31ca9c308d5a439f0536ded157e17774f2730584c25df56cf48f43833ba376bd796796d9a9dcd942de0ee9c2082af9da069e452a57bb8b81f
7
- data.tar.gz: d87a0a516beb1d428582324849c8018461dbcaac577c31284824f7674cb47761ec0a015462eac7f1d09842b296abc90bf93b60a0b9ef710d574b630f0cd73ad3
6
+ metadata.gz: 7e8d8bde06fc90d008c29ccfa234157a9d26d160557ecc6cef78cd201db533c5ec13cedfd1f4a3d05b2b5b33bef7b4dbe43cb4e9611b3025070f3594a0232892
7
+ data.tar.gz: 504e21a63bd2359ce50ef4e567fbb3b894c2d92aed4a1c37e2d40f105b4661ba11e698706891a96e564b6ef2cb799c6f0d5ac8cefb527083dab1eeeeab011f1d
data/bin/cliz CHANGED
@@ -3,25 +3,40 @@
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
10
+
11
+
8
12
 
9
13
  config = YAML.load(File.read('config.yml'))
14
+ output_dir = config['output_dir']
10
15
 
11
- OUTPUT_DIR = config['output_dir']
12
- BASE_DOMAIN = config['base_domain']
13
- DEPLOY_SCRIPT = config['deploy_script']
14
- PRE_SCRIPT = config['pre_script']
16
+ FileUtils.mkdir_p File.join output_dir, 'reverse-index'
15
17
 
16
18
  url = ARGV[0]
17
19
  short_suggestion = ARGV[1]
18
20
 
19
- system("sh #{PRE_SCRIPT}") if PRE_SCRIPT
21
+ unless short_suggestion
22
+ # check for reverse index
23
+ reverse_index_file = File.join output_dir, 'reverse-index', "#{Digest::SHA2.hexdigest url}"
24
+
25
+ if File.exist? reverse_index_file
26
+ base_domain = config['base_domain']
27
+ paths = File.open(reverse_index_file).readlines
28
+ for path in paths
29
+ puts File.join base_domain, path
30
+ end
31
+ exit
32
+ end
33
+ end
34
+
35
+
36
+ short_url = short config, url, short_suggestion
20
37
 
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)
38
+ # do reverse index
24
39
 
25
- system("sh #{DEPLOY_SCRIPT}") if DEPLOY_SCRIPT
40
+ reverse_index config, short_url
26
41
 
27
- puts File.join BASE_DOMAIN, short_suggestion
42
+ puts short_url
data/lib/functions.rb CHANGED
@@ -1,57 +1,107 @@
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
+ output_dir = config['output_dir']
97
+
98
+ long_url = long_url config, short_url
99
+
100
+ reverse_index_file = File.join output_dir, 'reverse-index', "#{Digest::SHA2.hexdigest long_url}"
101
+
102
+
103
+ File.open reverse_index_file, "a" do |f|
104
+ f.puts path(config, short_url)
105
+ end
56
106
  end
57
107
  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.6
4
+ version: 0.1.1
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