Class: Adcloud::Entity
Direct Known Subclasses
Advertisement, Campaign, CampaignPerformanceData, Connection, Customer, MediaFile, Product, Topic
Constant Summary collapse
- MAX_PER_PAGE =
200
Class Attribute Summary collapse
-
.api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
-
.connection ⇒ Object
Returns the value of attribute connection.
Instance Attribute Summary collapse
-
#errors ⇒ Hash
Errors hash.
Class Method Summary collapse
-
.all(filter = {}, page = 1, per_page = 50) {|result['_meta']| ... } ⇒ Array
Entities matching the criteria or all.
-
.all!(filter = {}) ⇒ Object
fetches all objects via pagination.
- .api_name ⇒ Object
-
.create(params = {}) ⇒ Enitity
Object has errors when creation failed.
-
.find(id) ⇒ Object
The entity with the unique identifier.
-
.find_by_name(name) ⇒ Object
The entity with the unique identifier.
Instance Method Summary collapse
- #connection ⇒ Object
-
#create ⇒ Boolean
True when successfully created - otherwise false.
- #destroy ⇒ Object
- #meta ⇒ Object
- #update ⇒ Object
Class Attribute Details
.api_endpoint ⇒ Object
Returns the value of attribute api_endpoint.
46 47 48 |
# File 'lib/adcloud/entity.rb', line 46 def api_endpoint @api_endpoint end |
.connection ⇒ Object
Returns the value of attribute connection.
46 47 48 |
# File 'lib/adcloud/entity.rb', line 46 def connection @connection end |
Instance Attribute Details
#errors ⇒ Hash
Returns 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.
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_name ⇒ Object
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.
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.
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.
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
#connection ⇒ Object
12 13 14 |
# File 'lib/adcloud/entity.rb', line 12 def connection self.class.connection end |
#create ⇒ Boolean
Returns 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 |
#destroy ⇒ Object
40 41 42 43 |
# File 'lib/adcloud/entity.rb', line 40 def destroy result = connection.delete("#{self.class.api_endpoint}/#{id}") self end |
#meta ⇒ Object
16 17 18 |
# File 'lib/adcloud/entity.rb', line 16 def self. end |
#update ⇒ Object
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 |