mailbot 0.0.1

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
+ SHA1:
3
+ metadata.gz: a50225982a8cfa58cc26463a849817a8430f4b6f
4
+ data.tar.gz: 1fa013de47f20e3e33bf483a027db0be56eda42a
5
+ SHA512:
6
+ metadata.gz: 1b3c336a2ae3e473f1019c06171c07ffe777ded64deb21fbc6d7e069a1689704bb7a0c5aed5545a194e14f30a8bcc5259f5c69629465b29dd9b9907257bf0dcb
7
+ data.tar.gz: 810bcb753c25457dafc336850f07e8132d49f64226a3927539f29dd375e0cc17e07b4b0108cac22bce5a454a9773247b24ef9c7808d29ecd94c37e18820c7d9e
data/.gitignore ADDED
@@ -0,0 +1,18 @@
1
+ *.gem
2
+ *.rbc
3
+ .env
4
+ .bundle
5
+ .config
6
+ .yardoc
7
+ Gemfile.lock
8
+ InstalledFiles
9
+ _yardoc
10
+ coverage
11
+ doc/
12
+ lib/bundler/man
13
+ pkg
14
+ rdoc
15
+ spec/reports
16
+ test/tmp
17
+ test/version_tmp
18
+ tmp
data/.travis.yml ADDED
@@ -0,0 +1,3 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.1.0
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mailbot.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 monzou
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,9 @@
1
+ # Mailbot
2
+
3
+ Learn with [Mailbox](http://www.mailboxapp.com/)
4
+
5
+ ## Usage
6
+
7
+ ```
8
+ $ mailbot sync --file study.md
9
+ ```
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new do |t|
5
+ t.libs << "lib"
6
+ t.libs << "test"
7
+ t.test_files = Dir.glob "test/**/*_test.rb"
8
+ end
data/bin/mailbot ADDED
@@ -0,0 +1,6 @@
1
+ #!/usr/bin/env ruby
2
+ $LOAD_PATH.unshift File.expand_path("../../lib", __FILE__)
3
+ require "mailbot"
4
+
5
+ Dotenv.load!
6
+ Mailbot::CommandFactory.create(ARGV).execute
@@ -0,0 +1,48 @@
1
+ module Mailbot
2
+
3
+ class CommandFactory
4
+
5
+ # @return [Mailbot::Commands::Base]
6
+ def self.create(*args)
7
+ new(*args).create
8
+ end
9
+
10
+ # @param argv [Array] ARGV
11
+ def initialize(argv)
12
+ @argv = argv
13
+ end
14
+
15
+ # @return [Mailbot::Commands::Base]
16
+ def create
17
+ command_class.new @argv
18
+ rescue Errors::CommandNotFound
19
+ terminate
20
+ end
21
+
22
+ private
23
+
24
+ def command_class
25
+ case command_name
26
+ when "sync"
27
+ Commands::Sync
28
+ else
29
+ raise Errors::CommandNotFound
30
+ end
31
+ end
32
+
33
+ def command_name
34
+ @argv[0]
35
+ end
36
+
37
+ def usage
38
+ "Usage: #{$0} <command> [options]"
39
+ end
40
+
41
+ def terminate
42
+ warn usage
43
+ exit 1
44
+ end
45
+
46
+ end
47
+
48
+ end
@@ -0,0 +1,6 @@
1
+ module Mailbot
2
+ module Commands
3
+ class Base
4
+ end
5
+ end
6
+ end
@@ -0,0 +1,30 @@
1
+ module Mailbot
2
+ module Commands
3
+ class Sync < Base
4
+
5
+ # @param argv [Array] ARGV
6
+ def initialize(argv)
7
+ @argv = argv
8
+ end
9
+
10
+ # Sync given file to Mailbox
11
+ def execute
12
+ Mailbot::Repository.new(file).sync
13
+ end
14
+
15
+ private
16
+
17
+ def file
18
+ options[:file]
19
+ end
20
+
21
+ def options
22
+ @options ||= Slop.parse!(@argv, help: true) do
23
+ banner "Usage: #{$0} sync [options]"
24
+ on "file=", "Path to the markdown file to sync"
25
+ end
26
+ end
27
+
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,85 @@
1
+ module Mailbot
2
+
3
+ class Entry
4
+
5
+ SYNC_MARK = "✓"
6
+ HEADER_LINE_PATTERN = /^#\s+(#{SYNC_MARK}\s*)?(.+)$/
7
+
8
+ attr_accessor :text
9
+
10
+ # @param text [String] The raw text of this entry
11
+ def initialize(text)
12
+ @text = text
13
+ end
14
+
15
+ # @return [String] A subject of this entry
16
+ def subject
17
+ headers.reject { |header| header =~ /#{SYNC_MARK}/ }.first
18
+ end
19
+
20
+ # @return [String] An HTML representation of this entry
21
+ def render_body
22
+ Mailbot::Renderer.render @text.sub(HEADER_LINE_PATTERN, "")
23
+ end
24
+
25
+ # @return [Boolean] True if this entry is already synced to Mailbox
26
+ def synced?
27
+ headers.include? SYNC_MARK
28
+ end
29
+
30
+ # @return [Mailbot::Entry] A synced entry
31
+ def sync
32
+ Entry.new(@text.sub HEADER_LINE_PATTERN, "# #{SYNC_MARK} \\2")
33
+ end
34
+
35
+ # @note Overridden
36
+ # @return [String] A String representation of this entry
37
+ def to_s
38
+ @text
39
+ end
40
+
41
+ class Parser
42
+
43
+ def initialize
44
+ @entries = []
45
+ @buffer = ""
46
+ end
47
+
48
+ # @param text [String] The raw text to parse to entries
49
+ # @return [Array<Mailbot::Entry>] An Array of parsed entries
50
+ def parse(text)
51
+ text.strip.each_line do |line|
52
+ if @buffer.empty?
53
+ @buffer = line
54
+ elsif line =~ HEADER_LINE_PATTERN
55
+ @buffer = line if flush
56
+ else
57
+ @buffer << line
58
+ end
59
+ end
60
+ flush
61
+ @entries
62
+ end
63
+
64
+ private
65
+
66
+ def flush
67
+ unless @buffer.empty?
68
+ @entries << Entry.new("#{@buffer.strip}\n")
69
+ true
70
+ else
71
+ false
72
+ end
73
+ end
74
+
75
+ end
76
+
77
+ private
78
+
79
+ def headers
80
+ @text.scan(HEADER_LINE_PATTERN).flatten.compact.map(&:strip)
81
+ end
82
+
83
+ end
84
+
85
+ end
@@ -0,0 +1,11 @@
1
+ module Mailbot
2
+ module Errors
3
+
4
+ class Base < StandardError
5
+ end
6
+
7
+ class CommandNotFound < Base
8
+ end
9
+
10
+ end
11
+ end
@@ -0,0 +1,3 @@
1
+ module Mailbot
2
+ LOGGER = Logger.new STDOUT
3
+ end
@@ -0,0 +1,38 @@
1
+ module Mailbot
2
+
3
+ module Mailer
4
+
5
+ BLOCK_SYMBOL = :mailbot_mailer_block
6
+
7
+ class << self
8
+
9
+ def deliver(subject: "", body: "")
10
+ block.call subject, body
11
+ end
12
+
13
+ def set(&block)
14
+ Thread.current[BLOCK_SYMBOL] = block
15
+ end
16
+
17
+ def block
18
+ Thread.current[BLOCK_SYMBOL] || default
19
+ end
20
+
21
+ def default
22
+ ->(subject, body) do
23
+ mailgun = Mailgun::Client.new ENV["MAILBOT_MAILGUN_API_KEY"]
24
+ params = {
25
+ :from => ENV["MAILBOT_MAIL_FROM"],
26
+ :to => ENV["MAILBOT_MAIL_TO"],
27
+ :subject => subject,
28
+ :html => body
29
+ }
30
+ mailgun.send_message ENV["MAILBOT_MAILGUN_DOMAIN"], params
31
+ end
32
+ end
33
+
34
+ end
35
+
36
+ end
37
+
38
+ end
@@ -0,0 +1,39 @@
1
+ module Mailbot
2
+
3
+ module Renderer
4
+
5
+ BLOCK_SYMBOL = :mailbot_markdown_block
6
+
7
+ class HTMLwithCodeRay < Redcarpet::Render::HTML
8
+ def block_code(code, language)
9
+ CodeRay.scan(code, language.to_sym).div
10
+ end
11
+ end
12
+
13
+ class << self
14
+
15
+ def render(text)
16
+ block.call text
17
+ end
18
+
19
+ def set(&block)
20
+ Thread.current[BLOCK_SYMBOL] = block
21
+ end
22
+
23
+ def block
24
+ Thread.current[BLOCK_SYMBOL] || default
25
+ end
26
+
27
+ def default
28
+ ->(text) do
29
+ renderer = HTMLwithCodeRay.new :filter_html => true, :hard_wrap => true
30
+ markdown = Redcarpet::Markdown.new renderer, :autolink => true, :space_after_headers => true, :fenced_code_blocks => true
31
+ markdown.render text
32
+ end
33
+ end
34
+
35
+ end
36
+
37
+ end
38
+
39
+ end
@@ -0,0 +1,43 @@
1
+ module Mailbot
2
+
3
+ class Repository
4
+
5
+ # @param path [String] The path to the text file to sync
6
+ def initialize(path)
7
+ @path = path
8
+ end
9
+
10
+ def sync
11
+ write(entries.map do |entry|
12
+ if entry.synced?
13
+ entry
14
+ else
15
+ begin
16
+ Mailbot::Mailer.deliver subject: entry.subject, body: entry.render_body
17
+ Mailbot::LOGGER.info "Succeeded to sync an entry: #{entry.subject}"
18
+ entry.sync
19
+ rescue => e
20
+ Mailbot::LOGGER.warn "Failed to sync an entry: #{entry.subject}\n#{e.backtrace.join("\n")}"
21
+ entry
22
+ end
23
+ end
24
+ end.map(&:text).join("\n"))
25
+ end
26
+
27
+ private
28
+
29
+ def write(text)
30
+ open(@path, "w:UTF-8").write text
31
+ end
32
+
33
+ def read
34
+ open(@path, "r:UTF-8").read
35
+ end
36
+
37
+ def entries
38
+ Mailbot::Entry::Parser.new.parse read
39
+ end
40
+
41
+ end
42
+
43
+ end
@@ -0,0 +1,3 @@
1
+ module Mailbot
2
+ VERSION = "0.0.1"
3
+ end
data/lib/mailbot.rb ADDED
@@ -0,0 +1,18 @@
1
+ require "logger"
2
+
3
+ require "dotenv"
4
+ require "slop"
5
+ require "coderay"
6
+ require "redcarpet"
7
+ require "mailgun"
8
+
9
+ require "mailbot/command_factory"
10
+ require "mailbot/commands/base"
11
+ require "mailbot/commands/sync"
12
+ require "mailbot/logger"
13
+ require "mailbot/entry"
14
+ require "mailbot/errors"
15
+ require "mailbot/mailer"
16
+ require "mailbot/renderer"
17
+ require "mailbot/repository"
18
+ require "mailbot/version"
data/mailbot.gemspec ADDED
@@ -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 'mailbot/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mailbot"
8
+ spec.version = Mailbot::VERSION
9
+ spec.authors = ["monzou"]
10
+ spec.email = ["1984tkr@gmail.com"]
11
+ spec.summary = "Learn with Mailbox"
12
+ spec.homepage = "https://github.com/monzou/mailbot"
13
+ spec.license = "MIT"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0")
16
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
17
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "dotenv"
21
+ spec.add_dependency "slop"
22
+ spec.add_dependency "coderay"
23
+ spec.add_dependency "redcarpet"
24
+ spec.add_dependency "mailgun-ruby"
25
+ spec.add_development_dependency "bundler", "~> 1.5"
26
+ spec.add_development_dependency "rake"
27
+ end
@@ -0,0 +1,50 @@
1
+ require "test_helper"
2
+
3
+ class EntryTest < Minitest::Unit::TestCase
4
+
5
+ def setup
6
+ @subject = Mailbot::Entry::Parser.new
7
+ end
8
+
9
+ def test_parse
10
+ entries = @subject.parse <<-"EOS"
11
+ # Entry 1
12
+
13
+ ## Entry 1 Sub Header
14
+
15
+ - foo
16
+ - bar
17
+
18
+ ```ruby
19
+ puts "foo"
20
+ ```
21
+
22
+ # ✓ Entry 2
23
+
24
+ ## Entry 2 Sub Header
25
+
26
+ - foo
27
+ - bar
28
+ EOS
29
+
30
+ assert_equal 2, entries.size
31
+
32
+ entry1 = entries[0]
33
+ assert_equal false, entry1.synced?
34
+ assert_equal "Entry 1", entry1.subject
35
+ assert_equal false, entry1.render_body.include?("<h1>")
36
+ assert_equal true, entry1.render_body.include?("<h2>")
37
+ assert_equal true, entry1.render_body.include?("<ul>")
38
+ assert_equal true, entry1.render_body.include?("<div class=\"CodeRay\">")
39
+
40
+ entry2 = entries[1]
41
+ assert_equal true, entry2.synced?
42
+ assert_equal "Entry 2", entry2.subject
43
+ assert_equal false, entry2.render_body.include?("<h1>")
44
+ assert_equal true, entry2.render_body.include?("<h2>")
45
+ assert_equal true, entry2.render_body.include?("<ul>")
46
+ assert_equal false, entry2.render_body.include?("<div class=\"CodeRay\">")
47
+
48
+ end
49
+
50
+ end
@@ -0,0 +1,5 @@
1
+ require "minitest/unit"
2
+ require "minitest/autorun"
3
+ require "minitest/pride"
4
+
5
+ require "mailbot"
metadata ADDED
@@ -0,0 +1,166 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mailbot
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - monzou
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2014-08-29 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: dotenv
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - ">="
18
+ - !ruby/object:Gem::Version
19
+ version: '0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: '0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: slop
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: coderay
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: redcarpet
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :runtime
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: mailgun-ruby
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :runtime
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: bundler
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '1.5'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '1.5'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rake
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description:
112
+ email:
113
+ - 1984tkr@gmail.com
114
+ executables:
115
+ - mailbot
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".travis.yml"
121
+ - Gemfile
122
+ - LICENSE.txt
123
+ - README.md
124
+ - Rakefile
125
+ - bin/mailbot
126
+ - lib/mailbot.rb
127
+ - lib/mailbot/command_factory.rb
128
+ - lib/mailbot/commands/base.rb
129
+ - lib/mailbot/commands/sync.rb
130
+ - lib/mailbot/entry.rb
131
+ - lib/mailbot/errors.rb
132
+ - lib/mailbot/logger.rb
133
+ - lib/mailbot/mailer.rb
134
+ - lib/mailbot/renderer.rb
135
+ - lib/mailbot/repository.rb
136
+ - lib/mailbot/version.rb
137
+ - mailbot.gemspec
138
+ - test/entry_test.rb
139
+ - test/test_helper.rb
140
+ homepage: https://github.com/monzou/mailbot
141
+ licenses:
142
+ - MIT
143
+ metadata: {}
144
+ post_install_message:
145
+ rdoc_options: []
146
+ require_paths:
147
+ - lib
148
+ required_ruby_version: !ruby/object:Gem::Requirement
149
+ requirements:
150
+ - - ">="
151
+ - !ruby/object:Gem::Version
152
+ version: '0'
153
+ required_rubygems_version: !ruby/object:Gem::Requirement
154
+ requirements:
155
+ - - ">="
156
+ - !ruby/object:Gem::Version
157
+ version: '0'
158
+ requirements: []
159
+ rubyforge_project:
160
+ rubygems_version: 2.4.1
161
+ signing_key:
162
+ specification_version: 4
163
+ summary: Learn with Mailbox
164
+ test_files:
165
+ - test/entry_test.rb
166
+ - test/test_helper.rb