libis-services 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (60) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +18 -0
  3. data/Gemfile +4 -0
  4. data/LICENSE.txt +22 -0
  5. data/README.md +36 -0
  6. data/Rakefile +6 -0
  7. data/lib/libis-services.rb +1 -0
  8. data/lib/libis/services.rb +21 -0
  9. data/lib/libis/services/aleph/search.rb +157 -0
  10. data/lib/libis/services/collective_access.rb +1 -0
  11. data/lib/libis/services/collective_access/cataloguing.rb +48 -0
  12. data/lib/libis/services/collective_access/connector.rb +151 -0
  13. data/lib/libis/services/collective_access/item_info.rb +36 -0
  14. data/lib/libis/services/collective_access/search.rb +26 -0
  15. data/lib/libis/services/collective_access/service.rb +80 -0
  16. data/lib/libis/services/digitool/digital_entity_manager.rb +223 -0
  17. data/lib/libis/services/digitool/digitool_connector.rb +46 -0
  18. data/lib/libis/services/digitool/meta_data_manager.rb +193 -0
  19. data/lib/libis/services/extend/http_fetch.rb +63 -0
  20. data/lib/libis/services/generic_search.rb +38 -0
  21. data/lib/libis/services/http_error.rb +21 -0
  22. data/lib/libis/services/oracle_client.rb +47 -0
  23. data/lib/libis/services/primo.rb +2 -0
  24. data/lib/libis/services/primo/limo.rb +33 -0
  25. data/lib/libis/services/primo/search.rb +46 -0
  26. data/lib/libis/services/rest_client.rb +66 -0
  27. data/lib/libis/services/rosetta.rb +1 -0
  28. data/lib/libis/services/rosetta/client.rb +107 -0
  29. data/lib/libis/services/rosetta/collection_handler.rb +45 -0
  30. data/lib/libis/services/rosetta/collection_info.rb +35 -0
  31. data/lib/libis/services/rosetta/deposit_activity.rb +48 -0
  32. data/lib/libis/services/rosetta/deposit_handler.rb +187 -0
  33. data/lib/libis/services/rosetta/ie.rb +19 -0
  34. data/lib/libis/services/rosetta/ie_handler.rb +29 -0
  35. data/lib/libis/services/rosetta/pds_handler.rb +60 -0
  36. data/lib/libis/services/rosetta/producer.rb +71 -0
  37. data/lib/libis/services/rosetta/producer_handler.rb +125 -0
  38. data/lib/libis/services/rosetta/service.rb +399 -0
  39. data/lib/libis/services/rosetta/sip.rb +26 -0
  40. data/lib/libis/services/rosetta/sip_handler.rb +30 -0
  41. data/lib/libis/services/rosetta/user.rb +70 -0
  42. data/lib/libis/services/rosetta/user_manager.rb +28 -0
  43. data/lib/libis/services/scope/search.rb +46 -0
  44. data/lib/libis/services/search_factory.rb +40 -0
  45. data/lib/libis/services/sharepoint/connector.rb +236 -0
  46. data/lib/libis/services/sharepoint/search.rb +19 -0
  47. data/lib/libis/services/soap_client.rb +57 -0
  48. data/lib/libis/services/soap_error.rb +22 -0
  49. data/lib/libis/services/version.rb +5 -0
  50. data/libis-services.gemspec +38 -0
  51. data/spec/primo_limo_spec.rb +394 -0
  52. data/spec/primo_search_spec.rb +39 -0
  53. data/spec/rosetta_collection_spec.rb +206 -0
  54. data/spec/rosetta_deposit_spec.rb +82 -0
  55. data/spec/rosetta_ie_spec.rb +345 -0
  56. data/spec/rosetta_pds_spec.rb +79 -0
  57. data/spec/rosetta_producer_spec.rb +270 -0
  58. data/spec/rosetta_sip_spec.rb +39 -0
  59. data/spec/spec_helper.rb +12 -0
  60. metadata +307 -0
