summaryrefslogtreecommitdiff
path: root/lib/trunk/kdbx.rb
blob: 01438fb86b4a5d0d8ac4bf1361d43a98c861bb68 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Trunk
  class KDBX
    include Enumerable

    def initialize(file, password:)
      require 'kdbx'
      require 'rexml/document'
      @file = Kdbx.open(file, password: password)
      @document = REXML::Document.new(@file.content)
    end

    def each
      REXML::XPath.each(@document, "//Entry") do |element|
        yield(
          REXML::XPath.each(element, "./String").inject({}) do |memo, x|
            key = REXML::XPath.first(x, "./Key").text
            memo[key.downcase.to_sym] = REXML::XPath.first(x, "./Value").text
            memo
          end
        )
      end
    end

    private

    def xml
      @file.content
    end
  end
end