Skip to content

Instantly share code, notes, and snippets.

@t0nylombardi
Created May 3, 2016 23:30
Show Gist options
  • Save t0nylombardi/801d82516fb221134b4bf90016c7e49d to your computer and use it in GitHub Desktop.
Save t0nylombardi/801d82516fb221134b4bf90016c7e49d to your computer and use it in GitHub Desktop.
module Tracker
class Rack
def initialize(app)
@app = app
end
def call(env)
@req = ::Rack::Request.new(env)
if @req.path_info =~ /tracker.gif/
result = Services::Params.decode(@req.query_string)
location = Services::Locations.lookup(@req.ip)
params = {
ip_address: location["ipAddress"],
campaign: result[:campaign],
banner_size: result[:banner_size],
station: result[:station],
city: location["cityName"],
state: location["regionName"],
user_agent: @req.user_agent,
referral: @req.referer
}
if TmpgImpression.create!(params)
[
200, { 'Content-Type' => 'image/gif' },
[File.read(File.join(File.dirname(__FILE__), 'tracker.gif'))]
]
else
Rails.logger.warn "\n\n Failed to create record on:#{Date.today}"
end
else
@app.call(env)
end
end
def split_params(str)
str = Base64.urlsafe_decode64(str)
arry = str.split(/&/)
hash = {}
arry.each do |a|
hash[a.scan(/^\w*/).join('').to_sym] = a.gsub(/^(\w*=)/,'')
end
hash
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment