wckbapi 0.0.3

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: f19f15da5b7cbb71977b93bb5870013f0d648642
4
+ data.tar.gz: 34f775ae14fca34bed053bf09f245c3eddb24e81
5
+ SHA512:
6
+ metadata.gz: 293512bdbd709cd94462d511db5de885177c43740b56440db0662b1ff7c51fcd92bfd6f3f6390755e879292b9ba252173906d241fd6173e97bfdb4b1b46df7e2
7
+ data.tar.gz: b835b0462aece2a77d9ddd95c3ebbdd8de743fb1f48dfc0a0ddffc99c9f369d0e0992563857ec29e52cbd256a8a4341c22b4bcf2a8b32378aa1880dbb66a5338
@@ -0,0 +1,245 @@
1
+ require 'uri'
2
+ require 'net/http'
3
+ require 'cgi'
4
+
5
+ module WCKBAPI
6
+
7
+ # The WCKBAPI::Client object provides a public facing interface to interacting
8
+ # with the WorldCat Knowledgebank API
9
+ #
10
+ # client = WCKBAPI::Client.new :wskey => [your world cat key], :debug => true|false, :institution_id => [your institution id]
11
+ # options:
12
+ # wskey
13
+ #
14
+ #
15
+ # More information can be found at:
16
+ # http://worldcat.org/devnet/wiki/SearchAPIDetails
17
+
18
+ class Client
19
+
20
+ # The constructor which must be passed a valid base url for an oai
21
+ # service:
22
+ #
23
+ # If you want to see debugging messages on STDERR use:
24
+ # :debug => true
25
+
26
+ def initialize(options={})
27
+ @debug = options[:debug]
28
+ @wskey = options[:wskey]
29
+ @institution_id = options[:institution_id]
30
+ end
31
+
32
+ # Equivalent to a Identify request. You'll get back a OAI::IdentifyResponse
33
+ # object which is essentially just a wrapper around a REXML::Document
34
+ # for the response.
35
+
36
+ #def OpenSearch(opts={})
37
+ # @base = URI.parse WORLDCAT_OPENSEARCH
38
+ # opts["wskey"] = @wskey
39
+ # xml = do_request(opts)
40
+ # return OpenSearchResponse.new(xml)
41
+ #end
42
+
43
+ def GetEntry(opts = {})
44
+ opts[:wskey] = @wskey
45
+ opts[:alt] = 'json'
46
+ if opts[:id] == nil
47
+ @base = URI.parse WORLDCAT_KB_URL + "entries/" + opts[:collection_uid] + "," + opts[:entry_uid]
48
+ opts.delete(:collection_uid)
49
+ opts.delete(:entry_uid)
50
+ else
51
+ @base = URI.parse opts[:id]
52
+ opts.delete(:id)
53
+ end
54
+
55
+ data = do_request(opts)
56
+ json = JSON.parse(data)
57
+ objResult = Result.new()
58
+ objResult.query = ""
59
+ objResult.startIndex = 1
60
+ objResult.totalResults = 1
61
+ objResult.itemsPerPage = 1
62
+ objE = Entry.new()
63
+ objE.load(json)
64
+ return objResult, Array(objE)
65
+ end
66
+
67
+ def SearchEntries(opts={})
68
+ opts[:wskey] = @wskey
69
+ opts[:alt] = 'json'
70
+ @base = URI.parse WORLDCAT_KB_URL + "entries/search"
71
+ data = do_request(opts)
72
+ json = JSON.parse(data)
73
+ objArray = Array.new()
74
+ objResult = Result.new()
75
+ objResult.query = json['os:Query']
76
+ objResult.startIndex = json['os:startIndex']
77
+ objResult.totalResults = json['os:totalResults']
78
+ objResult.itemsPerPage = json['os:itemsPerPage']
79
+ json['entries'].each {|item|
80
+ objE = Entry.new()
81
+ objE.load(item)
82
+ objArray.push(objE)
83
+ }
84
+ return objResult, objArray
85
+ end
86
+
87
+ def SearchProviders(opts={})
88
+ opts[:type] = 'search'
89
+ return GetProviderInfo(opts)
90
+ end
91
+
92
+ def GetProviderInfo(opts={})
93
+ if opts == nil
94
+ opts = Hash.new()
95
+ end
96
+
97
+ opts[:wskey] = @wskey
98
+ opts[:alt] = 'json'
99
+ if opts[:type] == nil
100
+ if opts[:collection_uid] == nil
101
+ @base = URI.parse WORLDCAT_KB_URL + "providers"
102
+ else
103
+ @base = URI.parse WORLDCAT_KB_URL + "providers/" + opts[:title]
104
+ end
105
+ opts.delete(:collection_uid)
106
+ data = do_request(opts)
107
+ json = JSON.parse(data)
108
+ objResult = Result.new()
109
+ if json['entries'] != nil
110
+ objResult.query =json['os:Query']
111
+ objResult.startIndex =json['os:startIndex']
112
+ objResult.totalResults = json['os:totalResults']
113
+ objResult.itemsPerPage = json['os:itemsPerPage']
114
+
115
+ objArray = Array.new()
116
+ json['entries'].each {|item|
117
+ objP = Provider.new()
118
+ objP.load(item)
119
+ objArray.push(objP)
120
+ }
121
+ return objResult, objArray
122
+ else
123
+ objResult.query = ""
124
+ objResult.startIndex = 1
125
+ objResult.totalResults = 1
126
+ objResult.itemsPerPage = 1
127
+
128
+ objP = Provider.new()
129
+ objP.load(json)
130
+ return objResult, Array(objP)
131
+ end
132
+ else
133
+ opts.delete(:type)
134
+ @base = URI.parse WORLDCAT_KB_URL + "providers/search"
135
+ data = do_request(opts)
136
+ json = JSON.parse(data)
137
+ objResult = Result.new()
138
+ objResult.query = json['os:Query']
139
+ objResult.startIndex = json['os:startIndex']
140
+ objResult.totalResults = json['os:totalResults']
141
+ objResult.itemsPerPage = json['os:itemsPerPage']
142
+
143
+ objArray = Array.new()
144
+ json['entries'].each {|item|
145
+ objP = Provider.new()
146
+ objP.load(item)
147
+ objArray.push(objP)
148
+ }
149
+ return objResult, objArray
150
+ end
151
+ end
152
+
153
+ def GetCollectionInfo(opts={})
154
+ opts[:wskey] = @wskey
155
+ opts[:alt] = 'json'
156
+ if opts[:type] == nil
157
+ if opts[:institution_id] != nil
158
+ @base = URI.parse WORLDCAT_KB_URL + "collections/" + opts[:collection_uid] + "," + opts[:institution_id]
159
+ else
160
+ @base = URI.parse WORLDCAT_KB_URL + "collections/" + opts[:collection_uid]
161
+ end
162
+ opts.delete(:institution_id)
163
+ opts.delete(:collection_uid)
164
+ data = do_request(opts)
165
+ json = JSON.parse(data)
166
+ objResult = Result.new()
167
+ objResult.query = ""
168
+ objResult.startIndex = 1
169
+ objResult.totalResults = 1
170
+ objResult.itemsPerPage = 1
171
+
172
+ objC = Collection.new()
173
+ objC.load(json)
174
+ return objResult, Array(objC)
175
+ else
176
+ opts.delete(:type)
177
+ @base = URI.parse WORLDCAT_KB_URL + "collections/search"
178
+ data = do_request(opts)
179
+ json = JSON.parse(data)
180
+ objResult = Result.new()
181
+ objResult.query = json['os:Query']
182
+ objResult.startIndex = json['os:startIndex']
183
+ objResult.totalResults = json['os:totalResults']
184
+ objResult.itemsPerPage = json['os:itemsPerPage']
185
+
186
+ objArray = Array.new()
187
+ json['entries'].each {|item|
188
+ objC = Collection.new()
189
+ objC.load(item)
190
+ objArray.push(objC)
191
+ }
192
+ return objResult, objArray
193
+ end
194
+ end
195
+
196
+ def SearchCollections(opts={})
197
+ opts[:type] = 'search'
198
+ return GetCollectionInfo(opts)
199
+ end
200
+
201
+
202
+ private
203
+
204
+ def do_request(hash)
205
+ uri = @base.clone
206
+
207
+ # build up the query string
208
+ parts = hash.entries.map do |entry|
209
+ #key = studly(entry[0].to_s)
210
+ key = entry[0].to_s
211
+ value = entry[1]
212
+ value = CGI.escape(entry[1].to_s)
213
+ "#{key}=#{value}"
214
+ end
215
+ uri.query = parts.join('&')
216
+ debug("doing request: #{uri.to_s}")
217
+ puts uri.to_s
218
+ begin
219
+ data = Net::HTTP.get(uri)
220
+ debug("got response: #{data}")
221
+ return data
222
+ rescue SystemCallError=> e
223
+ #raise WCKBAPI::Exception, 'HTTP level error during WCKBAPI request: '+e, caller
224
+ end
225
+ end
226
+
227
+ # convert foo_bar to fooBar thus allowing our ruby code to use
228
+ # the typical underscore idiom
229
+ def studly(s)
230
+ s.gsub(/_(\w)/) do |match|
231
+ match.sub! '_', ''
232
+ match.upcase
233
+ end
234
+ end
235
+
236
+ def debug(msg)
237
+ $stderr.print("#{msg}\n") if @debug
238
+ end
239
+
240
+ def to_h(default=nil)
241
+ Hash[ *inject([]) { |a, value| a.push value, default || yield(value) } ]
242
+ end
243
+
244
+ end
245
+ end
@@ -0,0 +1,33 @@
1
+ module WCKBAPI
2
+
3
+ class Collection
4
+ attr_accessor :id, :title, :provider_uid, :provider_name, :avaiable_entries, :selected_entries, :collection_name, :collection_uid, :owner_institution, :source_institution, :status, :dbkey, :source, :open, :collection_type, :uri
5
+
6
+ def initialize()
7
+ end
8
+ def load(item)
9
+ @id = item['id']
10
+ @title = item['title']
11
+ @provider_uid = item['kb:provider_uid']
12
+ @provider_name = item['kb:provider_name']
13
+ @available_entries = item['kb:available_entries']
14
+ @selected_entries = item['kb:selected_entries']
15
+ @collection_name = item['kb:collection_name']
16
+ @collection_uid = item['kb:collection_uid']
17
+ @owner_institution = item['kb:owner_institution']
18
+ @source_institution = item['kb:source_institution']
19
+ @status = item['kb:status']
20
+ @dbkey = item['kb:dbkey']
21
+ @source = item['kb:source']
22
+ @open = item['kb:open']
23
+ @collection_type = item['kb:collection_type']
24
+
25
+ item['links'].each {|t|
26
+ if t['rel'] == 'via'
27
+ @uri = t['href']
28
+ break
29
+ end
30
+ }
31
+ end
32
+ end
33
+ end
@@ -0,0 +1,30 @@
1
+ module WCKBAPI
2
+
3
+ class Entry
4
+ attr_accessor :id, :title, :entry_uid, :entry_status, :bkey, :collection_name, :collection_uid, :provider_uid, :provider_name, :oclcnum, :author, :isbn, :publisher, :coverage, :uri
5
+
6
+ def initialize()
7
+ end
8
+ def load(item)
9
+ @id = item['id']
10
+ @title = item['title']
11
+ @entry_uid = item['entry_uid']
12
+ @entry_status = item['entry_status']
13
+ @bkey = item['bkey']
14
+ @collection_name = item['kb:collection_name']
15
+ @collection_uid = item['kb:collection_uid']
16
+ @provider_uid = item['kb:provider_uid']
17
+ @provider_name = item['kb:provider_name']
18
+ @oclcnum = item['kb:oclcnum']
19
+ @author = item['kb:author']
20
+ @isbn = item['kb:isbn']
21
+ @publisher = item['kb:publisher']
22
+ @coverage = item['kb:coverage']
23
+ item['links'].each {|t|
24
+ if t['rel'] == 'via'
25
+ @uri = t['href']
26
+ end
27
+ }
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,18 @@
1
+ module WCKBAPI
2
+
3
+ class Provider
4
+ attr_accessor :id, :title, :provider_uid, :provider_name, :avaiable_entries, :selected_entries, :available_collections
5
+
6
+ def initialize()
7
+ end
8
+ def load(item)
9
+ @id = item['id']
10
+ @title = item['title']
11
+ @provider_uid = item['kb:provider_uid']
12
+ @provider_name = item['kb:provider_name']
13
+ @available_entries = item['kb:available_entries']
14
+ @selected_entries = item['kb:selected_entries']
15
+ @available_collections = item['kb:available_collections']
16
+ end
17
+ end
18
+ end
@@ -0,0 +1,9 @@
1
+ module WCKBAPI
2
+
3
+ class Result
4
+ attr_accessor :query, :startIndex, :totalResults, :itemsPerPage
5
+
6
+ def initialize()
7
+ end
8
+ end
9
+ end
data/lib/wckbapi.rb ADDED
@@ -0,0 +1,10 @@
1
+ require 'wckbapi/client'
2
+ require 'wckbapi/collection'
3
+ require 'wckbapi/provider'
4
+ require 'wckbapi/entry'
5
+ require 'wckbapi/result'
6
+ require 'nokogiri'
7
+ require 'json'
8
+
9
+ WORLDCAT_OPENSEARCH = 'http://www.worldcat.org/webservices/catalog/search/opensearch'
10
+ WORLDCAT_KB_URL = 'http://www.worldcat.org/webservices/kb/rest/'
data/test.rb ADDED
@@ -0,0 +1,112 @@
1
+ require 'rubygems'
2
+ require 'wckbapi'
3
+
4
+ client = WCKBAPI::Client.new :wskey => 'WUg1y65bNZbtvxa6E8ieROEKf2Hy7fjNDZatpRVViG4cUAyBaPsI18fPSR0SBQk8torAxvR3dxsw5uwe'
5
+
6
+ objR, objc = client.GetCollectionInfo(:collection_uid => 'NPG.journals')
7
+ puts "Collection Info: \n"
8
+ puts "Title: " + objc[0].title
9
+ puts "Collection_uid: " + objc[0].collection_uid
10
+
11
+
12
+ puts "\n"
13
+ puts "******************* Collection Info ******************"
14
+ objR, objc = client.GetCollectionInfo(:collection_uid => 'BMC.openAccess',
15
+ :institution_id => '128807',
16
+ :type => 'search')
17
+
18
+ puts "Collection Info (enum):\n"
19
+ puts "Number of results: " + objR.totalResults.to_s + "\n"
20
+ objc.each {|i|
21
+ puts "Title: " + i.title + "\n"
22
+ puts "Collection_uid: " + i.collection_uid + "\n"
23
+ puts "\n"
24
+ }
25
+
26
+ puts "\n"
27
+ puts "******************* Collection Info Search **************"
28
+ objR, objc = client.GetCollectionInfo(:institution_id => '128807',
29
+ :title => 'S%',
30
+ :type => 'search')
31
+
32
+ puts "Collection Info Search (enum):\n"
33
+ puts "Number of Results: " + objR.totalResults.to_s + "\n"
34
+ objc.each {|i|
35
+ puts "Title: " + i.title + "\n"
36
+ puts "Collection_uid: " + i.collection_uid + "\n"
37
+ puts "\n"
38
+ }
39
+
40
+ puts "\n"
41
+ puts "********************** SearchCollections ***************"
42
+ objR, objc = client.SearchCollections(:institution_id => "128807",
43
+ :title => 'S%')
44
+
45
+ puts "Search Collection (enum):\n"
46
+ puts "Number of Results: " + objR.totalResults.to_s + "\n"
47
+ objc.each {|i|
48
+ puts "Title: " + i.title + "\n"
49
+ puts "Collection_uid: " + i.collection_uid + "\n"
50
+ puts "\n"
51
+ }
52
+
53
+ puts "\n"
54
+ puts "*********************** Retrieve Providers *************"
55
+ objR, objP = client.GetProviderInfo()
56
+ objP.each {|i|
57
+ puts "Title: " + i.title + "\n"
58
+ puts "id: " + i.id + "\n"
59
+ }
60
+
61
+ puts "\n"
62
+ puts "********************** Retrieve Providers **"
63
+ objR, objP = client.GetProviderInfo(:title => 'BioOne')
64
+ objP.each {|i|
65
+ puts "Title: " + i.title + "\n"
66
+ puts "id: " + i.id + "\n"
67
+ }
68
+
69
+ puts "\n"
70
+ puts "********************** Retrieve Providers Search ********"
71
+ objR, objP = client.SearchProviders(:institution_id => 128807)
72
+ puts "Number of Results: " + objR.totalResults.to_s + "\n"
73
+ objP.each {|i|
74
+ puts "Title: " + i.title + "\n"
75
+ puts "id: " + i.id + "\n"
76
+ }
77
+
78
+ puts "\n"
79
+ puts "********************** Return and entry *****************"
80
+ objR, objE = client.GetEntry(:collection_uid => 'ebrary.humanities',
81
+ :entry_uid => '5639208')
82
+
83
+ objE.each {|i|
84
+ puts "Title: " + i.title + "\n"
85
+ puts "id: " + i.id + "\n"
86
+ }
87
+
88
+ puts "\n"
89
+ puts "********************* Return entry with entry id *******"
90
+
91
+ objR, objE = client.GetEntry(:id => 'http://worldcat.org/webservices/kb/rest/entries/ebrary.humanities,5639208')
92
+ objE.each {|i|
93
+ puts "Title: " + i.title + "\n"
94
+ puts "id: " + i.id + "\n"
95
+ }
96
+
97
+ puts "\n"
98
+
99
+ puts "\n"
100
+ puts "********************* Search Entries *******************"
101
+ objR, objE = client.SearchEntries(:institution_id => '128807',
102
+ :title => '"B%"')
103
+
104
+ puts "Number of Results: " + objR.totalResults.to_s + "\n"
105
+ objE.each {|i|
106
+ puts "Title: " + i.title + "\n"
107
+ puts "id: " + i.id + "\n"
108
+ }
109
+
110
+ puts "\n\n"
111
+
112
+ puts "finished with tests"
metadata ADDED
@@ -0,0 +1,53 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: wckbapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.3
5
+ platform: ruby
6
+ authors:
7
+ - Terry Reese
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-02-05 00:00:00.000000000 Z
12
+ dependencies: []
13
+ description: This component provides a ruby component for processing the Knowledge
14
+ Base API provided by OCLC. Users interested in using this component will need to
15
+ have an API key available from OCLC.
16
+ email: reese.2179@osu.edu
17
+ executables: []
18
+ extensions: []
19
+ extra_rdoc_files: []
20
+ files:
21
+ - lib/wckbapi.rb
22
+ - lib/wckbapi/client.rb
23
+ - lib/wckbapi/collection.rb
24
+ - lib/wckbapi/entry.rb
25
+ - lib/wckbapi/provider.rb
26
+ - lib/wckbapi/result.rb
27
+ - test.rb
28
+ homepage:
29
+ licenses:
30
+ - Public Domain
31
+ metadata: {}
32
+ post_install_message:
33
+ rdoc_options: []
34
+ require_paths:
35
+ - lib
36
+ required_ruby_version: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ required_rubygems_version: !ruby/object:Gem::Requirement
42
+ requirements:
43
+ - - ">="
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ requirements: []
47
+ rubyforge_project:
48
+ rubygems_version: 2.2.1
49
+ signing_key:
50
+ specification_version: 4
51
+ summary: Ruby component for processing the WorldCat KB API
52
+ test_files:
53
+ - test.rb