Skip to content

Instantly share code, notes, and snippets.

@grantges
Last active March 1, 2016 14:20
Show Gist options
  • Save grantges/d044d4e7f15e3ad86725 to your computer and use it in GitHub Desktop.
Save grantges/d044d4e7f15e3ad86725 to your computer and use it in GitHub Desktop.
Example Blocks for Appcelerator Arrow APIs

Block Examples for Appcelerator Arrow App

forwardgeocoder

Arrow Block for fetching latitude / longitude from an address ("borrowed" with pride from Leor Brenman)

Requires a Google API Key

  • Navigate to https://console.developers.google.com (you may need to create an account)
  • Click Enable APIs and get credentials like keys
  • Expand the More link under Google Maps APIs
  • Click Google Maps Geocoding API
  • Click the blue Enable button
  • From the left hand navigation, click on the Credentials option
  • Click the blue Create Credentials drop down, and select API Key
  • In the dialog box that pops up, select Server Key
  • Give a name for your key and hit the blue Create button (do not provide an IP address)
  • Copy your key and paste it into the variable assignment for googleMapAPIKey in the javascript code

notify_user

This block sends a push notification to specified ArrowDB Users as part of the workflow of the API.

var Arrow = require('arrow')
var request = require('request');
var googleMapAPIKey = "YOUR_GOOGLE_API_KEY_GOES_HERE";
var baseURL = "https://maps.googleapis.com/maps/api/geocode/json?address=";
var forwardGeocoder = Arrow.Block.extend({
name: 'forwardgeocoder',
description: 'ensures that latitude and longitude exist for the model',
action: function (req, resp, next) {
console.log('forwardGeocoder');
console.log(resp.body);
var body = JSON.parse(resp.body);
var data = body[body.key];
var dataLen = data.length;
var replies = 0;
if(dataLen){ //findAll
data.forEach(function (_row, _index) {
console.log(prepareAddress(_row.address));
var url=baseURL+encodeURIComponent(prepareAddress(_row.address))+"&key="+googleMapAPIKey;
console.log(url);
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var res = JSON.parse(body);
_row.latitude = res.results[0].geometry.location.lat;
_row.longitude = res.results[0].geometry.location.lng;
replies++;
if(replies == dataLen) {
setReply();
}
} else {
console.log("google maps geocode request error");
replies++;
if(replies == dataLen) {
setReply();
}
}
});
});
} else { //findOne
var url=baseURL+encodeURIComponent(prepareAddress(data.BillingStreet))+"&key="+googleMapAPIKey;
request(url, function (error, response, body) {
if (!error && response.statusCode == 200) {
var res = JSON.parse(body);
data.latitude = res.results[0].geometry.location.lat;
data.longitude = res.results[0].geometry.location.lng;
setReply();
} else {
console.log("google maps geocode request error");
setReply();
}
});
}
function prepareAddress(str) {
return str.replace(/(\r\n|\n|\r)/gm,",");
}
function setReply() {
console.log("post block addgps executed");
resp.success(body[body.key], next);
}
}
});
module.exports = forwardGeocoder;
/**
* This block sends notifications to recipients to let them know that a new message has been
* sent to them.
* @block
*/
var Arrow = require('arrow');
var notifyUser = Arrow.Block.extend({
name: 'notify_user',
description: 'sends a push notification to the intended user(s)',
action: function (req, resp, next) {
/**
* Get a handle to the associated arrowDB instance
*/
var arrowDB = Arrow.getConnector('appc.arrowdb').baseDB;
console.log('BLOCK::notify-user is sending push notificaitons to the following ids: '+ req.params.to_ids);
/**
* Make sure that there are IDs to send to first
*/
if(req.params.to_ids !== '' && req.params.to_ids !== null){
/**
* Send Push Notification using ArrowDB API for Push Notifications.
*
* Sample Code can be found here:
* http://docs.appcelerator.com/arrowdb/latest/#!/api/PushNotifications-method-notify
*/
arrowDB.pushNotificationsNotify({
channel: 'messages',
to_ids: req.params.to_ids,
payload: 'You have a new message!'
}, function(err, result) {
if (err) {
/**
* if there is an error, capture it, but don't fail the request as the
* message was created.
*/
console.error(err.message);
next();
} else {
/**
* Success!
*/
console.log('Notification sent successfully to -> '+ req.params.to_ids);
next();
}
});
} // endif
}
});
module.exports = notifyUser;
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment