Class: FastAPI
- Inherits:
-
Object
- Object
- FastAPI
- Defined in:
- lib/fastapi.rb
Constant Summary collapse
- @@result_types =
{ single: 0, multiple: 1, }
- @@api_comparator_list =
[ 'is', 'not', 'gt', 'gte', 'lt', 'lte', 'in', 'not_in', 'contains', 'icontains', 'is_null', 'not_null', ]
Instance Method Summary collapse
- #data ⇒ Object
- #data_json ⇒ Object
- #fetch(id, meta = {}) ⇒ Object
- #filter(filters = {}, meta = {}) ⇒ Object
-
#initialize(model) ⇒ FastAPI
constructor
A new instance of FastAPI.
- #inspect ⇒ Object
- #meta ⇒ Object
- #meta_json ⇒ Object
- #reject(message = 'Access denied') ⇒ Object
- #response ⇒ Object
- #to_hash ⇒ Object
Constructor Details
#initialize(model) ⇒ FastAPI
Returns a new instance of FastAPI.
26 27 28 29 30 31 32 |
# File 'lib/fastapi.rb', line 26 def initialize(model) @model = model @data = nil = nil @has_results = false @result_type = 0 end |
Instance Method Details
#data ⇒ Object
96 97 98 |
# File 'lib/fastapi.rb', line 96 def data @data end |
#data_json ⇒ Object
92 93 94 |
# File 'lib/fastapi.rb', line 92 def data_json Oj.dump(@data) end |
#fetch(id, meta = {}) ⇒ Object
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 |
# File 'lib/fastapi.rb', line 62 def fetch(id, = {}) result = fastapi_query({id: id}) = {} .each do |key, value| [key] = value end if result[:total] == 0 error = @model.to_s + ' id does not exist' else error = result[:error] end [:total] = result[:total] [:offset] = result[:offset] [:count] = result[:count] [:error] = error = @data = result[:data] @result_type = @@result_types[:multiple] self end |
#filter(filters = {}, meta = {}) ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/fastapi.rb', line 38 def filter(filters = {}, = {}) result = fastapi_query(filters) = {} .each do |key, value| [key] = value end [:total] = result[:total] [:offset] = result[:offset] [:count] = result[:count] [:error] = result[:error] = @data = result[:data] @result_type = @@result_types[:multiple] self end |
#inspect ⇒ Object
34 35 36 |
# File 'lib/fastapi.rb', line 34 def inspect "<#{self.class}: #{@model}>" end |
#meta ⇒ Object
104 105 106 |
# File 'lib/fastapi.rb', line 104 def end |
#meta_json ⇒ Object
100 101 102 |
# File 'lib/fastapi.rb', line 100 def Oj.dump() end |
#reject(message = 'Access denied') ⇒ Object
119 120 121 122 123 124 125 126 127 128 129 |
# File 'lib/fastapi.rb', line 119 def reject( = 'Access denied') Oj.dump({ meta: { total: 0, offset: 0, count: 0, error: .to_s }, data: [], }) end |
#response ⇒ Object
115 116 117 |
# File 'lib/fastapi.rb', line 115 def response Oj.dump(self.to_hash) end |
#to_hash ⇒ Object
108 109 110 111 112 113 |
# File 'lib/fastapi.rb', line 108 def to_hash { meta: , data: @data } end |