about summary refs log tree commit diff
path: root/app/services
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-05-05 11:26:04 -0400
committerEugen Rochko <eugen@zeonfederated.com>2017-05-05 17:26:04 +0200
commit20c37ed0f988298de75a3c41472222076806d243 (patch)
tree9fd1d3a688483e2fbc26e5ec02586db1ce7602b4 /app/services
parent9501a877048a34627155b04b75a9c75fdbb25cb9 (diff)
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
Diffstat (limited to 'app/services')
-rw-r--r--app/services/fetch_remote_resource_service.rb37
-rw-r--r--app/services/search_service.rb39
2 files changed, 58 insertions, 18 deletions
diff --git a/app/services/fetch_remote_resource_service.rb b/app/services/fetch_remote_resource_service.rb
index 2185ceb20..a0c270673 100644
--- a/app/services/fetch_remote_resource_service.rb
+++ b/app/services/fetch_remote_resource_service.rb
@@ -1,18 +1,41 @@
 # frozen_string_literal: true
 
 class FetchRemoteResourceService < BaseService
-  def call(url)
-    atom_url, body = FetchAtomService.new.call(url)
+  attr_reader :url
 
-    return nil if atom_url.nil?
+  def call(url)
+    @url = url
+    process_url unless atom_url.nil?
+  end
 
-    xml = Nokogiri::XML(body)
-    xml.encoding = 'utf-8'
+  private
 
-    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
diff --git a/app/services/search_service.rb b/app/services/search_service.rb
index e9745010b..1ed3f0032 100644
--- a/app/services/search_service.rb
+++ b/app/services/search_service.rb
@@ -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?
+    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
 
-    if query =~ /\Ahttps?:\/\//
-      resource = FetchRemoteResourceService.new.call(query)
+  def default_results
+    { accounts: [], hashtags: [], statuses: [] }
+  end
 
-      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?('@')
-    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
 
-    results
+  def remote_resource_symbol
+    remote_resource.class.name.downcase.pluralize.to_sym
   end
 end