fix(sidebar): keep trying on 202s

Also fixed a bug where the email could come back with different
capitalization causing a cache miss
This commit is contained in:
Evan Morikawa 2016-03-11 16:27:04 -05:00
parent 5ecc67b42b
commit 9cbe002a8b

View file

@ -1,11 +1,15 @@
# This file is in coffeescript just to use the existential operator! # This file is in coffeescript just to use the existential operator!
{AccountStore, EdgehillAPI} = require 'nylas-exports' {AccountStore, EdgehillAPI} = require 'nylas-exports'
MAX_RETRY = 10
module.exports = class ClearbitDataSource module.exports = class ClearbitDataSource
clearbitAPI: -> clearbitAPI: ->
return "https://person.clearbit.com/v2/combined" return "https://person.clearbit.com/v2/combined"
find: ({email}) -> find: ({email, tryCount}) ->
if (tryCount ? 0) >= MAX_RETRY
return Promise.resolve(null)
tok = AccountStore.tokenForAccountId(AccountStore.accounts()[0].id) tok = AccountStore.tokenForAccountId(AccountStore.accounts()[0].id)
new Promise (resolve, reject) => new Promise (resolve, reject) =>
EdgehillAPI.request EdgehillAPI.request
@ -14,34 +18,41 @@ module.exports = class ClearbitDataSource
pass: "" pass: ""
path: "/proxy/clearbit/#{@clearbitAPI()}/find?email=#{email}", path: "/proxy/clearbit/#{@clearbitAPI()}/find?email=#{email}",
success: (body, response) => success: (body, response) =>
resolve(@parseResponse(body, response, email)) @parseResponse(body, response, email, tryCount).then(resolve).catch(reject)
error: reject error: reject
# The clearbit -> Nylas adapater # The clearbit -> Nylas adapater
parseResponse: (body={}, response, requestedEmail) -> parseResponse: (body={}, response, requestedEmail, tryCount=0) =>
# This means it's in the process of fetching. Return null so we don't new Promise (resolve, reject) =>
# cache and try again. # This means it's in the process of fetching. Return null so we don't
if response.statusCode isnt 200 # cache and try again.
return null if response.statusCode is 202
setTimeout =>
@find({email: requestedEmail, tryCount: tryCount+1}).then(resolve).catch(reject)
, 1000
return
else if response.statusCode isnt 200
resolve(null)
return
person = body.person person = body.person
# This means there was no data about the person available. Return a # This means there was no data about the person available. Return a
# valid, but empty object for us to cache. This can happen when we # valid, but empty object for us to cache. This can happen when we
# have company data, but no personal data. # have company data, but no personal data.
if not person if not person
person = {email: requestedEmail} person = {email: requestedEmail}
return { resolve({
cacheDate: Date.now() cacheDate: Date.now()
email: person.email # Used as checksum email: requestedEmail # Used as checksum
bio: person.bio ? person.twitter?.bio ? person.aboutme?.bio, bio: person.bio ? person.twitter?.bio ? person.aboutme?.bio,
location: person.location ? person.geo?.city location: person.location ? person.geo?.city
currentTitle: person.employment?.title, currentTitle: person.employment?.title,
currentEmployer: person.employment?.name, currentEmployer: person.employment?.name,
profilePhotoUrl: person.avatar, profilePhotoUrl: person.avatar,
socialProfiles: @_socialProfiles(person) socialProfiles: @_socialProfiles(person)
} })
_socialProfiles: (person={}) -> _socialProfiles: (person={}) ->
profiles = {} profiles = {}