familysearch javascript sdk

31
FAMILYSEARCH JAVASCRIPT SDK Dallan Quass

Upload: dallan-quass

Post on 02-Jul-2015

158 views

Category:

Technology


3 download

DESCRIPTION

Slides from a talk given at RootsTech 2014 and also at the FamilySearch Developers Conference in September 2014

TRANSCRIPT

Page 1: FamilySearch Javascript SDK

FAMILYSEARCHJAVASCRIPT SDK

Dallan Quass

Page 2: FamilySearch Javascript SDK

DISCLAIMERSNot official - not an official FamilySearch projectNot supported - code is provided as-isNot maintained - everything currently works...

Page 3: FamilySearch Javascript SDK

WHY AN SDK?

Page 4: FamilySearch Javascript SDK

6500 LINES OF CODE

Page 5: FamilySearch Javascript SDK

6500 LINES OF CODE

Page 6: FamilySearch Javascript SDK

WHAT DOES IT DO?REST API calls return JSON

==>

Javascript function calls return Javascript objects

Page 7: FamilySearch Javascript SDK

FEATURESREST API calls can be made using single-line Javascript functions

Authentication is a single-line call

Access token can be saved in a cookie - notify on expiration

Sets headers, handles transient errors and throttling

Convenience functions to make it easy to process the response

Convenience functions return Javascript objects

Objects have single-line $save() and $delete() functions

You can extend object prototypes to include your own functions

Page 8: FamilySearch Javascript SDK

DEPENDENCIESEither jQuery or AngularJS

No other dependencies

Page 9: FamilySearch Javascript SDK
Page 10: FamilySearch Javascript SDK
Page 11: FamilySearch Javascript SDK

DIVERSIONPROMISES

ARE AWESOME!

Page 12: FamilySearch Javascript SDK

MR. JAVA CALLS MISS REST ABOUT THEPARTY

Mr. Java: Miss REST, what time is the party?

Miss REST: I'll know in about an hour

Mr. Java: I'll hold

An hour goes by

Miss REST: The party is at 7pm

Mr. Java: Thanks!

Mr. Java plans for the party

Page 13: FamilySearch Javascript SDK

MR. JAVASCRIPT TEXTS MISS REST ABOUTTHE PARTY

Mr. Javascript: Miss REST, would you text me back when you knowthe time of the party?

Miss REST: I promise I will

Mr. Javascript works on his family history

An hour goes by

Miss REST: The party is at 7pm

When Mr. Javascript is at a stopping spot in his family history, heplans for the party

Page 14: FamilySearch Javascript SDK

THINK OF A PROMISE AS A SINGLE-USEEVENT BUS

var promise = REST.asyncFunction();

