Improve api oembed controller (#3450)
* Add StreamEntryFinder class to parse URLs * Use StreamEntryFinder and clean up api/oembed controller
This commit is contained in:
parent
1dcfb90202
commit
3576fa0d59
4 changed files with 92 additions and 17 deletions
34
app/lib/stream_entry_finder.rb
Normal file
34
app/lib/stream_entry_finder.rb
Normal file
|
@ -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
|
Loading…
Add table
Add a link
Reference in a new issue