[gnome-maps/wip/routing: 61/65] HTTP: add a HTTP helper module



commit 2987299d0f16c2ffe9878a9d28843096ac93144e
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.

 src/gnome-maps.js.gresource.xml |    1 +
 src/http.js                     |   72 +++++++++++++++++++++++++++++++++++++++
 2 files changed, 73 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
index 957c575..4294679 100644
--- a/src/gnome-maps.js.gresource.xml
+++ b/src/gnome-maps.js.gresource.xml
@@ -5,6 +5,7 @@
     <file>config.js</file>
     <file>contextMenu.js</file>
     <file>geoclue.js</file>
+    <file>http.js</file>
     <file>main.js</file>
     <file>mainWindow.js</file>
     <file>mapLocation.js</file>
diff --git a/src/http.js b/src/http.js
new file mode 100644
index 0000000..0401cd3
--- /dev/null
+++ b/src/http.js
@@ -0,0 +1,72 @@
+/* -*- 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) {
+    let str = Utils.isDefined(obj)
+            ? obj.toString()
+            : '';
+    return Soup.URI.encode(str, null);
+}
+
+const Query = new Lang.Class({
+    Name: 'HTTPQuery',
+
+    // _query: {},
+
+    _init: function(obj) {
+        this._query = {};
+        for(let key in obj) {
+            this.add(key, obj[key]);
+        }
+    },
+
+    add: function(key, value) {
+        if(typeof this._query[key] === 'undefined') {
+            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 = [];
+        // log(JSON.stringify(this._query));
+        for(let key in this._query) {
+            let values = this._query[key];
+            let encKey = encode(key);
+            values.forEach(function(value) {
+                vars.push([encKey, encode(value)].join('='));
+            });
+        }
+        return vars.join('&');
+    }
+});


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