Class: Astrotrain::Mapping

Inherits:
Object
  • Object
show all
Includes:
DataMapper::Resource
Defined in:
lib/astrotrain/mapping.rb,
lib/astrotrain/mapping/http_post.rb,
lib/astrotrain/mapping/transport.rb

Defined Under Namespace

Classes: HttpPost, Transport

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.default_domainObject

Returns the value of attribute default_domain.



6
7
8
# File 'lib/astrotrain/mapping.rb', line 6

def default_domain
  @default_domain
end

.transportsObject

Returns the value of attribute transports.



7
8
9
# File 'lib/astrotrain/mapping.rb', line 7

def transports
  @transports
end

Class Method Details

.match(email_addresses) ⇒ Object

returns a mapping for the given array of email addresses



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/astrotrain/mapping.rb', line 27

def self.match(email_addresses)
  email_addresses.each do |email_address|
    email_address.strip!
    email_address.downcase!
    name, domain = email_address.split("@")
    if mapping = match_by_address(name, domain) || match_by_wildcard(name, domain)
      return [mapping, email_address]
    end
  end
  nil
end

.process(message, file = nil) ⇒ Object

Processes a given message. It finds a mapping, creates a LoggedMail record, and attempts to process the message.



41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/astrotrain/mapping.rb', line 41

def self.process(message, file = nil)
  LoggedMail.from(message, file) do |logged|
    save_logged = begin
      mapping, recipient = match(message.recipients)
      if mapping
        logged.recipient = recipient
        logged.mapping   = mapping
        begin
          mapping.process(message, recipient)
        rescue Astrotrain::ProcessingCancelled
          logged.error_message = "Cancelled."
        end
        logged.delivered_at = Time.now.utc
      end
      LoggedMail.log_processed # save successfully processed messages?
    rescue
      logged.error_message = "#{$!.message}\n#{$!.backtrace.join("\n")}"
    end
    Astrotrain.callback(:post_processing, message, mapping, logged)
    save_logged
  end
end

Instance Method Details

#destination_uses_email?Boolean

Returns:

  • (Boolean)


81
82
83
# File 'lib/astrotrain/mapping.rb', line 81

def destination_uses_email?
  transport == 'jabber'
end

#destination_uses_url?Boolean

Returns:

  • (Boolean)


77
78
79
# File 'lib/astrotrain/mapping.rb', line 77

def destination_uses_url?
  transport == 'http_post'
end

#find_reply_from(body) ⇒ Object

Looks for the mapping’s separator in the message body and pulls only the content above it. Assuming a separator of ‘====’…

This will be kept

On Thu, Sep 3, 2009 at 12:34 AM... (this will be removed)
====

> Everything here will be removed.


99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
# File 'lib/astrotrain/mapping.rb', line 99

def find_reply_from(body)
  return    if separator.blank?
  return '' if body.blank?
  lines = body.split("\n")
  delim_line = found_empty = nil

  (lines.size - 1).downto(0) do |i|
    line = lines[i]
    if !delim_line && line.include?(separator)
      delim_line = i
    elsif delim_line && !found_empty
      delim_line = i
      found_empty = line.strip.blank?
    elsif delim_line && found_empty
      if date_reply_line?(line) || line.strip.blank?
        delim_line = i
      else
        break
      end
    end
  end

  if delim_line
    body = if delim_line.zero?
      []
    elsif lines.size >= delim_line
      lines[0..delim_line-1]
    else
      lines
    end.join("\n")
  elsif body.frozen?
    body = body.dup
  end
  body.strip!
  body
end

#full_emailObject



85
86
87
# File 'lib/astrotrain/mapping.rb', line 85

def full_email
  "#{email_user}@#{email_domain}"
end

#match?(name, domain) ⇒ Boolean

returns true if the email matches this mapping. Wildcards in the name are allowed. A mapping with foo*@bar.com will match [email protected] and [email protected], but not [email protected].

Returns:

  • (Boolean)


73
74
75
# File 'lib/astrotrain/mapping.rb', line 73

def match?(name, domain)
  email_domain == domain && name =~ email_user_regex
end

#process(message, recipient) ⇒ Object

Processes a given message and recipient against the mapping’s transport.



65
66
67
68
69
# File 'lib/astrotrain/mapping.rb', line 65

def process(message, recipient)
  Astrotrain.callback(:pre_processing, message, self)
  Transport.process(message, self, recipient)

end