Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Import DevTo URL #12

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 26 additions & 0 deletions app/models/dev_to/url.rb
Original file line number Diff line number Diff line change
@@ -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
12 changes: 12 additions & 0 deletions test/models/dev_to/url_test.rb
Original file line number Diff line number Diff line change
@@ -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
Empty file removed test/services/rest/client_test.rb
Empty file.