-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlink.rb
51 lines (42 loc) · 928 Bytes
/
link.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
class Link
@@all = []
attr_accessor :name, :url
LINKS_DIR = './links/'
def initialize(name, url)
@name = name
@url = url
end
def save
unless File.file?(LINKS_DIR + slug)
File.open(LINKS_DIR + slug, 'w') {|f| f.write(@url) }
end
end
def slug
slug = @name.downcase.strip.gsub(' ', '-').gsub(/[^\w-]/, '')
bad_chars = [ '/', '\\', '?', '%', '*', ':', '|', '"', '<', '>', '.', ' ' ]
bad_chars.each do |bad_char|
slug.gsub!(bad_char, '_')
end
slug
end
def self.find name
if File.file?(LINKS_DIR + name)
url = File.open(LINKS_DIR + name).read
return Link.new(name, url)
else
return false
end
end
def self.all
@@all = []
Dir.foreach(LINKS_DIR) do |filename|
next if filename == '.' || filename == '..' || filename == '.DS_Store'
url = File.open(LINKS_DIR + filename).read
@@all << Link.new(filename, url)
end
@@all
end
def self.count
@@all.count
end
end