unit22

Untitled

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