Class: MandrillTemplateManager

Inherits:
Thor
  • Object
show all
Includes:
Thor::Actions
Defined in:
lib/mandrill_template/cli.rb

Constant Summary collapse

VERSION =
"0.2.2"

Instance Method Summary collapse

Instance Method Details

#delete(name) ⇒ Object



32
33
34
35
36
37
38
39
# File 'lib/mandrill_template/cli.rb', line 32

def delete(name)
  begin
    result = MandrillClient.client.templates.delete(name)
    puts result.to_yaml
  rescue Mandrill::UnknownTemplateError => e
    puts e.message
  end
end

#export(name) ⇒ Object



15
16
17
18
19
# File 'lib/mandrill_template/cli.rb', line 15

def export(name)
  template = MandrillClient.client.templates.info(name)
  meta, code, text  = build_template_for_export(template)
  save_as_local_template(name, meta, code, text)
end

#generate(name) ⇒ Object



42
43
44
45
46
47
# File 'lib/mandrill_template/cli.rb', line 42

def generate(name)
  new_template = MandrillTemplate::Local.new(name)
  puts new_template.class
  meta, code, text = build_template_for_export(new_template)
  save_as_local_template(name, meta, code, text)
end

#listObject



77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/mandrill_template/cli.rb', line 77

def list
  puts "Remote Templates"
  puts "----------------------"
  remote_templates = MandrillClient.client.templates.list
  remote_templates.map! do |template|
    template["has_diff"] = has_diff_between_draft_and_published?(template)
    template
  end

  if options[:verbose]
  Formatador.display_compact_table(
    remote_templates,
    ["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"]
  )
  else
    Formatador.display_compact_table(
      remote_templates,
      ["has_diff",
       "name",
       "slug",
       "from_email",
       "from_name",
       "subject",
       "labels",
       "from_name"]
    )
  end

  puts "Local Templates"
  puts "----------------------"
  Formatador.display_compact_table(
    collect_local_templates,
    [
      "name",
      "slug",
      "from_email",
      "from_name",
      "subject",
      "labels"
    ]
  )
end

#publish(name) ⇒ Object



50
51
52
# File 'lib/mandrill_template/cli.rb', line 50

def publish(name)
  puts MandrillClient.client.templates.publish(name).to_yaml
end

#render(name, params = nil) ⇒ Object



56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/mandrill_template/cli.rb', line 56

def render(name, params = nil)
  merge_vars =  params ? JSON.parse(File.read(params)) : []
  template = MandrillTemplate::Local.new(name)
  if template.avail
    if options[:handlebars]
      handlebars = Handlebars::Context.new
      h_template = handlebars.compile(template['code'])
      puts h_template.call(localize_merge_vars(merge_vars))
    else
      result = MandrillClient.client.templates.render template.name,
        [{"content"=>template["code"], "name"=>template.name}],
        merge_vars
      puts result["html"]
    end
  else
    puts "Template data not found #{name}. Please generate first."
  end
end

#upload(name) ⇒ Object



22
23
24
25
26
27
28
29
# File 'lib/mandrill_template/cli.rb', line 22

def upload(name)
  template = MandrillTemplate::Local.new(name)
  if template.avail
    upload_template(template)
  else
    puts "Template data not found #{name}. Please generate first."
  end
end