Module: MailCannon::EnvelopeBagMapReduce::ClassMethods

Defined in:
lib/mailcannon/envelope_bag_map_reduce.rb

Instance Method Summary collapse

Instance Method Details

#change_events_status_for_envelope_bag(id, from_sym, to_sym) ⇒ Object

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



44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# 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_pathObject



4
5
6
7
8
9
10
11
12
13
14
# 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_mapObject



27
28
29
30
31
32
33
# 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_reduceObject



35
36
37
38
39
40
41
# 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) ⇒ Object



72
73
74
75
76
77
78
79
80
81
82
83
# 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) ⇒ Object



16
17
18
19
20
21
# 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) ⇒ Object

private



62
63
64
65
66
67
68
69
70
# 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) ⇒ Object



23
24
25
# File 'lib/mailcannon/envelope_bag_map_reduce.rb', line 23

def statistics_for_envelope(id)
  self.stats
end