Class: Adcloud::Entity

Inherits:
Object
  • Object
show all
Includes:
Virtus
Defined in:
lib/adcloud/entity.rb

Constant Summary collapse

MAX_PER_PAGE =
200

Class Attribute Summary collapse

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Class Attribute Details

.api_endpointObject

Returns the value of attribute api_endpoint.



46
47
48
# File 'lib/adcloud/entity.rb', line 46

def api_endpoint
  @api_endpoint
end

.connectionObject

Returns the value of attribute connection.



46
47
48
# File 'lib/adcloud/entity.rb', line 46

def connection
  @connection
end

Instance Attribute Details

#errorsHash

Returns Errors hash.

Returns:

  • (Hash)

    Errors hash



100
101
102
# File 'lib/adcloud/entity.rb', line 100

def errors
  @errors
end

Class Method Details

.all(filter = {}, page = 1, per_page = 50) {|result['_meta']| ... } ⇒ Array

Returns Entities matching the criteria or all.

Yields:

  • (result['_meta'])

Returns:

  • (Array)

    Entities matching the criteria or all



61
62
63
64
65
# File 'lib/adcloud/entity.rb', line 61

def all(filter = {}, page = 1, per_page = 50)
  result = connection.get(self.api_endpoint, :filter => filter, :page => page, :per_page => per_page)
  yield result['_meta'] if block_given?
  result["items"].map { |raw_campaign| self.new(raw_campaign) }
end

.all!(filter = {}) ⇒ Object

fetches all objects via pagination. Be careful, this could be a lot!



68
69
70
71
72
73
74
75
76
77
# File 'lib/adcloud/entity.rb', line 68

def all!(filter = {})
  result = []
  page = 0
  total_pages = 1
  begin
    page += 1
    result += self.all(filter, page, MAX_PER_PAGE) { || total_pages = ['total_pages'] }
  end while page < total_pages
  result
end

.api_nameObject



52
53
54
# File 'lib/adcloud/entity.rb', line 52

def api_name
  self.name.demodulize.underscore
end

.create(params = {}) ⇒ Enitity

Returns Object has errors when creation failed.

Returns:

  • (Enitity)

    Object has errors when creation failed



92
93
94
95
96
# File 'lib/adcloud/entity.rb', line 92

def create(params = {})
  entity = self.new(params)
  entity.create
  entity
end

.find(id) ⇒ Object

Returns The entity with the unique identifier.

Returns:

  • (Object)

    The entity with the unique identifier



80
81
82
83
# File 'lib/adcloud/entity.rb', line 80

def find(id)
  result = connection.get("#{self.api_endpoint}/#{id}")
  self.new(result)
end

.find_by_name(name) ⇒ Object

Returns The entity with the unique identifier.

Returns:

  • (Object)

    The entity with the unique identifier



86
87
88
89
# File 'lib/adcloud/entity.rb', line 86

def find_by_name(name)
  result = connection.get("campaigns/find_by_name", name: name)
  result["items"].map { |raw_campaign| self.new(raw_campaign) }
end

Instance Method Details

#connectionObject



12
13
14
# File 'lib/adcloud/entity.rb', line 12

def connection
  self.class.connection
end

#createBoolean

Returns True when successfully created - otherwise false.

Returns:

  • (Boolean)

    True when successfully created - otherwise false



21
22
23
24
25
26
27
28
29
30
# File 'lib/adcloud/entity.rb', line 21

def create
  result = connection.post(self.class.api_endpoint, { self.class.api_name => attributes_for_create })
  if self.respond_to?(:id=) && !result['id'].nil?
    self.id = result['id']
  end
  true
rescue Adcloud::BadRequestError => ex
  derive_errors_from_error(ex)
  false
end

#destroyObject



40
41
42
43
# File 'lib/adcloud/entity.rb', line 40

def destroy
  result = connection.delete("#{self.class.api_endpoint}/#{id}")
  self
end

#metaObject



16
17
18
# File 'lib/adcloud/entity.rb', line 16

def meta
  self._meta
end

#updateObject



32
33
34
35
36
37
38
# File 'lib/adcloud/entity.rb', line 32

def update
  result = connection.put("#{self.class.api_endpoint}/#{id}", { self.class.api_name => attributes_for_update })
  true
rescue Adcloud::BadRequestError => ex
  derive_errors_from_error(ex)
  false
end