mokuji 0.2.2 → 1.0.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.
- data/Mokuji.gemspec +2 -2
- data/README +0 -1
- data/Rakefile +6 -13
- data/bin/mokuji +24 -71
- data/lib/mokuji.rb +75 -8
- data/lib/mokuji/converter.rb +158 -171
- data/lib/mokuji/exporter.rb +42 -54
- data/lib/mokuji/scanner.rb +82 -130
- data/lib/mokuji/validators.rb +67 -0
- data/lib/mokuji/version.rb +3 -7
- data/spec/converter_spec.rb +31 -59
- data/spec/exporter_spec.rb +27 -44
- data/spec/scanner_spec.rb +31 -48
- metadata +5 -7
- data/lib/mokuji/initializer.rb +0 -95
- data/spec/initializer_spec.rb +0 -42
data/lib/mokuji/exporter.rb
CHANGED
@@ -1,57 +1,45 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
# EXPORTER
|
5
|
-
# - Exports the data to a file
|
6
|
-
#
|
1
|
+
require 'mokuji/validators'
|
7
2
|
|
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
|
-
end
|
51
|
-
|
52
|
-
|
53
|
-
|
54
|
-
end # </Exporter>
|
3
|
+
module Mokuji
|
4
|
+
#
|
5
|
+
# { EXPORTER }
|
6
|
+
#
|
7
|
+
# === Info
|
8
|
+
#
|
9
|
+
# Exports the data to a file
|
10
|
+
#
|
11
|
+
# === How to use
|
12
|
+
#
|
13
|
+
# exporter = Mokuji::Exporter.new
|
14
|
+
# exporter.export data_from_converter, export_path
|
15
|
+
|
16
|
+
class Exporter
|
17
|
+
include Mokuji::Validators
|
18
|
+
|
19
|
+
def initialize
|
20
|
+
validate_configuration
|
21
|
+
end
|
22
|
+
|
23
|
+
def export string, path
|
24
|
+
validate_data_from_converter string
|
25
|
+
validate_export_path path
|
26
|
+
|
27
|
+
data_from_converter = string
|
28
|
+
list_name = (Mokuji::configuration['list_name'] ||= 'Untitled')
|
29
|
+
time = Time.now.strftime('%d_%B_%Y_(%I-%M%p)')
|
30
|
+
file_name = "#{list_name}_-_#{time}"
|
31
|
+
output_type = Mokuji::configuration['output_type']
|
32
|
+
|
33
|
+
file_extension = case output_type
|
34
|
+
when 'json' then 'json'
|
35
|
+
when 'html', 'plain_html' then 'html'
|
36
|
+
when 'plain_text' then 'txt'
|
37
|
+
end
|
38
|
+
|
39
|
+
Dir.chdir(path)
|
40
|
+
|
41
|
+
File::open("#{file_name}.#{file_extension}", 'w') { |f| f << data_from_converter }
|
42
|
+
end
|
43
|
+
end # </Exporter>
|
55
44
|
|
56
45
|
end
|
57
|
-
|
data/lib/mokuji/scanner.rb
CHANGED
@@ -1,133 +1,85 @@
|
|
1
|
-
|
2
|
-
|
3
|
-
#
|
4
|
-
# SCANNER
|
5
|
-
# - Lists the directory contents in an Array (flat) or a Hash (nested)
|
6
|
-
#
|
7
|
-
# { return => Array or Hash }
|
8
|
-
#
|
1
|
+
require 'mokuji/validators'
|
9
2
|
|
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
|
-
|
52
|
-
|
53
|
-
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
66
|
-
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
d.sort!
|
92
|
-
|
93
|
-
# Going over each file and directory
|
94
|
-
for x in d
|
95
|
-
|
96
|
-
# Hide certain files/directories
|
97
|
-
if x == '.' || x == '..' || x.slice(0) == '.' then; next; end
|
98
|
-
|
99
|
-
# Stuff
|
100
|
-
x_path = path + '/' + x
|
101
|
-
|
102
|
-
if File.directory? x_path then
|
103
|
-
|
104
|
-
hash['contents'] << NestedHashes::read_directory(x_path, x)
|
105
|
-
|
106
|
-
else
|
107
|
-
|
108
|
-
new_file_hash = {
|
109
|
-
|
110
|
-
'type' => 'file',
|
111
|
-
'name' => x
|
112
|
-
|
113
|
-
}
|
114
|
-
|
115
|
-
hash['contents'] << new_file_hash
|
116
|
-
|
117
|
-
end
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
# Return
|
122
|
-
return hash
|
123
|
-
|
124
|
-
end
|
125
|
-
|
126
|
-
end
|
127
|
-
|
128
|
-
|
129
|
-
|
130
|
-
end # </Scanner>
|
3
|
+
module Mokuji
|
4
|
+
#
|
5
|
+
# { SCANNER }
|
6
|
+
#
|
7
|
+
# ==== Info
|
8
|
+
#
|
9
|
+
# Lists the directory contents in nested hashes / arrays
|
10
|
+
# Dot files are hidden by default
|
11
|
+
#
|
12
|
+
# ==== Output example : Hash
|
13
|
+
#
|
14
|
+
# example = {
|
15
|
+
# type: 'directory'
|
16
|
+
# name: 'I am an example'
|
17
|
+
# contents: [ directory_hash, file_hash, file_hash ]
|
18
|
+
# }
|
19
|
+
#
|
20
|
+
# ==== How to use
|
21
|
+
#
|
22
|
+
# scanner = Mokuji::Scanner.new
|
23
|
+
# scanner_results = scanner.scan '/usr/local/Cellar/'
|
24
|
+
|
25
|
+
class Scanner
|
26
|
+
include Mokuji::Validators
|
27
|
+
|
28
|
+
attr_reader :path_to_scan
|
29
|
+
|
30
|
+
def initialize
|
31
|
+
validate_configuration
|
32
|
+
end
|
33
|
+
|
34
|
+
def scan path
|
35
|
+
validate_import_path path
|
36
|
+
@path_to_scan = path
|
37
|
+
return make_collection
|
38
|
+
end
|
39
|
+
|
40
|
+
private
|
41
|
+
|
42
|
+
def make_collection
|
43
|
+
directory_name = File.basename(@path_to_scan)
|
44
|
+
return read_directory(@path_to_scan, directory_name)
|
45
|
+
end
|
46
|
+
|
47
|
+
def read_directory path, directory_name
|
48
|
+
# Create new hash
|
49
|
+
hash = {
|
50
|
+
'type' => 'directory',
|
51
|
+
'name' => directory_name,
|
52
|
+
'contents' => []
|
53
|
+
}
|
54
|
+
|
55
|
+
# Going over each file and directory
|
56
|
+
d = Dir.entries(path).sort
|
57
|
+
|
58
|
+
# Keep certain files/directories out of the list
|
59
|
+
['.', '..', '.DS_Store', 'Thumbs.db'].each { |str| d.delete str }
|
60
|
+
|
61
|
+
# Optional
|
62
|
+
d.delete_if { |x| x[0] === '.' } unless Mokuji::configuration['show_dot_files']
|
63
|
+
|
64
|
+
# Loop over each item
|
65
|
+
d.each do |x|
|
66
|
+
x_path = path + '/' + x
|
67
|
+
|
68
|
+
if File.directory? x_path
|
69
|
+
hash['contents'] << read_directory(x_path, x)
|
70
|
+
else
|
71
|
+
new_file_hash = {
|
72
|
+
'type' => 'file',
|
73
|
+
'name' => x
|
74
|
+
}
|
75
|
+
|
76
|
+
hash['contents'] << new_file_hash
|
77
|
+
end
|
78
|
+
end
|
79
|
+
|
80
|
+
# Return
|
81
|
+
return hash
|
82
|
+
end
|
83
|
+
end # </Scanner>
|
131
84
|
|
132
85
|
end
|
133
|
-
|
@@ -0,0 +1,67 @@
|
|
1
|
+
module Mokuji
|
2
|
+
#
|
3
|
+
# { VALIDATORS }
|
4
|
+
|
5
|
+
module Validators
|
6
|
+
def self.validate_options hash
|
7
|
+
if hash['output_type']
|
8
|
+
unless ['json', 'html', 'plain_html', 'plain_text'].include? hash['output_type']
|
9
|
+
raise "Sorry, but, this output type is unknown."
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate_configuration
|
15
|
+
unless Mokuji.method_defined? :configuration
|
16
|
+
begin
|
17
|
+
require 'mokuji'
|
18
|
+
rescue LoadError
|
19
|
+
raise "What have you done with this gem? 'mokuji.rb' is missing!"
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
|
24
|
+
def validate_import_path path
|
25
|
+
case
|
26
|
+
when !File.directory?(path)
|
27
|
+
raise "Arr, that directory you want to scan, it doesn't quite exist."
|
28
|
+
when Dir.entries(path).slice(2..-1).empty?
|
29
|
+
raise "Arr, that directory you want to scan, it's empty."
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def validate_export_path path
|
34
|
+
case
|
35
|
+
when !File.directory?(path)
|
36
|
+
raise "Arr, that directory you want to export to, it doesn't quite exist."
|
37
|
+
end
|
38
|
+
end
|
39
|
+
|
40
|
+
def validate_data_from_scanner hash
|
41
|
+
case
|
42
|
+
when hash.class ==! Hash
|
43
|
+
raise "Wait a minute, this isn't a hash!"
|
44
|
+
when hash.empty?
|
45
|
+
raise "This hash is empty. Lame!"
|
46
|
+
end
|
47
|
+
end
|
48
|
+
|
49
|
+
def validate_data_from_converter string
|
50
|
+
case
|
51
|
+
when string.class ==! String
|
52
|
+
raise "Wait a minute, this isn't a string!"
|
53
|
+
when string.length === 0
|
54
|
+
raise "The length of this string is zero. Lame!"
|
55
|
+
end
|
56
|
+
end
|
57
|
+
|
58
|
+
def json_valid? str
|
59
|
+
begin
|
60
|
+
JSON.parse str
|
61
|
+
return true
|
62
|
+
rescue JSON::ParserError
|
63
|
+
return false
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end # </Validators>
|
67
|
+
end
|
data/lib/mokuji/version.rb
CHANGED
data/spec/converter_spec.rb
CHANGED
@@ -1,63 +1,35 @@
|
|
1
1
|
require 'mokuji/converter'
|
2
2
|
|
3
|
-
|
4
3
|
describe 'Converter' do
|
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
|
-
it do
|
39
|
-
|
40
|
-
# Parse the JSON that was just created
|
41
|
-
# and see if it fits
|
42
|
-
j = JSON.parse @dfc_json
|
43
|
-
j['contents'].to_a.should be_an_instance_of Array
|
44
|
-
|
45
|
-
end
|
46
|
-
|
47
|
-
|
48
|
-
# HTML
|
49
|
-
it do
|
50
|
-
|
51
|
-
@dfc_html.should be_an_instance_of String
|
52
|
-
|
53
|
-
end
|
54
|
-
|
55
|
-
|
56
|
-
it do
|
57
|
-
|
58
|
-
@dfc_html.should include '<html'
|
59
|
-
|
60
|
-
end
|
61
|
-
|
62
|
-
|
4
|
+
before do
|
5
|
+
@converter = Mokuji::Converter.new
|
6
|
+
|
7
|
+
@test_data = {
|
8
|
+
'type' => 'directory',
|
9
|
+
'name' => 'Icid',
|
10
|
+
'contents' => [ {'type' => 'file', 'name' => 'file.txt'} ]
|
11
|
+
}
|
12
|
+
end
|
13
|
+
|
14
|
+
it "should include the validators module" do
|
15
|
+
@converter.methods.should include :validate_configuration
|
16
|
+
end
|
17
|
+
|
18
|
+
# HTML
|
19
|
+
it do
|
20
|
+
Mokuji.configure 'output_type' => 'html'
|
21
|
+
@df_html = @converter.convert @test_data
|
22
|
+
|
23
|
+
@df_html.should be_an_instance_of String
|
24
|
+
@df_html.should include '<html'
|
25
|
+
end
|
26
|
+
|
27
|
+
# JSON
|
28
|
+
it do
|
29
|
+
Mokuji.configure 'output_type' => 'json'
|
30
|
+
@df_json = @converter.convert @test_data
|
31
|
+
|
32
|
+
@df_json.should be_an_instance_of String
|
33
|
+
@converter.json_valid?(@df_json).should be true
|
34
|
+
end
|
63
35
|
end
|