ldif_parser 0.2.0 → 0.4.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fc3525380089bfb196bb002b639e05b016bf6edba911653805c4085b10fbb56
4
- data.tar.gz: 5de5352b732b3a8095ea7937a31488bf3b4549f46ff8f6a2fa815d9a4cf65993
3
+ metadata.gz: f9262f1fd7f283d72ff8f203d665ad3d31f5363df95ec226e8971ddd62502cf1
4
+ data.tar.gz: fb8cc2496d2bff5b2adbb3193e2f7181f46546e0f0ce72446e8215ad3bef6dc0
5
5
  SHA512:
6
- metadata.gz: e3d31431f1bc6126d4108acca74be1040d2c4a3e2929f090a1210888d6b9740be21c61d981096033025086e67d8061425b3583e638fe6ae028769f020539fad7
7
- data.tar.gz: c08b135902b32c456a6d70261a3d732a66ec7d64cb47846fc48a1c6c34e90b222264cf7f72aba0e1891fb8dcb4790f21408e1df8a9ae6f4b06b6181bb85a986b
6
+ metadata.gz: 4dac1bc42a27f18bcf5abaa7a233a6f866e2464ddc84188475e0fe218600fc4dc0e171a3d890105f669891b3fe97f3bfe4539a2f990f2106005f2bc1d6cc644c
7
+ data.tar.gz: 2727a98da12bb5f3c5212361f36c7937086415a33c62a44dbda7bb4e771719a993ec4196a29c0ea86ee8f787be78ac66ee967d16734374e47cec0bee62c6c7cb
data/README.md CHANGED
@@ -5,7 +5,7 @@
5
5
  require 'ldif_parser'
6
6
 
7
7
  ldif_path = '/tmp/ldap.bak'
8
- result = LdifParser.parse_file(ldif_path, only: %w[displayName zimbraMailQuota mail])
8
+ result = LdifParser.parse_file(ldif_path, only: %w[displayName givenName sn mail])
9
9
 
10
10
  result.each do |res|
11
11
  p res
data/lib/entry_maker.rb CHANGED
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require_relative 'hash_insensitive'
3
4
  require 'base64'
4
5
 
5
6
  class LdifParser
@@ -25,6 +26,7 @@ class LdifParser
25
26
 
26
27
  def make
27
28
  hash = lines_decoded_to_h
29
+ hash.extend(HashInsensitive)
28
30
  hash.default = []
29
31
  hash
30
32
  end
@@ -39,11 +41,15 @@ class LdifParser
39
41
 
40
42
  def lines_decoded_to_h
41
43
  lines_decoded.each_with_object({}) do |(k, v), h|
42
- (h[k] ||= [])
44
+ (init_hash(h, k))
43
45
  h[k].push(v)
44
46
  end
45
47
  end
46
48
 
49
+ def init_hash(h, k)
50
+ h[k] ||= []
51
+ end
52
+
47
53
  def lines_decoded
48
54
  lines.map do |line|
49
55
  line_decoder(line)
@@ -0,0 +1,29 @@
1
+ module HashInsensitive
2
+ def dn
3
+ @dn ||= self['dn'].first
4
+ end
5
+
6
+ def insensitive_origin_keys
7
+ self.keys.select { |key| key.respond_to?(:to_s) }
8
+ end
9
+
10
+ def insensitive_keys
11
+ insensitive_origin_keys.map(&:to_s).map(&:downcase)
12
+ end
13
+
14
+ def default=(obj)
15
+ @default = obj
16
+ end
17
+
18
+ def default(key)
19
+ return @default unless key.respond_to?(:to_s)
20
+
21
+ tmp_key = key.to_s.downcase
22
+ tmp_index = insensitive_keys.index(tmp_key)
23
+ return @default if tmp_index.nil?
24
+
25
+ insensitive_origin_key = insensitive_origin_keys.at(tmp_index)
26
+
27
+ self[insensitive_origin_key]
28
+ end
29
+ end
data/lib/ldif_parser.rb CHANGED
@@ -4,7 +4,7 @@ require_relative 'entry_maker'
4
4
  require_relative 'version'
