Add specs (and refactor) of FetchRemoteResourceService and SearchService (#2812)

* Coverage for fetch remote resource service

* Refactor fetch remote resource service

* Coverage for search service

* Refactor search service
This commit is contained in:
Matt Jankowski
2017-05-05 11:26:04 -04:00
committed by Eugen Rochko
parent 9501a87704
commit 20c37ed0f9
4 changed files with 213 additions and 19 deletions

View File

@ -1,18 +1,41 @@
# frozen_string_literal: true
class FetchRemoteResourceService < BaseService
attr_reader :url
def call(url)
atom_url, body = FetchAtomService.new.call(url)
@url = url
process_url unless atom_url.nil?
end
return nil if atom_url.nil?
private
xml = Nokogiri::XML(body)
xml.encoding = 'utf-8'
if xml.root.name == 'feed'
def process_url
case xml_root
when 'feed'
FetchRemoteAccountService.new.call(atom_url, body)
elsif xml.root.name == 'entry'
when 'entry'
FetchRemoteStatusService.new.call(atom_url, body)
end
end
def fetched_atom_feed
@_fetched_atom_feed ||= FetchAtomService.new.call(url)
end
def atom_url
fetched_atom_feed.first
end
def body
fetched_atom_feed.last
end
def xml_root
xml_data.root.name
end
def xml_data
@_xml_data ||= Nokogiri::XML(body, nil, 'utf-8')
end
end

View File

@ -1,21 +1,38 @@
# frozen_string_literal: true
class SearchService < BaseService
attr_accessor :query
def call(query, limit, resolve = false, account = nil)
results = { accounts: [], hashtags: [], statuses: [] }
@query = query
return results if query.blank?
if query =~ /\Ahttps?:\/\//
resource = FetchRemoteResourceService.new.call(query)
results[:accounts] << resource if resource.is_a?(Account)
results[:statuses] << resource if resource.is_a?(Status)
else
results[:accounts] = AccountSearchService.new.call(query, limit, resolve, account)
results[:hashtags] = Tag.search_for(query.gsub(/\A#/, ''), limit) unless query.start_with?('@')
default_results.tap do |results|
if url_query?
results.merge!(remote_resource_results) unless remote_resource.nil?
elsif query.present?
results[:accounts] = AccountSearchService.new.call(query, limit, resolve, account)
results[:hashtags] = Tag.search_for(query.gsub(/\A#/, ''), limit) unless query.start_with?('@')
end
end
end
results
def default_results
{ accounts: [], hashtags: [], statuses: [] }
end
def url_query?
query =~ /\Ahttps?:\/\//
end
def remote_resource_results
{ remote_resource_symbol => [remote_resource] }
end
def remote_resource
@_remote_resource ||= FetchRemoteResourceService.new.call(query)
end
def remote_resource_symbol
remote_resource.class.name.downcase.pluralize.to_sym
end
end