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

Instance Method Details

#dataObject



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

def data
  @data
end

#data_jsonObject



92
93
94
# File 'lib/fastapi.rb', line 92

def data_json
  Oj.dump(@data)
end

#fetch(id, meta = {}) ⇒ Object



62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/fastapi.rb', line 62

def fetch(id, meta = {})

  result = fastapi_query({id: id})

   = {}

  meta.each do |key, value|
    [key] = value
  end

  if result[:total] == 0
    error = @model.to_s + ' id does not exist'
  else
    error = result[:error]
  end

  [:total] = result[:total]
  [:offset] = result[:offset]
  [:count] = result[:count]
  [:error] = error

   = 
  @data = result[:data]

  @result_type = @@result_types[:multiple]

  self

end

#filter(filters = {}, meta = {}) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/fastapi.rb', line 38

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]

   = 
  @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

#metaObject



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

def meta
  
end

#meta_jsonObject



100
101
102
# File 'lib/fastapi.rb', line 100

def meta_json
  Oj.dump(meta)
end

#reject(message = 'Access denied') ⇒ Object



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

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

#responseObject



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

def response
  Oj.dump(self.to_hash)
end

#to_hashObject



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

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