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 2d8e65ead4
commit ae4ca59526

View file

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