lib/r509/subject.rb in r509-0.9.2 vs lib/r509/subject.rb in r509-0.10.0
- old
+ new
@@ -1,29 +1,33 @@
require "openssl"
module R509
- # subject class. Used for building OpenSSL::X509::Name objects in a sane fashion
+ # The primary subject class. Used for building subject DNs in a sane fashion.
# @example
# subject = R509::Subject.new
# subject.CN= "test.test"
# subject.organization= "r509 LLC"
# @example
# subject = R509::Subject.new([['CN','test.test'],['O','r509 LLC']])
# @example
+ # subject = R509::Subject.new(:CN => 'test.test', :O => 'r509 LLC')
+ # @example
# # you can also use the friendly getter/setters with custom OIDs
# R509::OIDMapper.register("1.2.3.4.5.6.7.8","COI","customOID")
# subject = R509::Subject.new
# subject.COI="test"
# # or
# subject.customOID="test"
# # or
# subject.custom_oid="test"
class Subject
- # @param [Array, OpenSSL::X509::Name, R509::Subject, DER, nil] arg
+ # @param [Array, OpenSSL::X509::Name, R509::Subject, DER, Hash, nil] arg
def initialize(arg=nil)
if arg.kind_of?(Array)
@array = arg
+ elsif arg.kind_of?(Hash)
+ @array = arg.map { |k,v| [k.to_s.upcase,v] }
elsif arg.kind_of?(OpenSSL::X509::Name)
sanitizer = R509::NameSanitizer.new
@array = sanitizer.sanitize(arg)
elsif arg.kind_of?(R509::Subject)
@array = arg.to_a
@@ -95,10 +99,24 @@
# @return [Array] Array of form [['CN','langui.sh']]
def to_a
@array
end
+ # @return [Hash]
+ def to_h
+ hash = {}
+ @array.each do |el|
+ hash[el[0].to_sym] = el[1]
+ end
+ hash
+ end
+
+ # @return [YAML]
+ def to_yaml
+ self.to_h.to_yaml
+ end
+
# @private
def respond_to?(method_sym, include_private = false)
method_sym.to_s =~ /([^=]*)/
oid = oid_check($1)
if not oid.nil?
@@ -112,9 +130,13 @@
# Try to build methods for getting/setting various subject attributes
# dynamically. this will also cache methods that get built via instance_eval.
# This code will also allow you to set subject items for custom oids
# defined via R509::OIDMapper
+ #
+ # @example
+ # subject = R509::Subject.new
+ # subject.CN = 'test' # method built via method missing.
#
def method_missing(method_sym, *args, &block)
if method_sym.to_s =~ /(.*)=$/
sn = oid_check($1)
if not sn.nil?