Skip to content

Instantly share code, notes, and snippets.

/.rb Secret

Created April 24, 2016 19:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save anonymous/e25683e35199336250617c10bb65d112 to your computer and use it in GitHub Desktop.
Save anonymous/e25683e35199336250617c10bb65d112 to your computer and use it in GitHub Desktop.
# I have this API call stored in a Hashie Map called info
# Right now my logic to parse the first and last name this is something like this
# I'm looking for a better way to accomplish this.
# Basically I don't know exactly what I'll get back. Sometimes a feild will be included in the query, but sometimes not.
# I do it this way so if info.person.name is not returned I won't thow an name is not defined error
if info.person && info.person.name
if info.person.name.givenName
firstname = info.person.name.givenName
end
if info.person.name.familyName
lastname = info.person.name.familyName
end
end
info = {
"person": {
"id": "d54c54ad-40be-4305-8a34-0ab44710b90d",
"name": {
"fullName": "Alex MacCaw",
"givenName": "Alex",
"familyName": "MacCaw"
},
"email": "alex@alexmaccaw.com",
"//": "..."
},
"company": {
"id": "c5a6a9c5-303a-455a-935c-9dffcd2ed756",
"name": "Alex MacCaw",
"legalName": "Alex MacCaw",
"domain": "alexmaccaw.com",
"//": "..."
}
}
@smathy
Copy link

smathy commented Apr 24, 2016

In ruby < 2.3:

if person = info.person and name = person.name
  firstname, lastname = name.givenName, name.familyName
end

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment