lib/r509/subject.rb in r509-0.10.0 vs lib/r509/subject.rb in r509-1.0

- old
+ new

@@ -19,23 +19,23 @@ # subject.customOID="test" # # or # subject.custom_oid="test" class Subject # @param [Array, OpenSSL::X509::Name, R509::Subject, DER, Hash, nil] arg - def initialize(arg=nil) - if arg.kind_of?(Array) + def initialize(arg = nil) + if arg.is_a?(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) + elsif arg.is_a?(Hash) + @array = arg.map { |k, v| [k.to_s.upcase, v] } + elsif arg.is_a?(OpenSSL::X509::Name) sanitizer = R509::NameSanitizer.new @array = sanitizer.sanitize(arg) - elsif arg.kind_of?(R509::Subject) + elsif arg.is_a?(R509::Subject) @array = arg.to_a else @array = [] - if not (begin OpenSSL::ASN1.decode(arg) rescue nil end).nil? + unless (begin OpenSSL::ASN1.decode(arg) rescue nil end).nil? parse_asn1(arg) end end # see if X509 thinks this is okay @@ -57,26 +57,26 @@ @array.each do |item| if key == item[0] return item[1] end end - return nil + nil end # set key and value def []=(key, value) added = false - @array = @array.map{ |item| + @array = @array.map do |item| if key == item[0] added = true [key, value] else item end - } + end - if not added + unless added @array << [key, value] end # see if X509 thinks this is okay name @@ -116,12 +116,12 @@ end # @private def respond_to?(method_sym, include_private = false) method_sym.to_s =~ /([^=]*)/ - oid = oid_check($1) - if not oid.nil? + oid = oid_check(Regexp.last_match[1]) + if oid true else super(method_sym, include_private) end end @@ -137,51 +137,51 @@ # 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? - define_dynamic_setter(method_sym,sn) + sn = oid_check(Regexp.last_match[1]) + if sn + define_dynamic_setter(method_sym, sn) send(method_sym, args.first) else return super(method_sym, *args, &block) end else sn = oid_check(method_sym) - if not sn.nil? - define_dynamic_getter(method_sym,sn) + if sn + define_dynamic_getter(method_sym, sn) send(method_sym) else return super(method_sym, *args, &block) end end end - def define_dynamic_setter(name,sn) + def define_dynamic_setter(name, sn) instance_eval <<-RUBY - def #{name.to_s}(value) + def #{name}(value) self["#{sn}"]= value end RUBY end - def define_dynamic_getter(name,sn) + def define_dynamic_getter(name, sn) instance_eval <<-RUBY - def #{name.to_s} + def #{name} self["#{sn}"] end RUBY end def oid_check(name) - oid = OpenSSL::ASN1::ObjectId.new(camelize(name)) - oid.short_name + oid = OpenSSL::ASN1::ObjectId.new(camelize(name)) + oid.short_name end def camelize(sym) - sym.to_s.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join + sym.to_s.split('_').reduce([]) { |a, e| a.push(a.empty? ? e : e.capitalize) }.join end def parse_asn1(asn) asn = OpenSSL::ASN1.decode asn # parsing a subject DN @@ -211,11 +211,11 @@ # get the OID from the subject line that has this value oids = line.scan(/\/([\d\.]+)=#{component[:value]}/).flatten if oids.size == 1 oid = oids.first else - oid = oids.select{ |match| not used_oids.include?(match) }.first + oid = oids.select { |match| !used_oids.include?(match) }.first end # replace the "UNDEF" OID name in the array at the index the UNDEF was found array[component[:index]][0] = oid # remove the first occurrence of this in the subject line (so we can handle the same oid/value pair multiple times) line = line.sub("/#{oid}=#{component[:value]}", "") @@ -242,7 +242,6 @@ components << { :index => index, :value => array[index][1] } if array[index][0] == "UNDEF" end components end end - end