Class: Sendgrid::Recipient

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeRecipient

Returns a new instance of Recipient.



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

def initialize
  @api = ::SendGrid::API.new(api_key: Sendgrid.api_key)
  @errors = nil
  reset_properties
end

Instance Attribute Details

#emailObject

Returns the value of attribute email.



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

def email
  @email
end

#errorsObject (readonly)

Returns the value of attribute errors.



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

def errors
  @errors
end

#first_nameObject

Returns the value of attribute first_name.



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

def first_name
  @first_name
end

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#last_nameObject

Returns the value of attribute last_name.



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

def last_name
  @last_name
end

Instance Method Details

#create(data) ⇒ String, void

Create a user and return the id

Parameters:

  • email (Hash)

Returns:

  • (String, void)

    user id



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sendgrid/recipient.rb', line 38

def create(data)
  if !data || !data.is_a?(Hash) || data[:email].nil? || data[:email].empty?
    reset_properties
    if data.nil? || !data.is_a?(Hash)
      @errors = { status: "400", body: [{ "message" => "request body is invalid" }] }
    else
      @errors = { status: "400", body: [{ "message" => "Type of provided email is invalid, email must be a non empty string" }] }
    end
  else
    return @id if @id && @email == data[:email]
    self.find_by("email", data[:email])
    if @id.nil?
      @errors = nil
      request_data = [data]
      response = @api.client.contactdb.recipients.post(request_body: request_data)
      if response.status_code == "201"
        @id = JSON.parse(response.body)["persisted_recipients"]&.first
        @email = data[:email] if data[:email]
        @first_name = data[:first_name] if data[:first_name]
        @last_name = data[:last_name] if data[:last_name]
      else
        reset_properties
        @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
      end
    end
  end
  @id
end

#destroy(recipient_id = nil) ⇒ void

This method returns an undefined value.

Destroy an existing user

Parameters:

  • id (Integer, nil)

    of the recipient



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/sendgrid/recipient.rb', line 70

def destroy(recipient_id = nil)
  recipient_id ||= @id
  response = @api.client.contactdb.recipients._(recipient_id).delete()
  reset_properties
  if response.status_code == "204"
    @errors = nil
  else
    @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
  end
  nil
end

#find_by(field, value) ⇒ Hash?

find a single user

Parameters:

  • field (String)

    name

  • field (String)

    value

Returns:

  • (Hash, nil)


15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/sendgrid/recipient.rb', line 15

def find_by(field, value)
  user = nil
  data = { conditions: [{ and_or: "", field: field, value: value, operator: "eq" }] }
  response = @api.client.contactdb.recipients.search.post(request_body: data)
  if response.status_code == "200"
    @errors = nil
    user = JSON.parse(response.body)["recipients"]&.first
    if user
      @id = user["id"]
      @email = user["email"]
      @first_name = user["first_name"]
      @last_name = user["last_name"]
    end
  else
    reset_properties
    @errors = { status: response.status_code, body: JSON.parse(response.body)["errors"] }
  end
  user
end