From 531a2de60ffe946a7a9ff94ea3a91cdc1383de36 Mon Sep 17 00:00:00 2001 From: Marcin Kulik Date: Fri, 27 Feb 2015 14:38:18 +0000 Subject: [PATCH] Handle all asciicast attribute building outside of the controller --- app/controllers/api/asciicasts_controller.rb | 34 +--------- app/models/asciicast_params.rb | 32 +++++++--- app/services/asciicast_creator.rb | 4 +- spec/models/asciicast_params_spec.rb | 65 -------------------- 4 files changed, 27 insertions(+), 108 deletions(-) delete mode 100644 spec/models/asciicast_params_spec.rb diff --git a/app/controllers/api/asciicasts_controller.rb b/app/controllers/api/asciicasts_controller.rb index 0408da1..cb12734 100644 --- a/app/controllers/api/asciicasts_controller.rb +++ b/app/controllers/api/asciicasts_controller.rb @@ -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 diff --git a/app/models/asciicast_params.rb b/app/models/asciicast_params.rb index 1832a1f..3c697cf 100644 --- a/app/models/asciicast_params.rb +++ b/app/models/asciicast_params.rb @@ -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 diff --git a/app/services/asciicast_creator.rb b/app/services/asciicast_creator.rb index 6f3e4ae..5ba63b7 100644 --- a/app/services/asciicast_creator.rb +++ b/app/services/asciicast_creator.rb @@ -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 diff --git a/spec/models/asciicast_params_spec.rb b/spec/models/asciicast_params_spec.rb deleted file mode 100644 index 1b20dfe..0000000 --- a/spec/models/asciicast_params_spec.rb +++ /dev/null @@ -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