unit22

Untitled

Feb 17th, 2012
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.38 KB | None | 0 0
  1. (function () {
  2.  
  3. var fluid = require("infusion"),
  4. gpii = fluid.registerNamespace("gpii"),
  5. fs = fluid.require("fs"),
  6. http = fluid.require("http"),
  7. path = fluid.require("path"),
  8. eUC = "encodeURIComponent:";
  9.  
  10. fluid.defaults("gpii.dataSource", {
  11. gradeNames: ["autoInit", "fluid.littleComponent"],
  12. components: {
  13. urlExpander: {
  14. type: "gpii.dataSource.urlExpander"
  15. }
  16. },
  17. invokers: {
  18. get: "gpii.dataSource.get",
  19. resolveUrl: "gpii.dataSource.resolveUrl"
  20. },
  21. url: "",
  22. termMap: {},
  23. responseParser: null,
  24. writable: false
  25. });
  26.  
  27. gpii.dataSource.urlExpander = function (options) {
  28. var that = fluid.initLittleComponent("gpii.dataSource.urlExpander", options);
  29. return function (url) {
  30. return fluid.stringTemplate(url, that.options.vars);
  31. };
  32. };
  33.  
  34. fluid.defaults("gpii.dataSource.urlExpander", {
  35. gradeNames: ["fluid.littleComponent"],
  36. vars: {
  37. db: ""
  38. }
  39. });
  40.  
  41. fluid.demands("gpii.dataSource.urlExpander", "gpii.development", {
  42. options: {
  43. vars: {
  44. db: path.join(__dirname)
  45. }
  46. }
  47. });
  48.  
  49. fluid.demands("gpii.dataSource.get", "gpii.development", {
  50. funcName: "gpii.dataSource.FSGet",
  51. args: [
  52. "{dataSource}.options.responseParser",
  53. "{dataSource}.resolveUrl",
  54. "{arguments}.0",
  55. "{arguments}.1",
  56. "{arguments}.2"
  57. ]
  58. });
  59.  
  60. fluid.demands("gpii.dataSource.get", "gpii.production", {
  61. funcName: "gpii.dataSource.DBGet",
  62. args: [
  63. "{dataSource}.options.responseParser",
  64. "{dataSource}.options.host",
  65. "{dataSource}.options.port",
  66. "{dataSource}.resolveUrl",
  67. "{arguments}.0",
  68. "{arguments}.1",
  69. "{arguments}.2"
  70. ]
  71. });
  72.  
  73. fluid.demands("gpii.dataSource.resolveUrl", "gpii.dataSource", {
  74. funcName: "gpii.dataSource.resolveUrl",
  75. args: ["{urlExpander}", "{dataSource}.options.url", "{dataSource}.options.termMap", "{arguments}.0"]
  76. });
  77.  
  78. gpii.dataSource.DBGet = function (responseParser, host, port, resolveUrl, directModel, callback, errorCallback) {
  79. var path = resolveUrl(directModel);
  80. http.get({
  81. host: host,
  82. port: port,
  83. path: path
  84. }, function (res) {
  85. var data = JSON.parse(res.body);
  86. if (responseParser) {
  87. data = typeof responseParser === "string" ?
  88. fluid.invokeGlobalFunction(responseParser, [data, directModel]) : responseParser(data, directModel);
  89. }
  90. callback(data);
  91. }).on("error", function (error) {
  92. errorCallback(error.message, error);
  93. });
  94. };
  95.  
  96. gpii.dataSource.FSGet = function (responseParser, resolveUrl, directModel, callback, errorCallback) {
  97. var fileName = resolveUrl(directModel);
  98. fs.readFile(fileName, function (error, data) {
  99. if (error) {
  100. errorCallback(error.message, error);
  101. }
  102. data = JSON.parse(data);
  103. if (responseParser) {
  104. data = typeof responseParser === "string" ?
  105. fluid.invokeGlobalFunction(responseParser, [data, directModel]) : responseParser(data, directModel);
  106. }
  107. callback(data);
  108. });
  109. };
  110.  
  111. gpii.dataSource.resolveUrl = function (urlExpander, url, termMap, directModel) {
  112. var map = fluid.copy(termMap);
  113. map = fluid.transform(map, function (entry) {
  114. var encode = false;
  115. if (entry.indexOf(eUC) === 0) {
  116. encode = true;
  117. entry = entry.substring(eUC.length);
  118. }
  119. if (entry.charAt(0) === "%") {
  120. entry = fluid.get(directModel, entry.substring(1));
  121. }
  122. if (encode) {
  123. entry = encodeURIComponent(entry);
  124. }
  125. return entry;
  126. });
  127. var replaced = fluid.stringTemplate(url, map);
  128. replaced = urlExpander(replaced);
  129. return replaced;
  130. };
  131.  
  132. module.exports = gpii.dataSource;
  133.  
  134. })();
Advertisement
Add Comment
Please, Sign In to add comment