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 = {}) ⇒ 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.
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 @metadata = nil @has_results = false @result_type = 0 end |
Instance Method Details
#data ⇒ Array
Returns the data from the most recently executed ‘filter` or `fetch` call.
137 138 139 |
# File 'lib/fastapi.rb', line 137 def data @data end |
#data_json ⇒ String
Returns JSONified data from the most recently executed ‘filter` or `fetch` call.
144 145 146 |
# File 'lib/fastapi.rb', line 144 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.
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/fastapi.rb', line 104 def fetch(id, = {}) result = fastapi_query({id: id}) = {} .each do |key, value| [key] = value end if result[:total] == 0 error = {message: @model.to_s + ' id does not exist'} else error = result[:error] end [:total] = result[:total] [:offset] = result[:offset] [:count] = result[:count] [:error] = error @metadata = @data = result[:data] @result_type = @@result_types[:multiple] self end |
#filter(filters = {}, meta = {}) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters
44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 |
# File 'lib/fastapi.rb', line 44 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] @metadata = @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
207 208 209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/fastapi.rb', line 207 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.
151 152 153 |
# File 'lib/fastapi.rb', line 151 def @metadata end |
#meta_json ⇒ String
Returns JSONified metadata from the most recently executed ‘filter` or `fetch` call.
158 159 160 |
# File 'lib/fastapi.rb', line 158 def Oj.dump(@metadata, mode: :compat) end |
#reject(message = 'Access denied') ⇒ String
Returns a JSONified string representing a standardized empty API response, with a provided error message
226 227 228 229 230 231 232 233 234 235 236 237 238 |
# File 'lib/fastapi.rb', line 226 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
175 176 177 |
# File 'lib/fastapi.rb', line 175 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
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 |
# File 'lib/fastapi.rb', line 74 def safe_filter(filters = {}, = {}) result = fastapi_query(filters, true) = {} .each do |key, value| [key] = value end [:total] = result[:total] [:offset] = result[:offset] [:count] = result[:count] [:error] = result[:error] @metadata = @data = result[:data] @result_type = @@result_types[:multiple] self end |
#spoof(data = [], meta = {}) ⇒ String
Spoofs data from Model
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 |
# File 'lib/fastapi.rb', line 182 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.
165 166 167 168 169 170 |
# File 'lib/fastapi.rb', line 165 def to_hash { meta: @metadata, data: @data } end |