lib/wcc/contentful/link.rb in wcc-contentful-1.6.2 vs lib/wcc/contentful/link.rb in wcc-contentful-1.7.0

- old
+ new

@@ -3,24 +3,33 @@ class WCC::Contentful::Link attr_reader :id, :link_type, :raw LINK_TYPES = { Asset: 'Asset', - Link: 'Entry' + Link: 'Entry', + Tag: 'Tag' }.freeze def initialize(model, link_type = nil) - @id = model.try(:id) || model - @link_type = link_type - @link_type ||= model.is_a?(WCC::Contentful::Model::Asset) ? :Asset : :Link - @raw = - { - 'sys' => { - 'type' => 'Link', - 'linkType' => LINK_TYPES[@link_type] || link_type, - 'id' => @id + if model.is_a?(Hash) + raise ArgumentError, 'Not a Link' unless model.dig('sys', 'type') == 'Link' + + @raw = model + @id = model.dig('sys', 'id') + @link_type = model.dig('sys', 'linkType') + else + @id = model.try(:id) || model + @link_type = link_type + @link_type ||= model.is_a?(WCC::Contentful::Model::Asset) ? :Asset : :Link + @raw = + { + 'sys' => { + 'type' => 'Link', + 'linkType' => LINK_TYPES[@link_type] || link_type, + 'id' => @id + } } - } + end end alias_method :to_h, :raw end