about summary refs log tree commit diff
path: root/app/controllers
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/controllers
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/controllers')
-rw-r--r--app/controllers/api/oembed_controller.rb26
1 files changed, 9 insertions, 17 deletions
diff --git a/app/controllers/api/oembed_controller.rb b/app/controllers/api/oembed_controller.rb
index 656851f43..576188353 100644
--- a/app/controllers/api/oembed_controller.rb
+++ b/app/controllers/api/oembed_controller.rb
@@ -4,30 +4,22 @@ class Api::OEmbedController < ApiController
   respond_to :json
 
   def show
-    @stream_entry = stream_entry_from_url(params[:url])
-    @width        = params[:maxwidth].present?  ? params[:maxwidth].to_i  : 400
-    @height       = params[:maxheight].present? ? params[:maxheight].to_i : nil
+    @stream_entry = find_stream_entry.stream_entry
+    @width = maxwidth_or_default
+    @height = maxheight_or_default
   end
 
   private
 
-  def stream_entry_from_url(url)
-    params = Rails.application.routes.recognize_path(url)
-
-    raise ActiveRecord::RecordNotFound unless recognized_stream_entry_url?(params)
-
-    stream_entry(params)
+  def find_stream_entry
+    StreamEntryFinder.new(params[:url])
   end
 
-  def recognized_stream_entry_url?(params)
-    %w(stream_entries statuses).include?(params[:controller]) && params[:action] == 'show'
+  def maxwidth_or_default
+    (params[:maxwidth].presence || 400).to_i
   end
 
-  def stream_entry(params)
-    if params[:controller] == 'stream_entries'
-      StreamEntry.find(params[:id])
-    else
-      Status.find(params[:id]).stream_entry
-    end
+  def maxheight_or_default
+    params[:maxheight].present? ? params[:maxheight].to_i : nil
   end
 end