Class: ExvoAuth::Models::Message

Inherits:
Object
  • Object
show all
Includes:
ActiveModel::Serialization, ActiveModel::Validations
Defined in:
lib/exvo_auth/models/message.rb

Defined Under Namespace

Classes: RecordInvalid, RecordNotFound

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Message

Returns a new instance of Message.



14
15
16
17
18
# File 'lib/exvo_auth/models/message.rb', line 14

def initialize(attributes = {})
  attributes.each do |name, value|
    send("#{name}=", value)
  end
end

Instance Attribute Details

#created_atObject

Returns the value of attribute created_at.



12
13
14
# File 'lib/exvo_auth/models/message.rb', line 12

def created_at
  @created_at
end

#idObject

Returns the value of attribute id.



12
13
14
# File 'lib/exvo_auth/models/message.rb', line 12

def id
  @id
end

#labelObject

Returns the value of attribute label.



12
13
14
# File 'lib/exvo_auth/models/message.rb', line 12

def label
  @label
end

#readObject

Returns the value of attribute read.



12
13
14
# File 'lib/exvo_auth/models/message.rb', line 12

def read
  @read
end

#textObject

Returns the value of attribute text.



12
13
14
# File 'lib/exvo_auth/models/message.rb', line 12

def text
  @text
end

#user_uidObject

Returns the value of attribute user_uid.



12
13
14
# File 'lib/exvo_auth/models/message.rb', line 12

def user_uid
  @user_uid
end

Class Method Details

.allObject



30
31
32
33
34
# File 'lib/exvo_auth/models/message.rb', line 30

def self.all
  auth = ExvoAuth::Autonomous::Auth.instance
  response = auth.get("/api/private/app_messages.json")
  response.map{ |m| new(m) }
end

.create(attributes = {}) ⇒ Object



20
21
22
23
24
25
26
27
28
# File 'lib/exvo_auth/models/message.rb', line 20

def self.create(attributes = {})
  message = new(attributes)
  if message.valid?
    message.deliver
    message
  else
    raise RecordInvalid, message.errors.full_messages.join(", ")
  end
end

.find(id) ⇒ Object



36
37
38
39
40
41
42
43
44
# File 'lib/exvo_auth/models/message.rb', line 36

def self.find(id)
  auth = ExvoAuth::Autonomous::Auth.instance
  response = auth.get("/api/private/app_messages/#{id}.json")
  if response.code == 200
    new(response)
  else
    raise RecordNotFound, "Couldn't find #{model_name} with ID=#{id}"
  end
end

Instance Method Details

#deliverObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/exvo_auth/models/message.rb', line 46

def deliver
  auth = ExvoAuth::Autonomous::Auth.instance
  attributes = {
    :label    => label,
    :text     => text,
    :user_uid => user_uid
  }
  response = auth.post("/api/private/app_messages.json", :body => attributes)
  case response.code
    when 201 then
      response.parsed_response.each do |k, v|
        send("#{k}=", v)
      end
    when 422 then
      response.parsed_response.each{ |attr, error| errors.add(attr, error) }
      raise RecordInvalid, errors.full_messages.join(", ")
    else
      raise "Unknown error"  
  end

end

#persisted?Boolean

Returns:

  • (Boolean)


68
69
70
# File 'lib/exvo_auth/models/message.rb', line 68

def persisted?
  !!id
end