Skip to content

Instantly share code, notes, and snippets.

@mekhami
Last active February 27, 2016 18:55
Show Gist options
  • Save mekhami/97e811db30ad99815a67 to your computer and use it in GitHub Desktop.
Save mekhami/97e811db30ad99815a67 to your computer and use it in GitHub Desktop.
def get_phone_listeners(user)
stations = user.stations
return nil unless stations.present?
phone_listeners = NowSession.joins("LEFT JOIN data_gateway ON data_gateway.id=now_session.call_gateway_id").select("now_session.call_ani_e164").where("now_session.call_ani_e164 <> ''").where(call_gateway_id: stations.map(&:id))
end
def area_code_for_listener(listener)
listener[0].slice(0,3) if listener[0].to_s.length == 11 && listener[0].to_s.slice!(0) == "1"
end
def group_phone_listeners_by_area_code(phone_list)
phone_list.map{|p|area_code_for_listener(p)}
end
def lat_long_for_listener(listener)
AreaCodes.where(area_code: listener.area_code).select([:area_code, :lat, :long])
end
def format_phone_listeners_for_gmaps(listeners)
results = []
listeners.map do |listener|
area_code, lat, long = lat_long_for_listener(listener)
results << {area_code: area_code, lat: lat, long: long}
end
return results
end
def map_phone_listeners
format_phone_listeners_for_gmaps(group_phone_listeners_by_area_code(get_phone_listeners(current_user)))
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment