Class: EDSApi::Connection

Inherits:
Object
  • Object
show all
Defined in:
lib/ebsco-discovery-service-api.rb

Overview

Connection object. Does what it says. ConnectionHandler is what is usually desired and wraps auto-reonnect features, etc.

Direct Known Subclasses

ConnectionHandler

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#auth_tokenObject

Returns the value of attribute auth_token.



16
17
18
# File 'lib/ebsco-discovery-service-api.rb', line 16

def auth_token
  @auth_token
end

#debug_notesObject

Returns the value of attribute debug_notes.



16
17
18
# File 'lib/ebsco-discovery-service-api.rb', line 16

def debug_notes
  @debug_notes
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



17
18
19
# File 'lib/ebsco-discovery-service-api.rb', line 17

def password=(value)
  @password = value
end

#session_tokenObject

Returns the value of attribute session_token.



16
17
18
# File 'lib/ebsco-discovery-service-api.rb', line 16

def session_token
  @session_token
end

#userid=(value) ⇒ Object (writeonly)

Sets the attribute userid

Parameters:

  • value

    the value to set the attribute userid to.



17
18
19
# File 'lib/ebsco-discovery-service-api.rb', line 17

def userid=(value)
  @userid = value
end

Instance Method Details

#create_sessionObject

Create the session



72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
# File 'lib/ebsco-discovery-service-api.rb', line 72

def create_session
  uri = URI "#{API_URL}edsapi/rest/createsession?profile=#{@profile}"
  req = Net::HTTP::Get.new(uri.request_uri)
  req['x-authenticationToken'] = @auth_token
  req['Accept'] = "application/json"
  @debug_notes << "<p>CREATE SESSION Call to " << uri.to_s << " with auth token: " << req['x-authenticationToken'].to_s << "</p>";
#     Net::HTTP.start(uri.hostname, uri.port) { |http|

#       doc = JSON.parse(http.request(req).body)

#       return doc['SessionToken']

#     }

  Net::HTTP.start(uri.hostname, uri.port) { |http|
      begin
    return http.request(req).body
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    abort "No response from server"
  end
  }
end

#end_session(session_token) ⇒ Object

End the session



91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/ebsco-discovery-service-api.rb', line 91

def end_session(session_token)
  uri = URI "#{API_URL}edsapi/rest/endsession?sessiontoken=#{CGI::escape(session_token)}"
  req = Net::HTTP::Get.new(uri.request_uri)
  req['x-authenticationToken'] = @auth_token
  Net::HTTP.start(uri.hostname, uri.port) { |http|
      begin
    http.request(req)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    abort "No response from server"
  end
  }
  return true
end

#info(format = :xml) ⇒ Object

Info method



143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/ebsco-discovery-service-api.rb', line 143

def info(format = :xml)
  uri = URI "#{API_URL}edsapi/rest/Info"
  req = Net::HTTP::Get.new(uri.request_uri)
  req['x-authenticationToken'] = @auth_token
  req['x-sessionToken'] = @session_token
  req['Accept'] = 'application/json' #if format == :json

  @debug_notes << "<p>INFO Call to " << uri.to_s << " with auth token: " << req['x-authenticationToken'].to_s << " and session token: " << req['x-sessionToken'].to_s << "</p>";
  Net::HTTP.start(uri.hostname, uri.port) { |http|
      begin
    return http.request(req).body
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    abort "No response from server"
  end
  }
end

#ip_authenticate(format = :xml) ⇒ Object



57
58
59
60
61
62
63
64
65
66
67
68
69
70
# File 'lib/ebsco-discovery-service-api.rb', line 57

def ip_authenticate(format = :xml)
  uri = URI "#{API_URL_S}authservice/rest/ipauth"
  req = Net::Http:Post.new(uri.request_uri)
  req["Accept"] = "application/json" #if format == :json

  https = Net::HTTP.new(uri.hostname, uri.port)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  begin
    doc = JSON.parse(https.request(req).body)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    abort "No response from server"
  end
  @auth_token = doc['AuthToken']
