Skip to content

Commit

Permalink
Web Of Science Client - WosClient
Browse files Browse the repository at this point in the history
  • Loading branch information
dazza-codes committed Sep 28, 2017
1 parent 4d66afe commit 172b860
Show file tree
Hide file tree
Showing 4 changed files with 148 additions and 0 deletions.
5 changes: 5 additions & 0 deletions config/settings.yml
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,11 @@ SCIENCEWIRE:
TMPDIR: /tmp
ARTICLE_BASE_URI: https://ws.isiknowledge.com/cps/openurl/service?url_ver=Z39.88-2004&rft_id=info:ut/

## Web Of Science Auth Config
WOS:
AUTH_CODE: secret
LOG_LEVEL: info

DOI:
BASE_URI: https://dx.doi.org/

Expand Down
68 changes: 68 additions & 0 deletions lib/wos_client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
require 'savon'

# 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_WSDL = 'http://search.webofknowledge.com/esti/wokmws/ws/WOKMWSAuthenticate?wsdl'.freeze
SEARCH_WSDL = 'http://search.webofknowledge.com/esti/wokmws/ws/WokSearch?wsdl'.freeze

def initialize(auth_code, log_level = :info)
@auth_code = auth_code
@log_level = log_level
end

# A client for the authorization endpoint, using WSDL
# @return [Savon::Client]
def auth
@auth ||= Savon.client(
wsdl: AUTH_WSDL,
headers: { 'Authorization' => "Basic #{@auth_code}", 'SOAPAction' => [''] },
env_namespace: :soapenv,
log: true,
log_level: @log_level,
pretty_print_xml: true
)
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,
headers: { 'Cookie' => "SID=\"#{session_id}\"", 'SOAPAction' => '' },
env_namespace: :soapenv,
log: true,
log_level: @log_level,
pretty_print_xml: true
)
end

# Authenticates the session and returns the SID value
# @return [String] a session ID
def session_id
@session_id ||= begin
response = authenticate
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
auth.globals[:headers]['Cookie'] = "SID=\"#{session_id}\""
auth.call(:close_session)
@auth = nil
@session_id = nil
@search = nil
end

end
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>
67 changes: 67 additions & 0 deletions spec/lib/wos_client_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
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_auth) { 'secret' }
let(:wos_client) { described_class.new(wos_auth) }
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
it 'works' do
savon.expects(:authenticate).returns(auth_xml)
result = wos_client.search
expect(result).to be_an Savon::Client
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
before do
savon.expects(:authenticate).returns(auth_xml)
wos_client.session_id
end
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 172b860

Please sign in to comment.