Module: FileManager
- Included in:
- Biblionet::Extractors::Base, Bookshark::Crawler, Bookshark::Extractor
- Defined in:
- lib/bookshark/storage/file_manager.rb
Instance Method Summary collapse
-
#list_directories(options = {}) ⇒ Object
Lists directories in current path or in path specified by options hash.
-
#list_files(options = {}) ⇒ Object
Returns a list of all files in current directory or as specified in options hash.
-
#save_to(path, content) ⇒ Object
Saves some text/string to file.
Instance Method Details
#list_directories(options = {}) ⇒ Object
Lists directories in current path or in path specified by options hash.
Attributes
-
options
- The options hash accepts options for a more specialized directory search operation.
Options
-
:path
- The path where directory search will happen. -
:all
- If true, recursive search is enabled.
23 24 25 26 27 28 29 30 31 32 33 34 |
# File 'lib/bookshark/storage/file_manager.rb', line 23 def list_directories( = {}) = DEFAULTS.merge() path = [:path] all = [:all] path = "#{path}/" unless path == '' or path.end_with?('/') path = path+'**/' if all Dir.glob("#{path}*/") end |
#list_files(options = {}) ⇒ Object
Returns a list of all files in current directory or as specified in options hash.
Attributes
-
options
- The options hash accepts options for a more specialized file search operation.
Options
-
:path
- The path where file search will happen. -
:extension
- The extension of target files. -
:all
- If true, recursive search is enabled.
Examples
files = list_files
files = list_files path: 'html_pages'
files = list_files path: 'raw_html_pages/2', extension:'html'
files = list_files(path: 'ddc_pages', extension:'json', all:true).each do |file|
file.do_something
end
58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/bookshark/storage/file_manager.rb', line 58 def list_files( = {}) = DEFAULTS.merge() path = [:path] all = [:all] extension = [:extension] extension = ".#{extension}" unless extension == '' or extension.start_with?('.') file_wildcard = "*#{extension}" path = "#{path}/" unless path == '' or path.end_with?('/') path = path+'**/' if all Dir.glob("#{path}#{file_wildcard}") end |
#save_to(path, content) ⇒ Object
Saves some text/string to file.
Attributes
-
path
- The path to file(including filename) where content will be saved. -
content
- The text which will be saved to file.
Examples
save_to('data_pages/categories/cat_15.txt', 'Some text')
85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
# File 'lib/bookshark/storage/file_manager.rb', line 85 def save_to(path, content) begin dir = File.dirname(path) # Create a new directory (does nothing if directory exists or is a file) FileUtils.mkdir_p dir #unless File.dirname(path) == "." open(path, "w") do |f| f.write(content) end rescue StandardError => e puts e end end |