Class: ContentfulLite::Link

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

Overview

A link to any type of contentful entity

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input) ⇒ Link

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Link.

Parameters:



11
12
13
14
15
16
17
18
19
# File 'lib/contentful_lite/link.rb', line 11

def initialize(input)
  if input.is_a?(ContentfulLite::CommonData)
    @id = input.id
    @type = input.sys['type'].downcase.to_sym
  else
    @type = input['sys']['linkType'].downcase.to_sym
    @id = input['sys']['id']
  end
end

Instance Attribute Details

#idObject (readonly)

The unique id of the linked entity



5
6
7
# File 'lib/contentful_lite/link.rb', line 5

def id
  @id
end

#typeObject (readonly)

The type of the linked entity



7
8
9
# File 'lib/contentful_lite/link.rb', line 7

def type
  @type
end

Instance Method Details

#==(other) ⇒ Boolean

Equality comparison

Parameters:

  • other (Object)

    the object to compare

Returns:

  • (Boolean)

    true if other is ContentfulLite::Link with same id and type



24
25
26
# File 'lib/contentful_lite/link.rb', line 24

def ==(other)
  self.class == other.class && type == other.type && id == other.id
end

#as_jsonHash

Provided for compatibility with Rails JSON serializer

Returns:

  • (Hash)

    a Hash representation of the link, to be formated as JSON



30
31
32
33
34
35
36
37
38
# File 'lib/contentful_lite/link.rb', line 30

def as_json(*)
  {
    'sys' => {
      'type' => "Link",
      'linkType' => type.to_s.capitalize,
      'id' => id
    }
  }
end