end

#ip_init(profile) ⇒ Object



27
28
29
30
# File 'lib/ebsco-discovery-service-api.rb', line 27

def ip_init(profile)
  @profile = profile
  return self
end

#retrieve(dbid, an, highlightterms = "", format = :xml) ⇒ Object

Retrieve specific information



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/ebsco-discovery-service-api.rb', line 123

def retrieve(dbid, an, highlightterms = "", format = :xml)
  uri = URI "#{API_URL}edsapi/rest/retrieve?dbid=#{dbid}&an=#{an}"
  if highlightterms != ""
    uri = URI "#{API_URL}edsapi/rest/retrieve?dbid=#{dbid}&an=#{an}&highlightterms=#{highlightterms}"
  end
  req = Net::HTTP::Get.new(uri.request_uri)
  req['x-authenticationToken'] = @auth_token
  req['x-sessionToken'] = @session_token
  req['Accept'] = 'application/json' #if format == :json

  @debug_notes << "<p>RETRIEVE Call to " << uri.to_s << " with auth token: " << req['x-authenticationToken'].to_s << " and session token: " << req['x-sessionToken'].to_s << "</p>";

  Net::HTTP.start(uri.hostname, uri.port) { |http|
      begin
    return http.request(req).body
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    abort "No response from server"
  end
  }
end

#search(options, format = :xml) ⇒ Object

Run a search query, XML results are returned



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/ebsco-discovery-service-api.rb', line 105

def search(options, format = :xml)
      uri = URI "#{API_URL}edsapi/rest/Search?#{options}"
      #return uri.request_uri

      req = Net::HTTP::Get.new(uri.request_uri)
      req['x-authenticationToken'] = @auth_token
      req['x-sessionToken'] = @session_token
      req['Accept'] = 'application/json' #if format == :json

      @debug_notes << "<p>SEARCH Call to " << uri.to_s << " with auth token: " << req['x-authenticationToken'].to_s << " and session token: " << req['x-sessionToken'].to_s << "</p>";

      Net::HTTP.start(uri.hostname, uri.port) { |http|
        begin
        return http.request(req).body
      rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
        abort "No response from server"
      end
      }
end

#uid_authenticate(format = :xml) ⇒ Object



34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/ebsco-discovery-service-api.rb', line 34

def uid_authenticate(format = :xml)
  # DO NOT SEND CALL IF YOU HAVE A VALID AUTH TOKEN

  xml = "<UIDAuthRequestMessage xmlns='http://www.ebscohost.com/services/public/AuthService/Response/2012/06/01'><UserId>#{@userid}</UserId><Password>#{@password}</Password></UIDAuthRequestMessage>"
  uri = URI "#{API_URL_S}authservice/rest/uidauth"
  req = Net::HTTP::Post.new(uri.request_uri)
  req["Content-Type"] = "application/xml"
  req["Accept"] = "application/json" #if format == :json

  req.body = xml
  @debug_notes << "<p>UID Authentication Call to " << uri.to_s << ": " << xml << "</p>";
  https = Net::HTTP.new(uri.hostname, uri.port)
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  begin
    doc = JSON.parse(https.request(req).body)
  rescue Timeout::Error, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    about "No response from server"
  end
  if doc.has_key?('ErrorNumber')
     abort "Bad response from server - error code #{result['ErrorNumber']}"
  else
     @auth_token = doc['AuthToken']
  end      
end

#uid_init(userid, password, profile) ⇒ Object

Init the object with userid and pass.



20
21
22
23
24
25
26
# File 'lib/ebsco-discovery-service-api.rb', line 20

def uid_init(userid, password, profile)
  @debug_notes = ""
  @userid = userid
  @password = password
  @profile = profile
  return self
end