Skip to content

Commit

Permalink
Merge pull request #24 from betogrun/21-redirect-shortened-urls-to-th…
Browse files Browse the repository at this point in the history
…e-original-url

Resolve the compact url and redirect to the effective url
  • Loading branch information
betogrun authored Sep 21, 2022
2 parents 4ce4cc9 + 2ac77b4 commit 6d653bf
Show file tree
Hide file tree
Showing 9 changed files with 77 additions and 1 deletion.
20 changes: 20 additions & 0 deletions app/controllers/resolved_urls_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
# frozen_string_literal: true

class ResolvedUrlsController < ApplicationController
def show
ShortenedUrl::Resolve.call(key: permitted_params[:key])
.on_success { |result| redirect_to(result[:effective_url], allow_other_host: true) }
.on_failure(:invalid_key) { render_error('invalid_key', status: :unprocessable_entity) }
.on_failure(:record_not_found) { render_error('not_found', status: :not_found) }
end

private

def permitted_params
params.permit(:key)
end

def render_error(resource, status:)
send_file(Rails.public_path.join("#{resource}.html"), type: 'text/html; charset=utf-8', status: status)
end
end
2 changes: 2 additions & 0 deletions app/models/shortened_url.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# frozen_string_literal: true

class ShortenedUrl
attr_reader :id, :key, :compact_url, :effective_url

def initialize(id, key, compact_url, effective_url)
@id = id
@key = key
Expand Down
4 changes: 4 additions & 0 deletions app/models/shortened_url/key.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,10 @@ def initialize(value = nil)
@value = value || "#{SecureRandom.alphanumeric(6)}#{rand(1..9)}"
end

def self.to_proc
->(value) { new(value) }
end

def valid?
/[[:alnum:]]/.match?(value) && value.size == 7
end
Expand Down
8 changes: 8 additions & 0 deletions app/models/shortened_url/repository.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,5 +28,13 @@ def generate_key
rescue Timeout::Error
nil
end

def find_shortened_url(key:)
Record
.where(key: key.value)
.select(:id, :key, :compact_url, :effective_url)
.take
&.then(&BuildShortenedUrl)
end
end
end
18 changes: 18 additions & 0 deletions app/models/shortened_url/resolve.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
# frozen_string_literal: true

class ShortenedUrl
class Resolve < Micro::Case
attribute :key, default: proc(&::ShortenedUrl::Key)
attribute :repository, default: Repository

def call!
return Failure(:invalid_key) unless key.valid?

shortened_url = repository.find_shortened_url(key: key)

return Failure(:record_not_found) unless shortened_url

Success(result: { effective_url: shortened_url.effective_url })
end
end
end
3 changes: 2 additions & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
Rails.application.routes.draw do
mount Rswag::Ui::Engine => '/api-docs'
mount Rswag::Ui::Engine => '/api/docs'
mount Rswag::Api::Engine => '/api-docs'
# Define your application routes per the DSL in https://guides.rubyonrails.org/routing.html

# Defines the root path route ("/")
# root "articles#index"
get '/:key', to: 'resolved_urls#show'
post '/api/v1/shortened_urls', to: 'api/v1/shortened_urls#create'
end
1 change: 1 addition & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ services:
build: .
entrypoint: ./entrypoint.sh
command: bash -c "bundle exec rails s -p 3000 -b '0.0.0.0'"
stdin_open: true
tty: true
volumes:
- .:/usr/src/url-shortener-api
Expand Down
11 changes: 11 additions & 0 deletions public/invalid_key.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Invalid Key</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Invalid key</p>
</body>
</html>
11 changes: 11 additions & 0 deletions public/not_found.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<!DOCTYPE html>
<html lang="en">
<head>
<title>Not found</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
</head>
<body>
<p>Not found</p>
</body>
</html>

0 comments on commit 6d653bf

Please sign in to comment.