oclc_classify 0.0.1

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.
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ *.lock
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ source 'http://rubygems.org'
2
+
3
+ gem 'open-uri'
4
+ gem 'nokogiri'
5
+ gem 'shoulda'
data/README.txt ADDED
@@ -0,0 +1,59 @@
1
+ = oclc_classify
2
+
3
+ http://www.libcode.net/
4
+
5
+ == DESCRIPTION:
6
+
7
+ Retrieve dewey decimal numbers from OCLC's classify service
8
+
9
+ == FEATURES/PROBLEMS:
10
+
11
+ Very much a work in progress ...
12
+
13
+ == SYNOPSIS:
14
+
15
+ oclc = OCLC::Classify.new 'oclc', '610837844'
16
+ result = oclc.classify
17
+ p result.author
18
+ p result.title
19
+ r = result.recommendations 'ddc', :popular
20
+ s = result.recommendations 'ddc', :recent
21
+ t = result.recommendations 'ddc', :latest
22
+ p r
23
+ p s
24
+ p t
25
+ r.each {|k,v| puts k['nsfa'] }
26
+
27
+ == REQUIREMENTS:
28
+
29
+ nokogiri
30
+ open-uri
31
+
32
+ == INSTALL:
33
+
34
+ sudo gem install oclc_classify
35
+
36
+ == LICENSE:
37
+
38
+ (The MIT License)
39
+
40
+ Copyright (c) 2011 FIX
41
+
42
+ Permission is hereby granted, free of charge, to any person obtaining
43
+ a copy of this software and associated documentation files (the
44
+ 'Software'), to deal in the Software without restriction, including
45
+ without limitation the rights to use, copy, modify, merge, publish,
46
+ distribute, sublicense, and/or sell copies of the Software, and to
47
+ permit persons to whom the Software is furnished to do so, subject to
48
+ the following conditions:
49
+
50
+ The above copyright notice and this permission notice shall be
51
+ included in all copies or substantial portions of the Software.
52
+
53
+ THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
54
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
55
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
56
+ IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
57
+ CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
58
+ TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
59
+ SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/Rakefile ADDED
@@ -0,0 +1,28 @@
1
+ # -*- ruby -*-
2
+
3
+ require 'rake/testtask'
4
+
5
+ desc "Validate the gemspec"
6
+ task :gemspec do
7
+ gemspec.validate
8
+ end
9
+
10
+ desc "Build gem locally"
11
+ task :build => :gemspec do
12
+ system "gem build #{gemspec.name}.gemspec"
13
+ FileUtils.mkdir_p "pkg"
14
+ FileUtils.mv "#{gemspec.name}-#{gemspec.version}.gem", "pkg"
15
+ end
16
+
17
+ desc "Install gem locally"
18
+ task :install => :build do
19
+ system "gem install pkg/#{gemspec.name}-#{gemspec.version}"
20
+ end
21
+
22
+ Rake::TestTask.new do |t|
23
+ t.libs << "test"
24
+ t.test_files = FileList['test/test*.rb']
25
+ t.verbose = true
26
+ end
27
+
28
+ # vim: syntax=ruby
@@ -0,0 +1,140 @@
1
+ module OCLC
2
+
3
+ VERSION = '0.0.1'
4
+ require 'nokogiri'
5
+ require 'open-uri'
6
+
7
+ BASE_URL = 'http://classify.oclc.org/classify2/Classify?'
8
+
9
+ ARGUMENTS = [
10
+ 'stdnbr',
11
+ 'oclc',
12
+ 'isbn',
13
+ 'issn',
14
+ 'upc',
15
+ 'ident',
16
+ 'heading',
17
+ 'lccn',
18
+ 'lccn_pfx',
19
+ 'lccn_yr',
20
+ 'lccn_sno',
21
+ 'swid',
22
+ 'author',
23
+ 'title',
24
+ 'summary',
25
+ ]
26
+
27
+ ORDER_KEY = 'orderBy'
28
+ MXREC_KEY = 'maxRecs'
29
+
30
+ class Classify
31
+
32
+ attr_reader :url
33
+
34
+ ##
35
+ # oclc = OCLC::Classify.new 'oclc', '610837844'
36
+ # result = oclc.classify
37
+ # p result.author
38
+ # p result.title
39
+ # r = result.recommendations 'ddc', :popular
40
+ # s = result.recommendations 'ddc', :recent
41
+ # t = result.recommendations 'ddc', :latest
42
+ # p r
43
+ # p s
44
+ # p t
45
+ # r.each {|k,v| puts k['nsfa'] }
46
+
47
+ def initialize(input, value, summary = false, orderBy = nil, maxRecs = nil)
48
+ raise "Invalid argument parameter: " + input unless ARGUMENTS.include? input
49
+ @summary = summary ? '&summary=true' : '&summary=false'
50
+ @query_string = input + '=' + value + @summary
51
+ @orderBy = orderBy
52
+ @maxRecs = maxRecs.to_s if maxRecs
53
+ self
54
+ end
55
+
56
+ def orderBy(entry)
57
+ @orderBy ||= entry
58
+ end
59
+
60
+ def maxRecs(number)
61
+ @maxRecs ||= number.to_s
62
+ end
63
+
64
+ def classify
65
+ @url = construct_query
66
+ return OCLC::Classify::Response.new(@url)
67
+ end
68
+
69
+ private
70
+
71
+ def construct_query
72
+ query = BASE_URL + @query_string
73
+ query = query + '&' + ORDER_KEY + '=' + @orderBy if @orderBy
74
+ query = query + '&' + MXREC_KEY + '=' + @maxRecs if @maxRecs
75
+ return query
76
+ end
77
+
78
+ class Response
79
+
80
+ attr_reader :response, :code, :author, :title
81
+
82
+ XPATH = {
83
+ :code => '//response/@code',
84
+ :author => '//work/@author',
85
+ :title => '//work/@title',
86
+ :recommendations => '//recommendations',
87
+ }
88
+
89
+ KEYS = {
90
+ :popular => 'mostpopular',
91
+ :recent => 'mostrecent',
92
+ :latest => 'latestedition',
93
+ }
94
+
95
+ def initialize(url)
96
+ @response = Nokogiri::HTML(open(url))
97
+ @code = @response.xpath(XPATH[:code]).to_s
98
+ @author = @response.xpath(XPATH[:author]).to_s
99
+ @title = @response.xpath(XPATH[:title]).to_s
100
+ end
101
+
102
+ def recommendations(scheme, key)
103
+ raise "Invalid scheme: must be 'ddc' or 'lcc'" unless scheme =~ /(dd|lc)c/
104
+ raise "Invalid key: " + key.to_s unless KEYS.keys.include? key
105
+ path = '/' + scheme + '/' + KEYS[key]
106
+ if @code == '0' or @code == '2'
107
+ results = OCLC::Classify::Recommendations.new(@response.xpath(XPATH[:recommendations] + path))
108
+ results.process
109
+ return results.recommendations
110
+ else
111
+ return []
112
+ end
113
+ end
114
+
115
+ end
116
+
117
+ class Recommendations
118
+
119
+ attr_reader :recommendations
120
+
121
+ def initialize(xml)
122
+ @xml = xml
123
+ @recommendations = []
124
+ end
125
+
126
+ def process
127
+ @xml.each do |r|
128
+ box = {}
129
+ k = r.attributes.keys
130
+ r.attribute_nodes.each_with_index do |a, idx|
131
+ box[k[idx]] = a.value
132
+ end
133
+ @recommendations << box
134
+ end
135
+ end
136
+
137
+ end
138
+
139
+ end
140
+ end
@@ -0,0 +1,48 @@
1
+ #!/usr/bin/env gem build
2
+ # encoding: utf-8
3
+
4
+ require "base64"
5
+
6
+ Gem::Specification.new do |s|
7
+ s.name = %q{oclc_classify}
8
+ s.version = "0.0.1"
9
+ s.authors = ["Mark Cooper"]
10
+ s.date = %q{2011-11-22}
11
+ s.homepage = %q{http://www.libcode.net}
12
+ s.email = Base64.decode64("bWFya2NocmlzdG9waGVyY29vcGVyQGdtYWlsLmNvbQ==\n")
13
+
14
+ s.summary = "Retrieve dewey decimal numbers from OCLC's classify service"
15
+ s.description = "#{s.summary}"
16
+ s.cert_chain = nil
17
+ s.has_rdoc = true
18
+
19
+ # files
20
+ s.files = `git ls-files`.split("\n")
21
+ s.test_files = `git ls-files test`.split("\n")
22
+ s.extra_rdoc_files = ["README.txt"]
23
+ s.rdoc_options = ["--main", "README.txt"]
24
+
25
+ Dir["bin/*"].map(&File.method(:basename))
26
+ # s.default_executable = "example"
27
+ s.require_paths = ["lib"]
28
+
29
+ # Ruby version
30
+ s.required_ruby_version = ::Gem::Requirement.new("~> 1.9")
31
+
32
+ # dependencies
33
+ # RubyGems has runtime dependencies (add_dependency) and
34
+ # development dependencies (add_development_dependency)
35
+ # s.add_development_dependency "simple-templater", ">= 0.0.1.2"
36
+ s.add_development_dependency "bundler"
37
+
38
+ begin
39
+ require "changelog"
40
+ rescue LoadError
41
+ warn "You have to have changelog gem installed for post install message"
42
+ else
43
+ s.post_install_message = CHANGELOG.new.version_changes
44
+ end
45
+
46
+ # RubyForge
47
+ s.rubyforge_project = %q{oclc_classify}
48
+ end
@@ -0,0 +1,22 @@
1
+ require "shoulda"
2
+ require "oclc_classify"
3
+
4
+ class TestOclcClassify < Test::Unit::TestCase
5
+
6
+ context "" do
7
+
8
+ setup do
9
+ @oclc = OCLC::Classify.new 'oclc', '123456'
10
+ @isbn = OCLC::Classify.new 'isbn', '987654321', true
11
+ @oclc.classify
12
+ @isbn.classify
13
+ end
14
+
15
+ should "Have constructed correct url" do
16
+ assert_equal 'http://classify.oclc.org/classify2/Classify?oclc=123456&summary=false', @oclc.url
17
+ assert_equal 'http://classify.oclc.org/classify2/Classify?isbn=987654321&summary=true', @isbn.url
18
+ end
19
+
20
+ end
21
+
22
+ end
metadata ADDED
@@ -0,0 +1,68 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: oclc_classify
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Mark Cooper
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain:
12
+ date: 2011-11-22 00:00:00.000000000 -08:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: bundler
17
+ requirement: &10358940 !ruby/object:Gem::Requirement
18
+ none: false
19
+ requirements:
20
+ - - ! '>='
21
+ - !ruby/object:Gem::Version
22
+ version: '0'
23
+ type: :development
24
+ prerelease: false
25
+ version_requirements: *10358940
26
+ description: Retrieve dewey decimal numbers from OCLC's classify service
27
+ email: markchristophercooper@gmail.com
28
+ executables: []
29
+ extensions: []
30
+ extra_rdoc_files:
31
+ - README.txt
32
+ files:
33
+ - .gitignore
34
+ - Gemfile
35
+ - README.txt
36
+ - Rakefile
37
+ - lib/oclc_classify.rb
38
+ - oclc_classify.gemspec
39
+ - test/test_oclc_classify.rb
40
+ has_rdoc: true
41
+ homepage: http://www.libcode.net
42
+ licenses: []
43
+ post_install_message:
44
+ rdoc_options:
45
+ - --main
46
+ - README.txt
47
+ require_paths:
48
+ - lib
49
+ required_ruby_version: !ruby/object:Gem::Requirement
50
+ none: false
51
+ requirements:
52
+ - - ~>
53
+ - !ruby/object:Gem::Version
54
+ version: '1.9'
55
+ required_rubygems_version: !ruby/object:Gem::Requirement
56
+ none: false
57
+ requirements:
58
+ - - ! '>='
59
+ - !ruby/object:Gem::Version
60
+ version: '0'
61
+ requirements: []
62
+ rubyforge_project: oclc_classify
63
+ rubygems_version: 1.5.2
64
+ signing_key:
65
+ specification_version: 3
66
+ summary: Retrieve dewey decimal numbers from OCLC's classify service
67
+ test_files:
68
+ - test/test_oclc_classify.rb