coruro-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 1e972d5e9538e44b78c6c54d0d394bdea0cb13c3438606d46161e738f7280815
4
+ data.tar.gz: 0c757e7cd8cd4d66e63ba5a3d3c80bc3d395ed448f3755528c519995a6ba7b4e
5
+ SHA512:
6
+ metadata.gz: ee53e053db2fa3eee837b455ccf16a987cacb642b619b19f7e8aee1a7e5de991fdc7dd20d8b2a4c7a10c0865b5e1f80e6f8d2c43a100aed696d2a9863a86db75
7
+ data.tar.gz: 4c8c8567364f9c3fff19efbd22e97c46cf01778cffb1bc16852be7e277e34bde945d3a97637576ad73450796825dfac79e63fbd38e615cc501b5343e0a2291b6
data/.gitignore ADDED
@@ -0,0 +1,8 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in coruro-ruby.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,32 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ coruro-ruby (0.1.0)
5
+ mail (~> 2.7.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ coderay (1.1.2)
11
+ mail (2.7.0)
12
+ mini_mime (>= 0.1.1)
13
+ method_source (0.9.0)
14
+ mini_mime (1.0.0)
15
+ minitest (5.11.1)
16
+ pry (0.11.3)
17
+ coderay (~> 1.1.0)
18
+ method_source (~> 0.9.0)
19
+ rake (10.5.0)
20
+
21
+ PLATFORMS
22
+ ruby
23
+
24
+ DEPENDENCIES
25
+ bundler (~> 1.16)
26
+ coruro-ruby!
27
+ minitest (~> 5.0)
28
+ pry
29
+ rake (~> 10.0)
30
+
31
+ BUNDLED WITH
32
+ 1.16.1
data/README.md ADDED
@@ -0,0 +1,65 @@
1
+ # Coruro Ruby
2
+
3
+ The Ruby version of [Coruro](../README.md). This README provides details for the ruby-specific Coruro implementation.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'coruro-ruby'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install coruro-ruby
20
+
21
+ ## Usage
22
+
23
+
24
+ ```ruby
25
+ # Instantiate a Coruro instance. This also ensures Mailcatcher is running
26
+ coruro = Coruro.new(adapter: :mailcatcher)
27
+
28
+ # Load the emails mailcatcher caught to "your-fave@example.com"
29
+ messages_to_my_fave = coruro.where(to: /your-fave@example.com/)
30
+ # Since these are instances of a Mail object at the moment, which means they probably play nice with [EmailSpec::Matchers](https://github.com/email-spec/email-spec#rspec-matchers)
31
+ include EmailSpec::Matchers
32
+ messages_to_my_fave.each do
33
+ assert_must deliver_to("your-fave@example.com")
34
+ end
35
+ ```
36
+
37
+ ### Tearing Down Mailcatcher
38
+ Because Coruro manages a local mailcatcher process, you may want to clean up *after* your test suite runs. Otherwise you may wind up with an orphaned Mailcatcher process. This isn't the end of the world, since Mailcatcher will refuse to start a second process if one is running already, but if you don't want it to stick around you can perform a post-suite cleanup. This is done by calling `#stop` on an instance of Coruro. See the following example for Minitest:
39
+
40
+ ```ruby
41
+ Minitest.after_run do
42
+ Coruro.new(adapter: :mailcatcher).stop
43
+ end
44
+ ```
45
+ For those using Rspec, you will want to use the appropriate [`after` hook](https://relishapp.com/rspec/rspec-core/v/3-7/docs/hooks/before-and-after-hooks).
46
+
47
+ For Cucumber users, you will want want to use the [`at_exit` global hook](https://github.com/cucumber/cucumber/wiki/Hooks#global-hooks)
48
+
49
+ ## Development
50
+
51
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
52
+
53
+ To install this gem onto your local machine, run `bin/rake install`. To release a new version, update the version number in `version.rb`, and then run `bin/rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
54
+
55
+ ## Contributing
56
+
57
+ Bug reports and pull requests are welcome on GitHub at https://github.com/wecohere/coruro. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](../CODE_OF_CONDUCT.md). See [Our Contributing Guide](../CONTRIBUTING.md) for more details.
58
+
59
+ ## License
60
+
61
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
62
+
63
+ ## Code of Conduct
64
+
65
+ Everyone interacting in the coruro project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](../CODE_OF_CONDUCT.md).
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList["test/**/*_test.rb"]
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mailcatcher/client/ruby"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start(__FILE__)
data/bin/rake ADDED
@@ -0,0 +1,29 @@
1
+ #!/usr/bin/env ruby
2
+ # frozen_string_literal: true
3
+
4
+ #
5
+ # This file was generated by Bundler.
6
+ #
7
+ # The application 'rake' is installed as part of a gem, and
8
+ # this file is here to facilitate running it.
9
+ #
10
+
11
+ require "pathname"
12
+ ENV["BUNDLE_GEMFILE"] ||= File.expand_path("../../Gemfile",
13
+ Pathname.new(__FILE__).realpath)
14
+
15
+ bundle_binstub = File.expand_path("../bundle", __FILE__)
16
+
17
+ if File.file?(bundle_binstub)
18
+ if File.read(bundle_binstub, 150) =~ /This file was generated by Bundler/
19
+ load(bundle_binstub)
20
+ else
21
+ abort("Your `bin/bundle` was not generated by Bundler, so this binstub cannot run.
22
+ Replace `bin/bundle` by running `bundle binstubs bundler --force`, then run this command again.")
23
+ end
24
+ end
25
+
26
+ require "rubygems"
27
+ require "bundler/setup"
28
+
29
+ load Gem.bin_path("rake", "rake")
data/bin/setup ADDED
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ gem install mailcatcher
@@ -0,0 +1,31 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "coruro/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "coruro-ruby"
8
+ spec.version = Coruro::VERSION
9
+ spec.authors = ["Zee Spencer"]
10
+ spec.email = ["zee@wecohere.com"]
11
+
12
+ spec.summary = %q{Capybara-inspired testing library for emails and notifications}
13
+ spec.description = %q{Capybara-inspired testing library for emails and notifications. Query sent emails and make assertions against he and make assertions against them}
14
+ spec.homepage = "https://github.com/wecohere/coruro"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+
21
+ spec.bindir = "exe"
22
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
23
+ spec.require_paths = ["lib"]
24
+
25
+ spec.add_dependency "mail", "~> 2.7.0"
26
+
27
+ spec.add_development_dependency "bundler", "~> 1.16"
28
+ spec.add_development_dependency "rake", "~> 10.0"
29
+ spec.add_development_dependency "minitest", "~> 5.0"
30
+ spec.add_development_dependency "pry"
31
+ end
@@ -0,0 +1,86 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require 'open3'
4
+ require 'singleton'
5
+
6
+ class Coruro
7
+ # Translates between Curoro and Mailcatcher's API
8
+ class MailcatcherAdapter
9
+ attr_accessor :runner, :timeout
10
+ def initialize(timeout:)
11
+ self.timeout = timeout
12
+ end
13
+
14
+ def all
15
+ messages
16
+ end
17
+
18
+ def where(to: nil, from: nil, subject: nil)
19
+ result = []; start_time = Time.now.to_f
20
+ while (result.empty? || (Time.now.to_f - start_time) <= timeout)
21
+ result = messages.select do |message|
22
+ match?(to, message[:recipients]) ||
23
+ match?(from, message[:sender]) ||
24
+ match?(subject, message[:subject])
25
+ end.map(&method(:find_by))
26
+ end
27
+ result
28
+ end
29
+
30
+ def match?(query, value)
31
+ return false if query.nil?
32
+ return query.match?(value) if query.respond_to?(:match?) && !value.respond_to?(:any?)
33
+ return value.any? { |child| match?(query, child) } if value.respond_to?(:any?)
34
+ raise ArgumentError, "Query #{query} must respond to `match?` or Value #{value} must respond to `any?`"
35
+ end
36
+
37
+ extend Forwardable
38
+ def_delegators :runner, :up?, :start, :stop
39
+
40
+ def runner
41
+ @_runner ||= Runner.instance
42
+ end
43
+
44
+ private def messages
45
+ JSON.parse(Net::HTTP.get(URI("http://127.0.0.1:1080/messages")), symbolize_names: true)
46
+ end
47
+
48
+ private def raw_message(message_id)
49
+ Net::HTTP.get(URI("http://127.0.0.1:1080/messages/#{message_id}.eml"))
50
+ end
51
+
52
+
53
+ private def find_by(attributes)
54
+ message = Message.new(Mail.new(raw_message(attributes[:id])))
55
+ message.id = attributes[:id]
56
+ message
57
+ end
58
+
59
+ # Allows for launching and terminating mailcatcher programmaticaly
60
+ class Runner
61
+ include Singleton
62
+ attr_accessor :stdin, :stdout, :stderr, :thread
63
+
64
+ def start
65
+ return if up?
66
+ self.stdin, self.stdout, self.stderr, self.thread =
67
+ Open3.popen3({ "PATH" => ENV['PATH'] }, 'mailcatcher -f', { unsetenv_others:true })
68
+ end
69
+
70
+ def up?
71
+ response = Net::HTTP.get_response(URI('http://127.0.0.1:1080'))
72
+ response.is_a?(Net::HTTPSuccess)
73
+ rescue Errno::ECONNREFUSED, Errno::EADDRNOTAVAIL => _
74
+ false
75
+ end
76
+
77
+ def stop
78
+ return unless self.thread
79
+ self.stdin.close
80
+ self.stdout.close
81
+ self.stderr.close
82
+ `kill -9 #{ thread[:pid] }`
83
+ end
84
+ end
85
+ end
86
+ end
@@ -0,0 +1,5 @@
1
+ class Coruro
2
+ class Message < SimpleDelegator
3
+ attr_accessor :id
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ class Coruro
2
+ VERSION="0.1.0"
3
+ end
data/lib/coruro.rb ADDED
@@ -0,0 +1,36 @@
1
+ require 'mail'
2
+
3
+ require_relative 'coruro/version'
4
+
5
+ require_relative 'coruro/message'
6
+
7
+ require_relative 'coruro/mailcatcher_adapter'
8
+
9
+ class Coruro
10
+ attr_accessor :adapter
11
+ extend Forwardable
12
+ def_delegators :adapter, :all, :where, :stop
13
+
14
+ def initialize(adapter:, on_wait_tick: -> (count) { }, timeout: 0.1)
15
+ case adapter
16
+ when :mailcatcher
17
+ self.adapter = MailcatcherAdapter.new(timeout: timeout)
18
+ self.adapter.start
19
+ else
20
+ raise UnrecognizedAdapterError, adapter
21
+ end
22
+
23
+ wait_until_up(on_wait_tick)
24
+ end
25
+
26
+ def wait_until_up(on_tick)
27
+ count = 0
28
+ until adapter.up? || count > 5
29
+ on_tick.call(count)
30
+ sleep(1)
31
+ end
32
+ end
33
+
34
+ class UnrecognizedAdapterError < StandardError; end
35
+ end
36
+
metadata ADDED
@@ -0,0 +1,128 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: coruro-ruby
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Zee Spencer
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2018-02-14 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: mail
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: 2.7.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: 2.7.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.16'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.16'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: minitest
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '5.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: pry
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ description: Capybara-inspired testing library for emails and notifications. Query
84
+ sent emails and make assertions against he and make assertions against them
85
+ email:
86
+ - zee@wecohere.com
87
+ executables: []
88
+ extensions: []
89
+ extra_rdoc_files: []
90
+ files:
91
+ - ".gitignore"
92
+ - Gemfile
93
+ - Gemfile.lock
94
+ - README.md
95
+ - Rakefile
96
+ - bin/console
97
+ - bin/rake
98
+ - bin/setup
99
+ - coruro-ruby.gemspec
100
+ - lib/coruro.rb
101
+ - lib/coruro/mailcatcher_adapter.rb
102
+ - lib/coruro/message.rb
103
+ - lib/coruro/version.rb
104
+ homepage: https://github.com/wecohere/coruro
105
+ licenses:
106
+ - MIT
107
+ metadata: {}
108
+ post_install_message:
109
+ rdoc_options: []
110
+ require_paths:
111
+ - lib
112
+ required_ruby_version: !ruby/object:Gem::Requirement
113
+ requirements:
114
+ - - ">="
115
+ - !ruby/object:Gem::Version
116
+ version: '0'
117
+ required_rubygems_version: !ruby/object:Gem::Requirement
118
+ requirements:
119
+ - - ">="
120
+ - !ruby/object:Gem::Version
121
+ version: '0'
122
+ requirements: []
123
+ rubyforge_project:
124
+ rubygems_version: 2.7.4
125
+ signing_key:
126
+ specification_version: 4
127
+ summary: Capybara-inspired testing library for emails and notifications
128
+ test_files: []