Added ArticleWithVideoCreationService (#344)

This commit is contained in:
Araslanov Evgeny 2018-08-16 00:19:54 +03:00 committed by Ben Halpern
parent 4ba8296298
commit 8095c8fc4d
2 changed files with 47 additions and 13 deletions

View file

@ -7,22 +7,14 @@ class VideosController < ApplicationController
def create
authorize :video
@article = Article.new(body_markdown: "---\ntitle: Unpublished Video ~ #{rand(100000).to_s(26)}\npublished: false\ndescription: \ntags: \n---\n\n", processed_html: "")
@article.user_id = current_user.id
@article.show_comments = true
assign_video_attributes
@article.save!
@article = ArticleWithVideoCreationService.new(article_params, current_user).create!
render action: "js_response"
end
def assign_video_attributes
if params[:article][:video]
@article.video = params[:article][:video]
@article.video_state = "PROGRESSING"
@article.video_code = @article.video.split("dev-to-input-v0/")[1]
@article.video_source_url = "https://dw71fyauz7yz9.cloudfront.net/#{@article.video_code}/#{@article.video_code}.m3u8"
@article.video_thumbnail_url = "https://dw71fyauz7yz9.cloudfront.net/#{@article.video_code}/thumbs-#{@article.video_code}-00001.png"
private
end
def article_params
params.require(:article).permit(:video)
end
end

View file

@ -0,0 +1,42 @@
# frozen_string_literal: true
# rubocop:disable Layout/TrailingWhitespace
class ArticleWithVideoCreationService
VIDEO_SERVICE_URL = "https://dw71fyauz7yz9.cloudfront.net"
def initialize(article_params, current_user)
@article_params = article_params
@current_user = current_user
end
def create!
Article.create! do |article|
article.body_markdown = <<~BODY
---
title: Unpublished Video ~ #{rand(100000).to_s(26)}
published: false
description:
tags:
---
BODY
article.processed_html = ""
article.user_id = @current_user.id
article.show_comments = true
if @article_params[:video].present?
article.video = @article_params[:video]
article.video_state = "PROGRESSING"
video_code = article.video.split("dev-to-input-v0/")[1]
article.video_code = video_code
article.video_source_url = "#{VIDEO_SERVICE_URL}/#{video_code}/#{video_code}.m3u8"
thumb_name = "thumbs-#{video_code}-00001"
article.video_thumbnail_url = "#{VIDEO_SERVICE_URL}/#{video_code}/#{thumb_name}.png"
end
end
end
end
# rubocop:enable Layout/TrailingWhitespace