Class: FastAPI

Inherits:
Object
  • Object
show all
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

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

#dataArray

Returns the data from the most recently executed ‘filter` or `fetch` call.

Returns:

  • (Array)

    available data



114
115
116
# File 'lib/fastapi.rb', line 114

def data
  @data
end

#data_jsonString

Returns JSONified data from the most recently executed ‘filter` or `fetch` call.

Returns:

  • (String)

    available data in JSON format



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.

Parameters:

  • id (Integer)

    the id of the object to retrieve

  • meta (Hash) (defaults to: {})

    a hash containing custom metadata

Returns:

  • (FastAPI)

    the current instance



99
100
101
102
103
104
105
106
107
108
109
# File 'lib/fastapi.rb', line 99

def fetch(id, meta = {})

  filter({id: id}, meta)

  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

Parameters:

  • filters (Hash) (defaults to: {})

    a hash containing the intended filters

  • meta (Hash) (defaults to: {})

    a hash containing custom metadata

Returns:

  • (FastAPI)

    the current instance



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 = {}, meta = {}, safe = false)

  result = fastapi_query(filters, safe)

   = {}

  meta.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

#inspectObject



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

Parameters:

  • fields (Hash)

    Hash containing fields and their related errors

Returns:

  • (String)

    JSON data and metadata, with error



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

#metaHash

Returns the metadata from the most recently executed ‘filter` or `fetch` call.

Returns:

  • (Hash)

    available metadata



128
129
130
# File 'lib/fastapi.rb', line 128

def meta
  
end

#meta_jsonString

Returns JSONified metadata from the most recently executed ‘filter` or `fetch` call.

Returns:

  • (String)

    available metadata in JSON format



135
136
137
# File 'lib/fastapi.rb', line 135

def meta_json
  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

Parameters:

  • message (String) (defaults to: 'Access denied')

    Error message to be used in response

Returns:

  • (String)

    JSON data and metadata, with error



203
204
205
206
207
208
209
210
211
212
213
214
215
# File 'lib/fastapi.rb', line 203

def reject(message = 'Access denied')
  Oj.dump({
    meta: {
      total: 0,
      offset: 0,
      count: 0,
      error: {
        message: message.to_s
      }
    },
    data: [],
  }, mode: :compat)
end

#responseString

Intended to return the final API response

Returns:

  • (String)

    JSON data and metadata



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

Parameters:

  • filters (Hash) (defaults to: {})

    a hash containing the intended filters

  • meta (Hash) (defaults to: {})

    a hash containing custom metadata

Returns:

  • (FastAPI)

    the current instance



85
86
87
88
89
90
91
# File 'lib/fastapi.rb', line 85

def safe_filter(filters = {}, meta = {})

  filter(filters, meta, true)

  self

end

#spoof(data = [], meta = {}) ⇒ String

Spoofs data from Model

Returns:

  • (String)

    JSON data and metadata



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 = [], meta = {})

  if not meta.has_key? :total
    meta[:total] = data.count
  end

  if not meta.has_key? :offset
    meta[:offset] = 0
  end

  if not meta.has_key? :count
    meta[:count] = data.count
  end

  Oj.dump({
    meta: meta,
    data: data
  }, mode: :compat)

end

#to_hashHash

Returns both the data and metadata from the most recently executed ‘filter` or `fetch` call.

Returns:

  • (Hash)

    available data and metadata



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

Parameters:

  • fields (Array) (defaults to: [])

    an array containing fields to whitelist for the SQL query. Can also pass in fields as arguments.

Returns:

  • (FastAPI)

    the current instance



42
43
44
45
46
47
48
# File 'lib/fastapi.rb', line 42

def whitelist(fields = [])

  @whitelist_fields.concat fields

  self

end