Handle all asciicast attribute building outside of the controller
This commit is contained in:
parent
c6cc9fbdbc
commit
531a2de60f
@ -6,7 +6,7 @@ module Api
|
||||
attr_reader :asciicast
|
||||
|
||||
def create
|
||||
asciicast = asciicast_creator.create(*parse_request)
|
||||
asciicast = asciicast_creator.create(asciicast_attributes)
|
||||
render text: asciicast_url(asciicast), status: :created,
|
||||
location: asciicast
|
||||
|
||||
@ -29,37 +29,9 @@ module Api
|
||||
|
||||
private
|
||||
|
||||
def parse_request
|
||||
if legacy_format?
|
||||
attrs, username, token = parse_format_0_request
|
||||
else
|
||||
attrs, username, token = parse_format_1_request
|
||||
end
|
||||
|
||||
user = User.for_api_token!(token, username)
|
||||
|
||||
[attrs, user]
|
||||
end
|
||||
|
||||
def legacy_format?
|
||||
!params[:asciicast].try(:respond_to?, :read)
|
||||
end
|
||||
|
||||
def parse_format_0_request
|
||||
meta = JSON.parse(params[:asciicast][:meta].read)
|
||||
def asciicast_attributes
|
||||
username, token = basic_auth_credentials
|
||||
token ||= meta.delete('user_token')
|
||||
username ||= meta.delete('username')
|
||||
attrs = AsciicastParams.from_format_0_request(params[:asciicast].merge(meta: meta), request.user_agent)
|
||||
|
||||
[attrs, username, token]
|
||||
end
|
||||
|
||||
def parse_format_1_request
|
||||
username, token = basic_auth_credentials
|
||||
attrs = AsciicastParams.from_format_1_request(params[:asciicast], request.user_agent)
|
||||
|
||||
[attrs, username, token]
|
||||
AsciicastParams.build(params[:asciicast], username, token, request.user_agent)
|
||||
end
|
||||
|
||||
def basic_auth_credentials
|
||||
|
@ -1,10 +1,19 @@
|
||||
class AsciicastParams
|
||||
|
||||
def self.from_format_0_request(params, user_agent)
|
||||
meta = params[:meta]
|
||||
def self.build(asciicast_params, username, token, user_agent)
|
||||
if asciicast_params.try(:respond_to?, :read)
|
||||
from_format_1_request(asciicast_params, username, token, user_agent)
|
||||
else
|
||||
from_format_0_request(asciicast_params, username, token, user_agent)
|
||||
end
|
||||
end
|
||||
|
||||
def self.from_format_0_request(params, username, token, user_agent)
|
||||
meta = JSON.parse(params.delete(:meta).read)
|
||||
token ||= meta.delete('user_token')
|
||||
username ||= meta.delete('username')
|
||||
|
||||
attributes = {
|
||||
version: 0,
|
||||
command: meta['command'],
|
||||
duration: meta['duration'],
|
||||
shell: meta['shell'],
|
||||
@ -16,6 +25,8 @@ class AsciicastParams
|
||||
terminal_lines: meta['term']['lines'],
|
||||
terminal_type: meta['term']['type'],
|
||||
title: meta['title'],
|
||||
user: User.for_api_token!(token, username),
|
||||
version: 0,
|
||||
}
|
||||
|
||||
if meta['uname'] # old client, with useless user_agent
|
||||
@ -27,21 +38,22 @@ class AsciicastParams
|
||||
attributes
|
||||
end
|
||||
|
||||
def self.from_format_1_request(asciicast_file, user_agent)
|
||||
def self.from_format_1_request(asciicast_file, username, token, user_agent)
|
||||
asciicast = Oj.sc_parse(AsciicastHandler.new, asciicast_file)
|
||||
env = asciicast['env']
|
||||
|
||||
{
|
||||
version: 1,
|
||||
command: asciicast['command'],
|
||||
duration: asciicast['duration'],
|
||||
file: asciicast_file,
|
||||
shell: env && env['SHELL'],
|
||||
terminal_columns: asciicast['width'],
|
||||
terminal_lines: asciicast['height'],
|
||||
duration: asciicast['duration'],
|
||||
command: asciicast['command'],
|
||||
title: asciicast['title'],
|
||||
shell: env && env['SHELL'],
|
||||
terminal_type: env && env['TERM'],
|
||||
file: asciicast_file,
|
||||
title: asciicast['title'],
|
||||
user: User.for_api_token!(token, username),
|
||||
user_agent: user_agent,
|
||||
version: 1,
|
||||
}
|
||||
end
|
||||
|
||||
|
@ -1,7 +1,7 @@
|
||||
class AsciicastCreator
|
||||
|
||||
def create(attributes, user)
|
||||
asciicast = Asciicast.create!(attributes.merge(user: user))
|
||||
def create(attributes)
|
||||
asciicast = Asciicast.create!(attributes)
|
||||
AsciicastWorker.perform_async(asciicast.id)
|
||||
|
||||
asciicast
|
||||
|
@ -1,65 +0,0 @@
|
||||
require 'rails_helper'
|
||||
require 'stringio'
|
||||
|
||||
describe AsciicastParams do
|
||||
|
||||
describe '.build' do
|
||||
subject { described_class.build(input, user_agent) }
|
||||
|
||||
let(:input) { {
|
||||
meta: meta,
|
||||
stdout: stdout_data_file,
|
||||
stdout_timing: stdout_timing_file,
|
||||
} }
|
||||
|
||||
let(:user_agent) { 'asciinema/0.9.7' }
|
||||
|
||||
let(:stdout_data_file) { double('stdout_data_file') }
|
||||
let(:stdout_timing_file) { double('stdout_timing_file') }
|
||||
|
||||
let(:meta) { HashWithIndifferentAccess.new({
|
||||
command: '/bin/bash',
|
||||
duration: 11.146430015563965,
|
||||
shell: '/bin/zsh',
|
||||
term: { lines: 26, columns: 96, type: 'screen-256color' },
|
||||
title: 'bashing :)',
|
||||
user_token: 'f33e6188-f53c-11e2-abf4-84a6c827e88b',
|
||||
username: 'kill',
|
||||
}) }
|
||||
|
||||
let(:expected_attrs) { {
|
||||
command: '/bin/bash',
|
||||
duration: 11.146430015563965,
|
||||
shell: '/bin/zsh',
|
||||
stdin_data: nil,
|
||||
stdin_timing: nil,
|
||||
stdout_data: stdout_data_file,
|
||||
stdout_timing: stdout_timing_file,
|
||||
terminal_columns: 96,
|
||||
terminal_lines: 26,
|
||||
terminal_type: 'screen-256color',
|
||||
title: 'bashing :)',
|
||||
user_agent: 'asciinema/0.9.7',
|
||||
} }
|
||||
|
||||
let(:existing_user) { User.new }
|
||||
|
||||
before do
|
||||
allow(User).to receive(:for_api_token).
|
||||
with('f33e6188-f53c-11e2-abf4-84a6c827e88b') { existing_user }
|
||||
end
|
||||
|
||||
it { should eq(expected_attrs) }
|
||||
|
||||
context "when uname given" do
|
||||
before do
|
||||
meta[:uname] = 'Linux 3.9.9-302.fc19.x86_64'
|
||||
expected_attrs[:uname] = 'Linux 3.9.9-302.fc19.x86_64'
|
||||
expected_attrs.delete(:user_agent)
|
||||
end
|
||||
|
||||
it { should eq(expected_attrs) }
|
||||
end
|
||||
end
|
||||
|
||||
end
|
Loading…
Reference in New Issue
Block a user