file_crawler 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 4e4f51c23cb955ce757a85cd58d63df071f69790
4
+ data.tar.gz: c79180b275f203c50046d21a7223c564bd96e60f
5
+ SHA512:
6
+ metadata.gz: 865283a7e6a2994afc5ecb4fbd2930cbf165e8652c26378da2370bdac49f1a4b39161f533ef29849a850a6a9b7c9138e54d8f98627af347e3ec9937524fff8e1
7
+ data.tar.gz: fbcec8ecc3af5c7d6c1a9011002d916b71612abff1f30413f51f4fba7f28d2209c30c28b4e123ff0bca2a10000844f8260ae395e83d3d7650ef974d9e71173be
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2016 Hirohisa Kawasaki
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,41 @@
1
+ # FileCrawler
2
+
3
+ Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/file_crawler`. To experiment with that code, run `bin/console` for an interactive prompt.
4
+
5
+ TODO: Delete this and the text above, and describe your gem
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ ```ruby
12
+ gem 'file_crawler'
13
+ ```
14
+
15
+ And then execute:
16
+
17
+ $ bundle
18
+
19
+ Or install it yourself as:
20
+
21
+ $ gem install file_crawler
22
+
23
+ ## Usage
24
+
25
+ TODO: Write usage instructions here
26
+
27
+ ## Development
28
+
29
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
30
+
31
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
32
+
33
+ ## Contributing
34
+
35
+ Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/file_crawler.
36
+
37
+
38
+ ## License
39
+
40
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
41
+
@@ -0,0 +1,25 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'file_crawler/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "file_crawler"
8
+ spec.version = FileCrawler::VERSION
9
+ spec.authors = ["Hirohisa Kawasaki"]
10
+ spec.email = ["hirohisa.kawasaki@gmail.com"]
11
+
12
+ spec.description = "FileCrawler searches and controls files in local directory"
13
+ spec.summary = spec.description
14
+ spec.homepage = "https://github.com/hirohisa/file_crawler"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = %w(LICENSE.txt README.md file_crawler.gemspec) + Dir['lib/**/*.rb']
18
+ #spec.bindir = "bin"
19
+ #spec.executables = ["file_crawler"]
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ end
@@ -0,0 +1,111 @@
1
+ module FileCrawler
2
+
3
+ class Finder
4
+ attr_accessor :path
5
+
6
+ # directory [Boolean]
7
+ # exntesion [Array]
8
+ def initialize(path)
9
+ @path = path
10
+ select_in_path(path)
11
+ end
12
+
13
+ def rows
14
+ @rows ||= []
15
+ end
16
+
17
+ def select_in_path(path)
18
+ tap {
19
+ @rows = find_rows_in_path(path)
20
+ }
21
+ end
22
+
23
+ def find_rows_in_path(path)
24
+ Dir.entries(path).select {|item|
25
+ !item.start_with?('.')
26
+ }.map {|item|
27
+ path + '/' + item
28
+ }
29
+ end
30
+
31
+ def select_with_extension(extension)
32
+ tap {
33
+ @rows = find_rows_with_extension(extension, rows)
34
+ }
35
+ end
36
+
37
+ def find_rows_with_extension(extension, resource)
38
+ find_rows_files(resource).select {|item|
39
+ extension.any? {|e|
40
+ extname = File.extname(item).downcase
41
+ extname == ".#{e.downcase}"
42
+ }
43
+ }
44
+ end
45
+
46
+ def select_files
47
+ tap {
48
+ @rows = find_rows_files(rows)
49
+ }
50
+ end
51
+
52
+ def find_rows_files(resource)
53
+ resource.select {|item|
54
+ !File.directory?(item)
55
+ }
56
+ end
57
+
58
+ def select_directories
59
+ tap {
60
+ @rows = find_rows_directories(rows)
61
+ }
62
+ end
63
+
64
+ def find_rows_directories(resource)
65
+ resource.select {|item|
66
+ File.directory?(item)
67
+ }
68
+ end
69
+
70
+ def select_with_extension_in_directory(extension)
71
+ tap {
72
+ @rows = find_rows_with_extension_in_directories(extension, find_rows_directories(rows))
73
+ }
74
+ end
75
+
76
+ def find_rows_with_extension_in_directories(extension, directories)
77
+ directories.select {|directory|
78
+ variable_to_exist_with_extension_in_directory(extension, directory)
79
+ }
80
+ end
81
+
82
+ def variable_to_exist_with_extension_in_directory(extension, directory)
83
+ subfiles = find_rows_in_path(directory)
84
+ result = find_rows_with_extension(extension, subfiles)
85
+ return true if result.size > 0
86
+
87
+ subdirectories = find_rows_directories(subfiles).select {|subdirectory|
88
+ variable_to_exist_with_extension_in_directory(extension, subdirectory)
89
+ }
90
+ subdirectories.size > 0
91
+ end
92
+ end
93
+
94
+ def self.search(path, conditions = {})
95
+ finder = FileCrawler::Finder.new(path)
96
+
97
+ case
98
+ when !conditions[:extension].nil?
99
+ finder.select_with_extension(conditions[:extension])
100
+ when conditions[:directory] == true
101
+ finder.select_directories
102
+ when conditions[:directory] == false
103
+ finder.select_files
104
+ when !conditions[:extension_in_directory].nil?
105
+ finder.select_with_extension_in_directory(conditions[:extension_in_directory])
106
+ end
107
+
108
+ finder.rows
109
+ end
110
+
111
+ end
@@ -0,0 +1,3 @@
1
+ module FileCrawler
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,2 @@
1
+ require "file_crawler/version"
2
+ require "file_crawler/finder"
metadata ADDED
@@ -0,0 +1,92 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: file_crawler
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Hirohisa Kawasaki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2016-05-05 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.11'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.11'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description: FileCrawler searches and controls files in local directory
56
+ email:
57
+ - hirohisa.kawasaki@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - LICENSE.txt
63
+ - README.md
64
+ - file_crawler.gemspec
65
+ - lib/file_crawler.rb
66
+ - lib/file_crawler/finder.rb
67
+ - lib/file_crawler/version.rb
68
+ homepage: https://github.com/hirohisa/file_crawler
69
+ licenses:
70
+ - MIT
71
+ metadata: {}
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ requirements:
78
+ - - ">="
79
+ - !ruby/object:Gem::Version
80
+ version: '0'
81
+ required_rubygems_version: !ruby/object:Gem::Requirement
82
+ requirements:
83
+ - - ">="
84
+ - !ruby/object:Gem::Version
85
+ version: '0'
86
+ requirements: []
87
+ rubyforge_project:
88
+ rubygems_version: 2.5.1
89
+ signing_key:
90
+ specification_version: 4
91
+ summary: FileCrawler searches and controls files in local directory
92
+ test_files: []