Skip to content

Commit

Permalink
Set up RuboCop
Browse files Browse the repository at this point in the history
  • Loading branch information
infertux committed Jan 10, 2017
1 parent 86e1c9f commit 08db4db
Show file tree
Hide file tree
Showing 13 changed files with 192 additions and 163 deletions.
2 changes: 1 addition & 1 deletion .rspec
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
--color
--format d
--order random
--require spec_helper
17 changes: 17 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
Metrics:
Enabled: false

Style/AccessModifierIndentation:
EnforcedStyle: outdent

Style/SafeNavigation:
Enabled: false

Style/StringLiterals:
EnforcedStyle: double_quotes

Style/TrailingCommaInArguments:
EnforcedStyleForMultiline: comma

Style/TrailingCommaInLiteral:
EnforcedStyleForMultiline: comma
4 changes: 3 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
source 'https://rubygems.org'
# frozen_string_literal: true

source "https://rubygems.org"

# Specify your gem's dependencies in imap_guard.gemspec
gemspec
7 changes: 4 additions & 3 deletions Guardfile
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
# frozen_string_literal: true

# More info at https://github.com/guard/guard#readme

guard 'rspec' do
guard "rspec" do
watch(%r{^spec/.+_spec\.rb$})
watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
watch(%r{^lib/(.+)/(.+)\.rb$}) { |m| "spec/#{m[1]}/#{m[2]}_spec.rb" }
watch('spec/spec_helper.rb') { "spec" }
watch("spec/spec_helper.rb") { "spec" }
watch(%r{^spec/support/.+\.rb$}) { "spec" }
end

8 changes: 6 additions & 2 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
# frozen_string_literal: true

require "bundler/gem_tasks"
require "rspec/core/rake_task"
require "rubocop/rake_task"

RSpec::Core::RakeTask.new(:spec) do |t|
t.ruby_opts = '-w'
t.ruby_opts = "-w"
end

task default: :spec
RuboCop::RakeTask.new(:rubocop)

task default: [:spec, :rubocop]
28 changes: 15 additions & 13 deletions examples/example.rb
Original file line number Diff line number Diff line change
@@ -1,43 +1,45 @@
#!/usr/bin/env ruby

require 'imap_guard'
# frozen_string_literal: true
# rubocop:disable all

require "imap_guard"

SETTINGS = {
host: 'imap.googlemail.com',
host: "imap.googlemail.com",
port: 993,
username: 'you@gmail.com',
password: 'your_pass',
}
username: "you@gmail.com",
password: "your_pass",
}.freeze

settings = SETTINGS.merge({ read_only: false })
settings = SETTINGS.merge(read_only: false)
base_query = ImapGuard::Query.new.unflagged.unanswered.freeze
guard = ImapGuard::Guard.new settings
# guard.debug = ->(mail) { print "#{mail.subject}: " }
guard.login

guard.select 'INBOX'
guard.select "INBOX"

# Github
%w(github.com notifications@travis-ci.org app@gemnasium.com).map do |from|
base_query.dup.from(from)
end.each do |query|
guard.move query, 'INBOX.Github'
guard.move query, "INBOX.Github"
end

# To Do
guard.move base_query.dup.from("me").to("me"), 'INBOX.TODO'
guard.move base_query.dup.from("me").to("me"), "INBOX.TODO"

# Ops
guard.select 'INBOX.Ops'
guard.select "INBOX.Ops"
query = base_query.dup.seen
guard.delete query.dup.subject("monit alert -- ").before(7)
guard.delete query.dup.subject("CRON-APT completed on ").before(3)
guard.delete query.dup.subject("Logwatch for ").before(7)
guard.select 'INBOX'
guard.select "INBOX"

# Uni
guard.move base_query.dup.or.from("uni.tld").to("uni.tld"), 'INBOX.Uni'
guard.move base_query.dup.or.from("uni.tld").to("uni.tld"), "INBOX.Uni"

# Bye!
guard.disconnect

31 changes: 16 additions & 15 deletions imap_guard.gemspec
Original file line number Diff line number Diff line change
@@ -1,32 +1,33 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
# frozen_string_literal: true

lib = File.expand_path("../lib", __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)

Gem::Specification.new do |spec|
spec.name = "imap_guard"
spec.version = "1.0.0"
spec.version = "1.1.0"
spec.authors = ["Cédric Félizard"]
spec.email = ["cedric@felizard.fr"]
spec.description = %q{A guard for your IMAP server}
spec.description = "A guard for your IMAP server"
spec.summary = spec.description
spec.homepage = "https://github.com/infertux/imap_guard"
spec.license = "MIT"

spec.files = `git ls-files`.split($/)
spec.files = `git ls-files`.split($INPUT_RECORD_SEPARATOR)
spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ["lib"]

spec.add_dependency 'mail', '>= 2.5.3'
spec.add_dependency 'term-ansicolor', '>= 1.2.2'
spec.add_dependency "mail", ">= 2.5.3"
spec.add_dependency "term-ansicolor", ">= 1.2.2"

spec.add_development_dependency 'bundler', '>= 1.3'
spec.add_development_dependency 'rake'
spec.add_development_dependency 'simplecov'
spec.add_development_dependency 'rspec'
spec.add_development_dependency 'guard-rspec'
spec.add_development_dependency 'rb-inotify'
spec.add_development_dependency 'cane'
spec.add_development_dependency 'yard'
spec.add_development_dependency 'redcarpet' # for yardoc
spec.add_development_dependency "bundler", ">= 1.3"
spec.add_development_dependency "rake"
spec.add_development_dependency "rubocop"
spec.add_development_dependency "simplecov"
spec.add_development_dependency "rspec"
spec.add_development_dependency "cane"
spec.add_development_dependency "yard"
spec.add_development_dependency "redcarpet" # for yardoc
end
2 changes: 2 additions & 0 deletions lib/imap_guard.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
# frozen_string_literal: true

require "imap_guard/guard"
require "imap_guard/query"

Expand Down
68 changes: 34 additions & 34 deletions lib/imap_guard/guard.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
require 'net/imap'
require 'ostruct'
require 'mail'
require 'term/ansicolor'
# frozen_string_literal: true

require "net/imap"
require "ostruct"
require "mail"
require "term/ansicolor"

String.send(:include, Term::ANSIColor)
Term::ANSIColor::coloring = STDOUT.isatty
Term::ANSIColor.coloring = STDOUT.isatty

module ImapGuard
# Guard allows you to process your mailboxes.
class Guard
# List of required settings
REQUIRED_SETTINGS = [:host, :port, :username, :password]
REQUIRED_SETTINGS = [:host, :port, :username, :password].freeze

# List of optional settings
OPTIONAL_SETTINGS = [:read_only, :verbose]
OPTIONAL_SETTINGS = [:read_only, :verbose].freeze

# @return [Proc, nil] Matched emails are passed to this debug lambda if present
attr_accessor :debug
Expand All @@ -24,7 +27,7 @@ class Guard
# @return [String, nil] Currently selected mailbox
attr_reader :mailbox

def initialize settings
def initialize(settings)
self.settings = settings
end

Expand All @@ -39,7 +42,7 @@ def login

# Selects a mailbox (folder)
# @return [void]
def select mailbox
def select(mailbox)
if @settings.read_only
@imap.examine(mailbox) # open in read-only
else
Expand All @@ -53,47 +56,47 @@ def select mailbox
# @param mailbox Destination mailbox
# @param filter Optional filter block
# @return [void]
def move query, mailbox, &filter
operation = lambda { |message_id|
def move(query, mailbox, &filter)
operation = lambda do |message_id|
unless @settings.read_only
@imap.copy(message_id, mailbox)
@imap.store(message_id, "+FLAGS", [Net::IMAP::DELETED])
end

"moved to #{mailbox}".yellow
}
end
process query, operation, &filter
end

# Deletes messages matching the query and filter block
# @param query IMAP query
# @param filter Optional filter block
# @return [void]
def delete query, &filter
operation = lambda { |message_id|
def delete(query, &filter)
operation = lambda do |message_id|
unless @settings.read_only
@imap.store(message_id, "+FLAGS", [Net::IMAP::DELETED])
end

'deleted'.red
}
"deleted".red
end
process query, operation, &filter
end

# Runs operation on messages matching the query
# @param query IMAP query
# @param opration Lambda to call on each message
# @return [void]
def each query
operation = lambda { |message_id| yield message_id }
def each(query)
operation = ->(message_id) { yield message_id }
process query, operation
end

# Fetches a message from its UID
# @return [Mail]
# @note We use "BODY.PEEK[]" to avoid setting the \Seen flag.
def fetch_mail message_id
msg = @imap.fetch(message_id, 'BODY.PEEK[]').first.attr['BODY[]']
def fetch_mail(message_id)
msg = @imap.fetch(message_id, "BODY.PEEK[]").first.attr["BODY[]"]
Mail.read_from_string msg
end

Expand Down Expand Up @@ -125,15 +128,15 @@ def disconnect

private

def process query, operation
def process(query, operation)
message_ids = search query
count = message_ids.size

message_ids.each_with_index do |message_id, index|
print "Processing UID #{message_id} (#{index.succ}/#{count}): "

result = true
if block_given? or debug
if block_given? || debug
mail = fetch_mail message_id

debug.call(mail) if debug
Expand All @@ -151,7 +154,7 @@ def process query, operation
expunge
end

def search query
def search(query)
unless [Array, String].any? { |type| query.is_a? type }
raise TypeError, "Query must be either a string holding the entire search string, or a single-dimension array of search keywords and arguments."
end
Expand All @@ -164,18 +167,16 @@ def search query

def verbose
@verbose ||= if @settings.verbose
$stdout
else
# anonymous null object
Class.new do
def method_missing(*)
nil
end
end.new
end
$stdout
else
# anonymous null object
# rubocop:disable all
Class.new do def method_missing(*); nil end end.new
# rubocop:enable all
end
end

def settings= settings
def settings=(settings)
missing = REQUIRED_SETTINGS - settings.keys
raise ArgumentError, "Missing settings: #{missing}" unless missing.empty?

Expand All @@ -187,4 +188,3 @@ def settings= settings
end
end
end

Loading

0 comments on commit 08db4db

Please sign in to comment.