Class: WCC::Contentful::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/wcc/contentful/link.rb

Constant Summary collapse

{
  Asset: 'Asset',
  Link: 'Entry',
  Tag: 'Tag'
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model, link_type = nil) ⇒ Link

Returns a new instance of Link.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/wcc/contentful/link.rb', line 12

def initialize(model, link_type = nil)
  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

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



4
5
6
# File 'lib/wcc/contentful/link.rb', line 4

def id
  @id
end

Returns the value of attribute link_type.



4
5
6
# File 'lib/wcc/contentful/link.rb', line 4

def link_type
  @link_type
end

#rawObject (readonly) Also known as: to_h

Returns the value of attribute raw.



4
5
6
# File 'lib/wcc/contentful/link.rb', line 4

def raw
  @raw
end