Class: StrapiModel::Base

Inherits:
Object
  • Object
show all
Defined in:
lib/strapi_model/base.rb

Class Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(attributes = {}) ⇒ Base

Returns a new instance of Base.



8
9
10
11
12
# File 'lib/strapi_model/base.rb', line 8

def initialize(attributes = {})
  attributes.each do |key, value|
    define_singleton_method(key.to_s.underscore.downcase) { value }
  end
end

Class Attribute Details

.api_idObject



19
20
21
# File 'lib/strapi_model/base.rb', line 19

def api_id
  @api_id ||= self.name.to_s.underscore.dasherize.pluralize
end

Class Method Details

.allObject



23
24
25
26
# File 'lib/strapi_model/base.rb', line 23

def all
  response = Query.get(api_id)
  set_relation(response)
end

.find(id) ⇒ Object



37
38
39
40
# File 'lib/strapi_model/base.rb', line 37

def find(id)
  response = Query.get(api_id, '', id)
  self.new(response[:data][:attributes])
end

.find_by(**params) ⇒ Object



42
43
44
45
46
47
48
49
# File 'lib/strapi_model/base.rb', line 42

def find_by(**params)
  query_params = 'filters'
  params.each do |key, value|
    query_params += "[#{key.capitalize()}][$eq]=#{value}"
  end
  response = Query.get(api_id, query_params)
  self.new(response[:data][0][:attributes])
end

.where(**params) ⇒ Object



28
29
30
31
32
33
34
35
# File 'lib/strapi_model/base.rb', line 28

def where(**params)
  query_params = 'filters'
  params.each do |key, value|
    query_params += "[#{key.capitalize()}][$eq]=#{value}"
  end
  response = Query.get(api_id, query_params)
  set_relation(response)
end