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.
@@ -1,57 +1,45 @@
1
- module Mokuji
2
-
3
- #
4
- # EXPORTER
5
- # - Exports the data to a file
6
- #
1
+ require 'mokuji/validators'
7
2
 
8
- class Exporter
9
-
10
-
11
-
12
- attr_accessor :file_data, :converting_method, :file_name, :export_path
13
-
14
-
15
-
16
- def execute
17
-
18
- # Check
19
- fail_msg = "You forgot to set some variables."
20
- fail_msg << "Make sure you've set the path to scan and the converting method."
21
-
22
- raise fail_msg unless file_data || converting_method || export_path
23
-
24
- # Filename check
25
- @file_name = 'Untitled' unless file_name
26
-
27
- # Export path validation
28
- raise "The directory you want to export to doesn't exist" unless File.directory?(export_path)
29
-
30
- # Set file extension
31
- file_extension = case converting_method
32
-
33
- when :json then '.json'
34
- when :html then '.html'
35
-
36
- else '.txt'
37
-
38
- end
39
-
40
- # Change to the directory where the file should be saved
41
- Dir.chdir(export_path)
42
-
43
- # Creating the file
44
- File::open( file_name + file_extension, 'w' ) do |f|
45
-
46
- f << file_data
47
-
48
- end
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
-
@@ -1,133 +1,85 @@
1
- module Mokuji
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
- class Scanner
11
-
12
-
13
-
14
- attr_accessor :path_to_scan, :converting_method
15
-
16
-
17
-
18
- def execute
19
-
20
- # Validation
21
- raise "You can't scan nil, duh." unless path_to_scan
22
- raise "The directory you want to scan doesn't exist" unless File.directory?(path_to_scan)
23
-
24
- # Go to the directory
25
- Dir.chdir(path_to_scan)
26
-
27
- # Get the data
28
- data = case converting_method
29
-
30
- when :json then FlatArray::make
31
- when :html then NestedHashes::make
32
-
33
- else
34
- raise 'The given converting method is unknown'
35
-
36
- end
37
-
38
- end
39
-
40
-
41
-
42
- protected
43
-
44
-
45
-
46
- module FlatArray
47
-
48
- def self.make
49
-
50
- # Makes a flat array
51
- array = Dir.glob('**/**')
52
- array.sort!
53
-
54
- end
55
-
56
- end
57
-
58
-
59
-
60
- module NestedHashes
61
-
62
- def self.make
63
-
64
- # Directory (name)
65
- path = Dir.pwd
66
- path.chomp!('/')
67
-
68
- rindex = path.rindex('/') + 1
69
- directory_name = path.slice(rindex..-1)
70
-
71
- # Makes a hash with hashes in it
72
- NestedHashes::read_directory path, directory_name
73
-
74
- end
75
-
76
- def self.read_directory path, directory_name
77
-
78
- # Set
79
- hash = {
80
-
81
- 'type' => 'directory',
82
- 'name' => directory_name,
83
- 'contents' => []
84
-
85
- }
86
-
87
- # Open the directory
88
- d = Dir.entries(path)
89
-
90
- # Sort it
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
@@ -1,10 +1,6 @@
1
1
  module Mokuji
2
+ #
3
+ # { VERSION }
2
4
 
3
- #
4
- # MOKUJI VERSION
5
- #
6
-
7
- VERSION = "0.2.2"
8
-
5
+ VERSION = "1.0.0"
9
6
  end
10
-
@@ -1,63 +1,35 @@
1
1
  require 'mokuji/converter'
2
2
 
3
-
4
3
  describe 'Converter' do
5
-
6
-
7
- before do
8
-
9
- converter = Mokuji::Converter.new
10
- converter.time = Time.now.strftime('%d %B %Y (%I:%M%p)')
11
- converter.data_from_scanner = ['immafile.rb']
12
- converter.converting_method = :json
13
- @dfc_json = converter.execute
14
-
15
- test_hash = {
16
-
17
- 'type' => 'directory',
18
- 'name' => 'Icid',
19
- 'contents' => [ {'type' => 'file', 'name' => 'immafile.rb'} ]
20
-
21
- }
22
-
23
- converter.data_from_scanner = test_hash
24
- converter.converting_method = :html
25
- @dfc_html = converter.execute
26
-
27
- end
28
-
29
-
30
- # JSON
31
- it do
32
-
33
- @dfc_json.should be_an_instance_of String
34
-
35
- end
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