Last active
August 29, 2015 14:24
-
-
Save joshbirk/44d46349b7530555f00a to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //stop previous jobs | |
| List<CronTrigger> jobs = [SELECT ID from CronTrigger]; | |
| for (CronTrigger j : jobs) { | |
| System.abortJob(j.ID); | |
| } | |
| //create new ones | |
| System.schedule('Getting Kittens1', '0 5 * * * ?', new FlickrHandler()); | |
| System.schedule('Getting Kittens2', '0 15 * * * ?', new FlickrHandler()); | |
| System.schedule('Getting Kittens3', '0 45 * * * ?', new FlickrHandler()); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| //Schedulable interface allows for the class to be called on schedule | |
| public with sharing class FlickrHandler implements Schedulable { | |
| //this matches how Flickr JSON is formed | |
| public class FlickrList { | |
| public string title {get; set;} | |
| public string link {get; set;} | |
| public List<FlickrData> items {get; set;} | |
| } | |
| //this matches how Flickr JSON is formed | |
| public class FlickrData { | |
| public string title {get; set;} | |
| public string link {get; set;} | |
| public FlickrImage media {get; set;} | |
| public string author {get; set;} | |
| public string description {get; set;} | |
| } | |
| //this matches how Flickr JSON is formed | |
| public class FlickrImage { | |
| public string m {get; set;} | |
| } | |
| public FlickrList data {get; set;} | |
| //constructor as scheduled task or page controller | |
| public FlickrHandler() { | |
| if(ApexPages.currentPage() != null) { | |
| if(ApexPages.currentPage().getParameters().containsKey('tag')) { data = getFlickrData(ApexPages.currentPage().getParameters().get('tag')); } | |
| else {data = FlickrHandler.getFlickrData('Cute Kittens');} | |
| } | |
| } | |
| //constructor for extension to page controller | |
| public FlickrHandler(ApexPages.StandardController stc) { | |
| if(ApexPages.currentPage().getParameters().containsKey('tag')) { data = getFlickrData(ApexPages.currentPage().getParameters().get('tag')); } | |
| else {data = FlickrHandler.getFlickrData('Cute Kittens');} | |
| } | |
| //Called by the schedulable interface on execution | |
| public void execute(SchedulableContext context) { | |
| List<Flickr_Image__c> images = [SELECT ID from Flickr_Image__c]; | |
| delete images; | |
| FlickrHandler.saveFlickrData(); | |
| } | |
| //HTTP callouts are not allowed from scheduled tasks synchronously | |
| //@future makes it an asynchronous callout | |
| @future(callout=true) | |
| public static void saveFlickrData() { | |
| List<Flickr_Image__c> images = new List<Flickr_Image__c>(); | |
| FlickrList result = FlickrHandler.getFlickrData('Cute Kittens'); | |
| for(FlickrData d : result.items) { | |
| //save the medium size url | |
| images.add(new Flickr_Image__c(URL__c=d.media.m)); | |
| } | |
| insert images; | |
| } | |
| //the actual HTTP callout | |
| public static FlickrList getFlickrData(string tag) { | |
| HttpRequest req = new HttpRequest(); | |
| req.setMethod('GET'); | |
| req.setEndpoint('http://api.flickr.com/services/feeds/photos_public.gne?nojsoncallback=1&format=json&tags='+tag); | |
| HTTP http = new HTTP(); | |
| HTTPResponse res = http.send(req); | |
| return (FlickrList)JSON.deserialize(res.getBody().replace('\\\'',''),FlickrList.class); | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <apex:page standardController="Flickr_Image__c" recordSetVar="images"> | |
| <apex:repeat value="{!images}" var="img"> | |
| <div style="position: inline; float: left;"> | |
| <img src="{!img.URL__c}" /> | |
| </div> | |
| </apex:repeat> | |
| </apex:page> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment