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
  password_protected max_clicks click_count track_conversions
  routing_rules ab_variants
].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**attrs) ⇒ Link

Returns a new instance of Link.



194
195
196
197
198
199
200
# File 'lib/rerout/models.rb', line 194

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

Class Method Details

.from_hash(hash) ⇒ Object



202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
# File 'lib/rerout/models.rb', line 202

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'],
    password_protected: hash.fetch('password_protected', false),
    max_clicks: hash['max_clicks'],
    click_count: hash.fetch('click_count', 0),
    track_conversions: hash.fetch('track_conversions', false),
    routing_rules: (hash['routing_rules'] || []).map { |r| RoutingRule.from_hash(r) },
    ab_variants: (hash['ab_variants'] || []).map { |v| AbVariant.from_hash(v) }
  )
end

Instance Method Details

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



240
241
242
# File 'lib/rerout/models.rb', line 240

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

#hashObject



245
246
247
# File 'lib/rerout/models.rb', line 245

def hash
  to_h.hash
end

#to_hObject



229
230
231
232
233
234
235
236
237
238
# File 'lib/rerout/models.rb', line 229

def to_h
  ATTRS.to_h do |k|
    [k, case k
        when :tags then tags.map(&:to_h)
        when :routing_rules then routing_rules.map(&:to_h)
        when :ab_variants then ab_variants.map(&:to_h)
        else public_send(k)
        end]
  end
end