Commits

Don Park committed 8e02310

handle zero and more than one AP to search by

  • Participants
  • Parent commits b9e420b

Comments (0)

Files changed (2)

File sinatra/lib/geomena/web.rb

     post '/' do
       geodata = JSON.parse(request.body.read)
       @@log.info("W3C Lookup: #{geodata}")
-      return if geodata["wifi_towers"].nil?
-      macaddr = mac_clean(geodata["wifi_towers"].first["mac_address"])
-      response = GCouch.get_by_mac(macaddr)
-      ap = first_ap(response)
-      if ap
-        answer = {"location" => {"latitude" => ap["latitude"], 
-                                 "longitude" => ap["longitude"],
-                                 "accuracy" => 150},
-                                 "access_token" => "abc123"}
-        GCouch.log(macaddr,"success")
-        return answer.to_json
-      else
-        GCouch.log(macaddr,"failure")
+      towers = geodata["wifi_towers"]
+      return if towers.nil? || towers.size == 0
+      towers.each do |tower|
+        macaddr = mac_clean(tower["mac_address"])
+        response = GCouch.get_by_mac(macaddr)
+        ap = first_ap(response)
+        if ap
+          answer = {"location" => {"latitude" => ap["latitude"], 
+                                   "longitude" => ap["longitude"],
+                                   "accuracy" => 150},
+                                   "access_token" => "abc123"}
+          GCouch.log(macaddr,"success")
+          return answer.to_json
+        else
+          GCouch.log(macaddr,"failure")
+        end
       end
+      return ""
     end
     
     get '/ap/:mac' do |mac|

File sinatra/tests/geomena.rb

     assert_match '(new record)', last_response.body
   end
 
-  def test_w3c_locate
+  def test_w3c_locate_1
     mac = "123456abcdef"
     latitude = 10
     one_ap = '{"rows":[{"value":{"latitude":"'+latitude.to_s+'"}}]}'
     assert_match '"latitude":"'+latitude.to_s+'"', last_response.body
   end
 
+  def test_w3c_locate_2_one_missing
+    mac1 = "123456abcdef"
+    latitude1 = 10
+    mac2 = "123456uvwxyz"
+    latitude2 = 20
+    one_ap = '{"rows":[]}'
+    two_ap = '{"rows":[{"value":{"latitude":"'+latitude2.to_s+'"}}]}'
+    GCouch.expects(:get_by_mac).with(mac1).returns(one_ap)
+    GCouch.expects(:get_by_mac).with(mac2).returns(two_ap)
+
+    extra = {"wifi_towers" => [{"mac_address" => mac1}, {"mac_address" => mac2}]}
+    post "/", extra.to_json
+    assert_match '"latitude":"'+latitude2.to_s+'"', last_response.body
+  end
+
+  def test_w3c_locate_2_both_missing
+    mac1 = "123456abcdef"
+    mac2 = "123456uvwxyz"
+    no_ap = '{"rows":[]}'
+    GCouch.expects(:get_by_mac).with(mac1).returns(no_ap)
+    GCouch.expects(:get_by_mac).with(mac2).returns(no_ap)
+
+    extra = {"wifi_towers" => [{"mac_address" => mac1}, {"mac_address" => mac2}]}
+    post "/", extra.to_json
+puts last_response.body
+    assert_match "", last_response.body
+  end
+
+  def test_w3c_locate_empty
+    extra = {"wifi_towers" => []}
+    post "/", extra.to_json
+    assert_match "", last_response.body
+  end
+
   ## WRITE
 
   def test_post_new_ap