-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #24 from betogrun/21-redirect-shortened-urls-to-th…
…e-original-url Resolve the compact url and redirect to the effective url
- Loading branch information
Showing
9 changed files
with
77 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |