[gnome-maps/wip/osm-edit: 7/47] osmApi: Parse JSON output for OSM node objects



commit e0a374a0b523d1ee6e23a2ba42ba092cb378dc84
Author: Marcus Lundblad <ml update uu se>
Date:   Mon Jan 26 21:56:24 2015 +0100

    osmApi: Parse JSON output for OSM node objects

 src/osmConnection.js |   55 +++++++++++++++++++++++++++++++++++--------------
 1 files changed, 39 insertions(+), 16 deletions(-)
---
diff --git a/src/osmConnection.js b/src/osmConnection.js
index 2c24345..def01d4 100644
--- a/src/osmConnection.js
+++ b/src/osmConnection.js
@@ -20,6 +20,8 @@
  * Author: Marcus Lundblad <ml update uu se>
  */
 
+const OSMNode = imports.osmNode;
+
 const Lang = imports.lang;
 const Maps = imports.gi.GnomeMaps;
 const Soup = imports.gi.Soup;
@@ -47,28 +49,49 @@ const OSMConnection = new Lang.Class({
                 return;
             }
 
-           let json;
+           let json = this._parseXML(type, message.response_body);
+           let object = null;
 
-           print ('Parsing object of type: ' + type);
-           
-           switch (type) {
-           case 'node':
-               json = Maps.osm_parse_node(message.response_body.data,
-                                          message.response_body.length);
-               break;
-           default:
-               // TODO: implement parse methods for the other types as well...
-               json = message.response_body.data;
-           }
+           if (json != null)
+               object = this._createObject(type, json);
            
-            callback(true,
-                     message.status_code,
-                    json);
-        }));
+           if (object == null)
+               callback(false, message.status_code, null);
+           else
+               callback(true,
+                        message.status_code,
+                        object);
+        }).bind(this));
     },
     
     _getQueryUrl: function(type, id) {
        return BASE_URL + '/' + API_VERSION + '/' + type + '/' + id;
+    },
+
+    _parseXML: function(type, body) {
+       let jsonString;
+       
+       switch (type) {
+       case 'node':
+           jsonString = Maps.osm_parse_node(body.data, body.length);
+           break;
+       default:
+       }
+
+       return JSON.parse(jsonString);
+    },
+
+    _createObject: function(type, json) {
+       switch (type) {
+       case 'node':
+           return new OSMNode.OSMNode(json);
+       case 'way':
+           return new OSMWay.OSMWay(json);
+       case 'relation':
+           return new OSMRelation.OSMRelation(json);
+       default:
+           return null;
+       }
     }
 })
 


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