Skip to content

Instantly share code, notes, and snippets.

@trevordixon
Created March 20, 2014 20:41
Show Gist options
  • Save trevordixon/9673363 to your computer and use it in GitHub Desktop.
Save trevordixon/9673363 to your computer and use it in GitHub Desktop.
ruleset twilio {
meta {
use module b505206x2 alias LocationData
}
rule nearby {
select when location new_current
pre {
checkin = LocationData:get_location_data("fs_checkin");
lata = checkin.pick("$..lat");
lnga = checkin.pick("$..lng");
latb = event:attr("lat");
lngb = event:attr("lng");
r90 = math:pi()/2;
rEm = 3959; // radius of the Earth in miles
// convert co-ordinates to radians
rlata = math:deg2rad(lata);
rlnga = math:deg2rad(lnga);
rlatb = math:deg2rad(latb);
rlngb = math:deg2rad(lngb);
// distance between two co-ordinates in radians
dR = math:great_circle_distance(rlnga,r90 - rlata, rlngb,r90 - rlatb);
// distance between two co-ordinates in kilometers
dist = math:great_circle_distance(rlnga,r90 - rlata, rlngb,r90 - rlatb, rEm);
}
{
send_directive('location') with lat = lat and lng = lng and dist = dist and cow = "bell";
}
if (dist <= 5) then noop();
fired {
raise explicit event location_nearby with dist = dist;
}
else {
raise explicit event location_far with dist = dist;
}
}
}
ruleset examine_location {
meta {
name "Examine Location Data"
description <<
Examine Location Data
>>
author ""
logging off
use module b505206x2 alias LocationData
}
rule show_fs_location is active {
select when web cloudAppSelected
pre {
checkin = LocationData:get_location_data("fs_checkin");
venue = checkin.pick("$..venue");
lat = checkin.pick("$..lat");
lng = checkin.pick("$..lng");
city = checkin.pick("$..city");
shout = checkin.pick("$..shout");
createdAt = checkin.pick("$..createdAt");
}
notify("Last Checkin", "#{venue} in #{city}. #{lat}");
}
}
ruleset foursquare_checkins {
meta {
name "Foursquare Checkins"
description <<
Foursquare Checkins
>>
author ""
logging off
use module a169x701 alias CloudRain
use module a41x186 alias SquareTag
}
dispatch {
}
global {
subscription_maps = [
{"cid":"E54CD51E-B5E9-11E3-AA95-ED47293232C8"},
{"cid":"99B7B68C-B620-11E3-A744-EAA3E71C24E1"}
];
}
rule pass_fs_checkin {
select when foursquare checkin
foreach subscription_maps setting (subscription_map)
event:send(subscription_map, "location", "notification")
with attrs = {"checkin": event:attr("checkin")};
}
rule display_checkin is active {
select when web cloudAppSelected
pre {
venue = ent:venue;
city = ent:city;
shout = ent:shout;
createdAt = ent:createdAt;
my_html = <<
<ul>
<li><strong>Venue</strong>: #{venue}</li>
<li><strong>City</strong>: #{city}</li>
<li><strong>Shout</strong>: #{shout}</li>
<li><strong>Created</strong>: #{createdAt}</li>
</ul>
>>;
}
{
SquareTag:inject_styling();
CloudRain:createLoadPanel("Latest Foursquare Checkin", {}, my_html);
}
}
rule process_fs_checkin {
select when foursquare checkin
pre {
checkin = event:attr("checkin").decode();
venue = checkin.pick("$..venue.name");
lat = checkin.pick("$..location.lat");
lng = checkin.pick("$..location.lng");
city = checkin.pick("$..location.city");
shout = checkin.pick("$..shout", true).head();
createdAt = checkin.pick("$..createdAt");
}
send_directive(venue) 
  with checkin = venue;
fired {
set ent:venue venue;
set ent:lat lat;
set ent:lng lng;
set ent:city city;
set ent:shout shout;
set ent:createdAt createdAt;
raise pds event new_location_data with
key = "fs_checkin" and
value = {
"venue": venue,
"lat": lat,
"lng": lng,
"city": city,
"shout": shout,
"createdAt": createdAt
}
}
}
}
ruleset location_catch {
rule location_catch {
select when location notification
always {
set ent:checkin event:attr("checkin").decode();
}
}
rule location_show {
select when web cloudAppSelected
pre {
checkin = ent:checkin;
venue = checkin.pick("$..venue.name");
lat = checkin.pick("$..location.lat");
lng = checkin.pick("$..location.lng");
city = checkin.pick("$..location.city");
shout = checkin.pick("$..shout", true).head();
}
notify("Last Checkin", "#{venue} in #{city}. Said #{shout}.");
}
}
ruleset b505206x2 {
meta {
name "Location Data"
description <<
Location Data
>>
author ""
logging off
provides get_location_data
}
dispatch {
}
global {
get_location_data = function(key) {
ent:location{key};
};
}
rule add_location_item {
select when pds new_location_data
pre {
key = event:attr("key");
value = event:attr("value");
}
send_directive(key) 
  with location = value;
always {
set ent:location{key} value;
}
}
}
ruleset twilio {
meta {
key twilio {
"account_sid" : "AC07f0d42e3d7ce4e9dc3105b7f318c48d",
"auth_token"  : "69e2011c54873b46733e8b1f3add6e02"
}
use module a8x115 alias twilio with twiliokeys = keys:twilio()
}
rule near {
select when explicit location_nearby
{
twilio:send_sms("+13852086983", "+13852826444", event:attr("dist") + " miles away.");
send_directive("nearby") with dist = event:attr("dist");
}
}
rule far {
select when explicit location_far
{
send_directive("far") with dist = event:attr("dist");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment