postmark-spam_check 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.travis.yml +4 -0
- data/CODE_OF_CONDUCT.md +13 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +79 -0
- data/Rakefile +10 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/postmark/spam_check.rb +15 -0
- data/lib/postmark/spam_check/client.rb +42 -0
- data/lib/postmark/spam_check/report.rb +66 -0
- data/lib/postmark/spam_check/version.rb +5 -0
- data/postmark-spam_check.gemspec +25 -0
- metadata +102 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 1c66515023d6d89be09b058efb14e3469cec95d9
|
4
|
+
data.tar.gz: bdff52f5748403623beb06e7071945fc19671aed
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 22878ace8708d824312cabdb290b7851cc8a8028da24fa0d0648e2ee608b7480a5f5f3b9143400b746e690beb16aec42c0a058fc56cd89e82feac8f2b81d31eb
|
7
|
+
data.tar.gz: 0b0b6335b3b9761dc2cfe1b24d1eb09e552370e8fb40812d06f52cd124bf6768c789d0c90160a02349695058109bc6f239543d817a4a8a9cfad7775cfbb3797d
|
data/.gitignore
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,13 @@
|
|
1
|
+
# Contributor Code of Conduct
|
2
|
+
|
3
|
+
As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
|
4
|
+
|
5
|
+
We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
|
6
|
+
|
7
|
+
Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
|
8
|
+
|
9
|
+
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
|
10
|
+
|
11
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
|
12
|
+
|
13
|
+
This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
|
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Brad Pauly
|
4
|
+
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
7
|
+
in the Software without restriction, including without limitation the rights
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
10
|
+
furnished to do so, subject to the following conditions:
|
11
|
+
|
12
|
+
The above copyright notice and this permission notice shall be included in
|
13
|
+
all copies or substantial portions of the Software.
|
14
|
+
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
|
21
|
+
THE SOFTWARE.
|
data/README.md
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
# Postmark::SpamCheck
|
2
|
+
|
3
|
+
This is a simple client for http://spamcheck.postmarkapp.com/
|
4
|
+
|
5
|
+
I wrote it because the [one that is available](https://github.com/krasio/postmark_spamcheck) has a number of dependencies that I didn't want and is getting old. This library only uses the Ruby standard library.
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'postmark-spam_check'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install postmark-spam_check
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
Give it a raw email and an optional format and timeout.
|
26
|
+
|
27
|
+
```ruby
|
28
|
+
Postmark::SpamCheck.check(raw_email, report_format = :long, timeout = 60)
|
29
|
+
```
|
30
|
+
|
31
|
+
You can get a raw email by calling #to_s on a Mail object.
|
32
|
+
|
33
|
+
```ruby
|
34
|
+
mail = Mail.new do
|
35
|
+
from "bill.lumbergh@example.net"
|
36
|
+
to "peter.gibbons@example.com"
|
37
|
+
subject "TPS reports"
|
38
|
+
body "Peter, What's happening? Yeah, about those TPS reports. Did you get the memo?"
|
39
|
+
end
|
40
|
+
|
41
|
+
Postmark::SpamCheck.check(mail.to_s)
|
42
|
+
|
43
|
+
=> #<Postmark::SpamCheck::Report:0x007fd61e189970
|
44
|
+
@details=
|
45
|
+
[{:points=>-0.0, :rule=>"NO_RELAYS", :description=>"Informational: message was not relayed via SMTP"},
|
46
|
+
{:points=>-0.0, :rule=>"NO_RECEIVED", :description=>"Informational: message has no Received headers"}],
|
47
|
+
@email=
|
48
|
+
"Date: Mon, 19 Oct 2015 16:50:00 -0700\r\nFrom: bill.lumbergh@example.net\r\nTo: peter.gibbons@example.com\r\nMessage-ID: <562581a848336_c663feb09065bec23540@venus.local.mail>\r\nSubject: TPS reports\r\nMime-Version: 1.0\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nPeter, What's happening? Yeah, about those TPS reports. Did you get the memo?",
|
49
|
+
@error=nil,
|
50
|
+
@score="-0.0">
|
51
|
+
```
|
52
|
+
|
53
|
+
If you don't want the details, call with :short format instead. This will only fetch the score.
|
54
|
+
|
55
|
+
```ruby
|
56
|
+
Postmark::SpamCheck.check(mail.to_s, :short)
|
57
|
+
|
58
|
+
=> #<Postmark::SpamCheck::Report:0x007fd61e080088
|
59
|
+
@details=[],
|
60
|
+
@email=
|
61
|
+
"Date: Mon, 19 Oct 2015 16:50:00 -0700\r\nFrom: bill.lumbergh@example.net\r\nTo: peter.gibbons@example.com\r\nMessage-ID: <562581a848336_c663feb09065bec23540@venus.local.mail>\r\nSubject: TPS reports\r\nMime-Version: 1.0\r\nContent-Type: text/plain;\r\n charset=UTF-8\r\nContent-Transfer-Encoding: 7bit\r\n\r\nPeter, What's happening? Yeah, about those TPS reports. Did you get the memo?",
|
62
|
+
@error=nil,
|
63
|
+
@score="0.0">
|
64
|
+
```
|
65
|
+
|
66
|
+
## Development
|
67
|
+
|
68
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
69
|
+
|
70
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec 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).
|
71
|
+
|
72
|
+
## Contributing
|
73
|
+
|
74
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/bradpauly/postmark-spam_check. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
|
75
|
+
|
76
|
+
|
77
|
+
## License
|
78
|
+
|
79
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "postmark/spam_check"
|
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
|
data/bin/setup
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "postmark/spam_check/client"
|
2
|
+
require "postmark/spam_check/report"
|
3
|
+
require "postmark/spam_check/version"
|
4
|
+
|
5
|
+
module Postmark
|
6
|
+
module SpamCheck
|
7
|
+
def self.check(raw_email, report_format = :long, timeout = 60)
|
8
|
+
self.client(timeout).check(raw_email, report_format)
|
9
|
+
end
|
10
|
+
|
11
|
+
def self.client(timeout)
|
12
|
+
Postmark::SpamCheck::Client.new(timeout)
|
13
|
+
end
|
14
|
+
end
|
15
|
+
end
|
@@ -0,0 +1,42 @@
|
|
1
|
+
require 'net/http'
|
2
|
+
require 'json'
|
3
|
+
require 'uri'
|
4
|
+
|
5
|
+
# The Client is responsible for making requests to the remote API. It doesn't
|
6
|
+
# know about the format of the results, only that it is JSON and passes
|
7
|
+
# the parsed result to the Report, which will handle the rest.
|
8
|
+
module Postmark
|
9
|
+
module SpamCheck
|
10
|
+
class Client
|
11
|
+
SPAM_CHECK_URI = "http://spamcheck.postmarkapp.com/filter"
|
12
|
+
|
13
|
+
def initialize(timeout = 60)
|
14
|
+
@timeout = timeout
|
15
|
+
end
|
16
|
+
|
17
|
+
def check(raw_email, report_format = :long)
|
18
|
+
report = SpamCheck::Report.new(raw_email)
|
19
|
+
result = fetch_report(raw_email, report_format)
|
20
|
+
report.load_results(result)
|
21
|
+
rescue StandardError => error
|
22
|
+
report.error = error.message || error.inspect
|
23
|
+
report
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
def fetch_report(raw_email, report_format)
|
28
|
+
response = http_request({email: raw_email, options: report_format})
|
29
|
+
JSON.parse(response.body)
|
30
|
+
end
|
31
|
+
|
32
|
+
def http_request(params)
|
33
|
+
uri = URI.parse(SPAM_CHECK_URI)
|
34
|
+
http = Net::HTTP.new(uri.host, uri.port)
|
35
|
+
http.read_timeout = @timeout
|
36
|
+
request = Net::HTTP::Post.new(uri.request_uri, {'Content-Type' =>'application/json'})
|
37
|
+
request.body = params.to_json
|
38
|
+
http.request(request)
|
39
|
+
end
|
40
|
+
end
|
41
|
+
end
|
42
|
+
end
|
@@ -0,0 +1,66 @@
|
|
1
|
+
module Postmark
|
2
|
+
module SpamCheck
|
3
|
+
class Report
|
4
|
+
attr_reader :email, :score, :details
|
5
|
+
attr_accessor :error
|
6
|
+
|
7
|
+
def initialize(email)
|
8
|
+
@email = email
|
9
|
+
@score = nil
|
10
|
+
@error = nil
|
11
|
+
@details = []
|
12
|
+
end
|
13
|
+
|
14
|
+
def load_results(results)
|
15
|
+
# Truthy success means the report was generated successfully.
|
16
|
+
if results['success']
|
17
|
+
results_for_success(results)
|
18
|
+
else
|
19
|
+
results_for_failure(results)
|
20
|
+
end
|
21
|
+
|
22
|
+
self
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
def results_for_success(results)
|
27
|
+
@score = results['score']
|
28
|
+
parse_spamassassin_report(results['report'])
|
29
|
+
end
|
30
|
+
|
31
|
+
def results_for_failure(results)
|
32
|
+
@error = results['message']
|
33
|
+
end
|
34
|
+
|
35
|
+
def parse_spamassassin_report(report_text = nil)
|
36
|
+
return [] unless report_text
|
37
|
+
|
38
|
+
# NOTE: Possible multi-line description. Example:
|
39
|
+
|
40
|
+
# pts rule name description
|
41
|
+
# ---- ---------------------- --------------------------------------------------
|
42
|
+
# 0.0 FREEMAIL_FROM Sender email is commonly abused enduser mail provider
|
43
|
+
# (hello[at]example.com)
|
44
|
+
last_rule = nil
|
45
|
+
|
46
|
+
# Ignore the first two lines.
|
47
|
+
report_text.split(/\n/)[2..-1].each do |line|
|
48
|
+
rule, description = line[5..-1].strip.split(/\s{2,}/, 2).map(&:strip)
|
49
|
+
|
50
|
+
if description
|
51
|
+
points = line[0..3].strip.to_f
|
52
|
+
last_rule = { points: points, rule: rule, description: description }
|
53
|
+
@details << last_rule
|
54
|
+
|
55
|
+
# Nil description means second line of description is in "rule".
|
56
|
+
# Tack it on to the description of the last one.
|
57
|
+
elsif last_rule
|
58
|
+
last_rule[:description] << " #{rule}"
|
59
|
+
end
|
60
|
+
end
|
61
|
+
rescue StandardError => error
|
62
|
+
@error = error.message
|
63
|
+
end
|
64
|
+
end
|
65
|
+
end
|
66
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'postmark/spam_check/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "postmark-spam_check"
|
8
|
+
spec.version = Postmark::SpamCheck::VERSION
|
9
|
+
spec.authors = ["Brad Pauly"]
|
10
|
+
spec.email = ["hi@bradpauly.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{ruby client for http://spamcheck.postmarkapp.com/doc}
|
13
|
+
spec.description = %q{Takes a raw email and fetches the spamassassin score and report. No dependencies outside of ruby's standard libraries.}
|
14
|
+
spec.homepage = "https://github.com/bradpauly/postmark-spam_check"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
18
|
+
spec.bindir = "exe"
|
19
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
20
|
+
spec.require_paths = ["lib"]
|
21
|
+
|
22
|
+
spec.add_development_dependency "bundler", "~> 1.10"
|
23
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
24
|
+
spec.add_development_dependency "minitest"
|
25
|
+
end
|
metadata
ADDED
@@ -0,0 +1,102 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: postmark-spam_check
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Brad Pauly
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-10-21 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.10'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.10'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: minitest
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
description: Takes a raw email and fetches the spamassassin score and report. No dependencies
|
56
|
+
outside of ruby's standard libraries.
|
57
|
+
email:
|
58
|
+
- hi@bradpauly.com
|
59
|
+
executables: []
|
60
|
+
extensions: []
|
61
|
+
extra_rdoc_files: []
|
62
|
+
files:
|
63
|
+
- ".gitignore"
|
64
|
+
- ".travis.yml"
|
65
|
+
- CODE_OF_CONDUCT.md
|
66
|
+
- Gemfile
|
67
|
+
- LICENSE.txt
|
68
|
+
- README.md
|
69
|
+
- Rakefile
|
70
|
+
- bin/console
|
71
|
+
- bin/setup
|
72
|
+
- lib/postmark/spam_check.rb
|
73
|
+
- lib/postmark/spam_check/client.rb
|
74
|
+
- lib/postmark/spam_check/report.rb
|
75
|
+
- lib/postmark/spam_check/version.rb
|
76
|
+
- postmark-spam_check.gemspec
|
77
|
+
homepage: https://github.com/bradpauly/postmark-spam_check
|
78
|
+
licenses:
|
79
|
+
- MIT
|
80
|
+
metadata: {}
|
81
|
+
post_install_message:
|
82
|
+
rdoc_options: []
|
83
|
+
require_paths:
|
84
|
+
- lib
|
85
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
91
|
+
requirements:
|
92
|
+
- - ">="
|
93
|
+
- !ruby/object:Gem::Version
|
94
|
+
version: '0'
|
95
|
+
requirements: []
|
96
|
+
rubyforge_project:
|
97
|
+
rubygems_version: 2.2.2
|
98
|
+
signing_key:
|
99
|
+
specification_version: 4
|
100
|
+
summary: ruby client for http://spamcheck.postmarkapp.com/doc
|
101
|
+
test_files: []
|
102
|
+
has_rdoc:
|