Skip to content

Commit d744774

Browse files
committed
Install RuboCop and update code to pass
1 parent aa634fc commit d744774

File tree

15 files changed

+379
-291
lines changed

15 files changed

+379
-291
lines changed

.rubocop.yml

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
plugins:
3+
- rubocop-rspec
4+
5+
AllCops:
6+
NewCops: enable
7+
8+
Lint/DuplicateBranch:
9+
IgnoreLiteralBranches: true
10+
11+
Naming/FileName:
12+
Exclude:
13+
- 'lib/capybara-email.rb'
14+
15+
Metrics/AbcSize:
16+
CountRepeatedAttributes: false
17+
18+
Metrics/MethodLength:
19+
CountAsOne: ['array', 'hash', 'heredoc', 'method_call']
20+
Max: 15
21+
22+
RSpec/ExampleLength:
23+
Max: 10
24+
25+
RSpec/MultipleExpectations:
26+
Max: 5
27+
28+
Style/BlockDelimiters:
29+
EnforcedStyle: semantic
30+
31+
Style/Documentation:
32+
Enabled: false
33+
34+
Style/MultilineBlockChain:
35+
Enabled: false
36+
37+
Style/TrailingCommaInArguments:
38+
EnforcedStyleForMultiline: consistent_comma

Gemfile

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
1-
source 'https://rubygems.org'
1+
# frozen_string_literal: true
22

3-
if RUBY_VERSION >= '2.0'
4-
gem 'byebug'
5-
end
3+
source 'https://rubygems.org'
64

75
gemspec
6+
7+
gem 'actionmailer', '> 3.0'
8+
gem 'bourne'
9+
gem 'byebug' if RUBY_VERSION >= '2.0'
10+
gem 'rake'
11+
gem 'rspec'
12+
gem 'rubocop'
13+
gem 'rubocop-rspec'

Rakefile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
1+
# frozen_string_literal: true
2+
13
require 'bundler/setup'
24
require 'rspec/core/rake_task'
5+
36
Bundler::GemHelper.install_tasks
47

58
RSpec::Core::RakeTask.new('default') do |t|
69
t.pattern = FileList['spec/**/*_spec.rb']
710
end
8-

capybara-email.gemspec

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,23 @@
1-
# -*- encoding: utf-8 -*-
2-
require File.expand_path('../lib/capybara/email/version', __FILE__)
1+
# frozen_string_literal: true
2+
3+
require File.expand_path('lib/capybara/email/version', __dir__)
34

45
Gem::Specification.new do |gem|
5-
gem.authors = ['Brian Cardarella']
6-
7-
gem.description = %q{Test your ActionMailer and Mailer messages in Capybara}
8-
gem.summary = %q{Test your ActionMailer and Mailer messages in Capybara}
9-
gem.homepage = 'https://github.com/dockyard/capybara-email'
10-
gem.license = 'MIT'
6+
gem.authors = ['Brian Cardarella']
7+
8+
gem.description = 'Test your ActionMailer and Mailer messages in Capybara'
9+
gem.summary = 'Test your ActionMailer and Mailer messages in Capybara'
10+
gem.homepage = 'https://github.com/dockyard/capybara-email'
11+
gem.license = 'MIT'
12+
gem.required_ruby_version = '>= 2.7'
1113

12-
gem.files = Dir['lib/**/*.rb', 'LICENSE', 'README.md']
13-
gem.test_files = Dir['spec/**/*.rb']
14-
gem.name = 'capybara-email'
14+
gem.files = Dir['lib/**/*.rb', 'LICENSE', 'README.md']
15+
gem.name = 'capybara-email'
1516
gem.require_paths = ['lib']
16-
gem.version = Capybara::Email::VERSION
17+
gem.version = Capybara::Email::VERSION
1718

18-
gem.add_dependency 'mail'
1919
gem.add_dependency 'capybara', '>= 2.4', '< 4.0'
20-
gem.add_development_dependency 'actionmailer', '> 3.0'
21-
gem.add_development_dependency 'bourne'
22-
gem.add_development_dependency 'rspec'
23-
gem.add_development_dependency 'rake'
20+
gem.add_dependency 'mail'
21+
22+
gem.metadata['rubygems_mfa_required'] = 'true'
2423
end

lib/capybara-email.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
require 'capybara'
34
require 'mail'
45

lib/capybara/email.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
# frozen_string_literal: true
2+
23
module Capybara
34
module Node
45
autoload :Email, 'capybara/node/email'

lib/capybara/email/driver.rb

Lines changed: 83 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,94 +1,103 @@
11
# frozen_string_literal: true
2-
class Capybara::Email::Driver < Capybara::Driver::Base
3-
attr_reader :email
42

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
87

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
1612

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)
2018

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
2723

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
3627

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
3834

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
4243

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
5345

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) }
6348
end
64-
end
6549

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
6860

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
7072

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
7774
end
78-
else
79-
super
80-
end
81-
end
8275

83-
def respond_to_missing?(method_name, include_private = false)
84-
email.respond_to?(method_name, include_private || super)
85-
end
76+
private
8677

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
9089

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
93102
end
94103
end

0 commit comments

Comments
 (0)