about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
Diffstat (limited to 'app/lib')
-rw-r--r--app/lib/stream_entry_finder.rb34
1 files changed, 34 insertions, 0 deletions
diff --git a/app/lib/stream_entry_finder.rb b/app/lib/stream_entry_finder.rb
new file mode 100644
index 000000000..0ea33229c
--- /dev/null
+++ b/app/lib/stream_entry_finder.rb
@@ -0,0 +1,34 @@
+# frozen_string_literal: true
+
+class StreamEntryFinder
+  attr_reader :url
+
+  def initialize(url)
+    @url = url
+  end
+
+  def stream_entry
+    verify_action!
+
+    case recognized_params[:controller]
+    when 'stream_entries'
+      StreamEntry.find(recognized_params[:id])
+    when 'statuses'
+      Status.find(recognized_params[:id]).stream_entry
+    else
+      raise ActiveRecord::RecordNotFound
+    end
+  end
+
+  private
+
+  def recognized_params
+    Rails.application.routes.recognize_path(url)
+  end
+
+  def verify_action!
+    unless recognized_params[:action] == 'show'
+      raise ActiveRecord::RecordNotFound
+    end
+  end
+end