Class: SBF::Client::EntityEndpoint

Inherits:
Object
  • Object
show all
Defined in:
lib/stbaldricks/endpoints/lib/entity.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(target_class) ⇒ EntityEndpoint



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

def initialize(target_class)
  @orig_target_class = target_class
end

Instance Attribute Details

#orig_target_classObject (readonly)

Returns the value of attribute orig_target_class.



13
14
15
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 13

def orig_target_class
  @orig_target_class
end

Instance Method Details

#aggregate(filter = [], aggregate = {}) ⇒ Object

Calls the aggregate route for the entity.



125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 125

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_or_hash, with = {}) ⇒ Object

Calls the create route for the entity. Uses the class name to generate the uri

Raises:



36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 36

def create(entity_or_hash, with = {})
  entity = entity_or_hash.is_a?(Hash) ? target_class.new(entity_or_hash) : entity_or_hash
  raise SBF::Client::Error, 'Invalid Entity' unless entity.is_a?(SBF::Client::BaseEntity)

  with = normalize_with(with, entity)
  create_data = entity.dirty_data
  create_data.store(:with, with)

  response = SBF::Client::Api::Request.post_request("#{base_uri}/create", create_data)

  hydrated_entity(response, create_data, entity)
end

#delete(id) ⇒ Object

Calls the delete route for the entity



141
142
143
144
145
146
147
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 141

def delete(id)
  response = SBF::Client::Api::Request.post_request("#{base_uri}/delete/#{id}")

  error = SBF::Client::ErrorEntity.new(JSON.parse(response.body).symbolize!) unless ok?(response)

  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.



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 92

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, true) }
    SBF::Client::EntityCollection.new(parsed_response_body[:results], parsed_response_body[:total_count])
  else
    parsed_response_body = JSON.parse(response.body).symbolize!
    error = SBF::Client::ErrorEntity.new(parsed_response_body)
    SBF::Client::Api::Response.new(http_code: response.code, data: parsed_response_body[:data], error: error)
    collection = SBF::Client::EntityCollection.new
    collection.add_errors(error)
    collection.errors_http_code = response.code
    collection
  end
end

#find_first(filter = [], order = {}, with = {}) ⇒ Object

Calls the find route for the entity.



115
116
117
118
119
120
121
122
# File 'lib/stbaldricks/endpoints/lib/entity.rb', line 115

def find_first(filter = [], order = {}, with = {})
  response = find(filter, order, 1, 0, with)
  if response.is_a?(SBF::Client::EntityCollection)
    response.empty? ? nil : response.first
  else
    response
  end
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
84
85
86
87
88
89
# 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)

  if ok?(response)
    hydrated_entity(response, {}, nil)
  elsif response.code == 404
    nil
  else
    parsed_response_body = JSON.parse(response.body).symbolize!
    error = SBF::Client::ErrorEntity.new(parsed_response_body)
    SBF::Client::Api::Response.new(http_code: response.code, data: parsed_response_body[:data], error: error)
  end
end

#save(entity_or_hash, with = {}) ⇒ Object

Calls create if the id field is empty. Otherwise it calls update.



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

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.respond_to?(:id) || entity_or_hash.id.nil?

    update(entity_or_hash.id, entity_or_hash, with)
  else
    return create(entity_or_hash, with) if entity_or_hash[:id].nil?

    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



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 52

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.dirty_data
  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)

  hydrated_entity(response, data, entity_or_hash)
end