debuggable

 
Contact Us
 

Using REST Api’s as Models in CakePHP / A Google Calendar example

Posted on 7/5/06 by Felix Geisendörfer

Deprecated post

The authors of this post have marked it as deprecated. This means the information displayed is most likely outdated, inaccurate, boring or a combination of all three.

Policy: We never delete deprecated posts, but they are not listed in our categories or show up in the search anymore.

Comments: You can continue to leave comments on this post, but please consult Google or our search first if you want to get an answer ; ).

Before I forget: Thx to ros-soft for helping me with the https authentication function. Without him I might have lost my sanity after 2 hours of debugging and would have not been able to continue this blog here ; ).

Recently I've started using Google Calendar a lot. Not for everything, but for things like tracking the time I spent on projects of mine amongst other things. That's how this looks in my calendar (green are working hours, blue is private stuff):

calendar-example.png

Now after a while I realized that it would be interesting to list all the hours I spent on the projects I track in my "Working Hours" calendar (green). Unfortunatly Google does not yet offer such analytic tools for Google Calendar so I checked out the Google Calendar Data API to see how difficult it would be to fix this. Step by step I slowly began to play with it, and to create a CakePHP Model that I could use. Finally it became good enough for what I orginally needed, the list of projects, together with the hours that went into it:

project-list.png

So I decided to write a little tutorial about working with REST API's, like the Google Calendar one, and to publish the code I've written so far. It even has authentication build in, as well as a findAll function, But no way to add events to the calendar so far. Anyway for the people who are interested in stuff like this:

Here is a little snipped that shows how easy the usage is in the Controller:

class CalendarController extends AppController
{
    var $name = 'Calendar';
    var $uses = array('GoogleCalendar');
   
    // Replace this with the feedUrl you want to work with
    var $calURL = 'http://www.google.com/calendar/feeds/xxx/private-xxx/full';
   
    function beforeFilter()
    {
        $this->GoogleCalendar->authenticate('youremail@gmail.com', 'yourpassword', 'Company-App-Version');
    }
   
    function index()
    {        
        $items = $this->GoogleCalendar->findAll($this->calURL, array(), 1024, '+30 minutes');

        debug($items);    
    }
}

I hope that inspires people to do similar things with other API's in future ; ).

--Felix Geisendörfer aka the_undefined

 
&nsbp;

You can skip to the end and add a comment.

Daniel Hofstetter said on May 08, 2006:

Just fyi: MagpieRSS has a built-in caching function. The only thing you have to do is to define the cache directory with:

define('MAGPIE_CACHE_DIR', 'your cache directory');



Felix Geisendörfer said on May 08, 2006:

hey daniel, thx for that information. But I like to use the cakephp caching just because I know how it works and I've got more control over what get's cached. But it's good to know for non-cakephp usage of this library.

RosSoft  said on May 08, 2006:

One question. Is the feedUrl usually the same between calls? If it is then could be a good idea setting it in another call inside beforeFilter or something like that

chillu said on May 08, 2006:

fyi: if you're working on macosx, take a look at "timelog" (http://shareware.mediaatelier.com/), a lightweight timetracking-app. it writes the data into iCal, which you can again publish to webdav etc. and subscribe to with google calendar - plus you've got some nice bevels and statusbar-info on your current project-time.

Felix Geisendörfer said on May 08, 2006:

RosSoft: Depends on what you're doing. Each of your calendars has it's own feedUrl. There is also a feedUrl for your *entire* calendar data. You can find those when you look at the settings inside Google Calendar.

Felix Geisendörfer said on May 08, 2006:

hi chillu: I have a very hard time finding sympathie for mac osx, but I looked at the app and it seems interesting. Anyway, my time managment needs are fairly simple, and step by step I'll expand the Google Calendar Model so I can build my own application on it ; ).

Shane O'Sullivan said on Aug 14, 2006:

Hi
I've begun using your Google Calendar code and it's very impressive. Have you done any more work on it since this post? For example, creating new Entries, editing them, paging, querying etc?

Thanks
Shane

Felix Geisendörfer said on Aug 14, 2006:

Hey Shane,

unfortunatly I've not done any more work on the API recently. Maybe I will at some point, but not right now. Anyway, I'm glad you like it and I hope you can extend it to do what you need it to do.

One thing that I would do if I started working with the API again, would be to refactor it in order to use the WebModel I wrote, and maybe also the RssModel. You can find all of those in here: http://cakeforge.org/snippet/detail.php?type=packagever&id=27

[...] Using REST Api’s as Models in CakePHP / A Google Calendar example — Great example of using an api in cakephp [...]

This post is too old. We do not allow comments here anymore in order to fight spam. If you have real feedback or questions for the post, please contact us.