mandrill-template-manager 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: 32ca82c2977cb4fe68da1a2476622918cdafabe7
4
+ data.tar.gz: ae961d59944ce46502552ee4230057cbfe5bcac9
5
+ SHA512:
6
+ metadata.gz: 3e9534c5b89fc18687dff1c435c155e7a50491d47ca2e7b76345e7eee71ef7c71a9e3bff7863eced832d3c373e6929a335fd143bf2b3b9de2a84d57d6297a7f3
7
+ data.tar.gz: 23d4a348a2f661fb13055cb30efddfdf160db004f90545a0a1f3cd955942b74164952fb94bf8d832c22aab55a279423711a7d9b1438f91b8c6044c558f0486c4
@@ -0,0 +1,51 @@
1
+ # Created by https://www.gitignore.io
2
+
3
+ ### Ruby ###
4
+ *.gem
5
+ *.rbc
6
+ /.config
7
+ /coverage/
8
+ /InstalledFiles
9
+ /pkg/
10
+ /spec/reports/
11
+ /test/tmp/
12
+ /test/version_tmp/
13
+ /tmp/
14
+
15
+ ## Specific to RubyMotion:
16
+ .dat*
17
+ .repl_history
18
+ build/
19
+
20
+ ## Documentation cache and generated files:
21
+ /.yardoc/
22
+ /_yardoc/
23
+ /doc/
24
+ /rdoc/
25
+
26
+ ## Environment normalisation:
27
+ /.bundle/
28
+ /vendor/bundle
29
+ /lib/bundler/man/
30
+
31
+ # for a library or gem, you might want to ignore these files since the code is
32
+ # intended to run in multiple environments; otherwise, check them in:
33
+ # Gemfile.lock
34
+ # .ruby-version
35
+ # .ruby-gemset
36
+
37
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
38
+ .rvmrc
39
+
40
+
41
+ ### Vim ###
42
+ [._]*.s[a-w][a-z]
43
+ [._]s[a-w][a-z]
44
+ *.un~
45
+ Session.vim
46
+ .netrwhist
47
+ *~
48
+
49
+ ## Manual
50
+ /.envrc
51
+ /templates/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -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,7 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mandrill-template-manager.gemspec
4
+ gemspec
5
+
6
+ gem 'formatador', github: 'geemus/formatador'
7
+ gem 'unicode'
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 sawanoboly(HiganWorks LLC)
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.
@@ -0,0 +1,97 @@
1
+ # Mandrill Template Manager(CLI)
2
+
3
+ Manage [Mandrill Templates](https://mandrillapp.com/api/docs/templates.ruby.html) by CLI.
4
+
5
+ ## Install
6
+
7
+ Add Gemfile and bundle.
8
+
9
+ ```
10
+ gem 'mandrill-template-manager'
11
+ ```
12
+
13
+ ```
14
+ $ ./bin/mandrilltemplate
15
+ Commands:
16
+ mandrilltemplate delete NAME # delete template from remote.
17
+ mandrilltemplate export NAME # export template from remote to local files.
18
+ mandrilltemplate generate NAME # generate new template files.
19
+ mandrilltemplate help [COMMAND] # Describe available commands or one specific command
20
+ mandrilltemplate list # show template list both of remote and local.
21
+ mandrilltemplate publish NAME # publish template from draft.
22
+ mandrilltemplate upload NAME # upload template to remote as draft.
23
+ ```
24
+
25
+ ## Workflow
26
+
27
+ 1. generate new or export exist template as local files.
28
+ 2. modify and manage under version controle system.
29
+ 3. upload template.
30
+ 4. publish it.
31
+
32
+
33
+ ## Setup
34
+
35
+ APIKEY is read from environment variable.
36
+
37
+ ```
38
+ export MANDRILL_APIKEY='your api key'
39
+ ```
40
+
41
+ Next, check by list subcommand.
42
+
43
+ ```
44
+ $ mandrilltemplate list
45
+
46
+ Remote Templates
47
+ ----------------------
48
+ +----------+----------+----------+--------------+---------------------+---------------------+--------+---------------+-----------------+------------------+--------------------+-----------+-------------------+
49
+ | has_diff | name | slug | publish_name | draft_updated_at | published_at | labels | subject | publish_subject | from_email | publish_from_email | from_name | publish_from_name |
50
+ +----------+----------+----------+--------------+---------------------+---------------------+--------+---------------+-----------------+------------------+--------------------+-----------+-------------------+
51
+ ... remote templates ...
52
+
53
+ Local Templates
54
+ ----------------------
55
+ +----------+----------+------------------+-----------+--------------------+------------------------+
56
+ | name | slug | from_email | from_name | subject | labels |
57
+ +----------+----------+------------------+-----------+--------------------+------------------------+
58
+ ... local templates ...
59
+
60
+ ```
61
+
62
+
63
+ ## Template local files
64
+
65
+ Templates are stored into `templates/` directory.
66
+
67
+ ```
68
+ templates/
69
+ ├── ${template_name1}
70
+ │ ├── code # code contents
71
+ │ ├── metadata.yml # metadata of template
72
+ │ └── text # text contents
73
+ ├── ${template_name2}
74
+ │ ├── code
75
+ │ ├── metadata.yml
76
+ │ └── text
77
+ └── ${template_name3}
78
+ ├── code
79
+ ├── metadata.yml
80
+ └── text
81
+ ```
82
+
83
+ example of metadata.yml
84
+
85
+ ```
86
+ ---
87
+ slug: mission
88
+ labels: []
89
+ subject: "Today's your mission"
90
+ from_email: test@example.com
91
+ from_name: Boss
92
+ ```
93
+
94
+ ## License
95
+
96
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
97
+
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,9 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ $LOAD_PATH.unshift File.join(File.dirname(__FILE__), %w[.. lib])
4
+
5
+ require "thor"
6
+ require "mandrill_template/cli"
7
+ require "mandrill_template/monkey_create_file"
8
+
9
+ MandrillTemplateManager.start(ARGV)
@@ -0,0 +1,165 @@
1
+ require 'mandrill'
2
+ require 'mandrill_template/client'
3
+ require 'mandrill_template/template'
4
+ require 'formatador'
5
+ require 'unicode' unless ['jruby'].include?(RbConfig::CONFIG['ruby_install_name'])
6
+ require 'yaml'
7
+ require "mandrill_template/monkey_create_file"
8
+
9
+ class MandrillTemplateManager < Thor
10
+ include Thor::Actions
11
+ VERSION = "0.1.0"
12
+
13
+ desc "export NAME", "export template from remote to local files."
14
+ def export(name)
15
+ template = MandrillClient.client.templates.info(name)
16
+ meta, code, text = build_template_for_export(template)
17
+ save_as_local_template(name, meta, code, text)
18
+ end
19
+
20
+ desc "upload NAME", "upload template to remote as draft."
21
+ def upload(name)
22
+ template = MandrillTemplate::Local.new(name)
23
+ if template.avail
24
+ upload_template(template)
25
+ else
26
+ puts "Template data not found #{name}. Please generate first."
27
+ end
28
+ end
29
+
30
+ desc "delete NAME", "delete template from remote."
31
+ def delete(name)
32
+ begin
33
+ result = MandrillClient.client.templates.delete(name)
34
+ puts result.to_yaml
35
+ rescue Mandrill::UnknownTemplateError => e
36
+ puts e.message
37
+ end
38
+ end
39
+
40
+ desc "generate NAME", "generate new template files."
41
+ def generate(name)
42
+ new_template = MandrillTemplate::Local.new(name)
43
+ puts new_template.class
44
+ meta, code, text = build_template_for_export(new_template)
45
+ save_as_local_template(name, meta, code, text)
46
+ end
47
+
48
+ desc "publish NAME", "publish template from draft."
49
+ def publish(name)
50
+ puts MandrillClient.client.templates.publish(name).to_yaml
51
+ end
52
+
53
+ desc "list", "show template list both of remote and local."
54
+ def list
55
+ puts "Remote Templates"
56
+ puts "----------------------"
57
+ remote_templates = MandrillClient.client.templates.list
58
+ remote_templates.map! do |template|
59
+ template["has_diff"] = has_diff_between_draft_and_published?(template)
60
+ template
61
+ end
62
+
63
+ Formatador.display_compact_table(
64
+ remote_templates,
65
+ ["has_diff",
66
+ "name",
67
+ "slug",
68
+ "publish_name",
69
+ "draft_updated_at",
70
+ "published_at",
71
+ "labels",
72
+ "subject",
73
+ "publish_subject",
74
+ "from_email",
75
+ "publish_from_email",
76
+ "from_name",
77
+ "publish_from_name"]
78
+ )
79
+
80
+ puts "Local Templates"
81
+ puts "----------------------"
82
+ Formatador.display_compact_table(
83
+ collect_local_templates,
84
+ [
85
+ "name",
86
+ "slug",
87
+ "from_email",
88
+ "from_name",
89
+ "subject",
90
+ "labels"
91
+ ]
92
+ )
93
+ end
94
+
95
+ private
96
+
97
+ def has_diff_between_draft_and_published?(t)
98
+ %w[name code text subject].each do |key|
99
+ return true if t[key] != t["publish_#{key}"]
100
+ end
101
+ return true unless t['published_at']
102
+ false
103
+ end
104
+
105
+ def build_template_for_export(t)
106
+ [
107
+ {
108
+ "slug" => t['slug'],
109
+ "labels" => t['labels'],
110
+ "subject" => t['subject'],
111
+ "from_email" => t['from_email'],
112
+ "from_name" => t['from_name']
113
+ },
114
+ t['code'],
115
+ t['text']
116
+ ]
117
+ end
118
+
119
+ def save_as_local_template(name, meta, code, text)
120
+ empty_directory File.join("templates", name)
121
+ create_file File.join("templates", name, "metadata.yml"), meta.to_yaml
122
+ create_file File.join("templates", name, "code"), code
123
+ create_file File.join("templates", name, "text"), text
124
+ end
125
+
126
+ def collect_local_templates
127
+ local_templates = []
128
+ dirs = Dir.glob("templates/*").map {|path| path.split(File::SEPARATOR).last}
129
+ dirs.map do |dir|
130
+ begin
131
+ local_templates << MandrillTemplate::Local.new(dir)
132
+ rescue
133
+ next
134
+ end
135
+ end
136
+ local_templates
137
+ end
138
+
139
+ def upload_template(t)
140
+ if remote_template_exists?(t.name)
141
+ method = :update
142
+ else
143
+ method = :add
144
+ end
145
+ result = MandrillClient.client.templates.send(method, t.name,
146
+ t['from_email'],
147
+ t['from_name'],
148
+ t['subject'],
149
+ t['code'],
150
+ t['text'],
151
+ false, # publish
152
+ t['labels']
153
+ )
154
+ puts result.to_yaml
155
+ end
156
+
157
+ def remote_template_exists?(name)
158
+ begin
159
+ MandrillClient.client.templates.info(name)
160
+ true
161
+ rescue Mandrill::UnknownTemplateError
162
+ false
163
+ end
164
+ end
165
+ end
@@ -0,0 +1,8 @@
1
+ require 'mandrill'
2
+ require 'mandrill_template/client'
3
+
4
+ module MandrillClient
5
+ def self.client
6
+ Mandrill::API.new ENV['MANDRILL_APIKEY']
7
+ end
8
+ end
@@ -0,0 +1,11 @@
1
+ class Thor
2
+ module Actions
3
+ class CreateFile
4
+ class_eval do
5
+ def identical?
6
+ exists? && File.binread(destination).force_encoding(render.encoding) == render
7
+ end
8
+ end
9
+ end
10
+ end
11
+ end
@@ -0,0 +1,38 @@
1
+ require 'mandrill'
2
+ require 'yaml'
3
+
4
+ module MandrillTemplate
5
+ class Local < Hash
6
+ attr_reader :name, :avail
7
+
8
+ def initialize(name)
9
+ @name = name
10
+ meta, code, text = load_data(name)
11
+
12
+ self['name'] = name
13
+ self['slug'] = meta['slug'] ||= name
14
+ self['from_email'] = meta['from_email'] ||= nil
15
+ self['from_name'] = meta['from_name'] ||= nil
16
+ self['subject'] = meta['subject'] ||= nil
17
+ self['labels'] = meta['labels'] ||= []
18
+
19
+ self['code'] = code ||= nil
20
+ self['text'] = text ||= nil
21
+ end
22
+
23
+ def load_data(name)
24
+ if Dir.exists?(File.join("templates", name))
25
+ @avail = true
26
+ code = File.read(File.join("templates", name, "code"))
27
+ text = File.read(File.join("templates", name, "text"))
28
+ [
29
+ YAML.load_file(File.join("templates", name, "metadata.yml")),
30
+ code.empty? ? nil : code,
31
+ text.empty? ? nil : text
32
+ ]
33
+ else
34
+ [{}, nil, nil]
35
+ end
36
+ end
37
+ end
38
+ end
@@ -0,0 +1,30 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "mandrill-template-manager"
7
+ spec.version = "0.1.0"
8
+ spec.authors = ["sawanoboly"]
9
+ spec.email = ["sawanoboriyu@higanworks.com"]
10
+
11
+ spec.summary = %q{Manage Mandrill Template CLI}
12
+ spec.description = %q{Manage Mandrill Template CLI}
13
+ spec.homepage = "https://github.com/higanworks/mandrill-template-manager"
14
+ spec.license = "MIT"
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_dependency "thor"
22
+ spec.add_dependency "mandrill-api"
23
+ spec.add_dependency "formatador"
24
+ spec.add_dependency "unicode"
25
+ spec.add_development_dependency "bundler", "~> 1.10"
26
+ spec.add_development_dependency "rake", "~> 10.0"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "pry"
29
+ spec.add_development_dependency "pry-doc"
30
+ end
metadata ADDED
@@ -0,0 +1,184 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mandrill-template-manager
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - sawanoboly
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2015-08-12 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: thor
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: mandrill-api
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: formatador
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: unicode
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: bundler
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '1.10'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '1.10'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rake
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '10.0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '10.0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: rspec
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
+ - !ruby/object:Gem::Dependency
112
+ name: pry
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ - !ruby/object:Gem::Dependency
126
+ name: pry-doc
127
+ requirement: !ruby/object:Gem::Requirement
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: '0'
132
+ type: :development
133
+ prerelease: false
134
+ version_requirements: !ruby/object:Gem::Requirement
135
+ requirements:
136
+ - - ">="
137
+ - !ruby/object:Gem::Version
138
+ version: '0'
139
+ description: Manage Mandrill Template CLI
140
+ email:
141
+ - sawanoboriyu@higanworks.com
142
+ executables: []
143
+ extensions: []
144
+ extra_rdoc_files: []
145
+ files:
146
+ - ".gitignore"
147
+ - ".rspec"
148
+ - ".travis.yml"
149
+ - Gemfile
150
+ - LICENSE.txt
151
+ - README.md
152
+ - Rakefile
153
+ - bin/mandrilltemplate
154
+ - lib/mandrill_template/cli.rb
155
+ - lib/mandrill_template/client.rb
156
+ - lib/mandrill_template/monkey_create_file.rb
157
+ - lib/mandrill_template/template.rb
158
+ - mandrill-template-manager.gemspec
159
+ homepage: https://github.com/higanworks/mandrill-template-manager
160
+ licenses:
161
+ - MIT
162
+ metadata: {}
163
+ post_install_message:
164
+ rdoc_options: []
165
+ require_paths:
166
+ - lib
167
+ required_ruby_version: !ruby/object:Gem::Requirement
168
+ requirements:
169
+ - - ">="
170
+ - !ruby/object:Gem::Version
171
+ version: '0'
172
+ required_rubygems_version: !ruby/object:Gem::Requirement
173
+ requirements:
174
+ - - ">="
175
+ - !ruby/object:Gem::Version
176
+ version: '0'
177
+ requirements: []
178
+ rubyforge_project:
179
+ rubygems_version: 2.4.6
180
+ signing_key:
181
+ specification_version: 4
182
+ summary: Manage Mandrill Template CLI
183
+ test_files: []
184
+ has_rdoc: