-
Notifications
You must be signed in to change notification settings - Fork 2
/
api-impressions.rb
47 lines (37 loc) · 1.45 KB
/
api-impressions.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
require 'sinatra/base'
require 'mongoid'
require 'sinatra/jsonp'
require 'newrelic_rpm'
require 'sidekiq'
class Impression
include Mongoid::Document
include Mongoid::Timestamps::Created
field :external_user_id, type: String
field :external_product_id, type: String
field :position, type: Integer
field :anonymous, type: Boolean
validates_presence_of :external_user_id, :external_product_id
end
class ApiImpressions < Sinatra::Base
helpers Sinatra::Jsonp
configure do
set :environments, %w{development test production staging peixe}
Mongoid.load!("config/mongoid.yml", ENV['RACK_ENV'])
end
# routes
get '/' do
# content_type :json
data = {success: false, message: 'invalid endpoint', environment: settings.environment} #, db: "#{settings.db_host}:#{settings.db_port}/#{settings.db_base}"
jsonp data
end
get '/impression/:user_id/:product_id/:position' do
unless params[:user_id]=='0'
anonymous = params[:user_id].match(/^hmrtmp/) ? 1 : 0
impression_date = Time.now
Impression.with(write: { w: 0 }).create(external_user_id: params[:user_id], external_product_id: params[:product_id], position: params[:position], anonymous: anonymous, created_at: impression_date)
Sidekiq::Client.push('class' => 'ImpressionWorker', 'args' => [params[:user_id], params[:product_id], impression_date.utc.strftime("%FT%T.%3NZ")], 'queue' => "impression")
end
data = {success: true}
jsonp data
end
end