Improve api oembed controller (#3450)

* Add StreamEntryFinder class to parse URLs

* Use StreamEntryFinder and clean up api/oembed controller
This commit is contained in:
Matt Jankowski 2017-05-30 16:30:06 -04:00 committed by GitHub
parent 1dcfb90202
commit 3576fa0d59
4 changed files with 92 additions and 17 deletions

View File

@ -4,30 +4,22 @@ class Api::OEmbedController < ApiController
respond_to :json respond_to :json
def show def show
@stream_entry = stream_entry_from_url(params[:url]) @stream_entry = find_stream_entry.stream_entry
@width = params[:maxwidth].present? ? params[:maxwidth].to_i : 400 @width = maxwidth_or_default
@height = params[:maxheight].present? ? params[:maxheight].to_i : nil @height = maxheight_or_default
end end
private private
def stream_entry_from_url(url) def find_stream_entry
params = Rails.application.routes.recognize_path(url) StreamEntryFinder.new(params[:url])
raise ActiveRecord::RecordNotFound unless recognized_stream_entry_url?(params)
stream_entry(params)
end end
def recognized_stream_entry_url?(params) def maxwidth_or_default
%w(stream_entries statuses).include?(params[:controller]) && params[:action] == 'show' (params[:maxwidth].presence || 400).to_i
end end
def stream_entry(params) def maxheight_or_default
if params[:controller] == 'stream_entries' params[:maxheight].present? ? params[:maxheight].to_i : nil
StreamEntry.find(params[:id])
else
Status.find(params[:id]).stream_entry
end
end end
end end

View 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

View File

@ -1,6 +1,8 @@
require 'rails_helper' require 'rails_helper'
RSpec.describe Api::OEmbedController, type: :controller do RSpec.describe Api::OEmbedController, type: :controller do
render_views
let(:alice) { Fabricate(:account, username: 'alice') } let(:alice) { Fabricate(:account, username: 'alice') }
let(:status) { Fabricate(:status, text: 'Hello world', account: alice) } let(:status) { Fabricate(:status, text: 'Hello world', account: alice) }

View File

@ -0,0 +1,47 @@
# frozen_string_literal: true
require 'rails_helper'
describe StreamEntryFinder do
include RoutingHelper
describe '#stream_entry' do
context 'with a status url' do
let(:status) { Fabricate(:status) }
let(:url) { short_account_status_url(account_username: status.account.username, id: status.id) }
subject { described_class.new(url) }
it 'finds the stream entry' do
expect(subject.stream_entry).to eq(status.stream_entry)
end
end
context 'with a stream entry url' do
let(:stream_entry) { Fabricate(:stream_entry) }
let(:url) { account_stream_entry_url(stream_entry.account, stream_entry) }
subject { described_class.new(url) }
it 'finds the stream entry' do
expect(subject.stream_entry).to eq(stream_entry)
end
end
context 'with a plausible url' do
let(:url) { 'https://example.com/users/test/updates/123/embed' }
subject { described_class.new(url) }
it 'raises an error' do
expect { subject.stream_entry }.to raise_error(ActiveRecord::RecordNotFound)
end
end
context 'with an unrecognized url' do
let(:url) { 'https://example.com/about' }
subject { described_class.new(url) }
it 'raises an error' do
expect { subject.stream_entry }.to raise_error(ActiveRecord::RecordNotFound)
end
end
end
end