classifoclc 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: ac9cfb0520534dc348802194962647569fa8f18e
4
+ data.tar.gz: d2156f7446f3946d95070269fe49353cc4968366
5
+ SHA512:
6
+ metadata.gz: 751f946d8157c1c403e078fa4cb67b7e2e783713b6c05bd0c8cc4ad31fe4d8adee9d36df361066b7493d70e4a335aa2b8cf76b2a286da17f019f75cddf5d9cc9
7
+ data.tar.gz: d6563c241c508cbd1a8c5e359cd41b38a011735622db4235d6b70f4ce901a1bdf4104742cec2583801618dedc93802276b830472cc0c67dba1924c10fab42f2e
@@ -0,0 +1,16 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ Gemfile.lock
14
+ *.gem
15
+ # Emacs files
16
+ *~
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
@@ -0,0 +1,10 @@
1
+ sudo: false
2
+ language: ruby
3
+ rvm:
4
+ - 2.5
5
+ - 2.4
6
+ - 2.4
7
+ - 2.3
8
+ - 2.2
9
+ - 2.1
10
+ before_install: gem install bundler -v 1.16.1
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at github-smr@sneakemail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,12 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in classifoclc.gemspec
6
+ gemspec
7
+
8
+ # Added at 2018-04-08 10:18:30 -0400 by sean:
9
+ gem "webmock", "~> 3.3", :group => [:development]
10
+
11
+ # Added at 2018-04-08 11:15:19 -0400 by sean:
12
+ gem "nokogiri", "~> 1.8"
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2018 Sean Redmond
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,184 @@
1
+ # Classifoclc
2
+
3
+ Interface to OCLC's Classify service, "a FRBR-based prototype designed to
4
+ support the assignment of classification numbers and subject headings for books,
5
+ DVDs, CDs, and other types of materials." See the [OCLC
6
+ documentation](https://www.oclc.org/developer/develop/web-services/classify.en.html). Classifoclc
7
+ is aimed mainly at getting works from various other identifiers.
8
+
9
+ ## Installation
10
+
11
+ Add this line to your application's Gemfile:
12
+
13
+ ```ruby
14
+ gem 'classifoclc'
15
+ ```
16
+
17
+ And then execute:
18
+
19
+ $ bundle
20
+
21
+ Or install it yourself as:
22
+
23
+ $ gem install classifoclc
24
+
25
+ ## Usage
26
+
27
+ ### Look ups
28
+
29
+ The easiest way to use `Classifoclc` is to lookup works by several standard identifiers:
30
+
31
+ * `Classifoclc::isbn`
32
+ * `Classifoclc::lccn`
33
+ * `Classifoclc::oclc`
34
+ * `Classifoclc::owi`
35
+
36
+ Each of these returns an `Enumerator` that can be looped over to get the
37
+ individual `Classifoclc::Work` objects. A "work" is an abstraction, and each
38
+ work has one or more editions. An "edition" is a physical (or digital) item that
39
+ can be held by a library. From the `Classifoclc::Work` object you can get
40
+ several pieces of information, most usefully, perhaps, the number of editions
41
+ (`#edition_count`), the number of libraries that hold the work (`#holdings`) and
42
+ and that hold the work digitally (`#eholdings`), and the editions themselves (`#editions`)
43
+
44
+ > works = Classifoclc::isbn("0151592659")
45
+ > wrk = works.first
46
+ > wrk.title
47
+ => "Meridian"
48
+ > wrk.owi
49
+ => "201096"
50
+ > wrk.format
51
+ => "Book"
52
+ > wrk.itemtype
53
+ => "itemtype-book"
54
+ > wrk.edition_count
55
+ => 114
56
+ > wrk.holdings
57
+ => 3264
58
+ > wrk.eholdings
59
+ => 196
60
+ > wrk.editions # Returns an Enumerator
61
+ > wrk.authors # Returns an Array
62
+ > wrk.recommendations # Returns a Recommendations object
63
+
64
+
65
+ A work has an array authors which seems to include all the authors of all the
66
+ editions of a work, including their VIAF and LC identifiers:
67
+
68
+ > work = Classifoclc::isbn("0151592659").first
69
+ > auth = work.authors.first
70
+ > auth.name
71
+ => "Walker, Alice, 1944-"
72
+ > auth.viaf
73
+ => "108495772"
74
+ > auth.lc
75
+ => "n79109131"
76
+
77
+ You can also lookup by author(`Classifoclc::author`), title
78
+ (`Classifoclc::title`) and FAST identifier (`Classifoclc::fast`) but these can
79
+ return a _very large_ number of results that take a long time and thousands of
80
+ requests to return.
81
+
82
+ Finally, you can search by author and title:
83
+
84
+ > works = Classifoclc::authorAndTitle("Walker, Alice", "Meridian")
85
+ > works.count
86
+ => 5
87
+ > works.first.owi
88
+ => "201096"
89
+ > works.first.edition_count
90
+ => 114
91
+
92
+ ### Sort order
93
+
94
+ You can change how the results are sorted by passing an `:orderby` and/or
95
+ `:order` parameter to the lookup. `:orderby` can take one of the following
96
+ values:
97
+
98
+ |Contant|Meaning|
99
+ |-------|-------|
100
+ |`Classifoclc::OrderBy::EDITIONS`|Number of editions the work has (default)|
101
+ |`Classifoclc::OrderBy::HOLDINGS`|Number of libraries that hold the work|
102
+ |`Classifoclc::OrderBy::FIRSTYEAR`|Date of first edition|
103
+ |`Classifoclc::OrderBy::LASTYEAR`|Date of latest edition|
104
+ |`Classifoclc::OrderBy::LANGUAGE`|Language|
105
+ |`Classifoclc::OrderBy::HEADING`|FAST subject heading|
106
+ |`Classifoclc::OrderBy::WORKS`|Number of works with this FAST subject heading|
107
+ |`Classifoclc::OrderBy::SUBJTYPE`|FAST subject type|
108
+
109
+ `:order` can be either `Classifoclc::Order::ASC` (default) or `Classifoclc::Order::DESC`.
110
+
111
+ > works = Classifoclc::isbn("0151592659", :orderby => Classifoclc::OrderBy::HOLDINGS, :order => Classifoclc::Order::DESC)
112
+
113
+ ### Editions
114
+
115
+ Use the `#editions` method to get editions of a work. The method returns an `Enumerator`
116
+
117
+ > work = Classifoclc::isbn("0151592659").first
118
+ > work.edition_count
119
+ => 114
120
+ > work.editions.each do |ed|
121
+ # 114 loops
122
+ > end
123
+ > ed = work.editions.first
124
+ > ed.oclc
125
+ => "2005960"
126
+ > ed.title
127
+ => "Meridian"
128
+ > ed.format
129
+ => "Book"
130
+ > ed.itemtype
131
+ => "itemtype-book"
132
+ > ed.holdings
133
+ => 1420
134
+ > ed.eholdings
135
+ => 0
136
+ > ed.language
137
+ => "eng"
138
+ > ed.authors
139
+ => "Walker, Alice, 1944-"
140
+ > ed.classifications
141
+ => [{:edition=>"0", :ind1=>"0", :ind2=>"0", :sf2=>"00", :sfa=>"813.54", :tag=>"082"}, {:ind1=>"0", :ind2=>"0", :sfa=>"PS3573.A425", :tag=>"050"}, {:ind1=>"0", :ind2=>"0", :sfa=>"PZ4.W176", :tag=>"050"}]
142
+
143
+ Note that `Edition#authors` just returns a string, _not_ an Array of `Author` objects like `Work#authors`
144
+
145
+ For the meaning of the classifications, refer to the [OCLC documentation](https://www.oclc.org/developer/develop/web-services/classify/classification.en.html).
146
+
147
+ ### Recommendations
148
+
149
+ Each `Work` has a set of recommended classifications available through the `#recommendations` method. This returns a `Recommendations` object
150
+
151
+ > work = Classifoclc::isbn("0151592659").first
152
+ > recs = work.recommendations
153
+
154
+ The recommendations include an array of FAST subject headings
155
+
156
+ > recs.fast.first
157
+ => {:heading=>"Southern States", :holdings=>"2945", :ident=>"1244550"}
158
+
159
+ LCC and Dewey Decimal recommendations are each hashes with the possible keys `:mostPopular`, `:mostRecent`, and `:latestEdition`. For each key the value is an array of recommended classifiers:
160
+
161
+ > recs.lcc[:mostPopular].first
162
+ => {:holdings=>"3221", :nsfa=>"PS3573.A425", :sfa=>"PS3573.A425"}
163
+ recs.ddc[:mostPopular].first
164
+ => {:holdings=>"2632", :nsfa=>"813.54", :sfa=>"813.54"}
165
+
166
+ For the meaning of the classifications, refer to the [OCLC documentation](https://www.oclc.org/developer/develop/web-services/classify/classification.en.html).
167
+
168
+ ## Development
169
+
170
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
171
+
172
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
173
+
174
+ ## Contributing
175
+
176
+ Bug reports and pull requests are welcome on GitHub at https://github.com/seanredmond/classifoclc. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
177
+
178
+ ## License
179
+
180
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
181
+
182
+ ## Code of Conduct
183
+
184
+ Everyone interacting in the Classifoclc project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/seanredmond/classifoclc/blob/master/CODE_OF_CONDUCT.md).
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "classifoclc"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "classifoclc/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "classifoclc"
8
+ spec.version = Classifoclc::VERSION
9
+ spec.authors = ["Sean Redmond"]
10
+ spec.email = ["github-smr@sneakemail.com"]
11
+
12
+ spec.summary = %q{Interface to OCLC Classify service}
13
+ spec.homepage = "https://github.com/seanredmond/classifoclc"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
17
+ f.match(%r{^(test|spec|features)/})
18
+ end
19
+ spec.bindir = "exe"
20
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.16"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.0"
26
+ end
@@ -0,0 +1,201 @@
1
+ require "classifoclc/author.rb"
2
+ require "classifoclc/constants.rb"
3
+ require "classifoclc/edition.rb"
4
+ require "classifoclc/errors.rb"
5
+ require "classifoclc/recommendations"
6
+ require "classifoclc/version"
7
+ require "classifoclc/work"
8
+ require "nokogiri"
9
+ require "open-uri"
10
+ require "uri"
11
+
12
+ # Interface to OCLC's Classify service, "a FRBR-based prototype designed to
13
+ # support the assignment of classification numbers and subject headings for
14
+ # books, DVDs, CDs, and other types of materials."
15
+ #
16
+ # @see https://www.oclc.org/developer/develop/web-services/classify.en.html
17
+ # OCLC Documentation
18
+ module Classifoclc
19
+ # Number of records returned per request
20
+ @@maxRecs = 25
21
+
22
+ # @overload maxRecs
23
+ # Get the max records per request
24
+ # @return [Numeric]
25
+ # @overload maxRecs=(value)
26
+ # Set the max records
27
+ # @param value [Numeric] the new value
28
+ # @return [Numeric] the new value
29
+ def self.maxRecs
30
+ @@maxRecs
31
+ end
32
+
33
+ def self.maxRecs= m
34
+ @@maxRecs = m
35
+ end
36
+
37
+ # Find works by ISBN number
38
+ # @param [String] isbn ISBN number to search for
39
+ # @param [Hash] hsh more options
40
+ # @return [Enumerator<Classifoclc::Work>]
41
+ def self.isbn(isbn, hsh = {})
42
+ lookup(default_options(hsh, {:identifier => Id::ISBN, :value => isbn}))
43
+ end
44
+
45
+ # Find works by OCLC work ID
46
+ # @param [String] owi Work ID to search for
47
+ # @param [Hash] hsh more options
48
+ # @return [Enumerator<Classifoclc::Work>]
49
+ def self.owi(owi, hsh = {})
50
+ lookup(default_options(hsh, {:identifier => Id::OWI, :value => owi, :summary => false}))
51
+ end
52
+
53
+ # Find works by OCLC number
54
+ # @param [String] oclc OCLC number to search for
55
+ # @param [Hash] hsh more options
56
+ # @return [Enumerator<Classifoclc::Work>]
57
+ def self.oclc(oclc, hsh = {})
58
+ lookup(default_options(hsh, {:identifier => Id::OCLC, :value => oclc}))
59
+ end
60
+
61
+ # Find works by Library of Congress Control Number
62
+ # @param [String] lccn LCCN to search for
63
+ # @param [Hash] hsh more options
64
+ # @return [Enumerator<Classifoclc::Work>]
65
+ def self.lccn(lccn, hsh = {})
66
+ lookup(default_options(hsh, {:identifier => Id::LCCN, :value => lccn}))
67
+ end
68
+
69
+ # Find works by author
70
+ # @param [String] author Author to search for
71
+ # @param [Hash] hsh more options
72
+ # @return [Enumerator<Classifoclc::Work>]
73
+ def self.author(auth, hsh = {})
74
+ lookup(default_options(hsh, {:identifier => Id::AUTHOR, :value => auth}))
75
+ end
76
+
77
+ # Find works by title
78
+ # @param [String] title Titleto search for
79
+ # @param [Hash] hsh more options
80
+ # @return [Enumerator<Classifoclc::Work>]
81
+ def self.title(title, hsh = {})
82
+ lookup(default_options(hsh, {:identifier => Id::TITLE, :value => title}))
83
+ end
84
+
85
+ # Find works by author and title
86
+ # @param [String] author Author to search for
87
+ # @param [String] title Title to search for
88
+ # @param [Hash] hsh more options
89
+ # @return [Enumerator<Classifoclc::Work>]
90
+ def self.authorAndTitle(author, title, hsh = {})
91
+ lookup(default_options(hsh, {:identifier => [Id::AUTHOR, Id::TITLE], :value => [author, title], :summary => false}))
92
+ end
93
+
94
+ # Find works by FAST subject heading
95
+ # @param [String] fast FAST subject ID to search for
96
+ # @param [String] title Title to search for
97
+ # @param [Hash] hsh more options
98
+ # @return [Enumerator<Classifoclc::Work>]
99
+ def self.fast(ident, hsh = {})
100
+ lookup(default_options(hsh, {:identifier => Id::IDENT, :value => ident}))
101
+ end
102
+
103
+ private_class_method def self.lookup(hsh)
104
+ parsed = fetch_data(hsh)
105
+ resp_code = parsed.css('response').first['code']
106
+
107
+ if resp_code == '0'
108
+ owid = parsed.css('work').first['owi']
109
+ return owi(owid)
110
+ end
111
+
112
+ if resp_code == '2'
113
+ return Enumerator.new do |w|
114
+ w << Work::new(parsed)
115
+ end
116
+ end
117
+
118
+ if resp_code == "4"
119
+ if hsh[:identifier] == :owi
120
+ if parsed.css('work').map{|w| w['owi']}.include?(hsh[:value])
121
+ raise Classifoclc::InfiniteLoopError.new("The record for owi %s contains records also with the owi %s. Cannot fetch data as it would lead to an infinite loop" % [hsh[:value], hsh[:value]])
122
+ end
123
+ end
124
+ return multiwork(parsed, hsh)
125
+ end
126
+
127
+ if resp_code == '100'
128
+ raise BadIdentifierError.
129
+ new "%s is not allowed as an identifer" % hsh[:identifier]
130
+ end
131
+
132
+ if resp_code == '101'
133
+ raise BadIdentifierFormatError.
134
+ new "Invalid format (%s) for %s identifier" %
135
+ [hsh[:value], hsh[:identifier]]
136
+ end
137
+
138
+ if resp_code =='102'
139
+ return Enumerator.new do |w|
140
+ end
141
+ end
142
+
143
+ if resp_code == '200'
144
+ raise UnexpectedError.new "Unexpected error"
145
+ end
146
+
147
+ raise UnexpectedError.new "Unexpected response code %s" % resp_code
148
+ end
149
+
150
+ private_class_method def self.multiwork(parsed, hsh)
151
+ return Enumerator.new do |work|
152
+ loop do
153
+ if parsed.css("navigation next").empty?
154
+ next_page = nil
155
+ else
156
+ next_page = parsed.css("navigation next").first.text
157
+ end
158
+
159
+ parsed.css('work').each do |multi|
160
+ owi(multi['owi']).each do |w|
161
+ work << w
162
+ end
163
+ end
164
+ break if next_page.nil?
165
+ parsed = fetch_data(hsh.clone.merge({:startRec => next_page}))
166
+ end
167
+ end
168
+ end
169
+
170
+ private_class_method def self.default_options(hsh1, hsh2 = {})
171
+ {:orderby => OrderBy::HOLDINGS,
172
+ :order => Order::DESC,
173
+ :maxRecs => maxRecs,
174
+ :summary => true}.merge(hsh1).merge(hsh2)
175
+ end
176
+
177
+ private_class_method def self.api_params(hsh)
178
+ params = {:orderBy => "%s %s" % [hsh.delete(:orderby), hsh.delete(:order)]}.merge(hsh)
179
+ return params
180
+ end
181
+
182
+ private_class_method def self.param_string(hsh)
183
+ id = hsh.delete(:identifier)
184
+ val = hsh.delete(:value)
185
+
186
+ if id.is_a? Array
187
+ params = default_options(Hash[id.zip(val)], hsh)
188
+ else
189
+ params = default_options({id => val}, hsh)
190
+ end
191
+
192
+ return api_params(params)
193
+ .map{|k,v| "#{k}=#{URI.encode_www_form_component(v)}"}.join("&")
194
+ end
195
+
196
+ def self.fetch_data(hsh)
197
+ resp = open(URL % param_string(hsh.clone), "User-Agent" => "Classifoclc/#{Classifoclc::VERSION}",).read
198
+ parsed = Nokogiri::XML(resp)
199
+ return parsed
200
+ end
201
+ end
@@ -0,0 +1,21 @@
1
+ module Classifoclc
2
+ # Author of a Classifoclc::Work
3
+ class Author
4
+ # The author's name
5
+ # @return [String]
6
+ attr_reader :name
7
+
8
+ # The author's Library of Congress id number
9
+ # @return [String]
10
+ attr_reader :lc
11
+
12
+ # The author's VIAF number
13
+ # @return [String]
14
+ attr_reader :viaf
15
+ def initialize(name, lc, viaf)
16
+ @name = name
17
+ @lc = lc
18
+ @viaf = viaf
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ module Classifoclc
2
+
3
+ URL = "http://classify.oclc.org/classify2/Classify?%s"
4
+
5
+ # See http://classify.oclc.org/classify2/api_docs/classify.html
6
+
7
+ # API also allows stdnbr, ident, heading, author, and title but without
8
+ # pagination, the number of results may be too unwieldy
9
+ module Id
10
+ OCLC = :oclc
11
+ ISBN = :isbn
12
+ LCCN = :lccn
13
+ ISSN = :issn
14
+ UPC = :upc
15
+ OWI = :owi
16
+ AUTHOR = :author
17
+ TITLE = :title
18
+ IDENT = :ident
19
+ end
20
+
21
+ module OrderBy
22
+ # number of editions
23
+ EDITIONS = :mancount
24
+ # holdings
25
+ HOLDINGS = :hold
26
+ # date of first edition
27
+ FIRSTYEAR = :lyr
28
+ # date of latest edition
29
+ LASTYEAR = :hyr
30
+ # language
31
+ LANGUAGE = :ln
32
+ # FAST subject heading
33
+ HEADING = :sheading
34
+ # number of works with this FAST subject heading
35
+ WORKS = :works
36
+ # FAST subject type
37
+ SUBJTYPE = :type
38
+ end
39
+
40
+ module Order
41
+ ASC = :asc
42
+ DESC = :desc
43
+ end
44
+ end
@@ -0,0 +1,69 @@
1
+ module Classifoclc
2
+ # An expression of a work. The physical books libraries held (or
3
+ # their digitizations) are "manifestations" of these editions
4
+ class Edition
5
+ attr_reader :classifications
6
+
7
+ def initialize(node)
8
+ @edition = node
9
+ @classifications = load_classifications(node)
10
+ end
11
+
12
+ # The OCLC number of the edition
13
+ # @return [String]
14
+ def oclc
15
+ @edition['oclc']
16
+ end
17
+
18
+ # The author of the edition
19
+ # @return [String]
20
+ def authors
21
+ @edition['author']
22
+ end
23
+
24
+ # The title of the edition
25
+ # @return [String]
26
+ def title
27
+ @edition['title']
28
+ end
29
+
30
+ # The format of the edition
31
+ # @return [String]
32
+ def format
33
+ @edition['format']
34
+ end
35
+
36
+ # The type of item the edition
37
+ # @return [String]
38
+ def itemtype
39
+ @edition['itemtype']
40
+ end
41
+
42
+ # The number of libraries that hold a copy of this work
43
+ # @return [Integer]
44
+ def holdings
45
+ @edition['holdings'].to_i
46
+ end
47
+
48
+ # The number of libraries that hold a digital copy of this work
49
+ # @return [Integer]
50
+ def eholdings
51
+ @edition['eholdings'].to_i
52
+ end
53
+
54
+ # The language of item the edition
55
+ # @return [String]
56
+ def language
57
+ @edition['language']
58
+ end
59
+
60
+ def load_classifications(node)
61
+ cls = node.css("classifications class")
62
+ return nil if cls.empty?
63
+ cls.map{|c| Hash[c.keys.map{|k| [k.to_sym, c[k]]}]}
64
+ end
65
+
66
+ private :load_classifications
67
+ end
68
+ end
69
+
@@ -0,0 +1,9 @@
1
+ module Classifoclc
2
+ class BadIdentifierError < StandardError; end
3
+
4
+ class BadIdentifierFormatError < StandardError; end
5
+
6
+ class InfiniteLoopError < StandardError; end
7
+
8
+ class UnexpectedError < StandardError; end
9
+ end
@@ -0,0 +1,81 @@
1
+ module Classifoclc
2
+ class Recommendations
3
+ # A hash of links to Google Charts graphs showing the proportion
4
+ # of libraries using which identifiers
5
+ # @return [Hash]
6
+ attr_accessor :graphs
7
+
8
+ # Recommended FAST subject headings
9
+ # @return [Hash]
10
+ attr_accessor :fast
11
+
12
+ # Recommended Dewey Decimal classifications
13
+ # @return [Hash]
14
+ attr_accessor :ddc
15
+
16
+ # Recommended Library of Congress classifications
17
+ # @return [Hash]
18
+ attr_accessor :lcc
19
+
20
+ def initialize(node)
21
+ @graphs = load_graphs(node)
22
+ @fast = load_fast(node)
23
+ @ddc = load_classifications(node, 'ddc')
24
+ @lcc = load_classifications(node, 'lcc')
25
+ end
26
+
27
+ def load_graphs(node)
28
+ graphs = {}
29
+ node.css("graph").each do |g|
30
+ if g.parent.name == "recommendations"
31
+ graphs[:all] = g.text
32
+ else
33
+ graphs[g.parent.name.to_sym] = g.text
34
+ end
35
+ end
36
+
37
+ return graphs
38
+ end
39
+
40
+ def load_fast(node)
41
+ node.css("fast heading").map{|h| { :heading => h.text,
42
+ :holdings => h['heldby'],
43
+ :ident => h['ident']}
44
+ }
45
+ end
46
+
47
+ def load_classifications(node, e)
48
+ return nil if node.css(e).empty?
49
+ Hash[
50
+ # Get to desired element
51
+ node.css(e).
52
+ # Pick out all it's children that are elements but aren't <graph>
53
+ map{|d| d.children.
54
+ select{|c| c.element? and c.name != "graph"}}.
55
+ # It will be an Array containing one Array, so we just need to first
56
+ # element (i.e. the inner Array)
57
+ first.
58
+
59
+ # There can be multiples of the same element, so group the Array by
60
+ # element
61
+ group_by{|c| c.name}.
62
+
63
+ # Now we have a hash. Each key is the name of one of the child
64
+ # elements of the element started with (ddc or lcc). The values are
65
+ # hashes of all the elements with that name.
66
+ map{|k, v|
67
+ [
68
+ # Convert the names of the elements to symbols
69
+ k.to_sym,
70
+ # convert each element to a hash of its attributes where the
71
+ # keys are the attribute names converted to symbols, and the
72
+ # values the values of the attributes
73
+ v.map{|a| Hash[a.keys.map{|k| [k.to_sym, a[k]]}]
74
+ }
75
+ ]
76
+ }]
77
+ end
78
+
79
+ private :load_graphs, :load_fast, :load_classifications
80
+ end
81
+ end
@@ -0,0 +1,3 @@
1
+ module Classifoclc
2
+ VERSION = "1.0.0"
3
+ end
@@ -0,0 +1,118 @@
1
+ module Classifoclc
2
+ # An abstract work. A work is not a physical book, but the
3
+ # conceptual work of which all the physical books are manifestions
4
+ class Work
5
+ attr_reader :authors
6
+ def initialize(node)
7
+ @node = node
8
+ @work = node.css('work').first
9
+ @authors = node.css('author').
10
+ map{|a| Classifoclc::Author.new(a.text, a['lc'], a['viaf'])}
11
+ @recommendations = load_recommendations(node)
12
+ end
13
+
14
+ # Get the work ID
15
+ # @return [String]
16
+ def owi
17
+ @work['owi']
18
+ end
19
+
20
+ # Get the title
21
+ # @return [String]
22
+ def title
23
+ @work['title']
24
+ end
25
+
26
+ # Get the format
27
+ # @return [String]
28
+ def format
29
+ @work['format']
30
+ end
31
+
32
+ # Get the type of item
33
+ # @return [String]
34
+ def itemtype
35
+ @work['itemtype']
36
+ end
37
+
38
+ # Get the number of editions the work has
39
+ # @return [Integer]
40
+ def edition_count
41
+ @work['editions'].to_i
42
+ end
43
+
44
+ # Get the number of libraries that hold a copy of this work
45
+ # @return [Integer]
46
+ def holdings
47
+ @work['holdings'].to_i
48
+ end
49
+
50
+ # Get the number of libraries that hold a digital copy of this work
51
+ # @return [Integer]
52
+ def eholdings
53
+ @work['eholdings'].to_i
54
+ end
55
+
56
+ # Get the editions of this work
57
+ # @return [Enumerator<Classifoclc::Edition>]
58
+ def editions
59
+ Enumerator.new do |e|
60
+ pages.each do |pg|
61
+ pg.each do |edition|
62
+ e << edition
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ # Get the recommended classifications for this work
69
+ # @return [Array<Classifoclc::Recommendations>]
70
+ def recommendations
71
+ @recommendations
72
+ end
73
+
74
+ def full(hsh = {})
75
+ params = {:identifier => 'owi', :value => owi,
76
+ :summary => false}.merge(hsh)
77
+ data = Classifoclc::fetch_data(params)
78
+ editions = data.css('edition').map{|e| Edition::new(e)}
79
+
80
+ navigation = data.css("navigation")
81
+
82
+ next_page = nil
83
+ unless navigation.empty?
84
+ n = data.css("navigation next").first
85
+
86
+ if n.nil?
87
+ next_page = nil
88
+ else
89
+ next_page = n.text.to_i
90
+ end
91
+ end
92
+
93
+ return {:editions => editions, :next => next_page}
94
+ end
95
+
96
+ # Iterate over pages of results
97
+ def pages
98
+ hsh = full()
99
+ Enumerator.new do |page|
100
+ page << hsh[:editions]
101
+ loop do
102
+ break if hsh[:next].nil?
103
+ hsh = full(:startRec => hsh[:next])
104
+ page << hsh[:editions]
105
+ end
106
+ end
107
+ end
108
+
109
+ def load_recommendations(node)
110
+ recs = node.css('recommendations')
111
+ return nil if recs.empty?
112
+
113
+ return Recommendations.new(recs.first)
114
+ end
115
+
116
+ private :full, :pages, :load_recommendations
117
+ end
118
+ end
metadata ADDED
@@ -0,0 +1,105 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: classifoclc
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Sean Redmond
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-05-08 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.16'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.16'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rspec
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '3.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '3.0'
55
+ description:
56
+ email:
57
+ - github-smr@sneakemail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".gitignore"
63
+ - ".rspec"
64
+ - ".travis.yml"
65
+ - CODE_OF_CONDUCT.md
66
+ - Gemfile
67
+ - LICENSE.txt
68
+ - README.md
69
+ - Rakefile
70
+ - bin/console
71
+ - bin/setup
72
+ - classifoclc.gemspec
73
+ - lib/classifoclc.rb
74
+ - lib/classifoclc/author.rb
75
+ - lib/classifoclc/constants.rb
76
+ - lib/classifoclc/edition.rb
77
+ - lib/classifoclc/errors.rb
78
+ - lib/classifoclc/recommendations.rb
79
+ - lib/classifoclc/version.rb
80
+ - lib/classifoclc/work.rb
81
+ homepage: https://github.com/seanredmond/classifoclc
82
+ licenses:
83
+ - MIT
84
+ metadata: {}
85
+ post_install_message:
86
+ rdoc_options: []
87
+ require_paths:
88
+ - lib
89
+ required_ruby_version: !ruby/object:Gem::Requirement
90
+ requirements:
91
+ - - ">="
92
+ - !ruby/object:Gem::Version
93
+ version: '0'
94
+ required_rubygems_version: !ruby/object:Gem::Requirement
95
+ requirements:
96
+ - - ">="
97
+ - !ruby/object:Gem::Version
98
+ version: '0'
99
+ requirements: []
100
+ rubyforge_project:
101
+ rubygems_version: 2.6.14
102
+ signing_key:
103
+ specification_version: 4
104
+ summary: Interface to OCLC Classify service
105
+ test_files: []