Class: Sendly::TemplatesResource

Inherits:
Object
  • Object
show all
Defined in:
lib/sendly/templates_resource.rb

Instance Method Summary collapse

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.

Parameters:

  • name (String)

    Template name

  • text (String) (defaults to: nil)

    Template text, with {{variable}} placeholders

  • body (String, nil) (defaults to: nil)

    Deprecated alias for text. Sent as text. Pass text.

  • locale (String, nil) (defaults to: nil)

    Deprecated and unsupported. Templates are not scoped by locale. Passing a value raises ArgumentError; there is no replacement.

  • is_published (Boolean, nil) (defaults to: nil)

    Deprecated. Templates are always created as drafts, so false is accepted as a no-op and true raises ArgumentError. Call #publish on the new template instead.

Returns:

Raises:

  • (ArgumentError)

    if locale is given, if is_published is true, or if neither text nor body is given



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.

Parameters:

  • limit (Integer, nil) (defaults to: nil)

    Deprecated and unsupported. The list route returns every visible template in one response and does not paginate. Passing a value raises ArgumentError; slice the returned :templates array instead.

  • type (String, nil) (defaults to: nil)

    Deprecated and unsupported. The list route does not filter. Passing a value raises ArgumentError; select over the returned :templates with Sendly::Template#preset? / Sendly::Template#custom? instead.

  • locale (String, nil) (defaults to: nil)

    Deprecated and unsupported. Templates are not scoped by locale. Passing a value raises ArgumentError; there is no replacement.

Returns:

  • (Hash)

    :templates (ArraySendly::Template) and :pagination, which is always nil because the route does not paginate

Raises:

  • (ArgumentError)

    if limit, type or locale is given



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

Note:

Not available yet: the versioned API serves no unpublish route, so this call fails with a 404. To retire a published template today, #create and #publish a replacement, then #delete this one.

Unpublish a published template.



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.

Parameters:

  • id (String)

    Template ID

  • name (String, nil) (defaults to: nil)

    New name

  • text (String, nil) (defaults to: nil)

    New template text

  • body (String, nil) (defaults to: nil)

    Deprecated alias for text. Sent as text. Pass text.

  • locale (String, nil) (defaults to: nil)

    Deprecated and unsupported. Templates are not scoped by locale. Passing a value raises ArgumentError; there is no replacement.

  • is_published (Boolean, nil) (defaults to: nil)

    Deprecated. An update never changes a template's status, and only drafts are editable, so false is accepted as a no-op and true raises ArgumentError. Call #publish instead.

Returns:

Raises:

  • (ArgumentError)

    if locale is given or is_published is true



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