ldif_parser 0.2.0 → 0.5.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 9fc3525380089bfb196bb002b639e05b016bf6edba911653805c4085b10fbb56
4
- data.tar.gz: 5de5352b732b3a8095ea7937a31488bf3b4549f46ff8f6a2fa815d9a4cf65993
3
+ metadata.gz: 86f7b2757e53b3856dee02c28b75d3007ee67efcb016ad6186392d1b286a9015
4
+ data.tar.gz: 9e34979e64fbd4c6ba5ea5e08762941c4b136825312a9746d4ee4521cd46b381
5
5
  SHA512:
6
- metadata.gz: e3d31431f1bc6126d4108acca74be1040d2c4a3e2929f090a1210888d6b9740be21c61d981096033025086e67d8061425b3583e638fe6ae028769f020539fad7
7
- data.tar.gz: c08b135902b32c456a6d70261a3d732a66ec7d64cb47846fc48a1c6c34e90b222264cf7f72aba0e1891fb8dcb4790f21408e1df8a9ae6f4b06b6181bb85a986b
6
+ metadata.gz: 48ab36890e5d8aaa380d809f3f1e8a812f46fc5f01a455c926a21ee4de6746ed17f5c6d37b4649f35170abf46eee798973c0424d27fc6d585ddf6d6b6f39d4fe
7
+ data.tar.gz: 04fc17e10e1d27487d228d96307acece582f74cb86cdd9ba1159a3c68f026146af5d9ff0beb2f3ff0ef6911be5e09e287881d2475686cbc5c8c064402d805667
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)
@@ -53,7 +59,7 @@ class LdifParser
53
59
  def line_decoder(line)
54
60
  parts = line.scan(R_LINE_SPLIT).first
55
61
  parts[0] = parts[0].to_sym
56
- parts[2] = Base64.decode64(parts[2]).force_encoding('UTF-8') if BASE64_SEPARATOR == parts[1]
62
+ parts[2] = Base64.decode64(parts[2]).force_encoding('UTF-8') if parts[1] == BASE64_SEPARATOR
57
63
  parts.delete_at(1)
58
64
  parts
59
65
  end
@@ -0,0 +1,31 @@
1
+ # frozen_string_literal: true
2
+
3
+ module HashInsensitive
4
+ def dn
5
+ @dn ||= self['dn'].first
6
+ end
7
+
8
+ def insensitive_origin_keys
9
+ keys.select { |key| key.respond_to?(:to_s) }
10
+ end
11
+
12
+ def insensitive_keys
13
+ insensitive_origin_keys.map(&:to_s).map(&:downcase)
14
+ end
15
+
16
+ def default=(obj)
17
+ @default = obj
18
+ end
19
+
20
+ def default(key)
21
+ return @default unless key.respond_to?(:to_s)
22
+
23
+ tmp_key = key.to_s.downcase
24
+ tmp_index = insensitive_keys.index(tmp_key)
25
+ return @default if tmp_index.nil?
26
+
27
+ insensitive_origin_key = insensitive_origin_keys.at(tmp_index)
28
+
29
+ self[insensitive_origin_key]
30
+ end
31
+ end
data/lib/ldif_parser.rb CHANGED
@@ -4,7 +4,9 @@ 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
+ SPACE = ' '
9
+ DOUBLE_POINT = ':'
8
10
 
9
11
  class << self
10
12
  def parse_file(ldif_path, minimized: false, only: [], except: [])
@@ -19,11 +21,11 @@ class LdifParser
19
21
  end
20
22
 
21
23
  def included_pattern(patterns)
22
- @included_patterns = patterns.map { |pattern| "#{pattern}:" }
24
+ @included_patterns = patterns.map(&:downcase)
23
25
  end
24
26
 
25
27
  def excluded_pattern(patterns)
26
- @excluded_patterns = patterns.map { |pattern| "#{pattern}:" }
28
+ @excluded_patterns = patterns.map(&:downcase)
27
29
  end
28
30
 
29
31
  attr_reader :included_patterns, :excluded_patterns
@@ -48,16 +50,24 @@ class LdifParser
48
50
  def str_parts
49
51
  parts = []
50
52
  str = ''.dup
53
+ previous_key = nil
51
54
 
52
55
  @str.each_line do |line|
53
- next if line_has_to_be_excluded?(line)
56
+ line_key = get_line_key(line, previous_key)
57
+ next if line_key.nil?
54
58
 
55
- if line.start_with?(NEW_LDIF_OBJECT_PATTERN) && !str.empty?
59
+ if line_has_to_be_excluded?(line_key)
60
+ previous_key = line_key
61
+ next
62
+ end
63
+
64
+ if new_ldif_object?(line_key, str, line)
56
65
  parts << str
57
66
  str = ''.dup
58
67
  end
59
68
 
60
69
  str << line
70
+ previous_key = line_key
61
71
  end
62
72
 
63
73
  parts << str
@@ -67,9 +77,19 @@ class LdifParser
67
77
  parts
68
78
  end
69
79
 
70
- def line_has_to_be_excluded?(line)
71
- return false if line.start_with?(NEW_LDIF_OBJECT_PATTERN)
80
+ def new_ldif_object?(line_key, str, line)
81
+ line_key == NEW_LDIF_OBJECT_PATTERN && !str.empty? && !line.start_with?(SPACE)
82
+ end
83
+
84
+ def get_line_key(line, previous_key)
85
+ return previous_key if line.start_with?(SPACE)
86
+
87
+ line.split(DOUBLE_POINT).first&.downcase || previous_key
88
+ end
89
+
90
+ def line_has_to_be_excluded?(line_key)
91
+ return false if line_key == NEW_LDIF_OBJECT_PATTERN
72
92
 
73
- line.start_with?(*self.class.excluded_patterns) || (!self.class.included_patterns.empty? && !line.start_with?(*self.class.included_patterns))
93
+ self.class.excluded_patterns.include?(line_key) || (!self.class.included_patterns.empty? && !self.class.included_patterns.include?(line_key))
74
94
  end
75
95
  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 = 5
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.5.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Maxime Désécot
8
- autorequire:
8
+ autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2022-10-04 00:00:00.000000000 Z
11
+ date: 2023-10-17 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
@@ -31,7 +32,7 @@ metadata:
31
32
  homepage_uri: https://github.com/RaoH37/ldif_parser
32
33
  source_code_uri: https://github.com/RaoH37/ldif_parser
33
34
  changelog_uri: https://github.com/RaoH37/ldif_parser
34
- post_install_message:
35
+ post_install_message:
35
36
  rdoc_options: []
36
37
  require_paths:
37
38
  - lib
@@ -39,15 +40,15 @@ required_ruby_version: !ruby/object:Gem::Requirement
39
40
  requirements:
40
41
  - - ">="
41
42
  - !ruby/object:Gem::Version
42
- version: 2.6.0
43
+ version: '2.7'
43
44
  required_rubygems_version: !ruby/object:Gem::Requirement
44
45
  requirements:
45
46
  - - ">="
46
47
  - !ruby/object:Gem::Version
47
48
  version: '0'
48
49
  requirements: []
49
- rubygems_version: 3.3.22
50
- signing_key:
50
+ rubygems_version: 3.4.19
51
+ signing_key:
51
52
  specification_version: 4
52
53
  summary: LDIF parser
53
54
  test_files: []