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 ⇒ Array
Returns the data from the most recently executed ‘filter` or `fetch` call.
-
#data_json ⇒ String
Returns JSONified data from the most recently executed ‘filter` or `fetch` call.
-
#fetch(id, meta = {}) ⇒ FastAPI
Create and execute an optimized SQL query based on specified object id.
-
#filter(filters = {}, meta = {}, safe = false) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters.
-
#initialize(model) ⇒ FastAPI
constructor
A new instance of FastAPI.
- #inspect ⇒ Object
-
#invalid(fields) ⇒ String
Returns a JSONified string representing a rejected API response with invalid fields parameters.
-
#meta ⇒ Hash
Returns the metadata from the most recently executed ‘filter` or `fetch` call.
-
#meta_json ⇒ String
Returns JSONified metadata from the most recently executed ‘filter` or `fetch` call.
-
#reject(message = 'Access denied') ⇒ String
Returns a JSONified string representing a standardized empty API response, with a provided error message.
-
#response ⇒ String
Intended to return the final API response.
-
#safe_filter(filters = {}, meta = {}) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters.
-
#spoof(data = [], meta = {}) ⇒ String
Spoofs data from Model.
-
#to_hash ⇒ Hash
Returns both the data and metadata from the most recently executed ‘filter` or `fetch` call.
-
#whitelist(fields = []) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters.
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 @result_type = 0 @whitelist_fields = [] end |
Instance Method Details
#data ⇒ Array
Returns the data from the most recently executed ‘filter` or `fetch` call.
114 115 116 |
# File 'lib/fastapi.rb', line 114 def data @data end |
#data_json ⇒ String
Returns JSONified data from the most recently executed ‘filter` or `fetch` call.
121 122 123 |
# File 'lib/fastapi.rb', line 121 def data_json Oj.dump(@data, mode: :compat) end |
#fetch(id, meta = {}) ⇒ FastAPI
Create and execute an optimized SQL query based on specified object id. Provides customized error response if not found.
99 100 101 102 103 104 105 106 107 108 109 |
# File 'lib/fastapi.rb', line 99 def fetch(id, = {}) filter({id: id}, ) if [:total] == 0 [:error] = {message: @model.to_s + ' id does not exist'} end self end |
#filter(filters = {}, meta = {}, safe = false) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/fastapi.rb', line 55 def filter(filters = {}, = {}, safe = false) result = fastapi_query(filters, safe) = {} .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 |
#invalid(fields) ⇒ String
Returns a JSONified string representing a rejected API response with invalid fields parameters
184 185 186 187 188 189 190 191 192 193 194 195 196 197 |
# File 'lib/fastapi.rb', line 184 def invalid(fields) Oj.dump({ meta: { total: 0, offset: 0, count: 0, error: { message: 'invalid', fields: fields } }, data: [], }, mode: :compat) end |
#meta ⇒ Hash
Returns the metadata from the most recently executed ‘filter` or `fetch` call.
128 129 130 |
# File 'lib/fastapi.rb', line 128 def end |
#meta_json ⇒ String
Returns JSONified metadata from the most recently executed ‘filter` or `fetch` call.
135 136 137 |
# File 'lib/fastapi.rb', line 135 def Oj.dump(, mode: :compat) end |
#reject(message = 'Access denied') ⇒ String
Returns a JSONified string representing a standardized empty API response, with a provided error message
203 204 205 206 207 208 209 210 211 212 213 214 215 |
# File 'lib/fastapi.rb', line 203 def reject( = 'Access denied') Oj.dump({ meta: { total: 0, offset: 0, count: 0, error: { message: .to_s } }, data: [], }, mode: :compat) end |
#response ⇒ String
Intended to return the final API response
152 153 154 |
# File 'lib/fastapi.rb', line 152 def response Oj.dump(self.to_hash, mode: :compat) end |
#safe_filter(filters = {}, meta = {}) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters.
Runs through mode fastapi_safe_fields list
85 86 87 88 89 90 91 |
# File 'lib/fastapi.rb', line 85 def safe_filter(filters = {}, = {}) filter(filters, , true) self end |
#spoof(data = [], meta = {}) ⇒ String
Spoofs data from Model
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 |
# File 'lib/fastapi.rb', line 159 def spoof(data = [], = {}) if not .has_key? :total [:total] = data.count end if not .has_key? :offset [:offset] = 0 end if not .has_key? :count [:count] = data.count end Oj.dump({ meta: , data: data }, mode: :compat) end |
#to_hash ⇒ Hash
Returns both the data and metadata from the most recently executed ‘filter` or `fetch` call.
142 143 144 145 146 147 |
# File 'lib/fastapi.rb', line 142 def to_hash { meta: , data: @data } end |
#whitelist(fields = []) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters
42 43 44 45 46 47 48 |
# File 'lib/fastapi.rb', line 42 def whitelist(fields = []) @whitelist_fields.concat fields self end |