Class: Sendgrid::List

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid/list.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(data = {}) ⇒ List

Returns a new instance of List.



5
6
7
8
9
10
# File 'lib/sendgrid/list.rb', line 5

def initialize(data = {})
  @api = ::SendGrid::API.new(api_key: Sendgrid.api_key)
  @errors = nil
  reset_properties
  @name ||= data["name"]
end

Instance Attribute Details

#errorsObject (readonly)

Returns the value of attribute errors.



2
3
4
# File 'lib/sendgrid/list.rb', line 2

def errors
  @errors
end

#idObject (readonly)

Returns the value of attribute id.



2
3
4
# File 'lib/sendgrid/list.rb', line 2

def id
  @id
end

#nameObject

Returns the value of attribute name.



3
4
5
# File 'lib/sendgrid/list.rb', line 3

def name
  @name
end

#recipient_countObject (readonly)

Returns the value of attribute recipient_count.



2
3
4
# File 'lib/sendgrid/list.rb', line 2

def recipient_count
  @recipient_count
end

Instance Method Details

#add_recipient(recipient_id, list_id = nil) ⇒ void

This method returns an undefined value.

Add a user to a list

Parameters:

  • id (Integer)

    of the user

  • id (Integer, nil)

    of the list



34
35
36
37
38
39
40
41
42
43
# File 'lib/sendgrid/list.rb', line 34

def add_recipient(recipient_id, list_id = nil)
  if recipient_id && (list_id || @id)
    list_id ||= @id
    response = @api.client.contactdb.lists._(list_id).recipients._(recipient_id).post()
    if response.status_code != "201"
      @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
    end
  end
  nil
end

#find(id) ⇒ Hash?

find a list

Parameters:

  • id (Integer)

    of the list

Returns:

  • (Hash, nil)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sendgrid/list.rb', line 15

def find(id)
  list = nil
  response = @api.client.contactdb.lists._(id).get()
  if response.status_code == "200"
    list = JSON.parse(response.body)
    @id = list["id"]
    @name = list["name"]
    @recipient_count = list["recipient_count"]
  else
    reset_properties
    @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
  end
  list
end