Class: Nylas::RestfulModelCollection
- Inherits:
-
Object
- Object
- Nylas::RestfulModelCollection
show all
- Defined in:
- lib/restful_model_collection.rb
Instance Attribute Summary collapse
Instance Method Summary
collapse
Constructor Details
#initialize(model_class, api, filters = {}) ⇒ RestfulModelCollection
Returns a new instance of RestfulModelCollection.
8
9
10
11
12
13
|
# File 'lib/restful_model_collection.rb', line 8
def initialize(model_class, api, filters = {})
raise StandardError.new unless api.class <= Nylas::API
@model_class = model_class
@filters = filters
@_api = api
end
|
Instance Attribute Details
#filters ⇒ Object
Returns the value of attribute filters.
6
7
8
|
# File 'lib/restful_model_collection.rb', line 6
def filters
@filters
end
|
Instance Method Details
#all ⇒ Object
42
43
44
|
# File 'lib/restful_model_collection.rb', line 42
def all
range(0, Float::INFINITY)
end
|
#build(args) ⇒ Object
93
94
95
96
97
98
99
100
|
# File 'lib/restful_model_collection.rb', line 93
def build(args)
for key in args.keys
args[key.to_s] = args[key]
end
model = @model_class.new(@_api)
model.inflate(args)
model
end
|
#count ⇒ Object
31
32
33
34
35
36
|
# File 'lib/restful_model_collection.rb', line 31
def count
RestClient.get(url, params: @filters.merge(view: 'count')) { |response,request,result|
json = Nylas.interpret_response(result, response)
return json['count']
}
end
|
#delete(item_or_id) ⇒ Object
83
84
85
86
|
# File 'lib/restful_model_collection.rb', line 83
def delete(item_or_id)
item_or_id = item_or_id.id if item_or_id.is_a?(RestfulModel)
RestClient.delete("#{url}/#{id}")
end
|
#each ⇒ Object
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
|
# File 'lib/restful_model_collection.rb', line 15
def each
return enum_for(:each) unless block_given?
offset = 0
chunk_size = 100
finished = false
while (!finished) do
results = get_model_collection(offset, chunk_size)
results.each { |item|
yield item
}
offset += results.length
finished = results.length < chunk_size
end
end
|
#find(id) ⇒ Object
88
89
90
91
|
# File 'lib/restful_model_collection.rb', line 88
def find(id)
return nil unless id
get_model(id)
end
|
#first ⇒ Object
38
39
40
|
# File 'lib/restful_model_collection.rb', line 38
def first
get_model_collection.first
end
|
#inflate_collection(items = []) ⇒ Object
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
|
# File 'lib/restful_model_collection.rb', line 102
def inflate_collection(items = [])
models = []
return unless items.is_a?(Array)
items.each do |json|
if @model_class < RestfulModel
model = @model_class.new(@_api)
model.inflate(json)
else
model = @model_class.new(json)
end
models.push(model)
end
models
end
|
#range(offset = 0, limit = 100) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
|
# File 'lib/restful_model_collection.rb', line 62
def range(offset = 0, limit = 100)
accumulated = []
finished = false
chunk_size = 100
if limit < chunk_size
chunk_size = limit
end
while (!finished && accumulated.length < limit) do
results = get_model_collection(offset + accumulated.length, chunk_size)
accumulated = accumulated.concat(results)
finished = accumulated.length >= limit || results.length == 0 || (results.length % chunk_size != 0)
end
accumulated = accumulated[0..limit] if limit < Float::INFINITY
accumulated
end
|
#url ⇒ Object
118
119
120
|
# File 'lib/restful_model_collection.rb', line 118
def url
@_api.url_for_path("/#{@model_class.collection_name}")
end
|
#where(filters) ⇒ Object
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
# File 'lib/restful_model_collection.rb', line 46
def where(filters)
collection = self.clone
if collection.filters == nil
collection.filters = {}
else
collection.filters = Marshal.load(Marshal.dump(collection.filters))
end
collection.filters.merge!(filters)
collection
end
|