How do I exclude data from an API call that have a value of nil in Ruby? -
i'm trying pull user data nationbuilder api display on google maps. need latitude , longitude of these users, , if address given, nationbuilder provide 'lat' , 'lng' values in json hash. of users in database don't have lat , lng values, makes me error when ask lat , lng of each user.
in api call, want able exclude people have lat , lng value of nil aren't rendered stop me getting error.
here's call looks currently:
users = client.call(:people_tags, :people, tag: 'mapped')["results"] users.map { |user| new(user) }
please let me know if need more information answer question.
you reject users lat
or long
nil
:
users_with_location = users.reject { |user| user.lat.nil? || user.long.nil? }
or reject in place (remove them users
variable without making copy):
users.reject! { |user| user.lat.nil? || user.long.nil? }
so entire code like:
users = client.call(:people_tags, :people, tag: 'mapped')["results"] users.reject! { |user| user.lat.nil? || user.long.nil? } users.map { |user| new(user) }
Comments
Post a Comment