Don Park avatar Don Park committed 50b5815

Convert to couchrest

Comments (0)

Files changed (3)

sinatra/lib/geomena/couch.rb

-require 'json'
-require 'net/http'
+require 'couchrest'
 
 class GCouch
-  def self.conn(&blk)
-    Net::HTTP.start('localhost', 5984) do |http|
-      yield http
-    end
-  end
-  
+  @@db = CouchRest.database!("http://127.0.0.1:5984/geomena")
+  @@logdb = CouchRest.database!("http://127.0.0.1:5984/logs")
+
   def self.get_observation_count_for_mac(macaddr) 
-    body = conn do |http|
-      getparams = "key=#{macaddr.to_json}&group=true"
-      http.get("/geomena/_design/observations/_view/count?#{URI.escape(getparams)}")
-    end.body
-    rows = JSON.parse(body)["rows"]
+    rows = @@db.view('observations//count', {:key => macaddr, :group => true})["rows"]
     rows.first["value"] if rows.size > 0
   end
 
   def self.get_by_mac(macaddr) 
-    conn do |http|
-      startkey=[macaddr, "2038"].to_json
-      endkey=[macaddr, ""].to_json
-      getparams = "startkey=#{startkey}&endkey=#{endkey}&descending=true"
-      http.get("/geomena/_design/observations/_view/bymacdate?#{URI.escape(getparams)}")
-    end.body
+    startkey=[macaddr, "2038"]
+    endkey=[macaddr, ""]
+    ret = @@db.view('observations/bymacdate', {:startkey => startkey, :endkey => endkey, :descending => true})
+    return ret
   end
   
   def self.get_by_essid(essid) 
-    conn do |http|
-      payload = {"keys" => ["#{essid}"]}.to_json
-      http.post("/geomena/_design/observations/_view/byessid", payload)
-    end.body
+    @@db.view('observations/byessid', {:key => essid})
   end
   
-  def self.put(id, json)
-    conn do |http|
-      http.put("/geomena/#{id}", json)
-    end.body
-  end
-  
-  def self.post(json)
-    conn do |http|
-      http.post("/geomena", json)
-    end.body
+  def self.put(json)
+    @@db.save_doc(json)
   end
   
   def self.count
-    result = conn do |http|
-      http.get("/geomena/_design/observations/_view/count")
-    end.body
-    rows = JSON.parse(result)["rows"]
+    rows = @@db.view('observations/count')["rows"]
     if rows.size > 0
       rows.first["value"]
     else
   end
 
   def self.last
-    result = conn do |http|
-      http.get("/geomena/_design/observations/_view/bycreatedat")
-    end.body
+    @@db.view('observations/createdat')["rows"]
     JSON.parse(result)["rows"]
   end
 
   def self.log(essid, note)
     log = {:essid => essid, :note => note}
-    conn do |http|
-      http.post("/logs", log.to_json, {'Content-Type' => 'application/json'})
-    end.body
+    @@logdb.save_doc(log)
   end
 end

sinatra/lib/geomena/web.rb

     
     get '/ap/:mac' do |mac|
       response = GCouch.get_by_mac(mac)
+puts "response #{response.inspect}"
       ap = first_ap(response)
       ocount = GCouch.get_observation_count_for_mac(mac)
+puts ":locals => {:ap => #{ap}, :mac => #{mac}, :ocount => #{ocount}}"
       erb :apshow, :locals => {:ap => ap, :mac => mac, :ocount => ocount}
     end
     
                  :longitude => params[:longitude],
                  :essid => params[:essid]}
       if params[:id]
-        payload.merge!({:_rev => params[:rev]})
-        GCouch.put(params[:id], payload.to_json)
+        payload.merge!({:id => params[:id], :_rev => params[:rev]})
+        GCouch.put(payload)
       else
         payload.merge!({:created_at => Time.now.utc.iso8601})
-        GCouch.post(payload.to_json)
+        GCouch.put(payload)
       end
       redirect "/ap/#{params[:mac]}"
     end
     
     get '/essid/:essid' do |essid|
-      response = GCouch.get_by_essid(essid)
-      essids = JSON.parse(response)["rows"]
+      essids = GCouch.get_by_essid(essid)["rows"]
       if essids.size > 1
         erb :essids, :locals => {:essids => essids, :essid => essid}
       elsif essids.size == 1
     
     private
     
-    def first_ap(couchdata)
-      aps = JSON.parse(couchdata)
+    def first_ap(aps)
       if aps["rows"].size > 0
         aps["rows"].first["value"]
       end

sinatra/tests/geomena.rb

   def test_front_page
     get '/'
     assert last_response.ok?
-    assert_match 'Look up', last_response.body
+    assert_match 'Geomena: Wifi geolocation', last_response.body
   end
 
   def test_search_by_mac
   end
 
   def test_display_existing_ap
-    one_ap = '{"rows":[{"value":"{}"}]}'
+    latitude = 10
+    one_ap = {"rows"=>[{"value"=>{"latitude"=>latitude.to_s}}]}
     GCouch.expects(:get_by_mac).returns(one_ap)
     get '/ap/000102030405'
-    assert last_response.ok?
+    assert last_response.ok?, "HTTP Response #{last_response.status} #{last_response.body}"
     assert_match 'Access Point', last_response.body
   end
 
   def test_display_undefined_ap
     get '/ap/000102030405'
-    assert last_response.ok?
+    assert last_response.ok?, "HTTP Response #{last_response.status} #{last_response.body}"
     assert_match '(new record)', last_response.body
   end
 
   def test_w3c_locate_1
     mac = "123456abcdef"
     latitude = 10
-    one_ap = '{"rows":[{"value":{"latitude":"'+latitude.to_s+'"}}]}'
+    one_ap = {"rows"=>[{"value"=>{"latitude"=>latitude.to_s}}]}
     GCouch.expects(:get_by_mac).with(mac).returns(one_ap)
 
     extra = {"wifi_towers" => [{"mac_address" => mac}]}
     latitude1 = 10
     mac2 = "123456uvwxyz"
     latitude2 = 20
-    one_ap = '{"rows":[]}'
-    two_ap = '{"rows":[{"value":{"latitude":"'+latitude2.to_s+'"}}]}'
+    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)
 
   def test_w3c_locate_2_both_missing
     mac1 = "123456abcdef"
     mac2 = "123456uvwxyz"
-    no_ap = '{"rows":[]}'
+    no_ap = {"rows"=>[]}
     GCouch.expects(:get_by_mac).with(mac1).returns(no_ap)
     GCouch.expects(:get_by_mac).with(mac2).returns(no_ap)
 
 
   def test_post_new_ap
     mac = "12:34:56:ab:cd:f"
-    GCouch.expects(:post)
+    GCouch.expects(:put)
 
     extra = {:latitude => "45.5",
              :longitude => "-122.6",
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.