lib/attr_vault/keyring.rb in attr_vault-0.1.2 vs lib/attr_vault/keyring.rb in attr_vault-0.2.0

- old
+ new

@@ -1,18 +1,22 @@ module AttrVault class Key attr_reader :id, :value, :created_at - def initialize(id, value, created_at) + def initialize(id, value, created_at=nil) if id.nil? raise InvalidKey, "key id required" end if value.nil? raise InvalidKey, "key value required" end - if created_at.nil? - raise InvalidKey, "key created_at required" + begin + id = Integer(id) + rescue + if created_at.nil? + raise InvalidKey, "key created_at required" + end end @id = id @value = value @created_at = created_at @@ -31,20 +35,25 @@ attr_reader :keys def self.load(keyring_data) keyring = Keyring.new begin - candidate_keys = JSON.parse(keyring_data) - unless candidate_keys.respond_to? :each - raise InvalidKeyring, "does not respond to each" + candidate_keys = JSON.parse(keyring_data, symbolize_names: true) + + case candidate_keys + when Array + candidate_keys.each do |k| + created_at = Time.parse(k[:created_at]) if k.has_key?(:created_at) + keyring.add_key(Key.new(k[:id], k[:value], created_at || Time.now)) + end + when Hash + candidate_keys.each do |key_id, key| + keyring.add_key(Key.new(key_id.to_s, key)) + end + else + raise InvalidKeyring, "Invalid JSON structure" end - candidate_keys.each_with_index do |k| - created_at = unless k["created_at"].nil? - Time.parse(k["created_at"]) - end - keyring.add_key(Key.new(k["id"], k["value"], created_at || Time.now)) - end rescue StandardError => e raise InvalidKeyring, e.message end keyring end @@ -85,9 +94,16 @@ def digests(data) keys.map { |k| k.digest(data) } end def to_json - @keys.to_json + if @keys.all? { |k| k.created_at.nil? } + @keys.each_with_object({}) do |k,obj| + obj[k.id] = k.value + end.to_json + else + # Assume we are dealing with a legacy keyring + @keys.to_json + end end end end