From 7f3b98969f631911eb70314fbdcd872f0e55e785 Mon Sep 17 00:00:00 2001 From: evazion Date: Sun, 5 Oct 2014 11:59:06 -0500 Subject: [PATCH] Refactor normalize_for_artist_finder! Refactors things such that Sources::Site has a normalize_for_artist_finder! method that delegates to the strategy for the appropriate site. This way any site that needs to normalize URLs for the artist finder can do so. --- app/controllers/artists_controller.rb | 2 +- app/logical/sources/site.rb | 8 ++++++++ app/logical/sources/strategies/base.rb | 4 ++++ app/logical/sources/strategies/pixiv.rb | 5 ++--- 4 files changed, 15 insertions(+), 4 deletions(-) diff --git a/app/controllers/artists_controller.rb b/app/controllers/artists_controller.rb index 6144b1931..86ce765db 100644 --- a/app/controllers/artists_controller.rb +++ b/app/controllers/artists_controller.rb @@ -118,7 +118,7 @@ class ArtistsController < ApplicationController end def finder - url = Sources::Strategies::Pixiv.new(url).normalize_for_artist_finder!(params[:url]) + url = Sources::Site.new(params[:url]).normalize_for_artist_finder! @artists = Artist.url_matches(url).order("id desc").limit(20) respond_with(@artists) do |format| diff --git a/app/logical/sources/site.rb b/app/logical/sources/site.rb index 824a77025..9c13ba32b 100644 --- a/app/logical/sources/site.rb +++ b/app/logical/sources/site.rb @@ -20,6 +20,14 @@ module Sources end end + def normalize_for_artist_finder! + if available? + return strategy.normalize_for_artist_finder! + else + return url + end + end + def translated_tags untranslated_tags = tags untranslated_tags = untranslated_tags.map(&:first) diff --git a/app/logical/sources/strategies/base.rb b/app/logical/sources/strategies/base.rb index 902d6136f..6fad07ac5 100644 --- a/app/logical/sources/strategies/base.rb +++ b/app/logical/sources/strategies/base.rb @@ -18,6 +18,10 @@ module Sources raise NotImplementedError end + def normalize_for_artist_finder! + url + end + def site_name raise NotImplementedError end diff --git a/app/logical/sources/strategies/pixiv.rb b/app/logical/sources/strategies/pixiv.rb index b02e91dfb..7f55679b4 100644 --- a/app/logical/sources/strategies/pixiv.rb +++ b/app/logical/sources/strategies/pixiv.rb @@ -25,13 +25,12 @@ module Sources @pixiv_moniker end - def normalize_for_artist_finder!(url) - illust_id = illust_id_from_url(url) - + def normalize_for_artist_finder! # http://i2.pixiv.net/img04/img/syounen_no_uta/46170939_m.jpg if url =~ %r!/img/([^/]+)/\d+(?:_\w+)?\.(?:jpg|jpeg|png|gif)!i username = $1 else + illust_id = illust_id_from_url(url) get_metadata_from_spapi!(illust_id) do |metadata| username = metadata[24] end