Class: Stannp::RecipientsResource

Inherits:
Resource
  • Object
show all
Defined in:
lib/stannp/resources/recipients.rb

Instance Method Summary collapse

Methods inherited from Resource

#get_request, #initialize, #post_request, #url_for

Constructor Details

This class inherits a constructor from Stannp::Resource

Instance Method Details

#create(attributes:) ⇒ Object



16
17
18
19
# File 'lib/stannp/resources/recipients.rb', line 16

def create(attributes:)
  url = url_for(path: 'recipients/new')
  Recipient.new(post_request(url, body: attributes).body['data'])
end

#delete(id:) ⇒ Object



21
22
23
24
25
# File 'lib/stannp/resources/recipients.rb', line 21

def delete(id:)
  url = url_for(path: 'recipients/delete')
  post_request(url, body: { id: id.to_i })
  true
end

#delete_allObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/stannp/resources/recipients.rb', line 27

def delete_all
  url = url_for(path: 'recipients/deleteAll')
  post_request(url, body: { delete_all: true })
  true
rescue Stannp::Error => e
  # Despite the request actually deleteing all recipients, the API returns a 400. In those
  # cases, and when delete all is called on an empty recipient list, just return true.
  return true if e.message == '400: Nothing to delete'

  raise e
end

#get(id:) ⇒ Object



11
12
13
14
# File 'lib/stannp/resources/recipients.rb', line 11

def get(id:)
  url = url_for(path: "recipients/get/#{id}")
  Recipient.new(get_request(url).body['data'])
end

#import(group_id:, file:, options: {}) ⇒ Object



39
40
41
42
43
# File 'lib/stannp/resources/recipients.rb', line 39

def import(group_id:, file:, options: {})
  url = url_for(path: 'recipients/import')
  body = { group_id: group_id, file: file }.merge(options)
  post_request(url, body: body).body['success']
end

#list(group_id: nil) ⇒ Object



5
6
7
8
9
# File 'lib/stannp/resources/recipients.rb', line 5

def list(group_id: nil)
  id = group_id ? "/#{group_id}" : ''
  url = url_for(path: "recipients/list#{id}")
  List.from_request(data: get_request(url).body['data'], type: Recipient)
end