Add URL module and article_url, user_url helpers (#6833)

* Add AppURL with .article and .user

* Use URL.url and URL.user

* Add article_url and user_url helpers
This commit is contained in:
rhymes 2020-03-25 18:54:45 +01:00 committed by GitHub
parent 6acd1a62aa
commit cfb64a1c68
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
13 changed files with 66 additions and 29 deletions

View file

@ -166,6 +166,14 @@ module ApplicationHelper
# @example Add a path
# app_url("internal") #=> "https://dev.to/internal"
def app_url(uri = nil)
App.url(uri)
URL.url(uri)
end
def article_url(article)
URL.article(article)
end
def user_url(user)
URL.user(user)
end
end

View file

@ -1,15 +1,14 @@
module App
module_function
def protocol
# Utilities methods to safely build app wide URLs
module URL
def self.protocol
ApplicationConfig["APP_PROTOCOL"]
end
def domain
def self.domain
ApplicationConfig["APP_DOMAIN"]
end
# Creates an app internal URL
# Creates an app URL
#
# @note Uses protocol and domain specified in the environment, ensure they are set.
# @param uri [URI, String] parts we want to merge into the URL, e.g. path, fragment
@ -17,10 +16,24 @@ module App
# app_url #=> "https://dev.to"
# @example Add a path
# app_url("internal") #=> "https://dev.to/internal"
def url(uri = nil)
def self.url(uri = nil)
base_url = "#{protocol}#{domain}"
return base_url unless uri
URI.parse(base_url).merge(uri).to_s
end
# Creates an article URL
#
# @param article [Article] the article to create the URL for
def self.article(article)
url(article.path)
end
# Creates a user URL
#
# @param user [User] the user to create the URL for
def self.user(user)
url(user.username)
end
end

View file

@ -29,7 +29,7 @@ module Slack
end
def call
reports_url = App.url(
reports_url = URL.url(
Rails.application.routes.url_helpers.internal_reports_path,
)
@ -60,7 +60,7 @@ module Slack
format(
USER_DETAIL_TEMPLATE,
username: user.username,
url: App.url("/#{user.username}"),
url: URL.user(user),
email: user.email,
)
end

View file

@ -22,7 +22,7 @@ module Slack
end
def call
report_url = App.url(
report_url = URL.url(
Rails.application.routes.url_helpers.internal_report_path(report_id),
)

View file

@ -16,7 +16,7 @@ module Slack
def call
message = format(
MESSAGE_TEMPLATE,
url: App.url("/#{user.username}"),
url: URL.user(user),
)
SlackBotPingWorker.perform_async(

View file

@ -18,7 +18,7 @@ module Slack
message = format(
MESSAGE_TEMPLATE,
action: action,
url: App.url("/#{user.username}"),
url: URL.user(user),
)
SlackBotPingWorker.perform_async(

View file

@ -47,7 +47,7 @@
<meta name="keywords" content="<%= @article.cached_tag_list %>, software, coding, development, engineering, inclusive, community">
<meta property="og:type" content="article" />
<meta property="og:url" content="<%= app_url(@article.path) %>" />
<meta property="og:url" content="<%= article_url(@article) %>" />
<meta property="og:title" content="<%= @article.title %>" />
<meta property="og:description" content="<%= @article.description || "An article from the community" %>" />
<meta property="og:site_name" content="<%= community_qualified_name %>" />
@ -86,7 +86,7 @@
<h1 style="text-align:center;">⏳ Video Transcoding In Progress ⏳</h1>
<% end %>
<article itemscope itemtype="http://schema.org/Article" itemprop="mainEntity">
<meta itemprop="url" content="<%= app_url(@article.path) %>">
<meta itemprop="url" content="<%= article_url(@article) %>">
<meta itemprop="image" content="<%= article_social_image_url(@article) %>">
<div itemprop="publisher" itemscope itemtype="https://schema.org/Organization">
<div itemprop="logo" itemscope itemtype="https://schema.org/ImageObject">
@ -125,7 +125,7 @@
</h1>
<h3>
<span itemprop="author" itemscope itemtype="http://schema.org/Person">
<meta itemprop="url" content="<%= app_url(@user.username) %>">
<meta itemprop="url" content="<%= user_url(@user) %>">
<a href="/<%= @user.username %>" class="author">
<img class="profile-pic" src="<%= ProfileImage.new(@user).get(width: 50) %>" alt="<%= @user.username %> profile image" />
<span itemprop="name"><%= @user.name %></span>

View file

@ -1,9 +1,9 @@
<link rel="canonical" href="<%= app_url(@user.username) %>" />
<link rel="canonical" href="<%= user_url(@user) %>" />
<meta name="description" content="<%= @user.summary %>">
<meta name="keywords" content="software development,engineering,rails,javascript,ruby">
<meta property="og:type" content="website" />
<meta property="og:url" content="<%= app_url(@user.username) %>" />
<meta property="og:url" content="<%= user_url(@user) %>" />
<meta property="og:title" content="<%= @user.name %> — DEV Profile" />
<meta property="og:image" content="<%= user_social_image_url(@user) %>">
<meta property="og:description" content="<%= @user.summary %>" />

View file

@ -1,6 +1,11 @@
require "rails_helper"
RSpec.describe App, type: :lib do
RSpec.describe URL, type: :lib do
before do
allow(ApplicationConfig).to receive(:[]).with("APP_PROTOCOL").and_return("https://")
allow(ApplicationConfig).to receive(:[]).with("APP_DOMAIN").and_return("dev.to")
end
describe ".protocol" do
it "returns the value of APP_PROTOCOL env variable" do
expect(described_class.protocol).to eq(ApplicationConfig["APP_PROTOCOL"])
@ -14,11 +19,6 @@ RSpec.describe App, type: :lib do
end
describe ".url" do
before do
allow(ApplicationConfig).to receive(:[]).with("APP_PROTOCOL").and_return("https://")
allow(ApplicationConfig).to receive(:[]).with("APP_DOMAIN").and_return("dev.to")
end
it "creates the correct base app URL" do
expect(described_class.url).to eq("https://dev.to")
end
@ -36,4 +36,20 @@ RSpec.describe App, type: :lib do
expect(described_class.url(uri)).to eq("https://dev.to/internal#test")
end
end
describe ".article" do
let(:article) { build(:article, path: "/username1/slug") }
it "returns the correct URL for an article" do
expect(described_class.article(article)).to eq("https://dev.to#{article.path}")
end
end
describe ".user" do
let(:user) { build(:user) }
it "returns the correct URL for a user" do
expect(described_class.user(user)).to eq("https://dev.to/#{user.username}")
end
end
end

View file

@ -30,7 +30,7 @@ RSpec.describe Slack::Messengers::Feedback, type: :service do
message = get_argument_from_last_job("message")
expect(message).to include(user.username)
expect(message).to include(App.url("/#{user.username}"))
expect(message).to include(URL.user(user))
expect(message).to include(user.email)
end
@ -40,7 +40,7 @@ RSpec.describe Slack::Messengers::Feedback, type: :service do
end
message = get_argument_from_last_job("message")
url = App.url(
url = URL.url(
Rails.application.routes.url_helpers.internal_reports_path,
)

View file

@ -21,7 +21,7 @@ RSpec.describe Slack::Messengers::Note, type: :service do
expect(message).to include(default_params[:author_name])
expect(message).to include(default_params[:status])
url = App.url(
url = URL.url(
Rails.application.routes.url_helpers.internal_report_path(
default_params[:report_id],
),

View file

@ -13,7 +13,7 @@ RSpec.describe Slack::Messengers::PotentialSpammer, type: :service do
job = sidekiq_enqueued_jobs(worker: SlackBotPingWorker).last
message = job["args"].first["message"]
expect(message).to include(App.url("/#{user.username}"))
expect(message).to include(URL.user(user))
end
it "messages the proper channel with the proper username and emoji", :aggregate_failures do

View file

@ -19,7 +19,7 @@ RSpec.describe Slack::Messengers::RateLimit, type: :service do
message = job["args"].first["message"]
expect(message).to include(default_params[:action])
expect(message).to include(App.url("/#{user.username}"))
expect(message).to include(URL.user(user))
end
it "messages the proper channel with the proper username and emoji", :aggregate_failures do