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.



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

def auth_token
  @auth_token
end

#guestObject

Returns the value of attribute guest.



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

def guest
  @guest
end

#password=(value) ⇒ Object (writeonly)

Sets the attribute password

Parameters:

  • value

    the value to set the attribute password to.



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

def password=(value)
  @password = value
end

#session_tokenObject

Returns the value of attribute session_token.



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

def session_token
  @session_token
end

#userid=(value) ⇒ Object (writeonly)

Sets the attribute userid

Parameters:

  • value

    the value to set the attribute userid to.



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

def userid=(value)
  @userid = value
end

Instance Method Details

#create_sessionObject

Create the session



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

def create_session
  uri = URI "#{API_URL}edsapi/rest/createsession?profile=#{@profile}&guest=#{@guest}"
  req = Net::HTTP::Get.new(uri.request_uri)
  req['x-authenticationToken'] = @auth_token
  req['Accept'] = "application/json"
#     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, :read_timeout => 10) { |http|
      
      begin
    return http.request(req).body
  rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    raise "No response from server"
  end
  }
end

#end_session(session_token) ⇒ Object

End the session



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

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, :read_timeout => 10) { |http|
      begin
    http.request(req)
  rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    raise "No response from server"
  end
  }
  return true
end

#info(format = :xml) ⇒ Object

Info method



152
153
154
155
156
157
158
159
160
161
162
163
164
165
# File 'lib/ebsco-discovery-service-api.rb', line 152

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

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

#ip_authenticate(format = :xml) ⇒ Object



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

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.read_timeout=10
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  begin
    doc = JSON.parse(https.request(req).body)
  rescue Timeout::Error, Net::ReadTimeout, Errno::EINVAL, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    raise "No response from server"
  end
  @auth_token = doc['AuthToken']
end

#ip_init(profile, guest = 'y') ⇒ Object



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

def ip_init(profile, guest = 'y')
  @profile = profile
  @guest = guest
  return self
end

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

Retrieve specific information



126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
# File 'lib/ebsco-discovery-service-api.rb', line 126

def retrieve(dbid, an, highlightterms = "", ebookpreferredformat = "", format = :xml)
  uri = URI "#{API_URL}edsapi/rest/retrieve?dbid=#{dbid}&an=#{an}"
  if highlightterms != ""
    updateURI = uri.to_s
    updateURI = updateURI + "&highlightterms=#{highlightterms}"
    uri = URI updateURI
  end
  if ebookpreferredformat != ""
    updateURI = uri.to_s
    updateURI = updateURI + "&ebookpreferredformat=#{ebookpreferredformat}"
    uri = URI updateURI
  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


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

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

Run a search query, XML results are returned



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

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


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

#uid_authenticate(format = :xml) ⇒ Object



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

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
  https = Net::HTTP.new(uri.hostname, uri.port)
  https.read_timeout=10 
  https.use_ssl = true
  https.verify_mode = OpenSSL::SSL::VERIFY_NONE
  begin
    doc = JSON.parse(https.request(req).body)
  rescue Timeout::Error, Errno::EINVAL, Net::ReadTimeout, Errno::ECONNRESET, EOFError, Net::HTTPBadResponse, Net::HTTPHeaderSyntaxError, Net::ProtocolError => e
    about "No response from server"
  end
  if doc.has_key?('ErrorNumber')
     raise "Bad response from server - error code #{result['ErrorNumber']}"
  else
     @auth_token = doc['AuthToken']
  end      
end

#uid_init(userid, password, profile, guest = 'y') ⇒ Object

Init the object with userid and pass.



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

def uid_init(userid, password, profile, guest = 'y')
  @userid = userid
  @password = password
  @profile = profile
  @guest = guest
  return self
end