Skip to content

Instantly share code, notes, and snippets.

@thomaswitt
Last active January 4, 2016 02:29
Show Gist options
  • Save thomaswitt/8555845 to your computer and use it in GitHub Desktop.
Save thomaswitt/8555845 to your computer and use it in GitHub Desktop.
Rails Controller for the Meraki Presence API. You need to generate a models for saving these into the database via: rails generate model presence ap_mac:string client_mac:string last_seen:datetime rssi:integer
# 1. Generate a model Presence before:
# rails generate model presence ap_mac:string client_mac:string last_seen:datetime rssi:integer
#
# 2. Add a route:
# match 'meraki', :to => 'meraki#presenceapi', :as => :meraki
#
# 3. Test this controller via:
# curl -d data="{\"secret\":\"foobar\",\"probing\":[{\"ap_mac\":\"00:aa:bb:cc:dd:ee\",\"client_mac\":\"ff:ee:dd:cc:bb:aa\",\"last_seen\":\"Wed Jan 22 08:34:02.409 UTC 2014\",\"rssi\":\"16\"},{\"ap_mac\":\"11:22:33:44:55:66\",\"client_mac\":\"99:88:77:66:55:44\",\"last_seen\":\"Wed Jan 22 08:34:06.409 UTC 2014\",\"rssi\":\"29\"}]}" https://localhost:3000/meraki
#
# 4. Point Meraki to https://my.app.name/presenceapi
MERAKI_VALIDATOR = '12345'
MERAKI_SECRET = 'foobar'
class MerakiController < ApplicationController
skip_before_filter :verify_authenticity_token
def presenceapi
if request.post?
map = JSON.parse(params[:data]).with_indifferent_access
if map[:secret] == MERAKI_SECRET
map[:probing].each do |p|
Presence.create(ap_mac: p[:ap_mac], client_mac: p[:client_mac],
last_seen: DateTime.parse(p[:last_seen]),
rssi: p[:rssi])
end
render :text => '' and return
else
Rails::logger.warn("*** Meraki req with bad secret '#{map[:secret]}'")
raise 'access denied'
end
else
render :text => MERAKI_VALIDATOR
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment