module MailCannon::EnvelopeBagMapReduce::ClassMethods

Public Instance Methods

change_events_status_for_envelope_bag(id, from_sym, to_sym) click to toggle source

[from|to]sym = :new, :lock, :processed

# File lib/mailcannon/envelope_bag_map_reduce.rb, line 44
def change_events_status_for_envelope_bag(id, from_sym, to_sym)
  from_status = processed_status_for(from_sym)
  to_status = processed_status_for(to_sym)
  if from_sym
    query = MailCannon::SendgridEvent.where(envelope_bag_id: id, processed: from_status)
  else
    query = MailCannon::SendgridEvent.where(envelope_bag_id: id)
  end
  if query.kind_of?(Mongoid::Criteria)
    query.update_all(processed: to_status)
  else
    query.processed=to_status
    query.save
  end
  query
end
find_gem_root_path() click to toggle source
# File lib/mailcannon/envelope_bag_map_reduce.rb, line 4
def find_gem_root_path
  path = ""
  begin
    spec = Gem::Specification.find_by_name("mailcannon")
    path = spec.gem_dir
  rescue LoadError => e
    raise unless e.message =~ /mailcannon/
    puts 'mailcannon gem path not found'
  end
  path
end
js_map() click to toggle source
# File lib/mailcannon/envelope_bag_map_reduce.rb, line 27
def js_map
  root_path = find_gem_root_path
  root_path += "/" unless root_path.empty?
  @js_map ||= File.read("#{root_path}lib/mailcannon/reduces/envelope_bag_map.js")
  #TODO cache this
  @js_map
end
js_reduce() click to toggle source
# File lib/mailcannon/envelope_bag_map_reduce.rb, line 35
def js_reduce
  root_path = find_gem_root_path
  root_path += "/" unless root_path.empty?
  @js_reduce ||= File.read("#{root_path}lib/mailcannon/reduces/envelope_bag_reduce.js")
  #TODO cache this
  @js_reduce
end
processed_status_for(input) click to toggle source
# File lib/mailcannon/envelope_bag_map_reduce.rb, line 72
def processed_status_for(input)
  status_map = {
    lock: false,
    processed: true,
    new: nil
  }
  if [false,true,nil].include?(input)
    status_map = status_map.invert
    raise "Unexpected  input(#{input})" unless status_map.has_key?(input)
  end
  status_map[input]
end
reduce_statistics_for_envelope_bag(id) click to toggle source
# File lib/mailcannon/envelope_bag_map_reduce.rb, line 16
def reduce_statistics_for_envelope_bag(id)
  events = change_events_status_for_envelope_bag(id, nil, :lock)
  result = events.map_reduce(self.js_map, self.js_reduce).out(merge: "mail_cannon_envelope_bag_statistics")
  set_events_to(events,:processed)
  {raw: result.raw, count: events.count}
end
set_events_to(events,status) click to toggle source

private

# File lib/mailcannon/envelope_bag_map_reduce.rb, line 62
def set_events_to(events,status)
  status = processed_status_for(status)
  if events.kind_of?(Mongoid::Criteria)
    events.update_all(processed: status)
  else
    events.processed=status
    events.save
  end
end
statistics_for_envelope(id) click to toggle source
# File lib/mailcannon/envelope_bag_map_reduce.rb, line 23
def statistics_for_envelope(id)
  self.stats
end