[gnome-maps/wip/routing2: 4/9] HTTP: add a HTTP helper module
- From: Mattias Bengtsson <mattiasb src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-maps/wip/routing2: 4/9] HTTP: add a HTTP helper module
- Date: Fri, 2 May 2014 00:10:15 +0000 (UTC)
commit 19d61a3c438d01cc01efa839dfbb6f1d39cbab57
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 | 74 +++++++++++++++++++++++++++++++++++++++
2 files changed, 75 insertions(+), 0 deletions(-)
---
diff --git a/src/gnome-maps.js.gresource.xml b/src/gnome-maps.js.gresource.xml
index e2dc2fa..65f569c 100644
--- a/src/gnome-maps.js.gresource.xml
+++ b/src/gnome-maps.js.gresource.xml
@@ -6,6 +6,7 @@
<file>contextMenu.js</file>
<file>geoclue.js</file>
<file>layersPopover.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..183cfcd
--- /dev/null
+++ b/src/http.js
@@ -0,0 +1,74 @@
+/* -*- 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) {
+ if(!Utils.hasValue(obj))
+ return null;
+ return 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]);
+ }
+ },
+
+ 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(Utils.hasValue(encValue))
+ 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]