API Docs for: 0.50.0
Show:

File: src/Agent.js

  1. /*
  2. Copyright 2012 Rustici Software
  3.  
  4. Licensed under the Apache License, Version 2.0 (the "License");
  5. you may not use this file except in compliance with the License.
  6. You may obtain a copy of the License at
  7.  
  8. http://www.apache.org/licenses/LICENSE-2.0
  9.  
  10. Unless required by applicable law or agreed to in writing, software
  11. distributed under the License is distributed on an "AS IS" BASIS,
  12. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. See the License for the specific language governing permissions and
  14. limitations under the License.
  15. */
  16.  
  17. /**
  18. TinCan client library
  19.  
  20. @module TinCan
  21. @submodule TinCan.Agent
  22. **/
  23. (function () {
  24. "use strict";
  25.  
  26. /**
  27. @class TinCan.Agent
  28. @constructor
  29. */
  30. var Agent = TinCan.Agent = function (cfg) {
  31. this.log("constructor");
  32.  
  33. /**
  34. @property name
  35. @type String
  36. */
  37. this.name = null;
  38.  
  39. /**
  40. @property mbox
  41. @type String
  42. */
  43. this.mbox = null;
  44.  
  45. /**
  46. @property mbox_sha1sum
  47. @type String
  48. */
  49. this.mbox_sha1sum = null;
  50.  
  51. /**
  52. @property openid
  53. @type String
  54. */
  55. this.openid = null;
  56.  
  57. /**
  58. @property account
  59. @type TinCan.AgentAccount
  60. */
  61. this.account = null;
  62.  
  63. /**
  64. @property degraded
  65. @type Boolean
  66. @default false
  67. */
  68. this.degraded = false;
  69.  
  70. this.init(cfg);
  71. };
  72. Agent.prototype = {
  73. /**
  74. @property objectType
  75. @type String
  76. @default Agent
  77. */
  78. objectType: "Agent",
  79.  
  80. /**
  81. @property LOG_SRC
  82. */
  83. LOG_SRC: "Agent",
  84.  
  85. /**
  86. @method log
  87. */
  88. log: TinCan.prototype.log,
  89.  
  90. /**
  91. @method init
  92. @param {Object} [options] Configuration used to initialize
  93. */
  94. init: function (cfg) {
  95. this.log("init");
  96. var i,
  97. directProps = [
  98. "name",
  99. "mbox",
  100. "mbox_sha1sum",
  101. "openid"
  102. ],
  103. val
  104. ;
  105.  
  106. cfg = cfg || {};
  107.  
  108. // handle .9 split names and array properties into single interface
  109. if (typeof cfg.lastName !== "undefined" || typeof cfg.firstName !== "undefined") {
  110. cfg.name = "";
  111. if (typeof cfg.firstName !== "undefined" && cfg.firstName.length > 0) {
  112. cfg.name = cfg.firstName[0];
  113. if (cfg.firstName.length > 1) {
  114. this.degraded = true;
  115. }
  116. }
  117.  
  118. if (cfg.name !== "") {
  119. cfg.name += " ";
  120. }
  121.  
  122. if (typeof cfg.lastName !== "undefined" && cfg.lastName.length > 0) {
  123. cfg.name += cfg.lastName[0];
  124. if (cfg.lastName.length > 1) {
  125. this.degraded = true;
  126. }
  127. }
  128. } else if (typeof cfg.familyName !== "undefined" || typeof cfg.givenName !== "undefined") {
  129. cfg.name = "";
  130. if (typeof cfg.givenName !== "undefined" && cfg.givenName.length > 0) {
  131. cfg.name = cfg.givenName[0];
  132. if (cfg.givenName.length > 1) {
  133. this.degraded = true;
  134. }
  135. }
  136.  
  137. if (cfg.name !== "") {
  138. cfg.name += " ";
  139. }
  140.  
  141. if (typeof cfg.familyName !== "undefined" && cfg.familyName.length > 0) {
  142. cfg.name += cfg.familyName[0];
  143. if (cfg.familyName.length > 1) {
  144. this.degraded = true;
  145. }
  146. }
  147. }
  148.  
  149. if (typeof cfg.name === "object" && cfg.name !== null) {
  150. if (cfg.name.length > 1) {
  151. this.degraded = true;
  152. }
  153. cfg.name = cfg.name[0];
  154. }
  155. if (typeof cfg.mbox === "object" && cfg.mbox !== null) {
  156. if (cfg.mbox.length > 1) {
  157. this.degraded = true;
  158. }
  159. cfg.mbox = cfg.mbox[0];
  160. }
  161. if (typeof cfg.mbox_sha1sum === "object" && cfg.mbox_sha1sum !== null) {
  162. if (cfg.mbox_sha1sum.length > 1) {
  163. this.degraded = true;
  164. }
  165. cfg.mbox_sha1sum = cfg.mbox_sha1sum[0];
  166. }
  167. if (typeof cfg.openid === "object" && cfg.openid !== null) {
  168. if (cfg.openid.length > 1) {
  169. this.degraded = true;
  170. }
  171. cfg.openid = cfg.openid[0];
  172. }
  173. if (typeof cfg.account === "object" && cfg.account !== null && typeof cfg.account.homePage === "undefined" && typeof cfg.account.name === "undefined") {
  174. if (cfg.account.length === 0) {
  175. delete cfg.account;
  176. }
  177. else {
  178. if (cfg.account.length > 1) {
  179. this.degraded = true;
  180. }
  181. cfg.account = cfg.account[0];
  182. }
  183. }
  184.  
  185. if (cfg.hasOwnProperty("account")) {
  186. if (cfg.account instanceof TinCan.AgentAccount) {
  187. this.account = cfg.account;
  188. }
  189. else {
  190. this.account = new TinCan.AgentAccount (cfg.account);
  191. }
  192. }
  193.  
  194. for (i = 0; i < directProps.length; i += 1) {
  195. if (cfg.hasOwnProperty(directProps[i]) && cfg[directProps[i]] !== null) {
  196. val = cfg[directProps[i]];
  197. if (directProps[i] === "mbox" && val.indexOf("mailto:") === -1) {
  198. val = "mailto:" + val;
  199. }
  200. this[directProps[i]] = val;
  201. }
  202. }
  203. },
  204.  
  205. toString: function () {
  206. this.log("toString");
  207.  
  208. if (this.name !== null) {
  209. return this.name;
  210. }
  211. if (this.mbox !== null) {
  212. return this.mbox.replace("mailto:", "");
  213. }
  214. if (this.mbox_sha1sum !== null) {
  215. return this.mbox_sha1sum;
  216. }
  217. if (this.openid !== null) {
  218. return this.openid;
  219. }
  220. if (this.account !== null) {
  221. return this.account.toString();
  222. }
  223.  
  224. return this.objectType + ": unidentified";
  225. },
  226.  
  227. /**
  228. While a TinCan.Agent instance can store more than one inverse functional identifier
  229. this method will always only output one to be compliant with the statement sending
  230. specification. Order of preference is: mbox, mbox_sha1sum, openid, account
  231.  
  232. @method asVersion
  233. @param {String} [version] Version to return (defaults to newest supported)
  234. */
  235. asVersion: function (version) {
  236. this.log("asVersion: " + version);
  237. var result = {
  238. objectType: this.objectType
  239. };
  240.  
  241. version = version || TinCan.versions()[0];
  242.  
  243. if (version === "0.9") {
  244. if (this.mbox !== null) {
  245. result.mbox = [ this.mbox ];
  246. }
  247. else if (this.mbox_sha1sum !== null) {
  248. result.mbox_sha1sum = [ this.mbox_sha1sum ];
  249. }
  250. else if (this.openid !== null) {
  251. result.openid = [ this.openid ];
  252. }
  253. else if (this.account !== null) {
  254. result.account = [ this.account.asVersion(version) ];
  255. }
  256.  
  257. if (this.name !== null) {
  258. result.name = [ this.name ];
  259. }
  260. } else {
  261. if (this.mbox !== null) {
  262. result.mbox = this.mbox;
  263. }
  264. else if (this.mbox_sha1sum !== null) {
  265. result.mbox_sha1sum = this.mbox_sha1sum;
  266. }
  267. else if (this.openid !== null) {
  268. result.openid = this.openid;
  269. }
  270. else if (this.account !== null) {
  271. result.account = this.account.asVersion(version);
  272. }
  273.  
  274. if (this.name !== null) {
  275. result.name = this.name;
  276. }
  277. }
  278.  
  279. return result;
  280. }
  281. };
  282.  
  283. /**
  284. @method fromJSON
  285. @return {Object} Agent
  286. @static
  287. */
  288. Agent.fromJSON = function (agentJSON) {
  289. Agent.prototype.log("fromJSON");
  290. var _agent = JSON.parse(agentJSON);
  291.  
  292. return new Agent(_agent);
  293. };
  294. }());
  295.