promise.then(function(response) { // do something with the response});

function asyncFunction() { var deferred = $.Deferred();

setTimeout(function() { deferred.resolve("Response"); }, 60*1000);

return deferred.promise();}

Page 15: FamilySearch Javascript SDK

HANDLING REJECTIONvar promise = REST.asyncFunction();

promise.then(function(response) { // do something with the response}, function(errorResponse) { // handle the error});

function asyncFunction() { var deferred = $.Deferred();

setTimeout(function() { deferred.reject("Error"); }, 60*1000);

return deferred.promise();}

Page 16: FamilySearch Javascript SDK

PROMISES CAN BE NESTEDMr. Javascript: Miss REST, would you text me back when you know

the time of the party?

Miss REST: I promise I will

Mr. Javascript works on his family history

Miss REST asks her friend Miss Party Planner to fulfill herpromise to Mr. Javascript

Miss REST: Miss Party Planner, would you text Mr. Javascript whenyou know the time of the party?

Miss Party Planner: I promise I will

An hour goes by

Miss Party Planner: Mr. Javascript, The party is at 7pm

When Mr. Javascript is at a stopping spot in his family history, heplans for the party

Page 17: FamilySearch Javascript SDK

EXAMPLEvar promise = REST.asyncFunction();

promise.then(function(response) { // do something with the response});

function asyncFunction() { return $http('first URL').then(function(firstResponse) { var secondURL = fn(firstResponse); return $http(secondURL); });}

Page 18: FamilySearch Javascript SDK

PROMISES CAN BE AGGREGATEDMr. Javascript: Miss REST, would you let me know when you find out

what Amy and Bill are wearing to the party?

Miss REST: I promise I will

Miss REST texts Amy and Bill asking them what they are wearing

Once Miss REST hears back from both Amy and Bill, she fulfillsher promise to Mr. Javascript

Page 19: FamilySearch Javascript SDK

EXAMPLEvar promise = REST.asyncFunction();

promise.then(function(amyResponse, billResponse) { // do something with the responses});

function asyncFunction() { var amyPromise = amyAsyncFunction(); var billPromise = billAsyncFunction(); return $.when(amyPromise, billPromise);}

Page 20: FamilySearch Javascript SDK

ENOUGH TALKLet's Code!

Page 21: FamilySearch Javascript SDK

1. INITIALIZEFamilySearch.init({ app_key: 'WCQY-7J1Q-GKVV-7DNM-SQ5M-9Q5H-JX3H-CMJK', environment: 'sandbox', auth_callback: 'http://localhost:9000/#!/auth', http_function: $http, deferred_function: $q.defer, timeout_function: $timeout, save_access_token: true, auto_expire: true, auto_signin: true});

Page 22: FamilySearch Javascript SDK

2. AUTHENTICATEFamilySearch.getAccessToken().then(function() { // user is authenticated - OAuth dance is complete});

Page 23: FamilySearch Javascript SDK

3. MAKE API CALLSFamilySearch.getCurrentUserPersonId() .then(function(id) { FamilySearch.getAncestry(id) .then(function(response) { var persons = response.getPersons(); // do something with the Person objects }); });

FamilySearch.Person.prototype.isMale = function() { return this.display.gender === 'Male';};

Page 24: FamilySearch Javascript SDK

ANOTHER EXAMPLEfunction createMemoryForPerson(file, person) { var fd = new FormData(); fd.append('artifact', file); // create memory return FamilySearch.createMemory(fd).then(function(memoryId) {

// read memory to get memory artifact URL (memory.about) return FamilySearch.getMemory(memoryId).then(function(response) { var memory = response.getMemory();

// create a memory persona from the person's name and the memory artifact URL var memoryPersona = new FamilySearch.MemoryPersona(person.$getDisplayName(), memory.about);

// add the memory persona to the memory return memory.$addMemoryPersona(memoryPersona).then(function(memoryPersonaRef) {

// add the memory persona reference to the person return person.$addMemoryPersonaRef(memoryPersonaRef).then(function() { // success! return memory; }); }); }); });};

Page 25: FamilySearch Javascript SDK

PLUMBING & PORCELAIN

Page 26: FamilySearch Javascript SDK

PLUMBINGFamilySearch.get('/platform/memories/memories/'+ urlencode(memoryId)) .then(function(response) { // process response });

Page 27: FamilySearch Javascript SDK

USING THE SDKGithub repository

http://github.com/rootsdev/familysearch-javascript-sdk

Documentation

http://rootsdev.org/familysearch-javascript-sdk

Downloadable code

http://rootsdev.org/familysearch-javascript-sdk/familysearch-javascript-sdk.js

Page 28: FamilySearch Javascript SDK

CONTRIBUTINGPULL REQUESTS ARE WELCOME!

Page 29: FamilySearch Javascript SDK

COOL THINGS ABOUT THEBUILD WORKFLOW

jsHintKarmaTravis-CIGruntgrunt-ngdocs

Page 30: FamilySearch Javascript SDK

ROOTSDEVRootsDev: loose organization of software engineers interested in

creating software for family history.

github organization: a low-volume google group with often-interesting discussions

github.com/rootsdev

Please join us!rootsdev.org

Page 31: FamilySearch Javascript SDK

THE ENDSlides are at https://github.com/DallanQ/rootstech-2014-fs-js-

sdk-slides