Skip to content
Permalink
Browse files

add Wataridori::LinkReplacementRule

  • Loading branch information...
kokuyouwind committed Dec 5, 2018
1 parent b255e92 commit 80a2c018dabb4ca1a2b217e2d8ae7dfc4bf5ba76
Showing with 147 additions and 0 deletions.
  1. +1 −0 lib/wataridori.rb
  2. +67 −0 lib/wataridori/link_replacement_rule.rb
  3. +79 −0 spec/wataridori/link_replacement_rule_spec.rb
@@ -3,6 +3,7 @@
require 'wataridori/version'
require 'wataridori/client'
require 'wataridori/copy_result'
require 'wataridori/link_replacement_rule'
require 'wataridori/esa/client'
require 'wataridori/esa/ratelimit'
require 'wataridori/esa/response'
@@ -0,0 +1,67 @@
# frozen_string_literal: true

module Wataridori
class LinkReplacementRule
RELATIVE_POST_PATH = %r{^\/posts\/(\d+)}.freeze

def initialize(from, to, copy_results)
@from = from
@to = to
@posts_map = copy_results.each_with_object({}) do |result, posts_map|
posts_map[result.from.number] = result.to
end
end

# URLを置換する必要があるかを返す
def target?(link)
return true if post_relative_link?(link)

copied_post?(link)
end

def replaced(link)
return esa_host_replaced(link) unless copied_post?(link)

post_number = extract_post_number(link)
return post_number_replaced(link, post_number) if post_number

link
end

def post_relative_link?(link)
link.start_with?('/posts/')
end

private

attr_reader :from, :to, :posts_map

def copied_post?(link)
post_number = extract_post_number(link)
post_number && posts_map[post_number]
end

def post_number_replaced(link, post_number)
summary = posts_map[post_number]
if post_relative_link?(link)
link.gsub(RELATIVE_POST_PATH, "/posts/#{summary.number}")
else
summary.url
end
end

def esa_host_replaced(link)
if post_relative_link?(link)
"https://#{from}.esa.io#{link}"
else
link
end
end

def extract_post_number(link)
path = link.gsub("https://#{from}.esa.io", '')
m = path.match(RELATIVE_POST_PATH)
m ? m[1].to_i : nil
end
end
end
@@ -0,0 +1,79 @@
# frozen_string_literal: true

RSpec.describe Wataridori::LinkReplacementRule do
let(:rule) do
described_class.new(
'from',
'to',
[
Wataridori::CopyResult.create_by_hash(
from: { number: 1, url: 'https://from.esa.io/posts/1' },
to: { number: 10, url: 'http://to.esa.io/posts/10' }
)
]
)
end

describe '#target?' do
subject { rule.target?(link) }

context '移行対象記事の相対パス' do
let(:link) { '/posts/1' }
it { is_expected.to be_truthy }
end

context '移行対象記事の絶対パス' do
let(:link) { 'https://from.esa.io/posts/1' }
it { is_expected.to be_truthy }
end

context '移行対象外記事の相対パス' do
let(:link) { '/posts/5' }
it { is_expected.to be_truthy }
end

context '移行対象外記事の絶対パス' do
let(:link) { 'https://from.esa.io/posts/5' }
it { is_expected.to be_falsey }
end

context '関係のないURL' do
let(:link) { 'https://example.com/' }
it { is_expected.to be_falsey }
end
end

describe '#replaced' do
subject { rule.replaced(link) }

context '移行対象記事の相対パス' do
let(:link) { '/posts/1' }
it { is_expected.to eq('/posts/10') }
end

context '移行対象記事の絶対パス' do
let(:link) { 'https://from.esa.io/posts/1' }
it { is_expected.to eq('http://to.esa.io/posts/10') }
end

context '移行対象外記事の相対パス' do
let(:link) { '/posts/5' }
it { is_expected.to eq('https://from.esa.io/posts/5') }
end

context '移行対象外記事の絶対パス' do
let(:link) { 'https://from.esa.io/posts/5' }
it { is_expected.to eq('https://from.esa.io/posts/5') }
end

context '関係のないURL' do
let(:link) { 'https://example.com/' }
it { is_expected.to eq('https://example.com/') }
end
end

describe '#relative?' do
it { expect(rule.post_relative_link?('/posts/1')).to be_truthy }
it { expect(rule.post_relative_link?('https://from.esa.io/posts/1')).to be_falsey }
end
end

0 comments on commit 80a2c01

Please sign in to comment.
You can’t perform that action at this time.