pion 0.2.0 → 0.3.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/lib/pion.rb +58 -21
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 84a7550fcbc0110049a63b42b3fc1c715c11660d
|
4
|
+
data.tar.gz: e726c4dc3f929192d4046749ce3855fa708d6427
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d622f4258b7f3cde7451d896863e2aba88de7e5c19306b92698310649ba790b02bbf9d2583d52e32d1e91fad248ef308edb228de8c798ae7992ad119d2bf7294
|
7
|
+
data.tar.gz: c434e48b6c03c92704895aba3d5b5fc46a9a40c85612c5b1edc81f2bfee318acb0e116e309bf58bf8105b3a31da797c5fe1dee40b4a689ab12f06bff2a835c41
|
data/lib/pion.rb
CHANGED
@@ -1,23 +1,28 @@
|
|
1
|
+
# Pion!
|
1
2
|
class Pion
|
3
|
+
# Attributes
|
4
|
+
attr_accessor :options
|
5
|
+
|
2
6
|
# Version number for Pion
|
3
|
-
VERSION = "0.
|
7
|
+
VERSION = "0.3.0"
|
4
8
|
|
5
9
|
# Initialize a new Pion object
|
6
|
-
def initialize(
|
7
|
-
|
8
|
-
|
10
|
+
def initialize(options)
|
11
|
+
@options = method(:symbolize).call options
|
12
|
+
|
13
|
+
fail ArgumentError, "No directory given!" unless options.key? :directory
|
9
14
|
|
10
|
-
|
11
|
-
@extension = extension.gsub /\A\./, ""
|
12
|
-
@separator = separator
|
15
|
+
method(:set_options).call
|
13
16
|
end
|
14
17
|
|
15
18
|
# Generate urls from files
|
16
19
|
def generate
|
17
20
|
urls = []
|
18
21
|
|
19
|
-
Dir.glob "#{
|
20
|
-
|
22
|
+
Dir.glob "#{options[:directory]}/**/*.#{options[:extension]}" do |file|
|
23
|
+
hash = { file: file, url: method(:get_url).call(file) }
|
24
|
+
|
25
|
+
urls << hash unless file == "." || file == ".."
|
21
26
|
end
|
22
27
|
|
23
28
|
urls
|
@@ -25,20 +30,52 @@ class Pion
|
|
25
30
|
|
26
31
|
private
|
27
32
|
|
28
|
-
# Get directory or raise errors if it's faulty
|
29
|
-
def get_directory(directory)
|
30
|
-
return directory if directory.is_a? String and File.directory? directory
|
31
|
-
|
32
|
-
raise ArgumentError, "Directory name must be a string." unless directory.is_a? String
|
33
|
-
raise ArgumentError, "Directory must be a valid directory." unless File.directory? directory
|
34
|
-
end
|
35
|
-
|
36
33
|
# Convert filename to clean url
|
37
34
|
def get_url(file)
|
38
|
-
url = file.gsub
|
39
|
-
url = url.gsub
|
40
|
-
url = url.gsub
|
35
|
+
url = file.gsub(/\A#{options[:directory]}/, "")
|
36
|
+
url = url.gsub(/(\/index)?\.#{options[:extension]}\z/, "")
|
37
|
+
url = url.gsub(/#{Regexp.escape(options[:separator])}/, "/")
|
41
38
|
|
42
39
|
url == "" ? "/" : url
|
43
40
|
end
|
44
|
-
|
41
|
+
|
42
|
+
# Set directory or fail errors if it's faulty
|
43
|
+
def set_directory
|
44
|
+
dir = options[:directory]
|
45
|
+
|
46
|
+
return if dir.is_a?(String) && File.directory?(dir)
|
47
|
+
|
48
|
+
if !dir.is_a? String
|
49
|
+
fail ArgumentError, "Directory must be a string."
|
50
|
+
elsif !File.directory? dir
|
51
|
+
fail ArgumentError, "Directory must be a valid directory."
|
52
|
+
end
|
53
|
+
end
|
54
|
+
|
55
|
+
def set_default(type, default)
|
56
|
+
options[type] = default unless options.key? type
|
57
|
+
|
58
|
+
name = type.to_s.capitalize
|
59
|
+
|
60
|
+
unless options[type].is_a? String
|
61
|
+
fail ArgumentError, "#{name} must be a string"
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
def set_options
|
66
|
+
method(:set_directory).call
|
67
|
+
|
68
|
+
method(:set_default).call :extension, "html"
|
69
|
+
method(:set_default).call :separator, "/"
|
70
|
+
end
|
71
|
+
|
72
|
+
def symbolize(hash)
|
73
|
+
fail ArgumentError, "No Hash given!" unless hash.is_a? Hash
|
74
|
+
|
75
|
+
hash.reduce({}) do |memo, (key, value)|
|
76
|
+
memo[key.to_sym] = value
|
77
|
+
|
78
|
+
memo
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|