Class: FastAPI

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

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

#dataArray

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

Returns:

  • (Array)

    available data



89
90
91
# File 'lib/fastapi.rb', line 89

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



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.

Parameters:

  • id (Integer)

    the id of the object to retrieve

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

    a hash containing custom metadata

Returns:

  • (FastAPI)

    the current instance



76
77
78
79
80
81
82
83
84
# File 'lib/fastapi.rb', line 76

def fetch(id, meta = {})
  filter({ id: id }, meta)

  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

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



50
51
52
53
54
55
56
57
58
# File 'lib/fastapi.rb', line 50

def filter(filters = {}, meta = {}, safe = false)
  result = fastapi_query(filters, safe)

  @metadata    = meta.merge(result.slice(:total, :offset, :count, :error))
  @data        = result[:data]
  @result_type = @@result_types[:multiple]

  self
end

#inspectObject



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

Parameters:

  • fields (Hash)

    Hash containing fields and their related errors

Returns:

  • (String)

    JSON data and metadata, with error



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

#metaHash

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

Returns:

  • (Hash)

    available metadata



103
104
105
# File 'lib/fastapi.rb', line 103

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



110
111
112
# File 'lib/fastapi.rb', line 110

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



162
163
164
165
166
167
168
169
170
171
172
173
174
# File 'lib/fastapi.rb', line 162

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



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

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



66
67
68
# File 'lib/fastapi.rb', line 66

def safe_filter(filters = {}, meta = {})
  filter(filters, meta, true)
end

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

Spoofs data from Model

Returns:

  • (String)

    JSON data and metadata



131
132
133
134
135
136
137
# File 'lib/fastapi.rb', line 131

def spoof(data = [], meta = {})
  meta[:total]  ||= data.count
  meta[:count]  ||= data.count
  meta[:offset] ||= 0

  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



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

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



39
40
41
42
43
# File 'lib/fastapi.rb', line 39

def whitelist(fields = [])
  @whitelist_fields.concat(fields)

  self
end