pic_of_the_day 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1440f925ed69c6e103ab7baa3630ba2ce79fb89d
4
+ data.tar.gz: 8500f4cc0ba4e4f4ed493cbf2bdcbd84439b6602
5
+ SHA512:
6
+ metadata.gz: c0598bd38e8986b9e150009360774b470754f0b70042ccdf21cb19563e6e6285293c93f7333858854d44f833cced2cf0807a14f35b3d698ef47279949160a78d
7
+ data.tar.gz: 55e6698a828d9a225f30e349e9180036a0982539a416c361131f9d5ba98f3679a6bd5f36bae92e8df71aca1dfe3dab6104eeb3d8a656e5e056580494a62451cd
@@ -0,0 +1,10 @@
1
+ *.gem
2
+ /.bundle/
3
+ /.yardoc
4
+ /Gemfile.lock
5
+ /_yardoc/
6
+ /coverage/
7
+ /doc/
8
+ /pkg/
9
+ /spec/reports/
10
+ /tmp/
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.6
4
+ before_install: gem install bundler -v 1.10.5
data/Gemfile ADDED
@@ -0,0 +1,2 @@
1
+ source 'https://rubygems.org'
2
+ gemspec
@@ -0,0 +1,2 @@
1
+ Picks a random image from a list and sends it as an inline
2
+ attachment via email.
@@ -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
@@ -0,0 +1,11 @@
1
+ #!/usr/bin/ruby
2
+ require 'pic_of_the_day'
3
+
4
+ options = PicOfTheDay::OptionParser.new(ARGV).parse
5
+
6
+ begin
7
+ PicOfTheDay.send_email(**options)
8
+ rescue PicOfTheDay::Error => e
9
+ puts "#{e.class.name}: #{e.message}"
10
+ exit 1
11
+ end
@@ -0,0 +1,21 @@
1
+ require "pic_of_the_day/version"
2
+ require "pic_of_the_day/file_picker"
3
+ require "pic_of_the_day/mailer"
4
+ require "pic_of_the_day/option_parser"
5
+
6
+ module PicOfTheDay
7
+ Error = Class.new(::StandardError)
8
+ NoImagesLeftError = Class.new(Error)
9
+ MissingArgumentError = Class.new(Error)
10
+
11
+ def self.send_email(from:, to:, subject:, files:, blacklist_file_path:)
12
+ @picker = PicOfTheDay::FilePicker.new(files: files, blacklist_file_path: blacklist_file_path)
13
+
14
+ if picked_image = @picker.pick
15
+ @mailer = PicOfTheDay::Mailer.mail_with_picture(from: from, to: to, subject: subject, image_path: picked_image)
16
+ @mailer.deliver_now
17
+ else
18
+ raise NoImagesLeftError, "There are no images left that haven't been used yet."
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,39 @@
1
+ module PicOfTheDay
2
+ class FilePicker
3
+ def initialize(files:, blacklist_file_path:)
4
+ @files = files
5
+ @blacklist_file_path = blacklist_file_path
6
+ end
7
+
8
+ def pick
9
+ picked_file = random_file
10
+ return if picked_file.nil?
11
+ blacklist_file(picked_file)
12
+ picked_file
13
+ end
14
+
15
+ private
16
+
17
+ def random_file
18
+ unused_files.sample
19
+ end
20
+
21
+ def unused_files
22
+ @files - blacklisted_files
23
+ end
24
+
25
+ def blacklisted_files
26
+ if File.exist?(@blacklist_file_path)
27
+ File.readlines(@blacklist_file_path).map(&:chomp)
28
+ else
29
+ []
30
+ end
31
+ end
32
+
33
+ def blacklist_file(file_path)
34
+ File.open(@blacklist_file_path, 'a') do |f|
35
+ f.puts(file_path)
36
+ end
37
+ end
38
+ end
39
+ end
@@ -0,0 +1,21 @@
1
+ require 'action_mailer'
2
+
3
+ ActionMailer::Base.delivery_method = :sendmail
4
+ ActionMailer::Base.view_paths = File.expand_path('../../../templates/', __FILE__)
5
+
6
+ module PicOfTheDay
7
+ class Mailer < ActionMailer::Base
8
+ def mail_with_picture(from:, to:, subject:, image_path:)
9
+ @filename = File.basename(image_path)
10
+ attachments.inline[@filename] = File.read(image_path)
11
+
12
+ mail(
13
+ from: from,
14
+ to: to,
15
+ subject: subject,
16
+ template_path: "",
17
+ content_type: 'text/html'
18
+ )
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,44 @@
1
+ require 'optparse'
2
+
3
+ module PicOfTheDay
4
+ class OptionParser
5
+ def initialize(args)
6
+ @args = args
7
+ end
8
+
9
+ def parse
10
+ options = {}
11
+ options[:blacklist_file_path] = "#{ENV['PWD']}/pic_of_the_day.blacklist"
12
+
13
+ parser = ::OptionParser.new do |opts|
14
+ opts.banner = "Usage: #{$0} [options]"
15
+
16
+ opts.on("-f", "--from ADDRESS", "From") do |from|
17
+ options[:from] = from
18
+ end
19
+
20
+ opts.on("-t", "--to ADDRESS", "To") do |to|
21
+ options[:to] = to
22
+ end
23
+
24
+ opts.on("-s", "--subject SUBJECT", "Subject") do |subject|
25
+ options[:subject] = subject
26
+ end
27
+
28
+ opts.on("-b", "--blacklist", "Blacklist file path (default: #{options[:blacklist_file_path]}") do |blacklist|
29
+ options[:blacklist_file_path] = blacklist
30
+ end
31
+ end
32
+
33
+ parser.parse!(@args)
34
+
35
+ [:from, :to, :subject].each do |key|
36
+ next if options.key?(key)
37
+ raise MissingArgumentError, "Missing required argument --#{key}"
38
+ end
39
+
40
+ options[:files] = @args
41
+ options
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module PicOfTheDay
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'pic_of_the_day/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "pic_of_the_day"
8
+ spec.version = PicOfTheDay::VERSION
9
+ spec.authors = ["Florian Weingarten"]
10
+ spec.email = ["flo@hackvalue.de"]
11
+
12
+ spec.summary = %q{Send a random image via email}
13
+ spec.description = %q{Send a random image via email}
14
+ spec.homepage = "https://github.com/fw42/pic_of_the_day"
15
+
16
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
17
+ spec.bindir = "exe"
18
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_runtime_dependency "actionmailer", "~> 4.2"
22
+
23
+ spec.add_development_dependency "bundler", "~> 1.10"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "minitest", "~> 5.7"
26
+ spec.add_development_dependency "byebug", "~> 0"
27
+ end
@@ -0,0 +1,10 @@
1
+ <!DOCTYPE html>
2
+ <html>
3
+ <head>
4
+ <meta content='text/html; charset=UTF-8' http-equiv='Content-Type' />
5
+ </head>
6
+
7
+ <body>
8
+ <%= image_tag attachments[@filename].url %>
9
+ </body>
10
+ </html>
metadata ADDED
@@ -0,0 +1,126 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: pic_of_the_day
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Florian Weingarten
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-07-18 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: actionmailer
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '4.2'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '4.2'
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.10'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.10'
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.7'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '5.7'
69
+ - !ruby/object:Gem::Dependency
70
+ name: byebug
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: Send a random image via email
84
+ email:
85
+ - flo@hackvalue.de
86
+ executables: []
87
+ extensions: []
88
+ extra_rdoc_files: []
89
+ files:
90
+ - ".gitignore"
91
+ - ".travis.yml"
92
+ - Gemfile
93
+ - README.md
94
+ - Rakefile
95
+ - bin/pic_of_the_day
96
+ - lib/pic_of_the_day.rb
97
+ - lib/pic_of_the_day/file_picker.rb
98
+ - lib/pic_of_the_day/mailer.rb
99
+ - lib/pic_of_the_day/option_parser.rb
100
+ - lib/pic_of_the_day/version.rb
101
+ - pic_of_the_day.gemspec
102
+ - templates/mail_with_picture.html.erb
103
+ homepage: https://github.com/fw42/pic_of_the_day
104
+ licenses: []
105
+ metadata: {}
106
+ post_install_message:
107
+ rdoc_options: []
108
+ require_paths:
109
+ - lib
110
+ required_ruby_version: !ruby/object:Gem::Requirement
111
+ requirements:
112
+ - - ">="
113
+ - !ruby/object:Gem::Version
114
+ version: '0'
115
+ required_rubygems_version: !ruby/object:Gem::Requirement
116
+ requirements:
117
+ - - ">="
118
+ - !ruby/object:Gem::Version
119
+ version: '0'
120
+ requirements: []
121
+ rubyforge_project:
122
+ rubygems_version: 2.2.3
123
+ signing_key:
124
+ specification_version: 4
125
+ summary: Send a random image via email
126
+ test_files: []