My favorites | Sign in
Project Logo
                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
#!/usr/bin/env ruby
require 'date'
require 'rubygems'
require 'camping'
require 'reststop'
require 'vpim/vcard'
require 'yaml'
require 'composite_primary_keys'
require 'marc'

Markaby::Builder.set(:indent, 2)

DBCONF = YAML.load(File.open(File.join(File.dirname(__FILE__), "database.yml")))
Camping.goes :OpenBiblio

module OpenBiblio::Models
class Actor < Base
self.establish_connection(DBCONF[:openbiblio])
self.table_name = "member"
self.primary_key = "mbrid"
end

class BibRecord < Base
self.establish_connection(DBCONF[:openbiblio])
self.table_name = "biblio"
self.primary_key = "bibid"
has_many :bib_fields, :foreign_key => 'bibid'
end

class BibField < Base
self.establish_connection(DBCONF[:openbiblio])
self.table_name = "biblio_field"
set_primary_keys :bibid, :fieldid
belongs_to :bib_record, :foreign_key => 'bibid'
end

end

module OpenBiblio::Controllers
class Services < REST 'services'
def get
@data = {"actor"=>{"title"=>"patrons", "accept"=>["text/x-vcard", "text/directory;profile=vcard,text/directory"]}}
render(:read, :JSON)
end
end

class Actor < R '/actors/(\d*([-,]\d+)*)'
def get(id, *etc)
translated_id = IdTranslator.translate(id)
@data = {"source"=>"OpenBiblio ILS", "time"=>DateTime.now.to_s}
if translated_id.is_a?(String) or translated_id.is_a?(Fixnum)
actors = []
actors << OpenBiblio::Models::Actor.find(id)
elsif translated_id.is_a?(Array)
actors = OpenBiblio::Models::Actor.find(id)
else
actors = OpenBiblio::Models::Actor.find(:all)
end
@data["request"] = "http:" + self.URL("/actors/#{id}").to_s
@data["data"] = []
actors.each do | actor |
vcard = Vpim::Vcard::Maker.make2 do | vc |
vc.add_name do | name |
name.family = actor.last_name
name.given = actor.first_name

end
vc.add_addr do | addr |
addr.street = actor.address.split("\n")[0]
citystatepost = actor.address.split("\n")[1]
addr.locality = citystatepost.split(",")[0].strip
addr.region, addr.postalcode = citystatepost.split(",")[1].split(" ")
end
vc.add_tel(actor.work_phone) { | p | p.location = 'work'} if actor.work_phone and !actor.work_phone.empty?
vc.add_tel(actor.home_phone) { | p | p.location = 'home'} if actor.home_phone and !actor.home_phone.empty?
vc.add_email(actor.email)
vc.add_uid(actor.barcode_nmbr.to_s, "http://jangle.org/terms/#barcode")
vc.add_uid(actor.mbrid.to_s, "http://jangle.org/terms/#URI")
end
actor_hash = {:id=>"/actors/#{actor.mbrid}", :title=>"#{actor.last_name}, #{actor.first_name}", :updated=>actor.last_change_dt.xmlschema,
:created=>actor.create_dt.xmlschema, :content_type=>"text/x-card", :content=>vcard.to_s}
@data["data"] << actor_hash
end
render(:read, :JSON)
end
end

class Resource < R '/resources/(\d*([-,]\d+)*)'
def get(id, *etc)
translated_id = IdTranslator.translate(id)
@data = {"source"=>"OpenBiblio ILS", "time"=>DateTime.now.to_s}
if translated_id.is_a?(String) or translated_id.is_a?(Fixnum)
records = []
records << OpenBiblio::Models::BibRecord.find(id)
elsif translated_id.is_a?(Array)
records = OpenBiblio::Models::BibRecord.find(id)
else
cond = {:limit=>100}
@data["links"] = {:first=>"/resources/"}
@data["links"][:self] = @env["REQUEST_URI"]
@data["links"][:last] = "/resources/?page=#{OpenBiblio::Models::BibRecord.count/100}"
if @input.page
cond[:offset] = (@input.page.to_i * 100)
page = nil
page = "?page=#{(@input.page.to_i - 1)}" if @input.page.to_i > 2
@data["links"][:previous] = "/resources/#{page}"
end

records = OpenBiblio::Models::BibRecord.find(:all, cond)
end
@data["request"] = "http:" + self.URL("/resources/#{id}").to_s
@data["data"] = []
records.each do | record |
marc = MARC::Record.new
title = MARC::DataField.new("245", "0", "0")
title.append(MARC::Subfield.new("a", record.title))
title.append(MARC::Subfield.new("b", record.title_remainder)) if record.title_remainder
title.append(MARC::Subfield.new("c", record.responsibility_stmt)) if record.responsibility_stmt
marc << title
if record.author
author = MARC::DataField.new("100", "0", "")
author.append(MARC::Subfield.new("a", record.author))
marc << author
end
[record.topic1, record.topic2, record.topic3, record.topic4, record.topic4].each do | subject |
next unless subject
subj = MARC::DataField.new("650", "", "4")
subj.append(MARC::Subfield.new("a", subject))
marc << subj
end
record.bib_fields.each do | fld |
ind1 = fld.ind1_cd
ind1 = "" if ind1 == "N"
ind2 = fld.ind2_cd
ind2 = "" if ind2 == "N"
unless field = marc[fld.tag] and !marc[fld.tag][fld.subfield_cd]
field = MARC::DataField.new(fld.tag, ind1, ind2)
marc << field
end
field.append(MARC::Subfield.new(fld.subfield_cd, fld.field_data))
end
marcxml = marc.to_xml
record_hash = {:id=>"/resources/#{record.bibid}", :title=>record.title, :updated=>record.last_change_dt.xmlschema,
:created=>record.create_dt.xmlschema, :content_type=>"application/marc21+xml", :content=>marcxml.to_s}
@data["data"] << record_hash
end
render(:read, :JSON)
end
end
end

module OpenBiblio::Views
module JSON

def layout
yield
end
include OpenBiblio::Controllers
def read

@data.to_json
end
end
end

module OpenBiblio
class IdTranslator
def self.translate(str)
#if the request is simple (integer, list or nil) return that
return nil if str.empty?
return str if str.match(/^\d+$/)
return str.split(",") if str.match(/^(\d+,?)*$/)
#if the request includes a range (x-y), translate that into an array
ids = []
str.split(",").each do | id |
if id.match(/^\d+$/)
ids << id
else
start_rng, end_rng = id.split("-")
(start_rng..end_rng).each { | i | ids << i }
end
end
return ids
end
end
end
module Vpim
class Vcard
class Maker
def add_uid(value, type=nil)
uid = Vpim::DirectoryInfo::Field.create( 'UID', value.to_str )
uid["TYPE"] = type if type
@card << uid
end
end
end
end

Show details Hide details

Change log

r945 by rossfsinger on May 06, 2008   Diff
Added paging to resource feed.
Go to: 
Project members, sign in to write a code review

Older revisions

r938 by rossfsinger on Mar 31, 2008   Diff
Messed up some variable scoping.
r937 by rossfsinger on Mar 31, 2008   Diff
Eh, screwed, now committing this where
it's supposed to go.

-rfs
All revisions of this file

File info

Size: 6874 bytes, 198 lines

File properties

svn:executable
Hosted by Google Code