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
  @metadata = nil
  @has_results = false
  @result_type = 0
end

Instance Method Details

#dataArray

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

Returns:

  • (Array)

    available data



137
138
139
# File 'lib/fastapi.rb', line 137

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



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.

Parameters:

  • id (Integer)

    the id of the object to retrieve

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

    a hash containing custom metadata

Returns:

  • (FastAPI)

    the current instance



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

  result = fastapi_query({id: id})

   = {}

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

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



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

  result = fastapi_query(filters)

   = {}

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

#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



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

#metaHash

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

Returns:

  • (Hash)

    available metadata



151
152
153
# File 'lib/fastapi.rb', line 151

def meta
  @metadata
end

#meta_jsonString

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

Returns:

  • (String)

    available metadata in JSON format



158
159
160
# File 'lib/fastapi.rb', line 158

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

Parameters:

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

    Error message to be used in response

Returns:

  • (String)

    JSON data and metadata, with error



226
227
228
229
230
231
232
233
234
235
236
237
238
# File 'lib/fastapi.rb', line 226

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



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

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



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

  result = fastapi_query(filters, true)

   = {}

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

Returns:

  • (String)

    JSON data and metadata



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 = [], 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



165
166
167
168
169
170
# File 'lib/fastapi.rb', line 165

def to_hash
  {
    meta: @metadata,
    data: @data
  }
end