chimplate 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,3 @@
1
+ *.gem
2
+ .chimplate
3
+ *.html
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+ gem "mailchimp"
3
+ gem "json"
4
+ gem "thor"
5
+ gem "listen"
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,29 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ chimplate (0.0.1)
5
+ json
6
+ mailchimp
7
+ thor
8
+
9
+ GEM
10
+ remote: https://rubygems.org/
11
+ specs:
12
+ httparty (0.11.0)
13
+ multi_json (~> 1.0)
14
+ multi_xml (>= 0.5.2)
15
+ json (1.7.7)
16
+ mailchimp (0.0.8)
17
+ httparty
18
+ multi_json (1.7.2)
19
+ multi_xml (0.5.3)
20
+ thor (0.18.1)
21
+
22
+ PLATFORMS
23
+ ruby
24
+
25
+ DEPENDENCIES
26
+ chimplate!
27
+ json
28
+ mailchimp
29
+ thor
data/bin/chimplate ADDED
@@ -0,0 +1,53 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ begin
4
+ require 'chimplate'
5
+ rescue LoadError
6
+ require 'rubygems'
7
+ require 'chimplate'
8
+ end
9
+
10
+ require "thor"
11
+ require "listen"
12
+
13
+ class ChimplateCli < Thor
14
+ option :api_key, :default => ""
15
+ option :name, :default => "New Chimplate Project"
16
+ option :force, :default => false
17
+ desc "setup", "Write Chimplate project config files."
18
+
19
+ def setup
20
+ config = options.dup
21
+ force = config.delete :force
22
+
23
+ unless Chimplate::Base.write_config(config, force)
24
+ puts "Looks like this project is already set up, the config file already exists."
25
+ end
26
+ end
27
+
28
+ option :force, :default => false
29
+ desc "pull", "Pull templates from mailchimp account and write to disk."
30
+ def pull
31
+ Chimplate::Base.pull(options) do |filename|
32
+ ask("Do you want to overwrite #{filename}?") == "y"
33
+ end
34
+ end
35
+
36
+ desc "push", "Push current disk versions of templates to mailchimp."
37
+ def push
38
+ Chimplate::Base.push
39
+ end
40
+
41
+ desc "watch", "Watch for file changes and push them to Mailchimp."
42
+ def watch
43
+ puts "Watching for file changes in the current directory. Ctrl+C to quit.\n\n"
44
+
45
+ listener = Listen.to(Dir.pwd, :relative_paths => true, :filter => /\.html$/) do |modified, added, removed|
46
+ Chimplate::Base.push_file(modified.first)
47
+ end
48
+
49
+ listener.start
50
+ end
51
+ end
52
+
53
+ ChimplateCli.start(ARGV)
data/chimplate.gemspec ADDED
@@ -0,0 +1,22 @@
1
+ $:.push File.expand_path("../lib", __FILE__)
2
+ require "chimplate/version"
3
+
4
+ Gem::Specification.new do |s|
5
+ s.name = 'chimplate'
6
+ s.version = Chimplate::VERSION
7
+ s.platform = Gem::Platform::RUBY
8
+ s.date = '2013-04-13'
9
+ s.summary = "Chimplate"
10
+ s.description = "A small command line utility to ease local editing and version control of mailchimp user templates."
11
+ s.authors = ["Matt White"]
12
+ s.email = 'matt@bitforge.us'
13
+ s.files = `git ls-files`.split("\n")
14
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
15
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
16
+ s.homepage = 'http://bitforge.us'
17
+ s.require_paths = ["lib"]
18
+ s.add_dependency "mailchimp"
19
+ s.add_dependency "json"
20
+ s.add_dependency "thor"
21
+ s.add_dependency "listen"
22
+ end
data/lib/chimplate.rb ADDED
@@ -0,0 +1 @@
1
+ require "chimplate/base"
@@ -0,0 +1,98 @@
1
+ require "mailchimp"
2
+ require "yaml"
3
+
4
+ module Chimplate
5
+ class Base
6
+ def self.write_config(config, force = false)
7
+ FileUtils.rm destination if force
8
+
9
+ return false if File.exist? destination
10
+
11
+ File.open(destination, "w+") do |file|
12
+ file.write(config.to_hash.to_yaml)
13
+ end
14
+ end
15
+
16
+ def self.pull(options = {})
17
+ api.templates["user"].each do |template_info|
18
+ template = api.templateInfo "tid" => template_info["id"]
19
+ filename = Dir.pwd + "/" + template_info["id"].to_s + "-" + sanitize_filename(template_info["name"]) + ".html"
20
+
21
+ write = true
22
+ if File.exist? filename
23
+ if options[:force] || yield(filename)
24
+ FileUtils.rm filename
25
+ else
26
+ puts "Skipping #{filename}.\n"
27
+ write = false
28
+ end
29
+ end
30
+
31
+ if write
32
+ File.open(filename, "w+") do |file|
33
+ file.write(template["source"])
34
+ puts "Saved template #{filename}.\n";
35
+ end
36
+ end
37
+ end
38
+ end
39
+
40
+ def self.push
41
+ Dir.glob("*-*.html").each do |template_filename|
42
+ push_file(template_filename)
43
+ end
44
+ end
45
+
46
+ def self.push_file(template_filename)
47
+ tid, template_name = template_filename.gsub(".html", "").split("-")
48
+
49
+ File.open template_filename, "rb" do |file|
50
+ if tid == "new"
51
+ new_tid = api.templateAdd :name => template_name.gsub("_", " "), :html => file.read
52
+ new_filename = new_tid.to_s + '-' + template_name + ".html"
53
+ FileUtils.mv template_filename, new_filename
54
+
55
+ puts "Saved new template #{new_filename}.\n";
56
+ else
57
+ api.templateUpdate "id" => tid, "values" => { "html" => file.read }
58
+ puts "Updated template #{template_filename}.\n";
59
+ end
60
+ end
61
+ end
62
+
63
+ protected
64
+ def self.api
65
+ if !File.exist? destination
66
+ puts "No config file here, please run `chimplate setup` first"
67
+ exit
68
+ else
69
+ config = YAML.load_file(destination)
70
+ end
71
+
72
+ @api ||= Mailchimp::API.new config["api_key"]
73
+ end
74
+
75
+ def self.destination
76
+ Dir.pwd + "/" + ".chimplate"
77
+ end
78
+
79
+ #from http://stackoverflow.com/questions/1939333/how-to-make-a-ruby-string-safe-for-a-filesystem
80
+ def self.sanitize_filename(filename)
81
+ # Split the name when finding a period which is preceded by some
82
+ # character, and is followed by some character other than a period,
83
+ # if there is no following period that is followed by something
84
+ # other than a period (yeah, confusing, I know)
85
+ fn = filename.split(/(?<=.)\.(?=[^.])(?!.*\.[^.])/m)
86
+
87
+ # We now have one or two parts (depending on whether we could find
88
+ # a suitable period). For each of these parts, replace any unwanted
89
+ # sequence of characters with an underscore
90
+ fn.map! { |s| s.gsub(/[^a-z0-9\-]+/i, '_') }
91
+
92
+ # Finally, join the parts with a period and return the result
93
+ return fn.join '.'
94
+ end
95
+
96
+ end
97
+
98
+ end
@@ -0,0 +1,3 @@
1
+ module Chimplate
2
+ VERSION = "0.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,118 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: chimplate
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Matt White
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2013-04-13 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: mailchimp
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ! '>='
20
+ - !ruby/object:Gem::Version
21
+ version: '0'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ! '>='
28
+ - !ruby/object:Gem::Version
29
+ version: '0'
30
+ - !ruby/object:Gem::Dependency
31
+ name: json
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :runtime
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ - !ruby/object:Gem::Dependency
47
+ name: thor
48
+ requirement: !ruby/object:Gem::Requirement
49
+ none: false
50
+ requirements:
51
+ - - ! '>='
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
54
+ type: :runtime
55
+ prerelease: false
56
+ version_requirements: !ruby/object:Gem::Requirement
57
+ none: false
58
+ requirements:
59
+ - - ! '>='
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ - !ruby/object:Gem::Dependency
63
+ name: listen
64
+ requirement: !ruby/object:Gem::Requirement
65
+ none: false
66
+ requirements:
67
+ - - ! '>='
68
+ - !ruby/object:Gem::Version
69
+ version: '0'
70
+ type: :runtime
71
+ prerelease: false
72
+ version_requirements: !ruby/object:Gem::Requirement
73
+ none: false
74
+ requirements:
75
+ - - ! '>='
76
+ - !ruby/object:Gem::Version
77
+ version: '0'
78
+ description: A small command line utility to ease local editing and version control
79
+ of mailchimp user templates.
80
+ email: matt@bitforge.us
81
+ executables:
82
+ - chimplate
83
+ extensions: []
84
+ extra_rdoc_files: []
85
+ files:
86
+ - .gitignore
87
+ - Gemfile
88
+ - Gemfile.lock
89
+ - bin/chimplate
90
+ - chimplate.gemspec
91
+ - lib/chimplate.rb
92
+ - lib/chimplate/base.rb
93
+ - lib/chimplate/version.rb
94
+ homepage: http://bitforge.us
95
+ licenses: []
96
+ post_install_message:
97
+ rdoc_options: []
98
+ require_paths:
99
+ - lib
100
+ required_ruby_version: !ruby/object:Gem::Requirement
101
+ none: false
102
+ requirements:
103
+ - - ! '>='
104
+ - !ruby/object:Gem::Version
105
+ version: '0'
106
+ required_rubygems_version: !ruby/object:Gem::Requirement
107
+ none: false
108
+ requirements:
109
+ - - ! '>='
110
+ - !ruby/object:Gem::Version
111
+ version: '0'
112
+ requirements: []
113
+ rubyforge_project:
114
+ rubygems_version: 1.8.24
115
+ signing_key:
116
+ specification_version: 3
117
+ summary: Chimplate
118
+ test_files: []