|
1 | 1 | # frozen_string_literal: true |
2 | | -class Capybara::Email::Driver < Capybara::Driver::Base |
3 | | - attr_reader :email |
4 | 2 |
|
5 | | - def initialize(email) |
6 | | - @email = email |
7 | | - end |
| 3 | +module Capybara |
| 4 | + module Email |
| 5 | + class Driver < Capybara::Driver::Base |
| 6 | + attr_reader :email |
8 | 7 |
|
9 | | - def follow(url) |
10 | | - url = URI.parse(url) |
11 | | - host = "#{url.scheme}://#{url.host}" |
12 | | - host += ":#{url.port}" unless url.port == url.default_port |
13 | | - host_with_path = File.join(host, url.path) |
14 | | - Capybara.current_session.visit([host_with_path, url.query].compact.join('?')) |
15 | | - end |
| 8 | + def initialize(email) |
| 9 | + @email = email |
| 10 | + super() |
| 11 | + end |
16 | 12 |
|
17 | | - def body |
18 | | - dom.to_xml |
19 | | - end |
| 13 | + def follow(url) |
| 14 | + url = URI.parse(url) |
| 15 | + host = "#{url.scheme}://#{url.host}" |
| 16 | + host += ":#{url.port}" unless url.port == url.default_port |
| 17 | + host_with_path = File.join(host, url.path) |
20 | 18 |
|
21 | | - # Nokogiri object for traversing content |
22 | | - # |
23 | | - # @return Nokogiri::HTML::Document |
24 | | - def dom |
25 | | - @dom ||= Nokogiri::HTML(source) |
26 | | - end |
| 19 | + Capybara |
| 20 | + .current_session |
| 21 | + .visit([host_with_path, url.query].compact.join('?')) |
| 22 | + end |
27 | 23 |
|
28 | | - # Find elements based on given xpath |
29 | | - # |
30 | | - # @param [xpath string] |
31 | | - # |
32 | | - # @return [Array<Capybara::Driver::Node>] |
33 | | - def find(selector) |
34 | | - dom.xpath(selector).map { |node| Capybara::Email::Node.new(self, node) } |
35 | | - end |
| 24 | + def body |
| 25 | + dom.to_xml |
| 26 | + end |
36 | 27 |
|
37 | | - alias_method :find_xpath, :find |
| 28 | + # Nokogiri object for traversing content |
| 29 | + # |
| 30 | + # @return Nokogiri::HTML::Document |
| 31 | + def dom |
| 32 | + @dom ||= Nokogiri::HTML(source) |
| 33 | + end |
38 | 34 |
|
39 | | - def find_css(selector) |
40 | | - dom.css(selector).map { |node| Capybara::Email::Node.new(self, node) } |
41 | | - end |
| 35 | + # Find elements based on given xpath |
| 36 | + # |
| 37 | + # @param [xpath string] |
| 38 | + # |
| 39 | + # @return [Array<Capybara::Driver::Node>] |
| 40 | + def find(selector) |
| 41 | + dom.xpath(selector).map { |node| Capybara::Email::Node.new(self, node) } |
| 42 | + end |
42 | 43 |
|
43 | | - # String version of email HTML source |
44 | | - # |
45 | | - # @return String |
46 | | - def source |
47 | | - if email.mime_type == 'text/plain' |
48 | | - convert_to_html(raw) |
49 | | - else |
50 | | - raw |
51 | | - end |
52 | | - end |
| 44 | + alias find_xpath find |
53 | 45 |
|
54 | | - # Plain text email contents |
55 | | - # |
56 | | - # @return String |
57 | | - def raw |
58 | | - if email.mime_type =~ /\Amultipart\/(alternative|related|mixed)\Z/ |
59 | | - if email.html_part |
60 | | - return email.html_part.body.to_s |
61 | | - elsif email.text_part |
62 | | - return email.text_part.body.to_s |
| 46 | + def find_css(selector) |
| 47 | + dom.css(selector).map { |node| Capybara::Email::Node.new(self, node) } |
63 | 48 | end |
64 | | - end |
65 | 49 |
|
66 | | - return email.body.to_s |
67 | | - end |
| 50 | + # String version of email HTML source |
| 51 | + # |
| 52 | + # @return String |
| 53 | + def source |
| 54 | + if email.mime_type == 'text/plain' |
| 55 | + convert_to_html(raw) |
| 56 | + else |
| 57 | + raw |
| 58 | + end |
| 59 | + end |
68 | 60 |
|
69 | | - private |
| 61 | + # Plain text email contents |
| 62 | + # |
| 63 | + # @return String |
| 64 | + def raw |
| 65 | + if email.mime_type =~ %r{\Amultipart/(alternative|related|mixed)\Z} |
| 66 | + if email.html_part |
| 67 | + return email.html_part.body.to_s |
| 68 | + elsif email.text_part |
| 69 | + return email.text_part.body.to_s |
| 70 | + end |
| 71 | + end |
70 | 72 |
|
71 | | - def method_missing(method_name, *args, &block) |
72 | | - if email.respond_to?(method_name) |
73 | | - if args.empty? |
74 | | - email.send(method_name) |
75 | | - else |
76 | | - email.send(method_name, args) |
| 73 | + email.body.to_s |
77 | 74 | end |
78 | | - else |
79 | | - super |
80 | | - end |
81 | | - end |
82 | 75 |
|
83 | | - def respond_to_missing?(method_name, include_private = false) |
84 | | - email.respond_to?(method_name, include_private || super) |
85 | | - end |
| 76 | + private |
86 | 77 |
|
87 | | - def convert_to_html(text) |
88 | | - "<html><body>#{convert_links(text)}</body></html>" |
89 | | - end |
| 78 | + def method_missing(method_name, *args) |
| 79 | + if email.respond_to?(method_name) |
| 80 | + if args.empty? |
| 81 | + email.send(method_name) |
| 82 | + else |
| 83 | + email.send(method_name, args) |
| 84 | + end |
| 85 | + else |
| 86 | + super |
| 87 | + end |
| 88 | + end |
90 | 89 |
|
91 | | - def convert_links(text) |
92 | | - text.gsub(%r{(https?://\S+)}, %q{<a href="\1">\1</a>}) |
| 90 | + def respond_to_missing?(method_name, include_private = false) |
| 91 | + email.respond_to?(method_name, include_private || super) |
| 92 | + end |
| 93 | + |
| 94 | + def convert_to_html(text) |
| 95 | + "<html><body>#{convert_links(text)}</body></html>" |
| 96 | + end |
| 97 | + |
| 98 | + def convert_links(text) |
| 99 | + text.gsub(%r{(https?://\S+)}, %q(<a href="\1">\1</a>)) |
| 100 | + end |
| 101 | + end |
93 | 102 | end |
94 | 103 | end |
0 commit comments