classifieds 0.2.3 → 0.2.4

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a16234fab1931047beac73eb8b3f11c4db7ecf4
4
- data.tar.gz: e53323aee4d7707d55e1523f7b7a362e83add621
3
+ metadata.gz: 6f25849b631ac6f189e627d469dd412ef2d9ea26
4
+ data.tar.gz: 3e54f421ac4c4066f8b2dcea00197cfbf7cdc9b4
5
5
  SHA512:
6
- metadata.gz: e4445650f29ee0fdb032ec11092d5a8750faf878bfe576eaabd6eddd7860c41813fea0ace3ed95178e661c5a6ee9de17e1c4939e809102fb0335d5a5dcd2168b
7
- data.tar.gz: 3c86bd19806f5e72f4536a215a9a7937bfbfdfd6e8facae57998f1ccfe10a24496199d4899c94ac4a17aead00930014dc5da04190975a6ff45462f3d255dd23f
6
+ metadata.gz: 4018fd92068baae18dbc40d335a068ed11e135887c81df27c1ba0a1624b9fb2467a03be59d6c70ba071b07218de2011b0839335774216b5b033af5b3c9e9bd96
7
+ data.tar.gz: 0aebb7f8bc7d4525785b32024372eacd64c241ebf76cb6a56775c82196afb8dccd028e5927cf3a4f6018cf01a2afb902d1267c0e049361ed554563a8f6664db1
@@ -1,4 +1,5 @@
1
1
  require 'classifieds/exception'
2
+ require 'classifieds/utils'
2
3
  require 'classifieds/parser'
3
4
  require 'classifieds/encryptor'
4
5
  require 'classifieds/main'
@@ -2,7 +2,6 @@ require 'digest/sha1'
2
2
  require 'openssl'
3
3
  require 'base64'
4
4
  require 'fileutils'
5
- require 'find'
6
5
 
7
6
  require 'safe_colorize'
8
7
 
@@ -10,10 +9,11 @@ require 'thor'
10
9
 
11
10
  module Classifieds
12
11
  class Main < Thor
12
+ include Utils
13
13
  using SafeColorize
14
14
 
15
15
  def initialize(*args)
16
- unless classifieds?
16
+ unless classifieds_repository?
17
17
  STDERR.puts "#{SOURCE_FILE} is not found in this repository".color(:red)
18
18
  exit 1
19
19
  end
@@ -157,53 +157,5 @@ module Classifieds
157
157
  retype_password
158
158
  end
159
159
  end
