Module: DynamicLinks

Defined in:
app/models/dynamic_links/client.rb,
lib/dynamic_links.rb,
lib/dynamic_links/engine.rb,
lib/dynamic_links/version.rb,
lib/dynamic_links/configuration.rb,
lib/dynamic_links/strategy_factory.rb,
app/jobs/dynamic_links/application_job.rb,
app/models/dynamic_links/shortened_url.rb,
app/models/dynamic_links/application_record.rb,
app/helpers/dynamic_links/application_helper.rb,
app/mailers/dynamic_links/application_mailer.rb,
app/jobs/dynamic_links/generate_short_links_job.rb,
app/controllers/dynamic_links/application_controller.rb,
lib/dynamic_links/shortening_strategies/md5_strategy.rb,
lib/dynamic_links/shortening_strategies/base_strategy.rb,
lib/dynamic_links/shortening_strategies/mock_strategy.rb,
lib/dynamic_links/shortening_strategies/crc32_strategy.rb,
lib/dynamic_links/shortening_strategies/sha256_strategy.rb,
lib/dynamic_links/shortening_strategies/nano_id_strategy.rb,
lib/generators/dynamic_links/add_kgs_migration_generator.rb,
lib/dynamic_links/shortening_strategies/redis_counter_strategy.rb

Overview

Schema Information

Table name: dynamic_links_shortened_urls

id         :bigint           not null, primary key
client_id  :bigint
url        :string(2083)     not null
short_url  :string(10)       not null
expires_at :datetime
created_at :datetime         not null
updated_at :datetime         not null

Indexes

index_dynamic_links_shortened_urls_on_client_id  (client_id)
index_dynamic_links_shortened_urls_on_short_url  (short_url) UNIQUE

Defined Under Namespace

Modules: ApplicationHelper, Generators, ShorteningStrategies Classes: ApplicationController, ApplicationJob, ApplicationMailer, ApplicationRecord, Client, Configuration, Engine, GenerateShortLinksJob, ShortenedUrl, StrategyFactory

Constant Summary collapse

VERSION =
"0.1.0"

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.configurationObject



17
18
19
# File 'lib/dynamic_links.rb', line 17

def configuration
  @configuration ||= Configuration.new
end

Class Method Details

.configure {|configuration| ... } ⇒ Object

Yields:



21
22
23
# File 'lib/dynamic_links.rb', line 21

def configure
  yield(configuration)
end

.generate_short_url(original_url) ⇒ Object

mimic Firebase Dynamic Links API



44
45
46
47
48
49
50
51
52
# File 'lib/dynamic_links.rb', line 44

def self.generate_short_url(original_url)
  short_link = shorten_url(original_url)

  {
    shortLink: short_link,
    previewLink: "#{short_link}?preview=true",
    warning: []
  }
end

.shorten_url(url) ⇒ Object



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

def self.shorten_url(url)
  strategy_key = configuration.shortening_strategy

  begin
    strategy = StrategyFactory.get_strategy(strategy_key)
  rescue RuntimeError => e
    # This will catch the 'Unknown strategy' error from the factory
    raise "Invalid shortening strategy: #{strategy_key}. Error: #{e.message}"
  rescue ArgumentError
    raise "#{strategy_key} strategy needs to be initialized with arguments"
  rescue => e
    raise "Unexpected error while initializing the strategy: #{e.message}"
  end

  strategy.shorten(url)
end