@@ -0,0 +1,63 @@
1
+ # coding: utf-8
2
+
3
+ require 'net/http'
4
+ require 'set'
5
+
6
+ class Net::HTTPResponse
7
+ #noinspection RubyResolve
8
+ attr_accessor :final_uri
9
+ end
10
+
11
+
12
+ module Net
13
+
14
+ begin
15
+ require 'net/https'
16
+ HTTPS_SUPPORTED = true
17
+ rescue LoadError
18
+ end
19
+
20
+ class HTTP
21
+ def self.fetch(uri, args={}.freeze, &before_fetching)
22
+ uri = URI.parse(uri) unless uri.is_a? URI
23
+ proxy_host = args[:proxy_host]
24
+ proxy_port = args[:proxy_port] || 80
25
+ action = args[:action] || :get
26
+ data = args[:data]
27
+ max_redirects = args[:max_redirects] || 10
28
+
29
+ proxy_class = Proxy(proxy_host, proxy_port)
30
+ #noinspection RubyArgCount
31
+ request = proxy_class.new(uri.host, uri.port)
32
+
33
+ request.use_ssl = true if HTTPS_SUPPORTED && uri.scheme.eql?('https')
34
+
35
+ yield request if block_given?
36
+ #debugger
37
+ response = request.send(action, uri.path, data)
38
+
39
+ urls_seen = args[:_url_seen] || Set.new
40
+
41
+ case response
42
+ when Net::HTTPRedirection
43
+ if urls_seen.size < max_redirects && response['Location']
44
+ urls_seen << uri
45
+ new_uri = URI.parse(response['Location'])
46
+ if urls_seen.member? new_uri
47
+ nil
48
+ else
49
+ new_args = args.dup
50
+ new_args[:_urls_seen] = urls_seen
51
+
52
+ response = HTTP.fetch(new_uri, new_args, &before_fetching)
53
+ end
54
+ end
55
+ when Net::HTTPSuccess
56
+ response.final_uri = uri
57
+ else
58
+ response.error!
59
+ end
60
+ return response
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,38 @@
1
+ # coding: utf-8
2
+
3
+ require 'libis/services/extend/http_fetch'
4
+
5
+ module Libis
6
+ module Services
7
+
8
+ module GenericSearch
9
+
10
+ #noinspection RubyResolve
11
+ attr_accessor :host
12
+ #noinspection RubyResolve
13
+ attr_reader :query, :term, :index, :base
14
+ #noinspection RubyResolve
15
+ attr_reader :num_records, :set_number
16
+ #noinspection RubyResolve
17
+ attr_reader :record_pointer, :session_id
18
+
19
+ def query(query, options = {})
20
+ raise RuntimeError, 'to be implemented'
21
+ end
22
+
23
+ def find(term, options = {})
24
+ query(term, options)
25
+ end
26
+
27
+ def each(options = {}, &block)
28
+ raise RuntimeError, 'to be implemented'
29
+ end
30
+
31
+ def next_record(options = {}, &block)
32
+ raise RuntimeError, 'to be implemented'
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+ end
@@ -0,0 +1,21 @@
1
+ # encoding: utf-8
2
+
3
+ module Libis
4
+ module Services
5
+
6
+ class HttpError < Exception
7
+ attr_accessor :code, :header, :body
8
+
9
+ def initialize(error)
10
+ @code = error[:code]
11
+ @header = error[:header]
12
+ @body = error[:body]
13
+ end
14
+
15
+ def message
16
+ code.to_s
17
+ end
18
+ end
19
+
20
+ end
21
+ end
@@ -0,0 +1,47 @@
1
+ module Libis
2
+ module Services
3
+
4
+ class OracleClient
5
+
6
+ def initialize(database, user, password)
7
+ @database = database
8
+ @user = user
9
+ @password = password
10
+ end
11
+
12
+ def call(procedure, parameters = [])
13
+ params = ''
14
+ params = "'" + parameters.join("','") + "'" if parameters and parameters.size > 0
15
+ system "echo \"call #{procedure}(#{params});\" | sqlplus -S #{@user}/#{@password}@#{@database}"
16
+ end
17
+
18
+ def run(script, parameters = [])
19
+ params = ''
20
+ params = "\"" + parameters.join("\" \"") + "\"" if parameters and parameters.size > 0
21
+ process_result `sqlplus -S #{@user}/#{@password}@#{@database} @#{script} #{params}`
22
+ end
23
+
24
+ def execute(sql)
25
+ process_result `echo \"#{sql}\" | sqlplus -S #{@user}/#{@password}@#{@database}`
26
+ end
27
+
28
+ private
29
+
30
+ def process_result(log)
31
+ log.gsub!(/\n\n/, "\n")
32
+ rows_created = 0
33
+ log.match(/^(\d+) rows? created.$/) { |m| rows_created += m[1] }
34
+ rows_deleted = 0
35
+ log.match(/^(\d+) rows? deleted.$/) { |m| rows_deleted += m[1] }
36
+ rows_updated = 0
37
+ log.match(/^(\d+) rows? updated.$/) { |m| rows_updated += m[1] }
38
+ errors = Hash.new(0)
39
+ error_count = 0
40
+ log.match(/\nERROR .*\n([^\n]*)\n/) { |m| errors[m[1]] += 1; error_count += 1 }
41
+ {created: rows_created, updated: rows_updated, deleted: rows_deleted, errors: error_count, error_detail: errors}
42
+ end
43
+
44
+ end
45
+
46
+ end
47
+ end
@@ -0,0 +1,2 @@
1
+ require_relative 'primo/search'
2
+ require_relative 'primo/limo'
@@ -0,0 +1,33 @@
1
+ # coding: utf-8
2
+ require 'libis-tools'
3
+
4
+ require 'libis/services/rest_client'
5
+
6
+ module Libis
7
+ module Services
8
+ module Primo
9
+
10
+ class Limo
11
+ include ::Libis::Services::RestClient
12
+
13
+ def initialize(url = 'http://limo.libis.be')
14
+ configure(url)
15
+ end
16
+
17
+ def get_marc(alma_id)
18
+ get 'primo_library/libweb/jqprimo/helpers/record_helper.jsp', id: "#{alma_id}.xml"
19
+ end
20
+
21
+ def get_pnx(alma_id)
22
+ get 'primo_library/libweb/jqprimo/helpers/record_helper.jsp', id: "#{alma_id}.pnx"
23
+ end
24
+
25
+ protected
26
+
27
+ def result_parser(response)
28
+ Libis::Tools::XmlDocument.parse(response)
29
+ end
30
+ end
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,46 @@
1
+ # coding: utf-8
2
+ require 'json'
3
+
4
+ require 'libis/services/rest_client'
5
+ require 'libis/services/generic_search'
6
+
7
+ module Libis
8
+ module Services
9
+ module Primo
10
+
11
+ class Search
12
+ include ::Libis::Services::RestClient
13
+ include ::Libis::Services::GenericSearch
14
+
15
+ def initialize(url = 'https://services.libis.be')
16
+ configure(url)
17
+ end
18
+
19
+ def query(query, options = {})
20
+ index = options.delete(:index) || 'any'
21
+ get 'search', {query: "#{index}:#{query}"}.merge(options)
22
+ end
23
+
24
+ def find(term, options = {})
25
+ max_count = options.delete(:max_count) || 100
26
+ result = []
27
+ while result.size < max_count
28
+ reply = query(term, options.merge(from: result.size + 1))
29
+ max_count = [max_count, reply[:count]].min
30
+ reply[:data].each do |record|
31
+ next unless result.size < max_count
32
+ result << record[:display]
33
+ end
34
+ end
35
+ result
36
+ end
37
+
38
+ protected
39
+
40
+ def result_parser(response)
41
+ JSON.parse(response, symbolize_names: true)
42
+ end
43
+ end
44
+ end
45
+ end
46
+ end
@@ -0,0 +1,66 @@
1
+ # coding: utf-8
2
+
3
+ require 'rest-client'
4
+ require 'nori'
5
+ require 'json'
6
+ require 'gyoku'
7
+
8
+ module Libis
9
+ module Services
10
+
11
+ module RestClient
12
+ attr_reader :client
13
+
14
+ def configure(url, options = {})
15
+ @client = ::RestClient::Resource.new(url, options)
16
+ end
17
+
18
+ def get(path, params = {}, headers = {}, &block)
19
+ response = client[path].get({params: params}.merge headers, &block)
20
+ parse_result response, &block
21
+ rescue ::RestClient::Exception => e
22
+ return {error_type: e.class.name, error_name: e.message, response: parse_result(e.response, &block)}
23
+ end
24
+
25
+ def post_url(path, params = {}, headers = {}, &block)
26
+ response = client[path].post({params: params}.merge headers, &block)
27
+ parse_result response, &block
28
+ rescue ::RestClient::Exception => e
29
+ return {error_type: e.class.name, error_name: e.message, response: parse_result(e.response, &block)}
30
+ end
31
+
32
+ def post_data(path, payload, headers = {}, &block)
33
+ response = client[path].post(payload, headers, &block)
34
+ parse_result response, &block
35
+ rescue ::RestClient::Exception => e
36
+ return {error_type: e.class.name, error_name: e.message, response: parse_result(e.response, &block)}
37
+ end
38
+
39
+ def put_url(path, params = {}, headers = {}, &block)
40
+ response = client[path].put({params: params}.merge headers, &block)
41
+ parse_result response, &block
42
+ rescue ::RestClient::Exception => e
43
+ return {error_type: e.class.name, error_name: e.message, response: parse_result(e.response, &block)}
44
+ end
45
+
46
+ def put_data(path, payload, headers = {}, &block)
47
+ response = client[path].put(payload, headers, &block)
48
+ parse_result response, &block
49
+ rescue ::RestClient::Exception => e
50
+ return {error_type: e.class.name, error_name: e.message, response: parse_result(e.response, &block)}
51
+ end
52
+
53
+ protected
54
+
55
+ def parse_result(response)
56
+ block_given? ? yield(response) : result_parser(response)
57
+ end
58
+
59
+ def result_parser(response)
60
+ response
61
+ end
62
+
63
+ end
64
+
65
+ end
66
+ end
@@ -0,0 +1 @@
1
+ require_relative 'rosetta/service'
@@ -0,0 +1,107 @@
1
+ # coding: utf-8
2
+
3
+ require 'libis/services/soap_client'
4
+ require 'libis/tools/extend/hash'
5
+ require 'libis/tools/config'
6
+ require 'libis/tools/logger'
7
+
8
+ require 'awesome_print'
9
+
10
+ module Libis
11
+ module Services
12
+ module Rosetta
13
+
14
+ class Client
15
+ include ::Libis::Services::SoapClient
16
+ include ::Libis::Tools::Logger
17
+
18
+ def initialize(section, service, options = {})
19
+ opts = {strip_namespaces: true, logger: ::Libis::Tools::Config.logger}.merge options
20
+ base_url = opts.delete(:url) || 'http://depot.lias.be'
21
+ configure "#{base_url}/dpsws/#{section}/#{service}?wsdl", opts
22
+ end
23
+
24
+ def pds_handle=(handle)
25
+ @pds_handle = handle
26
+ end
27
+
28
+ def get_heart_bit
29
+ request :get_heart_bit
30
+ end
31
+
32
+ protected
33
+
34
+ def call_raw(operation, args = {})
35
+ data = request operation.to_s.to_sym, args
36
+
37
+ # remove wrapper
38
+ data = data["#{operation}_response".to_sym]
39
+ data.delete(:'@xmlns:ns1')
40
+
41
+ # drill down the returned Hash
42
+ data = data.first.last while data.is_a?(Hash) && 1 == data.size
43
+
44
+ data
45
+ end
46
+
47
+ def call(operation, args = {})
48
+ # upstream call
49
+ data = call_raw(operation, args)
50
+
51
+ # try to parse as XML
52
+ if data.is_a?(String)
53
+ xml_data = Libis::Tools::XmlDocument.parse(data).to_hash(
54
+ empty_tag_value: nil,
55
+ delete_namespace_attributes: true,
56
+ strip_namespaces: true,
57
+ convert_tags_to: lambda { |tag| tag.to_sym }
58
+ )
59
+ data = xml_data unless xml_data.empty?
60
+ end
61
+
62
+ return data unless data.is_a?(Hash)
63
+
64
+ # drill down
65
+ data = data.first.last if 1 == data.size
66
+
67
+ return data unless data.is_a?(Hash)
68
+
69
+ # check for errors
70
+ if data.delete :is_error
71
+ error data.delete :error_description
72
+ return nil
73
+ end
74
+
75
+ # only delete if there is other info. ProducerService isUserExists uses this field as return value.
76
+ data.delete :error_description if data.size > 1
77
+
78
+ # continue drilling down the Hash
79
+ data = data.first.last while data.is_a?(Hash) && 1 == data.size
80
+
81
+ # return
82
+ data
83
+ end
84
+
85
+ def request_object(method, klass, args = {})
86
+ data = call method, args
87
+ return nil unless data.is_a?(Hash)
88
+ klass.new(data)
89
+ end
90
+
91
+ def request_array(method, args = {})
92
+ data = call method, args
93
+ data = data.split(/[\s,]+/) if data.is_a?(String)
94
+ data = [data] if data.is_a?(Hash)
95
+ data.is_a?(Array) ? data : []
96
+ end
97
+
98
+ def reqeust_object_array(method, klass, args = {})
99
+ data = request_array(method, args)
100
+ data.map { |x| klass.new(x) }
101
+ end
102
+
103
+ end
104
+
105
+ end
106
+ end
107
+ end
@@ -0,0 +1,45 @@
1
+ # encoding: utf-8
2
+ require 'virtus'
3
+
4
+ require 'libis/tools/extend/hash'
5
+ require_relative 'collection_info'
6
+ require_relative 'client'
7
+
8
+ module Libis
9
+ module Services
10
+ module Rosetta
11
+
12
+ class CollectionHandler < Libis::Services::Rosetta::Client
13
+
14
+
15
+ def initialize(base_url = 'http://depot.lias.be', options = {})
16
+ super 'repository', 'CollectionWebServices', {url: base_url}.merge(options)
17
+ end
18
+
19
+ def get(id)
20
+ request_object :get_collection_by_id, Rosetta::CollectionInfo, pds_handle: @pds_handle, collection_id: id
21
+ end
22
+
23
+ def find(path)
24
+ request_object :get_collection_by_name, Rosetta::CollectionInfo, pds_handle: @pds_handle, collection_path: path
25
+ end
26
+
27
+ def create(collection_info)
28
+ collection_info = collection_info.attributes if collection_info.is_a? CollectionInfo
29
+ call :create_collection, pds_handle: @pds_handle, collection: collection_info
30
+ end
31
+
32
+ def delete(id)
33
+ call :delete_collection, pds_handle: @pds_handle, collection_id: id
34
+ end
35
+
36
+ def update(collection_info)
37
+ collection_info = collection_info.attributes if collection_info.is_a? CollectionInfo
38
+ call :update_collection, pds_handle: @pds_handle, collection: collection_info
39
+ end
40
+
41
+ end
42
+
43
+ end
44
+ end
45
+ end