Module: RescueGroups::Queryable::ClassMethods

Defined in:
lib/queryable.rb

Instance Method Summary collapse

Instance Method Details

#find(ids) ⇒ Object

method: find purpose: find one or many objects by primary key (id) param: ids Array<Integer> primary key(s) of object(s) that will

be requested

return: One found object if one id was given, otherwise an array of found objects

If the response was not successful, an exception is thrown


22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/queryable.rb', line 22

def find(ids)
  find_request = Requests::Find.new(ids, self, api_client)

  response = find_request.request

  unless response.success? && !response['data'].nil? && !response['data'].empty?
    fail(NotFound, "Unable to find #{ self.name } with id: #{ ids }")
  end

  objects = response['data'].map { |data| new(data) }

  [*ids].flatten.length == 1 ? objects.first : objects
end

#where(conditions) ⇒ Object

method: where purpose: find one or many objects by any supported filters param: conditions - <Hash> - mutliple keyed hash containing

  the conditions to search against.
example: { breed: 'daschund' }

return: <Array> -An array of found objects

If the response was not successful, an exception is thrown


43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/queryable.rb', line 43

def where(conditions)
  return find(conditions[:id]) if conditions.keys == [:id]

  where_request = Requests::Where.new(conditions, self, api_client, search_engine_class)

  response = where_request.request

  fail(InvalidRequest, "Problem with request #{ response.error }") unless response.success?

  response_with_additional = additional_request_data(where_request)

  response_with_additional['data'].map do |_data_id, data|
    new(data)
  end
end