5
5
 
6
6
  class LdifParser
7
- NEW_LDIF_OBJECT_PATTERN = 'dn:'
7
+ NEW_LDIF_OBJECT_PATTERN = 'dn'
8
8
 
9
9
  class << self
10
10
  def parse_file(ldif_path, minimized: false, only: [], except: [])
@@ -19,11 +19,11 @@ class LdifParser
19
19
  end
20
20
 
21
21
  def included_pattern(patterns)
22
- @included_patterns = patterns.map { |pattern| "#{pattern}:" }
22
+ @included_patterns = patterns.map(&:downcase)
23
23
  end
24
24
 
25
25
  def excluded_pattern(patterns)
26
- @excluded_patterns = patterns.map { |pattern| "#{pattern}:" }
26
+ @excluded_patterns = patterns.map(&:downcase)
27
27
  end
28
28
 
29
29
  attr_reader :included_patterns, :excluded_patterns
@@ -48,16 +48,24 @@ class LdifParser
48
48
  def str_parts
49
49
  parts = []
50
50
  str = ''.dup
51
+ previous_key = nil
51
52
 
52
53
  @str.each_line do |line|
53
- next if line_has_to_be_excluded?(line)
54
+ line_key = get_line_key(line, previous_key)
55
+ next if line_key.nil?
54
56
 
55
- if line.start_with?(NEW_LDIF_OBJECT_PATTERN) && !str.empty?
57
+ if line_has_to_be_excluded?(line_key)
58
+ previous_key = line_key
59
+ next
60
+ end
61
+
62
+ if new_ldif_object?(line_key, str, line)
56
63
  parts << str
57
64
  str = ''.dup
58
65
  end
59
66
 
60
67
  str << line
68
+ previous_key = line_key
61
69
  end
62
70
 
63
71
  parts << str
@@ -67,9 +75,19 @@ class LdifParser
67
75
  parts
68
76
  end
69
77
 
70
- def line_has_to_be_excluded?(line)
71
- return false if line.start_with?(NEW_LDIF_OBJECT_PATTERN)
78
+ def new_ldif_object?(line_key, str, line)
79
+ line_key == NEW_LDIF_OBJECT_PATTERN && !str.empty? && !line.start_with?(' ')
80
+ end
81
+
82
+ def get_line_key(line, previous_key)
83
+ return previous_key if line.start_with?(' ')
84
+
85
+ line.split(':').first&.downcase || previous_key
86
+ end
87
+
88
+ def line_has_to_be_excluded?(line_key)
89
+ return false if line_key == NEW_LDIF_OBJECT_PATTERN
72
90
 
73
- line.start_with?(*self.class.excluded_patterns) || (!self.class.included_patterns.empty? && !line.start_with?(*self.class.included_patterns))
91
+ self.class.excluded_patterns.include?(line_key) || (!self.class.included_patterns.empty? && !self.class.included_patterns.include?(line_key))
74
92
  end
75
93
  end
data/lib/version.rb CHANGED
@@ -7,7 +7,7 @@ class LdifParser
7
7
 
8
8
  module VERSION
9
9
  MAJOR = 0
10
- MINOR = 2
10
+ MINOR = 4
11
11
  TINY = 0
12
12
 
13
13
  STRING = [MAJOR, MINOR, TINY].compact.join('.')
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: ldif_parser
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.4.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxime Désécot
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-04 00:00:00.000000000 Z
11
+ date: 2022-10-12 00:00:00.000000000 Z
12
12
  dependencies: []
13
13
  description: Simple class to parse ldif file
14
14
  email:
@@ -20,6 +20,7 @@ files:
20
20
  - LICENSE
21
21
  - README.md
22
22
  - lib/entry_maker.rb
23
+ - lib/hash_insensitive.rb
23
24
  - lib/ldif_parser.rb
24
25
  - lib/version.rb
25
26
  - sig/entry_maker.rbs