Class: FastAPI::Wrapper
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) ⇒ Wrapper
constructor
A new instance of Wrapper.
- #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.
Methods included from Utilities
Constructor Details
#initialize(model) ⇒ Wrapper
Returns a new instance of Wrapper.
17 18 19 20 21 22 |
# File 'lib/fastapi.rb', line 17 def initialize(model) @model = model @data = nil = nil @whitelist_fields = [] end |
Instance Method Details
#data ⇒ Array
Returns the data from the most recently executed ‘filter` or `fetch` call.
81 82 83 |
# File 'lib/fastapi.rb', line 81 def data @data end |
#data_json ⇒ String
Returns JSONified data from the most recently executed ‘filter` or `fetch` call.
88 89 90 |
# File 'lib/fastapi.rb', line 88 def data_json Oj.dump(@data) end |
#fetch(id, meta = {}) ⇒ FastAPI
Create and execute an optimized SQL query based on specified object id. Provides customized error response if not found.
68 69 70 71 72 73 74 75 76 |
# File 'lib/fastapi.rb', line 68 def fetch(id, = {}) filter({ id: id }, ) if [:total].zero? [:error] = { message: "#{@model} with id: #{id} does not exist" } end self end |
#filter(filters = {}, meta = {}, safe = false) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters
43 44 45 46 47 48 49 50 |
# File 'lib/fastapi.rb', line 43 def filter(filters = {}, = {}, safe = false) result = fastapi_query(filters, safe) = .merge(result.slice(:total, :offset, :count, :error)) @data = result[:data] self end |
#inspect ⇒ Object
24 25 26 |
# File 'lib/fastapi.rb', line 24 def inspect "<#{self.class}: #{@model}>" end |
#invalid(fields) ⇒ String
Returns a JSONified string representing a rejected API response with invalid fields parameters
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/fastapi.rb', line 135 def invalid(fields) Oj.dump({ meta: { total: 0, offset: 0, count: 0, error: { message: 'invalid', fields: fields } }, data: [] }) end |
#meta ⇒ Hash
Returns the metadata from the most recently executed ‘filter` or `fetch` call.
95 96 97 |
# File 'lib/fastapi.rb', line 95 def end |
#meta_json ⇒ String
Returns JSONified metadata from the most recently executed ‘filter` or `fetch` call.
102 103 104 |
# File 'lib/fastapi.rb', line 102 def Oj.dump() end |
#reject(message = 'Access denied') ⇒ String
Returns a JSONified string representing a standardized empty API response, with a provided error message
154 155 156 157 158 159 160 161 162 163 164 165 166 |
# File 'lib/fastapi.rb', line 154 def reject( = 'Access denied') Oj.dump({ meta: { total: 0, offset: 0, count: 0, error: { message: } }, data: [] }) end |
#response ⇒ String
Intended to return the final API response
116 117 118 |
# File 'lib/fastapi.rb', line 116 def response Oj.dump(self.to_hash) end |
#safe_filter(filters = {}, meta = {}) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters.
Runs through mode fastapi_safe_fields list
58 59 60 |
# File 'lib/fastapi.rb', line 58 def safe_filter(filters = {}, = {}) filter(filters, , true) end |
#spoof(data = [], meta = {}) ⇒ String
Spoofs data from Model
123 124 125 126 127 128 129 |
# File 'lib/fastapi.rb', line 123 def spoof(data = [], = {}) [:total] ||= data.count [:count] ||= data.count [:offset] ||= 0 Oj.dump({ meta: , data: data }) end |
#to_hash ⇒ Hash
Returns both the data and metadata from the most recently executed ‘filter` or `fetch` call.
109 110 111 |
# File 'lib/fastapi.rb', line 109 def to_hash { meta: , data: @data } end |
#whitelist(fields = []) ⇒ FastAPI
Create and execute an optimized SQL query based on specified filters
32 33 34 35 36 |
# File 'lib/fastapi.rb', line 32 def whitelist(fields = []) @whitelist_fields.concat(fields) self end |