Skip to content

Commit

Permalink
[WIP] Web Of Knowledge Client - WosClient specs
Browse files Browse the repository at this point in the history
  • Loading branch information
dazza-codes committed Sep 27, 2017
1 parent f12e8d4 commit adc47da
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 7 deletions.
23 changes: 16 additions & 7 deletions lib/wos_client.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,17 @@
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
AUTH_WSDL = Settings.WOS.AUTH_WSDL
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,
Expand All @@ -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,
Expand All @@ -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
Expand Down
8 changes: 8 additions & 0 deletions spec/fixtures/wos_client/authenticate.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0"?>
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
<soap:Body>
<ns2:authenticateResponse xmlns:ns2="http://auth.cxf.wokmws.thomsonreuters.com">
<return>2F669ZtP6fRizIymX8V</return>
</ns2:authenticateResponse>
</soap:Body>
</soap:Envelope>
62 changes: 62 additions & 0 deletions spec/wos_client_spec.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit adc47da

Please sign in to comment.