Class: Rerout::Models::Link

Inherits:
Object
  • Object
show all
Defined in:
lib/rerout/models.rb

Overview

A short link.

Constant Summary collapse

ATTRS =
%i[
  code short_url domain_hostname target_url project_id expires_at
  is_active seo_title seo_description seo_image_url seo_canonical_url
  seo_noindex seo_updated_at created_at updated_at
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Link

Returns a new instance of Link.



18
19
20
21
# File 'lib/rerout/models.rb', line 18

def initialize(**attrs)
  ATTRS.each { |k| instance_variable_set(:"@#{k}", attrs[k]) }
  freeze
end

Class Method Details

.from_hash(hash) ⇒ Object



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/rerout/models.rb', line 23

def self.from_hash(hash)
  new(
    code: hash['code'],
    short_url: hash['short_url'],
    domain_hostname: hash['domain_hostname'],
    target_url: hash['target_url'],
    project_id: hash['project_id'],
    expires_at: hash['expires_at'],
    is_active: hash['is_active'],
    seo_title: hash['seo_title'],
    seo_description: hash['seo_description'],
    seo_image_url: hash['seo_image_url'],
    seo_canonical_url: hash['seo_canonical_url'],
    seo_noindex: hash.fetch('seo_noindex', true),
    seo_updated_at: hash['seo_updated_at'],
    created_at: hash['created_at'],
    updated_at: hash['updated_at']
  )
end

Instance Method Details

#==(other) ⇒ Object Also known as: eql?



47
48
49
# File 'lib/rerout/models.rb', line 47

def ==(other)
  other.is_a?(Link) && other.to_h == to_h
end

#hashObject



52
53
54
# File 'lib/rerout/models.rb', line 52

def hash
  to_h.hash
end

#to_hObject



43
44
45
# File 'lib/rerout/models.rb', line 43

def to_h
  ATTRS.to_h { |k| [k, public_send(k)] }
end