Class: Syncano::Resources::Notifications::Base

Inherits:
Base
  • Object
show all
Defined in:
lib/syncano/resources/notifications/base.rb

Overview

Base notification class used for inheritance

Direct Known Subclasses

Create, Destroy, Message, Update

Instance Attribute Summary

Attributes inherited from Base

#attributes, #destroyed, #id

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#[], #[]=, #batch, batch_create, #batch_destroy, #batch_save, #batch_update, count, #destroy, #destroyed?, find, #new_record?, #reload!, #save, #saved?, #update

Constructor Details

#initialize(client, attributes) ⇒ Base

Constructor for Syncano::Notifications::Base object

Parameters:



10
11
12
13
14
15
16
17
# File 'lib/syncano/resources/notifications/base.rb', line 10

def initialize(client, attributes)
  if attributes.is_a?(::Syncano::Packets::Base)
    super(client, {})
    self.attributes = { source: attributes.source, target: attributes.target, data: attributes.data }
  else
    super(client, attributes)
  end
end

Class Method Details

.all(client, scope_parameters = {}, conditions = {}) ⇒ Array

Wrapper for api “get” method Returns all objects from Syncano

Parameters:

  • client (Syncano::Clients::Base)
  • scope_parameters (Hash) (defaults to: {})
  • conditions (Hash) (defaults to: {})

Returns:

  • (Array)

    which contains Syncano::Resources::Base objects



43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/syncano/resources/notifications/base.rb', line 43

def self.all(client, scope_parameters = {}, conditions = {})
  mapping = {
      new: ::Syncano::Resources::Notifications::Create,
      change: ::Syncano::Resources::Notifications::Update,
      delete: ::Syncano::Resources::Notifications::Destroy,
      message: ::Syncano::Resources::Notifications::Message
  }

  response = perform_all(client, scope_parameters, conditions)
  response.data.to_a.collect do |attributes|
    type = attributes.delete(:type)
    mapping[type.to_sym].new(client, attributes.merge(scope_parameters))
  end
end

.create(client, attributes) ⇒ Syncano::Resources::Base

Wrapper for api “send” method Creates object in Syncano

Parameters:

Returns:



63
64
65
66
# File 'lib/syncano/resources/notifications/base.rb', line 63

def self.create(client, attributes)
  perform_create(client, nil, attributes)
  ::Syncano::Resources::Notifications::Message.new(client, map_to_scope_parameters(attributes))
end

.instantize_notification(client, packet) ⇒ Syncano::Notifications::Base

Proxy method for creating instance of proper subclass

Parameters:

Returns:

  • (Syncano::Notifications::Base)


23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/syncano/resources/notifications/base.rb', line 23

def self.instantize_notification(client, packet)
  if packet.message?
    ::Syncano::Resources::Notifications::Message.new(client, packet)
  else
    mapping = {
      new: ::Syncano::Resources::Notifications::Create,
      change: ::Syncano::Resources::Notifications::Update,
      delete: ::Syncano::Resources::Notifications::Destroy
    }

    mapping[packet.type.to_sym].new(client, packet)
  end
end