Class: FastAPI::Wrapper

Inherits:
Object
  • Object
show all
Includes:
Utilities
Defined in:
lib/fastapi.rb

Instance Method Summary collapse

Methods included from Utilities

#clamp

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

#dataArray

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

Returns:

  • (Array)

    available data



81
82
83
# File 'lib/fastapi.rb', line 81

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



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.

Parameters:

  • id (Integer)

    the id of the object to retrieve

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

    a hash containing custom metadata

Returns:

  • (FastAPI)

    the current instance



68
69
70
71
72
73
74
75
76
# File 'lib/fastapi.rb', line 68

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

  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

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



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

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

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

  self
end

#inspectObject



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

Parameters:

  • fields (Hash)

    Hash containing fields and their related errors

Returns:

  • (String)

    JSON data and metadata, with error



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

#metaHash

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

Returns:

  • (Hash)

    available metadata



95
96
97
# File 'lib/fastapi.rb', line 95

def meta
  
end

#meta_jsonString

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

Returns:

  • (String)

    available metadata in JSON format



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

def meta_json
  Oj.dump()
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



154
155
156
157
158
159
160
161
162
163
164
165
166
# File 'lib/fastapi.rb', line 154

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

#responseString

Intended to return the final API response

Returns:

  • (String)

    JSON data and metadata



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

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



58
59
60
# File 'lib/fastapi.rb', line 58

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

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

Spoofs data from Model

Returns:

  • (String)

    JSON data and metadata



123
124
125
126
127
128
129
# File 'lib/fastapi.rb', line 123

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

  Oj.dump({ meta: meta, data: data })
end

#to_hashHash

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

Returns:

  • (Hash)

    available data and metadata



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

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



32
33
34
35
36
# File 'lib/fastapi.rb', line 32

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

  self
end