160
-
161
- def classifieds
162
- Parser.parse(File.read(File.join(root_directory, SOURCE_FILE)).chomp)
163
- end
164
-
165
- def encrypt_data(data)
166
- encryptor = Encryptor.new(@password, root_directory.split('/').pop)
167
- encryptor.encrypt(data)
168
- end
169
-
170
- def decrypt_data(data)
171
- encryptor = Encryptor.new(@password, root_directory.split('/').pop)
172
- encryptor.decrypt(data)
173
- end
174
-
175
- def root_directory
176
- @root_directory ||=
177
- begin
178
- target_directory = File.expand_path(Dir.pwd)
179
- until target_directory == '/' do
180
- Find.find(target_directory) do |path|
181
- return target_directory if path =~ /#{Regexp.escape(SOURCE_FILE)}$/
182
- Find.prune if path =~ %r|^#{target_directory}/|
183
- end
184
- target_directory = File.expand_path('..', target_directory)
185
- end
186
-
187
- nil
188
- end
189
- end
190
-
191
- def classifieds?
192
- !!root_directory
193
- end
194
-
195
- def keygenerated?
196
- File.exists?(File.join(root_directory, PUBLIC_KEY_PATH)) && File.exists?(File.join(root_directory, COMMON_KEY_PATH))
197
- end
198
-
199
- def encrypted?(file)
200
- File.open(file, 'r') do |f|
201
- !!f.read(@prefix.size).to_s.match(/\A#{@prefix}\z/)
202
- end
203
- end
204
-
205
- def decrypted?(file)
206
- !encrypted?(file)
207
- end
208
160
  end
209
161
  end
@@ -1,62 +1,66 @@
1
1
  module Classifieds
2
2
  class Parser
3
- def self.parse(text)
4
- lines = text.each_line.map(&:chomp)
5
- result = lines.each_with_object([]) do |line, array|
6
- target = detect_target(line)
3
+ class << self
4
+ include Utils
7
5
 
8
- case target
9
- when Array
10
- if classifieds_ignore?(line)
11
- target.each do |file|
12
- array.delete(file)
6
+ def parse(text)
7
+ lines = text.each_line.map(&:chomp)
8
+ result = lines.each_with_object([]) do |line, array|
9
+ target = detect_target(line)
10
+
11
+ case target
12
+ when Array
13
+ if classifieds_ignore?(line)
14
+ target.each do |file|
15
+ array.delete(file)
16
+ end
17
+ else
18
+ array.concat(target)
19
+ end
20
+ when String
21
+ if classifieds_ignore?(line)
22
+ array.delete(target)
23
+ else
24
+ array << target
13
25
  end
14
- else
15
- array.concat(target)
16
- end
17
- when String
18
- if classifieds_ignore?(line)
19
- array.delete(target)
20
- else
21
- array << target
22
26
  end
23
27
  end
24
- end
25
28
 
26
- result
27
- end
29
+ result
30
+ end
28
31
 
29
- def self.detect_target(string)
30
- path = string.match(/^!?(.+)/)[1]
31
- absolute_path = File.join(Dir.pwd, path)
32
+ def detect_target(string)
33
+ path = string.match(/^!?(.+)/)[1]
34
+ absolute_path = File.join(root_directory, path)
32
35
 
33
- # Dir.glob notation
34
- if absolute_path.include?('*')
35
- recursive_glob(absolute_path)
36
- else
37
- case File.ftype(path)
38
- when 'file'
39
- absolute_path
40
- when 'directory'
41
- recursive_glob(File.join(absolute_path, '*'))
36
+ # Dir.glob notation
37
+ if absolute_path.include?('*')
38
+ recursive_glob(absolute_path)
42
39
  else
43
- raise ParseError, "invalid file type: #{string}"
40
+ case File.ftype(absolute_path)
41
+ when 'file'
42
+ absolute_path
43
+ when 'directory'
44
+ recursive_glob(File.join(absolute_path, '*'))
45
+ else
46
+ raise ParseError, "invalid file type: #{string}"
47
+ end
44
48
  end
49
+ rescue Errno::ENOENT
45
50
  end
46
- rescue Errno::ENOENT
47
- end
48
51
 
49
- def self.classifieds_ignore?(string)
50
- string.start_with?('!')
51
- end
52
+ def classifieds_ignore?(string)
53
+ string.start_with?('!')
54
+ end
52
55
 
53
- def self.recursive_glob(pattern)
54
- Dir.glob(pattern).each_with_object([]) do |path, array|
55
- case File.ftype(path)
56
- when 'file'
57
- array << path
58
- when 'directory'
59
- array.concat(recursive_glob(File.join(path, '*')))
56
+ def recursive_glob(pattern)
57
+ Dir.glob(pattern).each_with_object([]) do |absolute_path, array|
58
+ case File.ftype(absolute_path)
59
+ when 'file'
60
+ array << absolute_path
61
+ when 'directory'
62
+ array.concat(recursive_glob(File.join(absolute_path, '*')))
63
+ end
60
64
  end
61
65
  end
62
66
  end
@@ -0,0 +1,53 @@
1
+ require 'find'
2
+
3
+ module Classifieds
4
+ module Utils
5
+ def classifieds
6
+ Parser.parse(File.read(File.join(root_directory, SOURCE_FILE)).chomp)
7
+ end
8
+
9
+ def classifieds_repository?
10
+ !!root_directory
11
+ end
12
+
13
+ def keygenerated?
14
+ File.exists?(File.join(root_directory, PUBLIC_KEY_PATH)) && File.exists?(File.join(root_directory, COMMON_KEY_PATH))
15
+ end
16
+
17
+ def root_directory
18
+ @root_directory ||=
19
+ begin
20
+ target_directory = File.expand_path(Dir.pwd)
21
+ until target_directory == '/' do
22
+ Find.find(target_directory) do |path|
23
+ return target_directory if path =~ %r|/#{Regexp.escape(SOURCE_FILE)}$|
24
+ Find.prune if path =~ %r|^#{target_directory}/|
25
+ end
26
+ target_directory = File.expand_path('..', target_directory)
27
+ end
28
+
29
+ nil
30
+ end
31
+ end
32
+
33
+ def encrypt_data(data)
34
+ encryptor = Encryptor.new(@password, root_directory.split('/').pop)
35
+ encryptor.encrypt(data)
36
+ end
37
+
38
+ def decrypt_data(data)
39
+ encryptor = Encryptor.new(@password, root_directory.split('/').pop)
40
+ encryptor.decrypt(data)
41
+ end
42
+
43
+ def encrypted?(file)
44
+ File.open(file, 'r') do |f|
45
+ !!f.read(@prefix.size).to_s.match(/\A#{@prefix}\z/)
46
+ end
47
+ end
48
+
49
+ def decrypted?(file)
50
+ !encrypted?(file)
51
+ end
52
+ end
53
+ end
@@ -1,3 +1,3 @@
1
1
  module Classifieds
2
- VERSION = '0.2.3'
2
+ VERSION = '0.2.4'
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: classifieds
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.3
4
+ version: 0.2.4
5
5
  platform: ruby
6
6
  authors:
7
7
  - kaihar4
@@ -88,6 +88,7 @@ files:
88
88
  - lib/classifieds/exception.rb
89
89
  - lib/classifieds/main.rb
90
90
  - lib/classifieds/parser.rb
91
+ - lib/classifieds/utils.rb
91
92
  - lib/classifieds/version.rb
92
93
  homepage: https://github.com/kaihar4/classifieds
93
94
  licenses: