Class: SBF::Client::EntityEndpoint
- Inherits:
-
Object
- Object
- SBF::Client::EntityEndpoint
- Defined in:
- lib/stbaldricks/endpoints/lib/entity.rb
Direct Known Subclasses
CommunicateEndpoint, ConfigEndpoint, ContactEndpoint, EventApplicationEndpoint, EventEndpoint, FundEndpoint, KidEndpoint, KidHonorEndpoint, MatchingGiftCompanyEndpoint, MessageEndpoint, NewsletterRecipientEndpoint, ParticipantEndpoint, PhotoEndpoint, RecurringGiftEndpoint, SearchEndpoint, ShaveScheduleEndpoint, UserEndpoint
Instance Attribute Summary collapse
-
#orig_target_class ⇒ Object
readonly
Returns the value of attribute orig_target_class.
Instance Method Summary collapse
-
#aggregate(filter = [], aggregate = {}) ⇒ Object
Calls the aggregate route for the entity.
-
#create(entity, with = {}) ⇒ Object
Calls the create route for the entity.
-
#delete(id) ⇒ Object
Calls the delete route for the entity.
-
#find(filter = [], order = {}, limit = 20, offset = 0, with = {}) ⇒ Object
Calls the find route for the entity.
-
#find_first(filter = [], order = {}, with = {}) ⇒ Object
Calls the find route for the entity.
-
#get(id, with = {}) ⇒ SBF::Client::BaseEntity
Calls the get route for the entity.
-
#initialize(target_class) ⇒ EntityEndpoint
constructor
A new instance of EntityEndpoint.
-
#save(entity_or_hash, with = {}) ⇒ Object
Calls create if the id field is empty.
-
#update(id = nil, entity_or_hash = nil, with = {}) ⇒ Object
Calls the update route for the entity.
Constructor Details
#initialize(target_class) ⇒ EntityEndpoint
Returns a new instance of EntityEndpoint.
15 16 17 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 15 def initialize(target_class) @orig_target_class = target_class end |
Instance Attribute Details
#orig_target_class ⇒ Object (readonly)
Returns the value of attribute orig_target_class.
12 13 14 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 12 def orig_target_class @orig_target_class end |
Instance Method Details
#aggregate(filter = [], aggregate = {}) ⇒ Object
Calls the aggregate route for the entity.
112 113 114 115 116 117 118 119 120 121 122 123 124 125 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 112 def aggregate(filter = [], aggregate = {}) filter = filter.to_json unless filter.is_a? String aggregate = aggregate.to_json unless aggregate.is_a? String response = SBF::Client::Api::Request.get_request("#{base_uri}/aggregate", filter: filter, aggregate: aggregate) parsed_response_body = JSON.parse(response.body).symbolize! if ok?(response) data = parsed_response_body[:aggregate] else error = SBF::Client::ErrorEntity.new(parsed_response_body) end SBF::Client::Api::Response.new(http_code: response.code, data: data, error: error) end |
#create(entity, with = {}) ⇒ Object
Calls the create route for the entity. Uses the class name to generate the uri
33 34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 33 def create(entity, with = {}) raise SBF::Client::Error, 'Invalid Entity' unless entity.is_a?(SBF::Client::BaseEntity) with = normalize_with(with, entity) create_data = entity.to_hash create_data.store(:with, with) response = SBF::Client::Api::Request.post_request("#{base_uri}/create", create_data) entity, error = hydrate_entity(response, create_data, entity) SBF::Client::Api::Response.new(http_code: response.code, data: entity, error: error) end |
#delete(id) ⇒ Object
Calls the delete route for the entity
128 129 130 131 132 133 134 135 136 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 128 def delete(id) response = SBF::Client::Api::Request.post_request("#{base_uri}/delete/#{id}") unless ok?(response) error = SBF::Client::ErrorEntity.new(JSON.parse(response.body).symbolize!) end SBF::Client::Api::Response.new(http_code: response.code, data: nil, error: error) end |
#find(filter = [], order = {}, limit = 20, offset = 0, with = {}) ⇒ Object
Calls the find route for the entity.
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 86 def find(filter = [], order = {}, limit = 20, offset = 0, with = {}) filter = filter.to_json unless filter.is_a? String order = order.to_json unless order.is_a? String with = normalize_with(with) response = SBF::Client::Api::Request.get_request("#{base_uri}/find", filter: filter, order: order, limit: limit, offset: offset, with: with) parsed_response_body = JSON.parse(response.body).symbolize! if ok?(response) parsed_response_body[:results].map! { |entity_data| target_class.new(entity_data) } data = parsed_response_body else error = SBF::Client::ErrorEntity.new(parsed_response_body) end SBF::Client::Api::Response.new(http_code: response.code, data: data, error: error) end |
#find_first(filter = [], order = {}, with = {}) ⇒ Object
Calls the find route for the entity.
105 106 107 108 109 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 105 def find_first(filter = [], order = {}, with = {}) api_response = find(filter, order, 1, 0, with) api_response.data = api_response.data[:results].first unless api_response.error? api_response end |
#get(id, with = {}) ⇒ SBF::Client::BaseEntity
Calls the get route for the entity. Uses the class name to generate the uri.
75 76 77 78 79 80 81 82 83 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 75 def get(id, with = {}) with = normalize_with(with) response = SBF::Client::Api::Request.get_request("#{base_uri}/get/#{id}", with: with) data, error = hydrate_entity(response, {}, nil) SBF::Client::Api::Response.new(http_code: response.code, data: data, error: error) end |
#save(entity_or_hash, with = {}) ⇒ Object
Calls create if the id field is empty. Otherwise it calls update.
20 21 22 23 24 25 26 27 28 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 20 def save(entity_or_hash, with = {}) if entity_or_hash.is_a?(SBF::Client::BaseEntity) return create(entity_or_hash, with) if entity_or_hash.id.nil? return update(entity_or_hash.id, entity_or_hash, with) else return create(entity_or_hash, with) if entity_or_hash[:id].nil? return update(entity_or_hash[:id], entity_or_hash, with) end end |
#update(id = nil, entity_or_hash = nil, with = {}) ⇒ Object
Calls the update route for the entity. Uses the class name to generate the uri
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 50 def update(id = nil, entity_or_hash = nil, with = {}) if entity_or_hash.is_a?(SBF::Client::BaseEntity) # If someone has passed in an entity, just convert it to a hash data = entity_or_hash.to_hash elsif entity_or_hash.is_a?(Hash) # If someone has passed in a hash, make sure all of it's values match fields in the entity class data = sanitize(entity_or_hash) else raise SBF::Client::Error, 'Invalid Data' end with = normalize_with(with, entity_or_hash) data.store(:with, with) uri = id ? "#{base_uri}/update/#{id}" : "#{base_uri}/update" response = SBF::Client::Api::Request.post_request(uri, data) data, error = hydrate_entity(response, data, entity_or_hash) SBF::Client::Api::Response.new(http_code: response.code, data: data, error: error) end |