Using Google Tasks to build a brewing task list

Once we've scheduled our brew day, it's always good to have a brew day task list that we can use to make sure our brewing session is a successful one. To do that, we can take advantage of the Google Tasks API to create a task list that can guide us through our brew day. The Google Tasks API is very similar to the Google Calendar API, which makes our service very similar to the one we created to interact with the Google Calendar API.

As with the Google Calendar API, we need to add another authorization scope to our Google authentication configuration so that we can gain access to the user's tasks. The following code shows the new authorization scope we need to add to our authentication service so that we can get access to the user's tasks:

.run(function (googlePlusAuthentication) {
  googlePlusAuthentication.configure({
    'clientId': '<YOUR_CLIENT_ID>',
    'scope': ['https://www.googleapis.com/auth/plus.login',
      'https://www.googleapis.com/auth/userinfo.email',
      'https://www.googleapis.com/auth/calendar',
      'https://www.googleapis.com/auth/tasks']
  });
})

With this change, we now have read-write access to the user's Google Tasks, allowing us to add new brewing tasks to the user's existing task list or one we create specifically for our application.

Our service will start out with a method that will retrieve a task list by a name that will also ensure the Google Tasks API JavaScript library is loaded, and once loaded, it will invoke another method on the API to retrieve a list of task lists for the user and then finally either retrieve the task list or create a new task list if requested.

var getTaskListByName = function(name, create){
  taskListName = name;
  createIfDoesNotExist = create;
  gapi.client.load('tasks', 'v1', handleTasksClientLoaded);
};

messaging.subscribe(events.message._GET_TASK_LIST_, getTaskListByName);

var handleTasksClientLoaded = function() {
  messaging.publish(events.message._SERVER_REQUEST_STARTED_);
  var request = gapi.client.tasks.tasklists.list();
  request.execute(handleGetTasksList);
};

var handleGetTasksList = function(resp) {
  if(resp.items.length > 0){
    angular.forEach(resp.items, function(item){
      if(item.title === taskListName){
        taskListId = item.id;
      }
    });
  }

  if (taskListId){
    getGoogleTaskListById(taskListId);
  }
  else {
    if(createIfDoesNotExist){
      createTaskList();
    }
    else {
      getTaskListFailed();
    }
  }
};

var getTaskListFailed = function(){
  messaging.publish(events.message._SERVER_REQUEST_ENDED_);
  messaging.publish(events.message._GET_TASK_LIST_FAILED_);
};

var getGoogleTaskListById = function(id){
  var request = gapi.client.tasks.tasks.list({'tasklist': id});
  request.execute(handleGetGoogleTaskListById);
};

var handleGetGoogleTaskListById = function(resp){
  if(resp.items.length > 0){
    messaging.publish(events.message._SERVER_REQUEST_ENDED_);
    messaging.publish(events.message._GET_TASK_LIST_COMPLETE_, [resp]);
  }
  else {
    getTaskListFailed();
  }
};

var createTaskList = function(){
  var request = gapi.client.tasks.tasklists.insert({"resource" 
    :{'title': taskListName}});
  request.execute(handleCreateTaskListRequest);
};

var handleCreateTaskListRequest = function(resp){
  if(resp && resp.title === taskListName){
    taskListId = resp.id;
    getGoogleTaskListById(taskListId);
  }
  else {
    getTaskListFailed();
  }
};

Again, we start out with the gapi.client.load method requesting the tasks API and pass in the callback handler, handleTasksClientLoaded, which will be called once the JavaScript library is loaded.

Once the library is loaded, we make a call to the tasks.tasklist.list method to retrieve a list of the user's task lists and pass in the callback handler. The callback handler, handleGetTaskList, will then iterate through the list looking for a matching title and then request the list by id calling getGoogleTaskListById.

The method getGoogleTaskListById makes an API call to tasks.tasks.list, passing in the id of the task list to return and passes in the callback handler, handleGetGoogleTaskListById, which in turn publishes the message _GET_TASK_LIST_COMPLETE_ with the returned task list or calls the method getTaskListFailed that publishes _GET_TASK_LIST_FAILED_.

If the task list does not exist and the caller has passed in true for the create parameter, the service will call the method createTaskList, which will call the tasks.tasklist.insert method to create the task list with the name passed into the call getTaskListByName.

The rest of the service provides a similar set of CRUD methods to allow the creation, update, and deletion of tasks associated with a given task list. Each one requires the unique id of the task list and an event that will be acted upon. The following is the code for the deleteTask method:

var deleteTask = function(taskListId, task){
  messaging.publish(events.message._SERVER_REQUEST_STARTED_);
  var request = gapi.client.tasks.tasks.delete(
    {'tasklist': taskListId, 'task': task.id});
  request.execute(handleDeleteTaskRequest);
};

messaging.subscribe(events.message._DELETE_TASK_, deleteTask);

var handleDeleteTaskRequest = function(resp){
  if(resp){
    getGoogleTaskListById(taskListId);
  }
  else {
    messaging.publish(events.message._DELETE_TASK_FAILED_);
  }
};

Again based on the method called, a corresponding call is made to the tasks.tasks.x method to insert, update, and delete a task. A callback method is provided to handle the response from the API call and if successful, a call is made to getGoogleTaskListById, which will in turn retrieve the updated list and publish the _GET_TASK_LIST_COMPLETE_ message causing all subscribers to update their copy of the task list with the one sent with the notification.

Tasks have several properties that we will use when we create our brew day tasks. The following is an example of creating a new task for our brew day task list:

var addPrepareStarterTask = function(currentRecipe, 
  scheduledDate){
  var dueDate = moment(scheduledDate).subtract('days', 1);

  var task = {'title': 'Prepare Starter: ' + currentRecipe.Name,
    'status': 'needsAction',
    'due': dueDate.format('YYYY-MM-DD')
  };

  messaging.publish(events.message._CREATE_TASK_, 
    [brewingTaskListId, task]);
};

This method starts off by calculating the due date for the task by subtracting a day from the schedule date, since the task needs to occur prior to our brew day. We then create our task object providing a title, a status, and the due date formatted in the ISO 8601 Date Format using the moment.js library. Finally, the method publishes the _CREATE_TASK_ event, passing in the unique ID of the Brewing task list and the new task to insert into the task list.

We've covered all of the functionality we'll be using in our application; however, there is more that you can do with the Google Tasks API. You can find out more about the Google Tasks API, the Task resource, and its parameters on the Google Tasks API web page at https://developers.google.com/google-apps/tasks/.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.147.13.192