Skip to content

Instantly share code, notes, and snippets.

@divyanshu707
Created June 1, 2016 20:06
Show Gist options
  • Save divyanshu707/c7474064e4415751491284501642738a to your computer and use it in GitHub Desktop.
Save divyanshu707/c7474064e4415751491284501642738a to your computer and use it in GitHub Desktop.
class TrackWorker
@queue = :tracking
def self.perform params
params = params.with_indifferent_access
device_id = params[:device_id]
ts = params[:timestamp]
lat = params[:latitude]
lon = params[:longitude]
t = Latlon.create({:device_id => device_id, :lat => lat, :lon => lon, :ts => ts})
gl = Geolocation.find_by(:lat => lat, :lon => lon)
if gl == nil
remote_data = fetch_geolocation lat, lon
remote_data = remote_data.with_indifferent_access
if remote_data
l = Geolocation.create(:lat => remote_data[:latitude], :lon => remote_data[:longitude], :country => remote_data[:country], :city => remote_data[:city])
t.geolocation_id = l.id
t.save!
end
else
t.geolocation_id = gl.id
t.save!
end
end
def self.fetch_geolocation lat, lon
res = Net::HTTP.get_response(URI.parse("http://127.0.0.1:5000/?latitude=#{lat}&longitude=#{lon}"))
if res.code == "200"
return JSON.parse(res.body)
else
Rails.logger.info "Non 200 response from mobile tracking server for lat #{lat}, lon #{lon} with body #{res.body}"
return nil
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment