Class: StrapiModel::Base
- Inherits:
-
Object
- Object
- StrapiModel::Base
- Defined in:
- lib/strapi_model/base.rb
Class Attribute Summary collapse
Class Method Summary collapse
Instance Method Summary collapse
-
#initialize(attributes = {}) ⇒ Base
constructor
A new instance of Base.
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_id ⇒ Object
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
.all ⇒ Object
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 |