Class: FastAPI
- Inherits:
-
Object
- Object
- FastAPI
- Defined in:
- lib/fastapi.rb
Constant Summary collapse
- @@result_types =
{ single: 0, multiple: 1 }
- @@api_comparator_list =
%w( 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.
23 24 25 26 27 28 29 |
# File 'lib/fastapi.rb', line 23 def initialize(model) @model = model @data = nil @metadata = nil @result_type = 0 @whitelist_fields = [] end |
Instance Method Details
#data ⇒ Array
Returns the data from the most recently executed ‘filter` or `fetch` call.
89 90 91 |
# File 'lib/fastapi.rb', line 89 def data @data end |
#data_json ⇒ String
Returns JSONified data from the most recently executed ‘filter` or `fetch` call.
96 97 98 |
# File 'lib/fastapi.rb', line 96 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.
76 77 78 79 80 81 82 83 84 |
# File 'lib/fastapi.rb', line 76 def fetch(id, = {}) filter({ id: id }, ) if @metadata[:total].zero? @metadata[:error] = { message: "#{@model} id does not exist" } end self end |
#filter(filters = {}, meta = {}, safe = false) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters
50 51 52 53 54 55 56 57 58 |
# File 'lib/fastapi.rb', line 50 def filter(filters = {}, = {}, safe = false) result = fastapi_query(filters, safe) @metadata = .merge(result.slice(:total, :offset, :count, :error)) @data = result[:data] @result_type = @@result_types[:multiple] self end |
#inspect ⇒ Object
31 32 33 |
# File 'lib/fastapi.rb', line 31 def inspect "<#{self.class}: #{@model}>" end |
#invalid(fields) ⇒ String
Returns a JSONified string representing a rejected API response with invalid fields parameters
143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
# File 'lib/fastapi.rb', line 143 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.
103 104 105 |
# File 'lib/fastapi.rb', line 103 def @metadata end |
#meta_json ⇒ String
Returns JSONified metadata from the most recently executed ‘filter` or `fetch` call.
110 111 112 |
# File 'lib/fastapi.rb', line 110 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
162 163 164 165 166 167 168 169 170 171 172 173 174 |
# File 'lib/fastapi.rb', line 162 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
124 125 126 |
# File 'lib/fastapi.rb', line 124 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
66 67 68 |
# File 'lib/fastapi.rb', line 66 def safe_filter(filters = {}, = {}) filter(filters, , true) end |
#spoof(data = [], meta = {}) ⇒ String
Spoofs data from Model
131 132 133 134 135 136 137 |
# File 'lib/fastapi.rb', line 131 def spoof(data = [], = {}) [:total] ||= data.count [:count] ||= data.count [:offset] ||= 0 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.
117 118 119 |
# File 'lib/fastapi.rb', line 117 def to_hash { meta: @metadata, data: @data } end |
#whitelist(fields = []) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters
39 40 41 42 43 |
# File 'lib/fastapi.rb', line 39 def whitelist(fields = []) @whitelist_fields.concat(fields) self end |