Class: Capybara::Email::Driver

Inherits:
Driver::Base
  • Object
show all
Defined in:
lib/capybara/email/driver.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(email) ⇒ Driver

Returns a new instance of Driver.



4
5
6
# File 'lib/capybara/email/driver.rb', line 4

def initialize(email)
  @email = email
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(meth, *args, &block) ⇒ Object (private)



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/capybara/email/driver.rb', line 70

def method_missing(meth, *args, &block)
  if email.respond_to?(meth)
    if args.empty?
      email.send(meth)
    else
      email.send(meth, args)
    end
  else
    super
  end
end

Instance Attribute Details

#emailObject (readonly)

Returns the value of attribute email.



2
3
4
# File 'lib/capybara/email/driver.rb', line 2

def email
  @email
end

Instance Method Details

#bodyObject



16
17
18
# File 'lib/capybara/email/driver.rb', line 16

def body
  dom.to_xml
end

#domObject

Nokogiri object for traversing content

Returns:

  • Nokogiri::HTML::Document



23
24
25
# File 'lib/capybara/email/driver.rb', line 23

def dom
  @dom ||= Nokogiri::HTML(source)
end

#find(selector) ⇒ Array<Capybara::Driver::Node> Also known as: find_xpath

Find elements based on given xpath

Parameters:

  • (xpath string)

Returns:

  • (Array<Capybara::Driver::Node>)


32
33
34
# File 'lib/capybara/email/driver.rb', line 32

def find(selector)
  dom.xpath(selector).map { |node| Capybara::Email::Node.new(self, node) }
end

#find_css(selector) ⇒ Object



38
39
40
# File 'lib/capybara/email/driver.rb', line 38

def find_css(selector)
  dom.css(selector).map { |node| Capybara::Email::Node.new(self, node) }
end

#follow(url) ⇒ Object



8
9
10
11
12
13
14
# File 'lib/capybara/email/driver.rb', line 8

def follow(url)
  url = URI.parse(url)
  host = "#{url.scheme}://#{url.host}"
  host << ":#{url.port}" unless url.port == url.default_port
  host_with_path = File.join(host, url.path)
  Capybara.current_session.visit([host_with_path, url.query].compact.join('?'))
end

#rawObject

Plain text email contents

Returns:

  • String



56
57
58
59
60
61
62
63
64
65
66
# File 'lib/capybara/email/driver.rb', line 56

def raw
  if email.mime_type =~ /\Amultipart\/(alternative|related|mixed)\Z/
    if email.html_part
      return email.html_part.body.to_s
    elsif email.text_part
      return email.text_part.body.to_s
    end
  end

  return email.body.to_s
end

#sourceObject

String version of email HTML source

Returns:

  • String



45
46
47
48
49
50
51
# File 'lib/capybara/email/driver.rb', line 45

def source
  if email.mime_type == 'text/plain'
    convert_to_html(raw)
  else
    raw
  end
end