Skip to content

Instantly share code, notes, and snippets.

@kballenegger
Created September 2, 2013 16:25
Show Gist options
  • Select an option

  • Save kballenegger/6414667 to your computer and use it in GitHub Desktop.

Select an option

Save kballenegger/6414667 to your computer and use it in GitHub Desktop.
Fetching active campaigns by iTunes ID, using Kongo :).
# configure Kongo :)
Kongo::Collection.add_extension(:apps, Module.new do
def find_by_itunes_id(id)
find_many(itunes: id, live_notified: true)
end
self; end)
Apps = Kongo::Collection.new(:apps)
Kongo::Collection.add_extension(:campaigns, Module.new do
def find_by_app(app)
find_many('$or': [{app_id: app._id.to_s}, {promote_in: app._id.to_s}])
end
def find_by_itunes_id(id)
Apps.find_by_itunes_id(id).to_enum.map do |a|
find_by_app(a).to_a
end.flatten
end
self; end)
Kongo::Model.add_extension(:campaigns, Module.new do
def switched_on?
self.switches && self.switches.map {|k,v| v.to_i }.reduce(&:*) == 1
end
def expired?
!((!self.date_min || self.date_min < Time.now.to_i) &&
(!self.date_max || self.date_max > Time.now.to_i))
end
def active?
switched_on? && !expired?
end
def advertiser?
self.market_type == 'advertiser'
end
def publisher?
self.market_type == 'publisher'
end
self; end)
Campaigns = Kongo::Collection.new(:campaigns)
# use it
# for example:
# Campaigns.find_by_itunes_id('...').select(&:advertiser?).select(&:active?)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment