about summary refs log tree commit diff
path: root/app/lib
diff options
context:
space:
mode:
authorMatt Jankowski <mjankowski@thoughtbot.com>2017-05-30 16:30:06 -0400
committerGitHub <noreply@github.com>2017-05-30 16:30:06 -0400
commit3576fa0d591db69a1727153a1130ff5bebf37167 (patch)
tree93fd386972bcf128b91d0296865239631286ef11 /app/lib
parent1dcfb902024a7d4049306d9bc48c499856e2e429 (diff)
Improve api oembed controller (#3450)
* Add StreamEntryFinder class to parse URLs

* Use StreamEntryFinder and clean up api/oembed controller
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