Class: Mutt::Ldap::Query::Worker
- Inherits:
-
Object
- Object
- Mutt::Ldap::Query::Worker
- Defined in:
- lib/mutt/ldap/query/worker.rb
Instance Attribute Summary collapse
-
#query ⇒ Object
readonly
Returns the value of attribute query.
Instance Method Summary collapse
- #config ⇒ Object
- #config_path ⇒ Object
- #configured? ⇒ Boolean
- #edit_config ⇒ Object
- #gen_config ⇒ Object
-
#initialize(query) ⇒ Worker
constructor
A new instance of Worker.
- #mail_attributes ⇒ Object
- #sample_config ⇒ Object
- #search ⇒ Object
- #search_attributes ⇒ Object
- #search_filter ⇒ Object
Constructor Details
#initialize(query) ⇒ Worker
Returns a new instance of Worker.
14 15 16 |
# File 'lib/mutt/ldap/query/worker.rb', line 14 def initialize(query) @query = query end |
Instance Attribute Details
#query ⇒ Object (readonly)
Returns the value of attribute query.
12 13 14 |
# File 'lib/mutt/ldap/query/worker.rb', line 12 def query @query end |
Instance Method Details
#config ⇒ Object
27 28 29 |
# File 'lib/mutt/ldap/query/worker.rb', line 27 def config @config ||= YAML.load_file(config_path) end |
#config_path ⇒ Object
18 19 20 21 |
# File 'lib/mutt/ldap/query/worker.rb', line 18 def config_path @xdg ||= XDG::Environment.new File.join(@xdg.config_home, 'mutt-ldap-query', 'config.yml') end |
#configured? ⇒ Boolean
23 24 25 |
# File 'lib/mutt/ldap/query/worker.rb', line 23 def configured? File.exist?(config_path) end |
#edit_config ⇒ Object
65 66 67 |
# File 'lib/mutt/ldap/query/worker.rb', line 65 def edit_config system("${EDITOR:-vi} #{config_path}") end |
#gen_config ⇒ Object
58 59 60 61 62 63 |
# File 'lib/mutt/ldap/query/worker.rb', line 58 def gen_config raise "#{config_path} already exist." if configured? FileUtils.mkdir_p(File.dirname(config_path)) File.write(config_path, sample_config, perm: 0o600) end |
#mail_attributes ⇒ Object
54 55 56 |
# File 'lib/mutt/ldap/query/worker.rb', line 54 def mail_attributes %w[mail mailAlternateAddress] end |
#sample_config ⇒ Object
69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/mutt/ldap/query/worker.rb', line 69 def sample_config <<~CONFIG :host: ldap.example.com :port: 389 :base: ou=people,dc=example,dc=com :auth: :method: :simple :username: cn=mutt-ldap-query,ou=services,dc=example,dc=com :password: secret :encryption: :method: :start_tls CONFIG end |
#search ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 |
# File 'lib/mutt/ldap/query/worker.rb', line 31 def search result = [] ldap = Net::LDAP.new(config) ldap.search(attributes: search_attributes, filter: search_filter) do |entry| mail_attributes.map { |x| entry[x] }.flatten.each do |mail| result << "#{mail}\t#{entry['cn'].first}\t#{entry['description'].first}" end end result end |
#search_attributes ⇒ Object
44 45 46 |
# File 'lib/mutt/ldap/query/worker.rb', line 44 def search_attributes %w[cn mail mailAlternateAddress description] end |
#search_filter ⇒ Object
48 49 50 51 52 |
# File 'lib/mutt/ldap/query/worker.rb', line 48 def search_filter res = Net::LDAP::Filter.eq('objectClass', 'person') res &= query.map { |q| search_attributes.map { |a| Net::LDAP::Filter.contains(a, q) } }.flatten.inject(:|) if query.any? res end |