Source

geomena / sinatra / lib / geomena / couch.rb

Full commit
require 'couchrest'

class GCouch
  @@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) 
    rows = @@db.view('observations//count', {:key => macaddr, :group => true})["rows"]
    rows.first["value"] if rows.size > 0
  end

  def self.get_by_mac(macaddr) 
    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) 
    @@db.view('observations/byessid', {:key => essid})
  end
  
  def self.put(json)
    @@db.save_doc(json)
  end
  
  def self.count
    rows = @@db.view('observations/count')["rows"]
    if rows.size > 0
      rows.first["value"]
    else
      0
    end
  end

  def self.last
    @@db.view('observations/createdat')["rows"]
    JSON.parse(result)["rows"]
  end

  def self.log(essid, note)
    log = {:essid => essid, :note => note}
    @@logdb.save_doc(log)
  end
end