Silence websockets install echo
[sitka/sitka-tools.git] / overdrive-eg-opac / build / od_session.js
CommitLineData
7c653aa3
JD
1// Generated by CoffeeScript 1.8.0
2var __hasProp = {}.hasOwnProperty,
3 __extends = function(child, parent) { for (var key in parent) { if (__hasProp.call(parent, key)) child[key] = parent[key]; } function ctor() { this.constructor = child; } ctor.prototype = parent.prototype; child.prototype = new ctor(); child.__super__ = parent.prototype; return child; };
4
5define(['json', 'lodash', 'od_config'], function(json, _, config) {
6 var Creds, Labels, Links, Prefs, Session, Token, U;
7 U = (function() {
8 function U() {}
9
10 U.prototype.update = function(x) {
11 var t;
12 if (!x) {
13 return;
14 }
15 t = this;
16 __extends(t, x);
17 };
18
19 U.prototype.store = function() {
20 return json.stringify(this, (function(n, v) {
21 if (n === 'prototype') {
22 return void 0;
23 } else {
24 return v;
25 }
26 }), ' ');
27 };
28
29 U.prototype.retrieve = function(x) {
30 return x = (function() {
31 if (_.isString(x)) {
32 try {
33 return json.parse(x);
34 } catch (_error) {
35 return void 0;
36 }
37 }
38 })();
39 };
40
41 U.prototype.proxy = function(x) {
42 var y;
43 if (!x) {
44 return;
45 }
46 y = x;
47 y = y.replace('https://', '//');
48 y = y.replace('http://', '//');
49 y = y.replace('//oauth-patron.overdrive.com', '/od/oauth-patron');
50 y = y.replace('//oauth.overdrive.com', '/od/oauth');
51 y = y.replace('//patron.api.overdrive.com', '/od/api-patron');
52 y = y.replace('//api.overdrive.com', '/od/api');
53 y = y.replace('//images.contentreserve.com', '/od/images');
54 y = y.replace('//fulfill.contentreserve.com', '/od/fulfill');
55 return y;
56 };
57
58 U.prototype.proxies = function(x) {
59 var l, n, v;
60 for (n in x) {
61 v = x[n];
62 if (l = v.href) {
63 v.href = this.proxy(l);
64 }
65 }
66 return x;
67 };
68
69 return U;
70
71 })();
72 Prefs = (function(_super) {
73 __extends(Prefs, _super);
74
75 Prefs["default"] = {
76 barcode: '',
77 email_address: '',
78 home_library: ''
79 };
80
81 function Prefs(x) {
82 this.update(x);
83 }
84
85 Prefs.prototype.update = function(x) {
86 return Prefs.__super__.update.call(this, x || Prefs["default"]);
87 };
88
89 return Prefs;
90
91 })(U);
92 Creds = (function(_super) {
93 __extends(Creds, _super);
94
95 Creds["default"] = {
96 username: '',
97 password: 'xxxx'
98 };
99
100 function Creds(x) {
101 this.update(x);
102 }
103
104 Creds.prototype.update = function(x) {
105 return Creds.__super__.update.call(this, x || Creds["default"]);
106 };
107
108 Creds.prototype.un = function() {
109 return this.barcode || this.username;
110 };
111
112 Creds.prototype.pw = function(required) {
113 if (required) {
114 return this.password;
115 } else {
116 return 'xxxx';
117 }
118 };
119
120 return Creds;
121
122 })(U);
123 Token = (function(_super) {
124 __extends(Token, _super);
125
126 Token["default"] = {
127 access_token: void 0,
128 expires_in: void 0,
129 scope: void 0,
130 token_type: void 0
131 };
132
133 function Token(x) {
134 this.update(x);
135 }
136
137 Token.prototype.update = function(x) {
138 return Token.__super__.update.call(this, x || Token["default"]);
139 };
140
141 Token.prototype.is_patron_access = function() {
142 return /patron/i.test(this.scope);
143 };
144
145 return Token;
146
147 })(U);
148 Links = (function(_super) {
149 __extends(Links, _super);
150
151 Links["default"] = {
152 token: {
153 href: '//oauth.overdrive.com/token'
154 },
155 libraries: {
156 href: "//api.overdrive.com/v1/libraries/" + config.accountID
157 },
158 patrontoken: {
159 href: '//oauth-patron.overdrive.com/patrontoken'
160 },
161 patrons: {
162 href: '//patron.api.overdrive.com/v1/patrons/me'
163 },
164 holds: {
165 href: ''
166 },
167 checkouts: {
168 href: ''
169 },
170 products: '',
171 advantageAccounts: '',
172 search: '',
173 availability: ''
174 };
175
176 function Links(x, logged_in) {
177 this.update(x);
178 if (x) {
179 this.calibrate(logged_in);
180 }
181 return;
182 }
183
184 Links.prototype.update = function(x) {
185 if (x == null) {
186 Links.__super__.update.call(this, this.proxies(Links["default"]));
187 }
188 if (x != null ? x.links : void 0) {
189 Links.__super__.update.call(this, this.proxies(x.links));
190 }
191 if (x != null ? x.linkTemplates : void 0) {
192 Links.__super__.update.call(this, this.proxies(x.linkTemplates));
193 }
194 };
195
196 Links.prototype.calibrate = function(logged_in) {
197 if (!logged_in) {
198 this.search = this.availability = '';
199 }
200 return this;
201 };
202
203 return Links;
204
205 })(U);
206 Labels = (function(_super) {
207 __extends(Labels, _super);
208
209 function Labels(x) {
210 this.update(x);
211 }
212
213 Labels.prototype.update = function(x) {
214 if (x != null ? x.formats : void 0) {
215 return Labels.__super__.update.call(this, this.to_object(x.formats, 'id', 'name'));
216 }
217 };
218
219 Labels.prototype.to_object = function(from, key, value) {
220 var to, x, _i, _len;
221 to = {};
222 if ((from != null ? from.length : void 0) > 0) {
223 for (_i = 0, _len = from.length; _i < _len; _i++) {
224 x = from[_i];
225 to[x[key]] = x[value];
226 }
227 }
228 return to;
229 };
230
231 return Labels;
232
233 })(U);
234 Session = (function(_super) {
235 __extends(Session, _super);
236
237 function Session(x, logged_in) {
238 x = this.retrieve(x);
239 this.prefs = new Prefs(x != null ? x.prefs : void 0);
240 this.creds = new Creds(x != null ? x.creds : void 0);
241 this.token = new Token(x != null ? x.token : void 0);
242 this.links = new Links(x, logged_in);
243 this.labels = new Labels(x);
244 return;
245 }
246
247 return Session;
248
249 })(U);
250 return Session;
251});