[gnome-maps/wip/routing2: 1/7] HTTP: add a HTTP helper module



commit 6135cc271439fd56b42ca691b69151760f291684
Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
Date:   Sun Aug 25 02:03:25 2013 +0200

    HTTP: add a HTTP helper module
    
    Http.Query is a class for easy construction of HTTP query-lines,
    that is the part after '?' and before '#' in an HTTP URL.
    
    https://bugzilla.gnome.org/show_bug.cgi?id=728695

 src/gnome-maps.js.gresource.xml |    1 +
 src/http.js                     |   75 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
index 993c82c..07aa483 100644
--- a/src/gnome-maps.js.gresource.xml
+++ b/src/gnome-maps.js.gresource.xml
@@ -6,6 +6,7 @@
     <file>contextMenu.js</file>
     <file>epaf.js</file>
     <file>geoclue.js</file>
+    <file>http.js</file>
     <file>layersPopover.js</file>
     <file>main.js</file>
     <file>mainWindow.js</file>
diff --git a/src/http.js b/src/http.js
new file mode 100644
index 0000000..6535a86
--- /dev/null
+++ b/src/http.js
@@ -0,0 +1,75 @@
+/* -*- Mode: JS2; indent-tabs-mode: nil; js2-basic-offset: 4 -*- */
+/* vim: set et ts=4 sw=4: */
+/*
+ * Copyright (c) 2013 Mattias Bengtsson.
+ *
+ * GNOME Maps is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by the
+ * Free Software Foundation; either version 2 of the License, or (at your
+ * option) any later version.
+ *
+ * GNOME Maps is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
+ * or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
+ * for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with GNOME Maps; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
+ *
+ * Author: Mattias Bengtsson <mattias jc bengtsson gmail com>
+ */
+
+const Soup = imports.gi.Soup;
+
+const Lang = imports.lang;
+
+const Utils = imports.utils;
+
+function encode(obj) {
+    return obj === null
+        ? null
+        : Soup.URI.encode(obj.toString(), null);
+}
+
+const Query = new Lang.Class({
+    Name: 'Query',
+
+    _init: function(obj) {
+        this._query = {};
+        for(let key in obj) {
+            this.add(key, obj[key]);
+        }
+    },
+
+    // a value === null represents an empty value
+    add: function(key, value) {
+        // Initialize query field if it isn't already
+        if(!Utils.isArray(this._query[key])) {
+            this._query[key] = [];
+        }
+
+        if(Utils.isArray(value)) {
+            this._query[key] = this._query[key].concat(value);
+        }
+        else {
+            this._query[key].push(value);
+        }
+    },
+
+    toString: function() {
+        let vars = [];
+        for(let key in this._query) {
+            let values = this._query[key];
+            let encKey = encode(key);
+            values.forEach(function(value) {
+                let encValue = encode(value);
+                if(encValue !== null)
+                    vars.push([encKey, encValue].join('='));
+                else
+                    vars.push(encKey);
+            });
+        }
+        return vars.join('&');
+    }
+});


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]