From adc47da04b9cb65e1eb62302f3208b0dc091347a Mon Sep 17 00:00:00 2001 From: "Darren L. Weber, Ph.D" Date: Wed, 27 Sep 2017 10:16:07 -0700 Subject: [PATCH] [WIP] Web Of Knowledge Client - WosClient specs --- lib/wos_client.rb | 23 ++++++--- spec/fixtures/wos_client/authenticate.xml | 8 +++ spec/wos_client_spec.rb | 62 +++++++++++++++++++++++ 3 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 spec/fixtures/wos_client/authenticate.xml create mode 100644 spec/wos_client_spec.rb diff --git a/lib/wos_client.rb b/lib/wos_client.rb index 796bd05da..9ddc37d89 100644 --- a/lib/wos_client.rb +++ b/lib/wos_client.rb @@ -1,6 +1,8 @@ require 'savon' -# See http://savonrb.com/version2/client.html +# A Web of Science (or Web of Knowledge) client +# It uses the WSDL service definitions from WOS +# It uses the savon gem for SOAP, see http://savonrb.com/version2/client.html class WosClient AUTH_CODE = Settings.WOS.AUTH_CODE @@ -8,11 +10,8 @@ class WosClient SEARCH_WSDL = Settings.WOS.SEARCH_WSDL LOG_LEVEL = Settings.WOS.LOG_LEVEL.to_sym - def initialize - auth - search - end - + # A client for the authorization endpoint, using WSDL + # @return [Savon::Client] def auth @auth ||= Savon.client( wsdl: AUTH_WSDL, @@ -24,10 +23,14 @@ def auth ) end + # Calls authenticate on the authentication endpoint + # @return [Savon::Response] def authenticate auth.call(:authenticate) end + # A client for the search endpoint, using WSDL + # @return [Savon::Client] def search @search ||= Savon.client( wsdl: SEARCH_WSDL, @@ -39,14 +42,20 @@ def search ) end + # Authenticates the session and returns the SID value + # @return [String] a session ID def session_id @session_id ||= begin response = authenticate - response.http.headers['Set-Cookie'].sub('SID=', '') + response.body[:authenticate_response][:return] end end + # Calls close_session on the authentication endpoint + # Resets the session_id and the search client + # @return [nil] def session_close + # note that the auth client doesn't need to be reset here auth.call(:close_session) @session_id = nil @search = nil diff --git a/spec/fixtures/wos_client/authenticate.xml b/spec/fixtures/wos_client/authenticate.xml new file mode 100644 index 000000000..4e5455c40 --- /dev/null +++ b/spec/fixtures/wos_client/authenticate.xml @@ -0,0 +1,8 @@ + + + + + 2F669ZtP6fRizIymX8V + + + diff --git a/spec/wos_client_spec.rb b/spec/wos_client_spec.rb new file mode 100644 index 000000000..5d4a4e504 --- /dev/null +++ b/spec/wos_client_spec.rb @@ -0,0 +1,62 @@ +require 'spec_helper' + +# http://savonrb.com/version2/testing.html +# require the helper module +require 'savon/mock/spec_helper' + +describe WosClient do + include Savon::SpecHelper + + # set Savon in and out of mock mode + before(:all) { savon.mock! } + after(:all) { savon.unmock! } + + let(:wos_client) { described_class.new } + let(:auth_xml) { File.read('spec/fixtures/wos_client/authenticate.xml') } + + describe '#new' do + it 'works' do + expect(wos_client).to be_an described_class + end + end + + describe '#auth' do + it 'works' do + result = wos_client.auth + expect(result).to be_an Savon::Client + end + end + + describe '#authenticate' do + it 'authenticates with the service' do + savon.expects(:authenticate).returns(auth_xml) + response = wos_client.authenticate + expect(response).to be_successful + end + end + + describe '#search' do + xit 'works' do + savon.expects(:authenticate).returns(auth_xml) + result = wos_client.search + expect(result).not_to be_nil + end + end + + describe '#session_id' do + it 'works' do + savon.expects(:authenticate).returns(auth_xml) + result = wos_client.session_id + expect(result).to be_an String + expect(result).to eq '2F669ZtP6fRizIymX8V' + end + end + + describe '#session_close' do + it 'works' do + savon.expects(:close_session).returns('') + result = wos_client.session_close + expect(result).to be_nil + end + end +end