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 tags created_at updated_at
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Link

Returns a new instance of Link.



48
49
50
51
52
# File 'lib/rerout/models.rb', line 48

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

Class Method Details

.from_hash(hash) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/rerout/models.rb', line 54

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'],
    tags: (hash['tags'] || []).map { |t| Tag.from_hash(t) },
    created_at: hash['created_at'],
    updated_at: hash['updated_at']
  )
end

Instance Method Details

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



81
82
83
# File 'lib/rerout/models.rb', line 81

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

#hashObject



86
87
88
# File 'lib/rerout/models.rb', line 86

def hash
  to_h.hash
end

#to_hObject



75
76
77
78
79
# File 'lib/rerout/models.rb', line 75

def to_h
  ATTRS.to_h do |k|
    [k, k == :tags ? tags.map(&:to_h) : public_send(k)]
  end
end