Class: Sendly::TemplatesResource
- Inherits:
-
Object
- Object
- Sendly::TemplatesResource
- Defined in:
- lib/sendly/templates_resource.rb
Instance Method Summary collapse
-
#clone(id, name: nil) ⇒ Object
Copy an existing template into a new draft.
-
#create(name:, text: nil, body: nil, locale: nil, is_published: nil) ⇒ Sendly::Template
Create a template.
- #delete(id) ⇒ Object
- #generate(description:, category: nil) ⇒ Object
- #get(id) ⇒ Object
-
#initialize(client) ⇒ TemplatesResource
constructor
A new instance of TemplatesResource.
-
#list(limit: nil, type: nil, locale: nil) ⇒ Hash
List templates visible to the API key, presets included.
- #publish(id) ⇒ Object
-
#unpublish(id) ⇒ Object
Unpublish a published template.
-
#update(id, name: nil, text: nil, body: nil, locale: nil, is_published: nil) ⇒ Sendly::Template
Update a draft template.
Constructor Details
#initialize(client) ⇒ TemplatesResource
Returns a new instance of TemplatesResource.
87 88 89 |
# File 'lib/sendly/templates_resource.rb', line 87 def initialize(client) @client = client end |
Instance Method Details
#clone(id, name: nil) ⇒ Object
Note:
Not available yet: the versioned API serves no clone route, so this call fails with a 404. To copy a template today, read it with #get and pass its Sendly::Template#text to #create.
Copy an existing template into a new draft.
229 230 231 232 233 234 |
# File 'lib/sendly/templates_resource.rb', line 229 def clone(id, name: nil) body = {} body[:name] = name if name response = @client.post("/templates/#{id}/clone", body) Template.new(response) end |
#create(name:, text: nil, body: nil, locale: nil, is_published: nil) ⇒ Sendly::Template
Create a template. New templates start as drafts; call #publish to make one usable.
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 |
# File 'lib/sendly/templates_resource.rb', line 152 def create(name:, text: nil, body: nil, locale: nil, is_published: nil) reject_locale!(locale) if is_published raise ArgumentError, "is_published: true is not supported on create: templates are " \ "always created as drafts. Publishing is a separate call - use " \ "publish(id) on the returned template instead." end content = text || body raise ArgumentError, "text is required" if content.nil? response = @client.post("/templates", { name: name, text: content }) Template.new(response) end |
#delete(id) ⇒ Object
205 206 207 |
# File 'lib/sendly/templates_resource.rb', line 205 def delete(id) @client.delete("/templates/#{id}") end |
#generate(description:, category: nil) ⇒ Object
236 237 238 239 240 241 |
# File 'lib/sendly/templates_resource.rb', line 236 def generate(description:, category: nil) body = { description: description } body[:category] = category if category response = @client.post("/templates/generate", body) GeneratedTemplate.new(response) end |
#get(id) ⇒ Object
131 132 133 134 |
# File 'lib/sendly/templates_resource.rb', line 131 def get(id) response = @client.get("/templates/#{id}") Template.new(response) end |
#list(limit: nil, type: nil, locale: nil) ⇒ Hash
List templates visible to the API key, presets included.
107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/sendly/templates_resource.rb', line 107 def list(limit: nil, type: nil, locale: nil) unless limit.nil? raise ArgumentError, "limit is not supported: the templates list route returns every " \ "visible template in one response and does not paginate. Slice " \ "the returned :templates array instead." end unless type.nil? raise ArgumentError, "type is not supported: the templates list route does not filter. " \ "Select over the returned :templates array with " \ "Sendly::Template#preset? or #custom? instead." end reject_locale!(locale) response = @client.get("/templates") { templates: (response["templates"] || []).map { |t| Template.new(t) }, pagination: response["pagination"] } end |
#publish(id) ⇒ Object
209 210 211 212 |
# File 'lib/sendly/templates_resource.rb', line 209 def publish(id) response = @client.post("/templates/#{id}/publish") Template.new(response) end |
#unpublish(id) ⇒ Object
219 220 221 222 |
# File 'lib/sendly/templates_resource.rb', line 219 def unpublish(id) response = @client.post("/templates/#{id}/unpublish") Template.new(response) end |
#update(id, name: nil, text: nil, body: nil, locale: nil, is_published: nil) ⇒ Sendly::Template
Update a draft template. Published templates cannot be edited; publish a new template instead.
185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/sendly/templates_resource.rb', line 185 def update(id, name: nil, text: nil, body: nil, locale: nil, is_published: nil) reject_locale!(locale) if is_published raise ArgumentError, "is_published: true is not supported on update: an update never " \ "changes a template's status. Publishing is a separate call - " \ "use publish(id) instead." end content = text || body request_body = {} request_body[:name] = name if name request_body[:text] = content if content response = @client.patch("/templates/#{id}", request_body) Template.new(response) end |