Class: Sendgrid::Subuser

Inherits:
Object
  • Object
show all
Defined in:
lib/sendgrid_api/subuser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(response = {}) ⇒ Subuser

Returns a new instance of Subuser.



48
49
50
51
52
53
54
# File 'lib/sendgrid_api/subuser.rb', line 48

def initialize(response={})
  @first_name = response["first_name"]
  @last_name  = response["last_name"]
  @email      = response["email"]
  @activated  = response["active"] == "true"
  @errors     = response["errors"] || []
end

Instance Attribute Details

#activatedObject (readonly) Also known as: activated?

Returns the value of attribute activated.



46
47
48
# File 'lib/sendgrid_api/subuser.rb', line 46

def activated
  @activated
end

#emailObject (readonly)

Returns the value of attribute email.



46
47
48
# File 'lib/sendgrid_api/subuser.rb', line 46

def email
  @email
end

#errorsObject (readonly)

Returns the value of attribute errors.



46
47
48
# File 'lib/sendgrid_api/subuser.rb', line 46

def errors
  @errors
end

#first_nameObject (readonly)

Returns the value of attribute first_name.



46
47
48
# File 'lib/sendgrid_api/subuser.rb', line 46

def first_name
  @first_name
end

#last_nameObject (readonly)

Returns the value of attribute last_name.



46
47
48
# File 'lib/sendgrid_api/subuser.rb', line 46

def last_name
  @last_name
end

Class Method Details

.allObject



32
33
34
# File 'lib/sendgrid_api/subuser.rb', line 32

def self.all
  search
end

.clientObject



42
43
44
# File 'lib/sendgrid_api/subuser.rb', line 42

def self.client
  @client ||= Client.new(Sendgrid.api_user, Sendgrid.api_key)
end

.create(attributes) ⇒ Object



17
18
19
20
21
22
23
24
# File 'lib/sendgrid_api/subuser.rb', line 17

def self.create(attributes)
  response = client.request("user.add", defaults.merge(attributes))
  if response.errors?
    new(response.body)
  else
    get(attributes[:email])
  end
end

.defaultsObject



3
4
5
6
7
8
9
10
11
12
13
14
15
# File 'lib/sendgrid_api/subuser.rb', line 3

def self.defaults
  {
    :username   => "", :email => "",
    :first_name => "", :last_name => "",
    :website    => "",
    :phone      => "Unknown",
    :address    => "Unknown",
    :city       => "Unknown",
    :state      => "Unknown",
    :country    => "Unknown",
    :zip        => "Unknown"
  }
end

.get(email) ⇒ Object



36
37
38
39
40
# File 'lib/sendgrid_api/subuser.rb', line 36

def self.get(email)
  return nil unless email
  return nil if (subusers = search("email" => email)).empty?
  subusers.first
end

.search(attributes = {}) ⇒ Object



26
27
28
29
30
# File 'lib/sendgrid_api/subuser.rb', line 26

def self.search(attributes={})
  response = client.request("user.profile", attributes.merge!("task" => "get"))
  return [] if response.errors?
  response.body.collect { |subuser| new(subuser) }
end

Instance Method Details

#activateObject



62
63
64
# File 'lib/sendgrid_api/subuser.rb', line 62

def activate
  client.request("user.enable", {"user" => email}).successful?
end

#deactivateObject



58
59
60
# File 'lib/sendgrid_api/subuser.rb', line 58

def deactivate
  client.request("user.disable", {"user" => email}).successful?
end

#statisticsObject



66
67
68
69
# File 'lib/sendgrid_api/subuser.rb', line 66

def statistics
  response = client.request("user.stats", {"user" => email})
  response.errors? ? [] : response.body
end