From e52c48103bbad95fd273bac5ba04c5abc50f5d9f Mon Sep 17 00:00:00 2001 From: Lucian Ghinda Date: Fri, 17 Feb 2023 05:26:12 +0200 Subject: [PATCH 1/2] Added model for DevTo::Url --- app/models/dev_to/url.rb | 26 ++++++++++++++++++++++++++ test/models/dev_to/url_test.rb | 12 ++++++++++++ 2 files changed, 38 insertions(+) create mode 100644 app/models/dev_to/url.rb create mode 100644 test/models/dev_to/url_test.rb diff --git a/app/models/dev_to/url.rb b/app/models/dev_to/url.rb new file mode 100644 index 0000000..3391531 --- /dev/null +++ b/app/models/dev_to/url.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true + +module DevTo + class Url + DEV_TO_HOST = "dev.to" + + delegate :to_s, to: :url + + def self.valid?(url) + uri = URI.parse(url) + + uri.host == DEV_TO_HOST + end + + def initialize(original_url) + @original_url = original_url + @url = URI.parse(original_url) + end + + def username = @_username ||= url.path.delete_prefix("/").split("/")&.first + + private + + attr_reader :url + end +end diff --git a/test/models/dev_to/url_test.rb b/test/models/dev_to/url_test.rb new file mode 100644 index 0000000..6b9dafe --- /dev/null +++ b/test/models/dev_to/url_test.rb @@ -0,0 +1,12 @@ +# frozen_string_literal: true + +require "test_helper" + +class DevTo::UrlTest < ActiveSupport::TestCase + test "it returns the correct username" do + initial_url = "https://dev.to/author_name/article-slug" + dev_to_url = DevTo::Url.new(initial_url) + + assert_equal "author_name", dev_to_url.username + end +end From cc32de7a1b511f77a40b63222af50a3e82b1d62a Mon Sep 17 00:00:00 2001 From: Lucian Ghinda Date: Fri, 17 Feb 2023 05:26:18 +0200 Subject: [PATCH 2/2] Removed unused test file --- test/services/rest/client_test.rb | 0 1 file changed, 0 insertions(+), 0 deletions(-) delete mode 100644 test/services/rest/client_test.rb diff --git a/test/services/rest/client_test.rb b/test/services/rest/client_test.rb deleted file mode 100644 index e69de29..0000000