class ProviderStatusesController < ApplicationController
layout 'test' if MagicNumber['test_layout']
- before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy, :licenses_import, :artists_import, :original_pictures_import, :import]
-
- private
-
- def licenses_port
- licenses = export_by('licenses_export', @provider_status, params[:date])
- return nil unless licenses
- ProviderLicense.import @provider_status.provider.id, licenses
- end
-
- def artists_port
- artists = export_by('artists_export', @provider_status, params[:date])
- return nil unless artists
- ProviderArtist.import @provider_status.provider.id, artists
- end
-
- def original_pictures_port
- original_pictures = export_by('original_pictures_export', @provider_status, params[:date])
- return nil unless original_pictures
- pictures = export_by('pictures_export', @provider_status, params[:date])
- return nil unless pictures
- ProviderOriginalPicture.import @provider_status.provider.id, original_pictures, pictures
- end
-
- def port
- exports = export_by('export', @provider_status, params[:date])
- return nil unless exports
- ProviderStatus.import @provider_status.provider.id, exports
- end
-
- public
+ before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy, :licenses_import, :artists_import, :original_pictures_import, :import, :import_all]
def index
@page = ProviderStatus.page params[:page]
def licenses_import
@provider_status = ProviderStatus.show(params[:id], @admin)
raise ActiveRecord::Forbidden if @provider_status.status == 0
- @failures = licenses_port
+ @failures = @provider_status.licenses_import params[:date]
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @failures.to_json() }
def artists_import
@provider_status = ProviderStatus.show(params[:id], @admin)
raise ActiveRecord::Forbidden if @provider_status.status == 0
- @failures = artists_port
+ @failures = @provider_status.artists_import params[:date]
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @failures.to_json() }
def original_pictures_import
@provider_status = ProviderStatus.show(params[:id], @admin)
raise ActiveRecord::Forbidden if @provider_status.status == 0
- @failures = original_pictures_port
+ @failures = @provider_status.original_pictures_import params[:date]
respond_to do |format|
format.html # show.html.erb
format.json { render :json => @failures.to_json() }
end
end
+ def import_all
+ ProviderStatus.find(:all).each do |provider_status|
+ next unless provider_status.status == 3
+ @provider_status = provider_status
+ @failures = port
+ end
+ respond_to do |format|
+ format.html { render :text => 'ok'}
+ format.json { render :json => {}.to_json() }
+ end
+ end
+
end
Provider.import_urls(urls) {|name, attr| Provider.store(name, attr)}
end
+ def export_url action
+ u = self.demander_url + (self.demander_url[-1] == '/' ? '' : '/')
+ u = URI.join(u, action + '.json')
+ u.to_s
+ end
+
end
self.save
end
+ def ymd_to_time ymd_str
+ return nil if ymd_str.blank?
+ date = nil
+ begin
+ date = Time.parse(ymd_str[0..3] + '/' + ymd_str[4..5] + '/' + ymd_str[6..7])
+ rescue
+ date = nil
+ end
+ date
+ end
+
+ def export_url action, date
+ u = self.provider.export_url action
+ prm = '?auth_token=' + self.token
+ prm = prm + '&date=' + date.strftime("%Y%m%d") unless date.blank?
+ u = URI.join(u, prm)
+ u.to_s
+ end
+
+ def export_from_provider url
+ res = nil
+ begin
+ json = RestClient.get url
+ res = JSON.parse json
+ rescue
+ end
+ res
+ end
+
+ def export_by action, ymd
+ t = ymd_to_time ymd
+ url = export_url action, t
+ export_from_provider(url)
+ end
+
+ def licenses_import date
+ licenses = self.export_by('licenses_export', date)
+ return nil unless licenses
+ ProviderLicense.import self.provider.id, licenses
+ end
+
+ def artists_import date
+ artists = self.export_by('artists_export', date)
+ return nil unless artists
+ ProviderArtist.import self.provider.id, artists
+ end
+
+ def original_pictures_import date
+ original_pictures = self.export_by('original_pictures_export', date)
+ return nil unless original_pictures
+ pictures = self.export_by('pictures_export', date)
+ return nil unless pictures
+ ProviderOriginalPicture.import self.provider.id, original_pictures, pictures
+ end
+
+ def port
+ exports = export_by('export', @provider_status, params[:date])
+ return nil unless exports
+ ProviderStatus.import @provider_status.provider.id, exports
+ end
+
def self.import pid, exports
res = {}
ProviderStatus.transaction do
context '事前チェックしておく' do
before do
ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
- ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json')
- ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
- ProviderLicense.stub(:import).with(any_args).and_return([])
+ ProviderStatus.stub(:licenses_import).with(any_args).and_return([])
end
it '借受状況モデルに単体取得を問い合わせている' do
ProviderStatus.should_receive(:show).exactly(1)
get :licenses_import, :id => @ps.id
end
- it '日付文字列変換を依頼している' do
- ProviderStatusesController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
- get :licenses_import, :id => @ps.id, :date => '20111010'
- end
- it 'エクスポートurl取得を依頼している' do
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
- ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'licenses_export', @ps.token, Time.parse('2011/10/10')).exactly(1)
- get :licenses_import, :id => @ps.id, :date => '20111010'
- end
- it '貸手からのエクスポートを依頼している' do
- ProviderStatusesController.any_instance.should_receive(:export_from_provider).exactly(1)
- get :licenses_import, :id => @ps.id
- end
- it 'ライセンス対照表モデルにインポートを依頼している' do
- ProviderLicense.should_receive(:import).exactly(1)
+ it 'インポートを依頼している' do
+ ProviderStatus.any_instance.should_receive(:licenses_import).exactly(1)
get :licenses_import, :id => @ps.id
end
end
context 'つつがなく終わるとき' do
before do
ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
- ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json')
- ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
- ProviderLicense.stub(:import).with(any_args).and_return([])
+ ProviderStatus.any_instance.stub(:licenses_import).with(any_args).and_return([])
end
it 'ステータスコード200 OKを返す' do
get :licenses_import, :id => @ps.id
context '事前チェックしておく' do
before do
ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
- ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json')
- ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes])
- ProviderArtist.stub(:import).with(any_args).and_return([])
+ ProviderStatus.any_instance.stub(:artists_import).with(any_args).and_return([])
end
it '借受状況モデルに単体取得を問い合わせている' do
ProviderStatus.should_receive(:show).exactly(1)
get :artists_import, :id => @ps.id
end
- it '日付文字列変換を依頼している' do
- ProviderStatusesController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1)
- get :licenses_import, :id => @ps.id, :date => '20111010'
- end
- it 'エクスポートurl取得を依頼している' do
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
- ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'artists_export', @ps.token, Time.parse('2011/10/10')).exactly(1)
- get :artists_import, :id => @ps.id, :date => '20111010'
- end
- it '貸手からのエクスポートを依頼している' do
- ProviderStatusesController.any_instance.should_receive(:export_from_provider).exactly(1)
- get :artists_import, :id => @ps.id
- end
- it '絵師対照表モデルにインポートを依頼している' do
- ProviderArtist.should_receive(:import).exactly(1)
+ it 'インポートを依頼している' do
+ ProviderStatus.any_instance.should_receive(:artists_import).exactly(1)
get :artists_import, :id => @ps.id
end
end
context 'つつがなく終わるとき' do
before do
ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10'))
- ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil)
- ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json')
- ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes])
- ProviderArtist.stub(:import).with(any_args).and_return([])
+ ProviderStatus.any_instance.stub(:artists_import).with(any_args).and_return([])
end
it 'ステータスコード200 OKを返す' do
get :artists_import, :id => @ps.id
context '事前チェックしておく' do
before do
ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
- ProviderStatusesController.any_instance.stub(:export_by).with(any_args).and_return([])
- ProviderOriginalPicture.stub(:import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
+ ProviderStatus.any_instance.stub(:original_pictures_import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
end
it '借受状況モデルに単体取得を問い合わせている' do
ProviderStatus.should_receive(:show).exactly(1)
get :original_pictures_import, :id => @ps.id
end
- it '貸手からの原画エクスポート,実素材エクスポートを依頼している' do
- ProviderStatusesController.any_instance.should_receive(:export_by).with(any_args).exactly(2)
- get :original_pictures_import, :id => @ps.id
- end
- it '原画対照表モデルにインポートを依頼している' do
- ProviderOriginalPicture.should_receive(:import).exactly(1)
+ it 'インポートを依頼している' do
+ ProviderStatus.any_instance.should_receive(:original_pictures_import).exactly(1)
get :original_pictures_import, :id => @ps.id
end
end
context 'つつがなく終わるとき' do
before do
ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1)
- ProviderStatusesController.any_instance.stub(:export_by).with(any_args).and_return([@opattr])
- ProviderOriginalPicture.stub(:import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
+ ProviderStatus.any_instance.stub(:original_pictures_import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
end
it 'ステータスコード200 OKを返す' do
get :original_pictures_import, :id => @ps.id
end
end
+ describe 'エクスポートurl取得に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id, :demander_url => 'http://demander.url/'
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ end
+ it 'エクスポートurl文字列を返す' do
+ r = @provider.export_url 'action'
+ r.should eq 'http://demander.url/action.json'
+ end
+ end
+ context '借手urlが/で終わらないとき' do
+ before do
+ @provider.demander_url = 'http://demander.url'
+ end
+ it '末尾に/を追加する' do
+ r = @provider.export_url 'action'
+ r.should eq 'http://demander.url/action.json'
+ end
+ end
+ end
+
end
end
end
+ describe 'エクスポートurl取得に於いて' do
+ before do
+ @token = 'aaaaaaaaaaaaaaaaa'
+ @ps = FactoryGirl.create :provider_status, :token => @token
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context '事前チェックしておく' do
+ before do
+ Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
+ end
+ it '借手にエクスポートurl取得を依頼している' do
+ Provider.any_instance.should_receive(:export_url).exactly(1)
+ @ps.export_url 'action', Time.now
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
+ end
+ it 'エクスポートurl文字列を返す' do
+ r = @ps.export_url 'action', nil
+ r.should eq 'http://demander.url/action.json?auth_token=' + @token
+ end
+ end
+ context 'エクスポート日時が設定されているとき' do
+ before do
+ Provider.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json')
+ end
+ it 'エクスポート日時を追加する' do
+ r = @ps.export_url 'action', Time.parse('2000/1/1')
+ r.should match /20000101/
+ end
+ end
+ end
+
+ describe '貸手からのエクスポート通信に於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context '事前チェックしておく' do
+ before do
+ RestClient.stub(:get).with(any_args).and_return('{}')
+ JSON.stub(:parse).with(any_args).and_return({})
+ end
+ it 'Restクライアントに取得通信を依頼している' do
+ RestClient.should_receive(:get).exactly(1)
+ @ps.export_from_provider 'url'
+ end
+ it 'JSONライブラリにパースを依頼している' do
+ JSON.should_receive(:parse).exactly(1)
+ @ps.export_from_provider 'url'
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ RestClient.stub(:get).with(any_args).and_return('{}')
+ JSON.stub(:parse).with(any_args).and_return({})
+ end
+ it 'JSONデータを返す' do
+ r = @ps.export_from_provider 'url'
+ r.is_a?(Hash).should be_true
+ end
+ end
+ context 'エクスポートでエラーが発生したとき' do
+ before do
+ RestClient.stub(:get).with(any_args).and_raise(StandardError)
+ end
+ it 'nilを返す' do
+ r = @ps.export_from_provider 'url'
+ r.should be_nil
+ end
+ end
+ context 'パースでエラーが発生したとき' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+ end
+ it 'nilを返す' do
+ r = @ps.export_from_provider 'url'
+ r.should be_nil
+ end
+ end
+ end
+
+ describe '貸手からのエクスポートに於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context '事前チェックしておく' do
+ before do
+ ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+ ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+ ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
+ end
+ it '日付文字列変換を依頼している' do
+ ProviderStatus.any_instance.should_receive(:ymd_to_time).exactly(1)
+ @ps.export_by 'action', '20111010'
+ end
+ it 'エクスポートurl取得を依頼している' do
+ ProviderStatus.any_instance.should_receive(:export_url).exactly(1)
+ @ps.export_by 'action', '20111010'
+ end
+ it '貸手からのエクスポート通信を依頼している' do
+ ProviderStatus.any_instance.should_receive(:export_from_provider).exactly(1)
+ @ps.export_by 'action', '20111010'
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+ ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+ ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes])
+ end
+ it 'リストを返す' do
+ r = @ps.export_by 'action', '20111010'
+ r.is_a?(Array).should be_true
+ end
+ end
+ context 'エクスポートでエラーが発生したとき' do
+ before do
+ ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+ ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+ ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return(nil)
+ end
+ it 'nilを返す' do
+ r = @ps.export_by 'action', '20111010'
+ r.should be_nil
+ end
+ end
+ context 'パースでエラーが発生したとき' do
+ before do
+ ProviderStatus.any_instance.stub(:ymd_to_time).with(any_args).and_return(Time.parse('2000/1/1'))
+ ProviderStatus.any_instance.stub(:export_url).with(any_args).and_return('http://demander.url/action.json?auth_token=aaaaaaaaaaaaaaaa')
+ ProviderStatus.any_instance.stub(:export_from_provider).with(any_args).and_return(nil)
+ end
+ it 'nilを返す' do
+ r = @ps.export_by 'action', '20111010'
+ r.should be_nil
+ end
+ end
+ end
+
+ describe 'ライセンスインポートに於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context '事前チェックしておく' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([@license.attributes])
+ ProviderLicense.stub(:import).with(any_args).and_return([])
+ end
+ it '貸手からのエクスポートを依頼している' do
+ ProviderStatus.any_instance.should_receive(:export_by).exactly(1)
+ r = @ps.licenses_import '20111010'
+ end
+ it 'ライセンス対照表モデルにインポートを依頼している' do
+ ProviderLicense.should_receive(:import).exactly(1)
+ r = @ps.licenses_import '20111010'
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([@license.attributes])
+ ProviderLicense.stub(:import).with(any_args).and_return([])
+ end
+ it 'ライセンスインポート結果を返す' do
+ r = @ps.licenses_import '20111010'
+ r.should eq []
+ end
+ end
+ context 'エクスポートに失敗したとき' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+ end
+ it 'nilを返す' do
+ r = @ps.licenses_import '20111010'
+ r.should be_nil
+ end
+ end
+ end
+
+ describe '絵師インポートに於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context '事前チェックしておく' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+ ProviderArtist.stub(:import).with(any_args).and_return([])
+ end
+ it '貸手からのエクスポートを依頼している' do
+ ProviderStatus.any_instance.should_receive(:export_by).exactly(1)
+ @ps.artists_import '20111010'
+ end
+ it '絵師対照表モデルにインポートを依頼している' do
+ ProviderArtist.should_receive(:import).exactly(1)
+ @ps.artists_import '20111010'
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+ ProviderArtist.stub(:import).with(any_args).and_return([])
+ end
+ it '絵師インポート結果を返す' do
+ r = @ps.artists_import '20111010'
+ r.should eq []
+ end
+ end
+ context 'エクスポートに失敗したとき' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+ end
+ it 'nilを返す' do
+ r = @ps.artists_import '20111010'
+ r.should be_nil
+ end
+ end
+ end
+
+ describe '素材インポートに於いて' do
+ before do
+ @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa'
+ @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id
+ end
+ context '事前チェックしておく' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+ ProviderOriginalPicture.stub(:import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
+ end
+ it '貸手からのエクスポートを依頼している' do
+ ProviderStatus.any_instance.should_receive(:export_by).exactly(2)
+ @ps.original_pictures_import '20111010'
+ end
+ it '原画対照表モデルにインポートを依頼している' do
+ ProviderOriginalPicture.should_receive(:import).exactly(1)
+ @ps.original_pictures_import '20111010'
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return([])
+ ProviderOriginalPicture.stub(:import).with(any_args).and_return({:original_pictures => [], :pictures => [], :resource_pictures => []})
+ end
+ it '原画インポート結果を返す' do
+ r = @ps.original_pictures_import '20111010'
+ r.is_a?(Hash).should be_true
+ end
+ it 'インポート失敗リストを取得している' do
+ r = @ps.original_pictures_import '20111010'
+ r[:original_pictures].should be_empty
+ r[:pictures].should be_empty
+ r[:resource_pictures].should be_empty
+ end
+ end
+ context 'エクスポートに失敗したとき' do
+ before do
+ ProviderStatus.any_instance.stub(:export_by).with(any_args).and_return(nil)
+ end
+ it 'nilを返す' do
+ r = @ps.original_pictures_import '20111010'
+ r.should be_nil
+ end
+ end
+ end
+
end