-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
4d66afe
commit 172b860
Showing
4 changed files
with
148 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |