trek-patrest 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.
@@ -0,0 +1,46 @@
1
+ # PatREST client for Ruby
2
+ Client library for the [Ann Arbor District Library](http://www.aadl.org)'s [PatREST API](http://www.blyberg.net/downloads/patrest_1.3_overview.pdf).
3
+
4
+ ## Installing
5
+
6
+ ### Setup
7
+ Start by setting your library API's base url. For AADL, it's 'http://www.aadl.org/rest/'
8
+
9
+ require 'patrest'
10
+ include PatRest
11
+ PatRest::OPTIONS = {'BASE_URI' => http://www.aadl.org/rest/}
12
+
13
+ ### Finding Records
14
+ You can find a specific record by it's id
15
+
16
+ Record.find(1250150).inspect
17
+ # => #<PatRest::Record:0x111fc28 @attributes={"xmlns:p"=>"http://www.aadl.org", "bibnum"=>"1250150", "pubinfo"=>"New York : Little, Brown and Co., 2005", "isbn"=>"0316160172", "recordlink"=>"http://www.aadl.org/cat/seek/record=1250150", "callnum"=>"Teen Fiction", "copies"=>"61", "xmlns:xlink"=>"http://www.w3.org/1999/xlink", "title"=>"Twilight", "price"=>"$17.99", "holds"=>"80", "author"=>"Meyer, Stephenie, 1973-", "catdate"=>"10-03-2005", "avail"=>"7 copies available at Malletts Adult, Traverwood Adult and West Adult", "series"=>"Twilight saga ; 1", "subject"=>["Vampires -- Fiction", "High schools -- Fiction", "Washington (State) -- Fiction"], "desc"=>"498 p", "summary"=>"When seventeen-year-old Bella leaves Phoenix to live with her father in Forks, Washington, she meets an exquisitely handsome boy at school for whom she feels an overwhelming attraction and who she comes to realize is not wholly human", "lang"=>"eng", "ccimglink"=>"http://www.aadl.org/cat/ccimg/1250150", "edition"=>"1st ed", "mattype"=>"a", "fulltitle"=>"Twilight / a novel by Stephenie Meyer", "coverimglink"=>"http://syndetics.com/hw7.pl?isbn=0316160172+A/SC.gif"}>
18
+
19
+ ### Data access
20
+ Access data with dot notation for attributes
21
+
22
+ record = Record.find(1250150)
23
+ record.title
24
+ #=> "Twilight"
25
+ record.subject
26
+ #=> ["Vampires -- Fiction", "High schools -- Fiction", "Washington (State) -- Fiction"]
27
+
28
+ ### Search
29
+ You can perform any of the PatREST search types
30
+
31
+ search = Search.new(:title, 'twilight')
32
+ search.execute
33
+ search.first
34
+ # => <PatRest::Record:0x10a65a8 @attributes={"bibnum"=>"1286255", "recordlink"=>"http://www.aadl.org/cat/seek/record=1286255", "xlink:href"=>"http://www.aadl.org/rest/record/1286255/", "title"=>"Rapunzel, the one with all the hair", "xmlrecordlink"=>"http://www.aadl.org/rest/record/1286255/", "author"=>nil, "id"=>"1286255", "xlink:title"=>"Rapunzel, the one with all the hair / Wendy Mass", "fulltitle"=>"Rapunzel, the one with all the hair / Wendy Mass"}>
35
+
36
+ ### Patron-specific data
37
+ Patron data access requires the patron's token.
38
+
39
+ patron = Patron.new('1234567abcedef')
40
+ patron.holds
41
+ # => [#<PatRest::Record:0x1088530 @attributes=["Holds", {"xmlns:p"=>"http://www.aadl.org", "xmlns:xlink"=>"http://www.w3.org/1999/xlink", "Record"=>{"bibnum"=>"1067614", "isbn"=>"$48.00", "recordlink"=>"http://www.aadl.org/cat/seek/record=1067614", "xlink:href"=>"http://www.aadl.org/rest/record/1067614/", "title"=>"Envisioning information", "xmlrecordlink"=>"http://www.aadl.org/rest/record/1067614/", "author"=>"Tufte, Edward R., 1942-", "id"=>"1067614", "xlink:title"=>"Envisioning information / Edward R. Tufte", "imglink"=>"http://syndetics.com/hw7.pl?isbn=$48.00+A/SC.gif", "pickuploc"=>"Downtown Library", "canceldate"=>"2010-04-12", "fulltitle"=>"Envisioning information / Edward R. Tufte", "holdstatus"=>"2 of 4 holds"}}]>]
42
+ patron.items.first
43
+ # => #<PatRest::Record:0x1040a00 @attributes={"bibnum"=>"1277702", "recordlink"=>"http://www.aadl.org/cat/seek/record=1277702", "xlink:href"=>"http://www.aadl.org/rest/record/1277702/", "title"=>"Make [periodical]: technology on your time", "xmlrecordlink"=>"http://www.aadl.org/rest/record/1277702/", "author"=>nil, "id"=>"1277702", "xlink:title"=>"Make [periodical]: technology on your time", "duedate"=>"2009-05-02", "fulltitle"=>"Make [periodical]: technology on your time"}>
44
+
45
+
46
+
@@ -0,0 +1,20 @@
1
+ $:.unshift(File.dirname(__FILE__))
2
+
3
+ require 'rubygems'
4
+ require 'httparty'
5
+ require 'uri'
6
+ require 'patrest/patron'
7
+ require 'patrest/record'
8
+ require 'patrest/search'
9
+
10
+ module PatRest
11
+ OPTIONS = {'BASE_URI' => ''}
12
+ class RecordNotFound < StandardError
13
+ end
14
+
15
+ class InvalidSearchType < StandardError
16
+ end
17
+
18
+ class InvalidPatronToken < StandardError
19
+ end
20
+ end
@@ -0,0 +1,30 @@
1
+ module PatRest
2
+ class Patron
3
+ include HTTParty
4
+ format :xml
5
+ base_uri 'http://www.aadl.org/rest/'
6
+
7
+ attr_reader :token
8
+
9
+ def initialize(token)
10
+ @token = token
11
+ end
12
+
13
+ def items
14
+ @items ||= parse_single_or_many_records(Patron.get("/checkouts/#{token}")['Checkouts'])
15
+ end
16
+
17
+ def holds
18
+ @holds ||= parse_single_or_many_records(Patron.get("/holds/#{token}")['Holds'])
19
+ end
20
+
21
+ private
22
+ def parse_single_or_many_records(query_result)
23
+ unless query_result['Record'].is_a? Array
24
+ [PatRest::Record.new(query_result['Record'])]
25
+ else
26
+ query_result['Record'].collect {|attrs| PatRest::Record.new(attrs) }
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,27 @@
1
+ module PatRest
2
+ class Record
3
+ include HTTParty
4
+ format :xml
5
+ base_uri 'http://www.aadl.org/rest/record'
6
+
7
+ attr_accessor :attributes
8
+ def self.find(record_number)
9
+ attrs = get("/#{record_number}")
10
+ raise PatRest::RecordNotFound if attrs['Record']['error']
11
+ self.new(attrs['Record'])
12
+ end
13
+
14
+ def self.search(type, string, options = {:per_page => 10, :page => 1})
15
+
16
+ end
17
+
18
+ def initialize(attrs)
19
+ self.attributes = attrs
20
+ end
21
+
22
+ def method_missing(method)
23
+ method = method.to_s
24
+ attributes.has_key?(method) ? attributes[method] : raise(NoMethodError)
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,56 @@
1
+ module PatRest
2
+ class Search
3
+ FACETS = %w(title author callnum keyword subject gvtdocnum stdnum titlekey controlnum barcode record bibnum itemnum)
4
+ include HTTParty
5
+ format :xml
6
+ base_uri 'http://www.aadl.org/rest/search'
7
+
8
+ attr_accessor :type, :query, :per_page, :current_page, :results
9
+
10
+ def initialize(type, query, options = {:per_page => 10, :page => 1})
11
+ raise InvalidSearchType unless Search::FACETS.include?(type.to_s)
12
+ self.type = type
13
+ self.query = query
14
+ self.per_page = options[:per_page]
15
+ self.current_page = options[:page]
16
+ end
17
+
18
+ def escaped_query
19
+ URI.escape(self.query)
20
+ end
21
+
22
+ def execute
23
+ results = Search.get("/#{type}/#{escaped_query}/#{per_page}/#{current_page}")
24
+ @results = results['SearchResult']['Records']['Record'].collect {|attrs| PatRest::Record.new(attrs)}
25
+ self
26
+ end
27
+
28
+ def next
29
+ self.current_page += 1
30
+ self.execute
31
+ end
32
+
33
+ def previous
34
+ self.current_page -= 1
35
+ self.current_page = 0 if self.current_page < 0 # cannot go into negative search results
36
+ self.execute
37
+ end
38
+
39
+ def results
40
+ @results || []
41
+ end
42
+
43
+ def method_missing(method, *args)
44
+ unless self.results.respond_to?(method)
45
+ message = "undefined method `#{method.to_s}' for \"#{@self}\":#{@self.class.to_s}"
46
+ raise NoMethodError, message
47
+ end
48
+
49
+ if block_given?
50
+ self.results.send(method, *args) { |*block_args| yield(*block_args) }
51
+ else
52
+ self.results.send(method, *args)
53
+ end
54
+ end
55
+ end
56
+ end
metadata ADDED
@@ -0,0 +1,67 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: trek-patrest
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.2
5
+ platform: ruby
6
+ authors:
7
+ - Trek Glowacki
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+
12
+ date: 2009-04-26 00:00:00 -07:00
13
+ default_executable:
14
+ dependencies:
15
+ - !ruby/object:Gem::Dependency
16
+ name: httparty
17
+ type: :runtime
18
+ version_requirement:
19
+ version_requirements: !ruby/object:Gem::Requirement
20
+ requirements:
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 0.4.3
24
+ version:
25
+ description: patrest is a Ruby library for talking to libraries using the Ann Arbor District Library\'s PatREST API.'
26
+ email: trek.glowacki@gmail.com
27
+ executables: []
28
+
29
+ extensions: []
30
+
31
+ extra_rdoc_files: []
32
+
33
+ files:
34
+ - README.markdown
35
+ - lib/patrest.rb
36
+ - lib/patrest/patron.rb
37
+ - lib/patrest/record.rb
38
+ - lib/patrest/search.rb
39
+ has_rdoc: false
40
+ homepage: http://github.com/trek/patrest-ruby
41
+ post_install_message:
42
+ rdoc_options:
43
+ - --inline-source
44
+ - --charset=UTF-8
45
+ require_paths:
46
+ - lib
47
+ required_ruby_version: !ruby/object:Gem::Requirement
48
+ requirements:
49
+ - - ">="
50
+ - !ruby/object:Gem::Version
51
+ version: "0"
52
+ version:
53
+ required_rubygems_version: !ruby/object:Gem::Requirement
54
+ requirements:
55
+ - - ">="
56
+ - !ruby/object:Gem::Version
57
+ version: "0"
58
+ version:
59
+ requirements: []
60
+
61
+ rubyforge_project:
62
+ rubygems_version: 1.2.0
63
+ signing_key:
64
+ specification_version: 2
65
+ summary: patrest is a Ruby library for talking to libraries using the Ann Arbor District Library\'s PatREST API.'
66
+ test_files: []
67
+