Module: Hermes::Extractors

Included in:
Deliverer, Provider
Defined in:
lib/support/extractors.rb

Instance Method Summary collapse

Instance Method Details

#complex_extract(address_container) ⇒ Object

when passing in to/from addresses that are complex objects like a Hash or Twitter::Client instance, they will be YAMLed and then Base64ed since Mail::Message really only wants to play with strings for these fields



54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/support/extractors.rb', line 54

def complex_extract(address_container)
  if B64Y.decodable?(address_container)
    {
      decoded: true,
      value: B64Y.decode(address_container)
    }
  else
    {
      decoded: false,
      value: address_container
    }
  end
end

#extract_from(rails_message, format: :full) ⇒ Object

format can be full|name|address



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/support/extractors.rb', line 21

def extract_from(rails_message, format: :full)
  from = complex_extract(rails_message.from.first)
  return from[:value] if from[:decoded]

  case format
  when :full
    rails_message[:from].formatted.first
  when :name
    rails_message[:from].address_list.addresses.first.name
  when :address
    rails_message[:from].address_list.addresses.first.address
  end
end

#extract_html(rails_message) ⇒ Object



4
5
6
7
8
9
10
# File 'lib/support/extractors.rb', line 4

def extract_html(rails_message)
  if rails_message.html_part
    rails_message.html_part.body.decoded
  else
    rails_message.content_type =~ /text\/html/ ? rails_message.body.decoded : nil
  end
end

#extract_text(rails_message) ⇒ Object



12
13
14
15
16
17
18
# File 'lib/support/extractors.rb', line 12

def extract_text(rails_message)
  if rails_message.multipart?
    rails_message.text_part ? rails_message.text_part.body.decoded.strip : nil
  else
    rails_message.content_type =~ /text\/plain/ ? rails_message.body.decoded.strip : nil
  end
end

#extract_to(rails_message, format: :full) ⇒ Object

format can be full|name|address



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/support/extractors.rb', line 36

def extract_to(rails_message, format: :full)
  to = complex_extract(rails_message.to.first)
  return to[:value] if to[:decoded]

  case format
  when :full
    rails_message[:to].formatted.first
  when :name
    rails_message[:to].address_list.addresses.first.name
  when :address
    rails_message[:to].address_list.addresses.first.address
  end
end