Sha256: f30c3a08fed2d9b66135d045efcbeded728c70445b964394f38bd678ba42e13c

Contents?: true

Size: 834 Bytes

Versions: 2

Compression:

Stored size: 834 Bytes

Contents

require "authorized_keys"

module AuthorizedKeys
  class Key
    attr_accessor :options, :content, :comment

    def initialize(key_string)
      options, self.content, self.comment = *Components.extract(key_string)

      raise "Bad key" unless self.content

      self.options = options.split(/,/)
    end

    def to_s
      options = self.options.join(",") unless self.options.empty?
      [options, content, comment].compact.join(" ")
    end

    def ==(key)
      key = self.class.new(key) if key.is_a?(String)
      content == key.content
    end

  private

    module Components
      OPTIONS = '(.*?)\\s*'
      CONTENT = '(ssh-(?:[dr]sa)\\s.*?)'
      COMMENT = '(?:\\s+(.*))?'

      def self.extract(key_string)
        key_string.scan(/^#{OPTIONS}#{CONTENT}#{COMMENT}$/).flatten.map(&:to_s)
      end
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
authorized_keys-1.0.1 lib/authorized_keys/key.rb
authorized_keys-1.0.0 lib/authorized_keys/key.rb