-
Notifications
You must be signed in to change notification settings - Fork 137
/
datatypes.rb
332 lines (276 loc) · 6.85 KB
/
datatypes.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
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
91
92
93
94
95
96
97
98
99
100
101
102
103
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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
# encoding: utf-8
require 'time'
require 'date'
require 'base64'
module Parse
# A pointer to a Parse object
# https://parseplatform.github.io/docs/rest/guide/#data-types
class Pointer
attr_accessor :parse_object_id
attr_accessor :class_name
alias id parse_object_id
def self.make(class_name, object_id)
Pointer.new(
Protocol::KEY_CLASS_NAME => class_name,
Protocol::KEY_OBJECT_ID => object_id
)
end
def initialize(data)
@class_name = data[Protocol::KEY_CLASS_NAME]
@parse_object_id = data[Protocol::KEY_OBJECT_ID]
end
# make it easier to deal with the ambiguity of whether
# you're passed a pointer or object
def pointer
self
end
def eql?(other)
Parse.object_pointer_equality?(self, other)
end
alias == eql?
def hash
Parse.object_pointer_hash(self)
end
def new?
false
end
def to_h(*_a)
{
Protocol::KEY_TYPE => Protocol::TYPE_POINTER,
Protocol::KEY_CLASS_NAME => @class_name,
Protocol::KEY_OBJECT_ID => @parse_object_id
}
end
alias as_json to_h
def to_json(*a)
to_h.to_json(*a)
end
# Retrieve the Parse object referenced by this pointer.
def get(client = nil)
Parse.get(@class_name, @parse_object_id, client) if @parse_object_id
end
def to_s
"#{@class_name}:#{@parse_object_id}"
end
end
# A Parse Date
# https://parseplatform.github.io/docs/rest/guide/#data-types
class Date
attr_accessor :value
def initialize(data)
if data.respond_to?(:iso8601)
@value = data
elsif data.is_a? Hash
@value = DateTime.parse data['iso']
elsif data.is_a? String
@value = DateTime.parse data
else
raise "data doesn't act like time #{data.inspect}"
end
end
def eql?(other)
self.class.equal?(other.class) &&
value == other.value
end
alias == eql?
def hash
value.hash
end
def <=>(other)
value <=> other.value
end
def method_missing(method, *args, &block)
if value.respond_to?(method)
value.send(method, *args, &block)
else
super(method)
end
end
def respond_to?(method, include_private = false)
super || value.respond_to?(method, include_private)
end
def to_h(*_a)
{
Protocol::KEY_TYPE => Protocol::TYPE_DATE,
'iso' => value.to_time.utc.iso8601(3)
}
end
alias as_json to_h
def to_json(*a)
to_h.to_json(*a)
end
end
# Bytes
class Bytes
attr_accessor :value
def initialize(data)
bytes = data['base64']
@value = Base64.decode64(bytes)
end
def eql?(other)
self.class.equal?(other.class) &&
value == other.value
end
alias == eql?
def hash
value.hash
end
def <=>(other)
value <=> other.value
end
def method_missing(method, *args, &block)
if value.respond_to?(method)
value.send(method, *args, &block)
else
super(method)
end
end
def respond_to?(method, include_private = false)
super || value.respond_to?(method, include_private)
end
def to_h(*_a)
{
Protocol::KEY_TYPE => Protocol::TYPE_BYTES,
'base64' => Base64.encode64(@value)
}
end
alias as_json to_h
def to_json(*a)
to_h.to_json(*a)
end
end
# Increment (or decrement) counter
# https://parseplatform.github.io/docs/rest/guide/#counters
class Increment
attr_accessor :amount
def initialize(amount)
@amount = amount
end
def eql?(other)
self.class.equal?(other.class) &&
amount == other.amount
end
alias == eql?
def hash
amount.hash
end
def to_h(*_a)
{
Protocol::KEY_OP => Protocol::KEY_INCREMENT,
Protocol::KEY_AMOUNT => @amount
}
end
alias as_json to_h
def to_json(*a)
to_h.to_json(*a)
end
end
# Array operation
# https://parseplatform.github.io/docs/rest/guide/#arrays
class ArrayOp
attr_accessor :operation
attr_accessor :objects
def initialize(operation, objects)
@operation = operation
@objects = objects
end
def eql?(other)
self.class.equal?(other.class) &&
operation == other.operation &&
objects == other.objects
end
alias == eql?
def hash
operation.hash ^ objects.hash
end
def to_h(*_a)
{
Protocol::KEY_OP => operation,
Protocol::KEY_OBJECTS => @objects
}
end
alias as_json to_h
def to_json(*a)
to_h.to_json(*a)
end
end
# GeoPoint
# https://parseplatform.github.io/docs/rest/guide/#geopoints
class GeoPoint
attr_accessor :longitude, :latitude
def initialize(data)
@longitude = data['longitude'] || data[:longitude]
@latitude = data['latitude'] || data[:latitude]
end
def eql?(other)
self.class.equal?(other.class) &&
longitude == other.longitude &&
latitude == other.latitude
end
alias == eql?
def hash
longitude.hash ^ latitude.hash
end
def to_h(*_a)
{
Protocol::KEY_TYPE => Protocol::TYPE_GEOPOINT,
'latitude' => @latitude,
'longitude' => @longitude
}
end
alias as_json to_h
def to_json(*a)
to_h.to_json(*a)
end
def to_s
"(#{latitude}, #{longitude})"
end
end
# File
# https://parseplatform.github.io/docs/rest/guide/#uploading-files
class File
attr_accessor :local_filename
attr_accessor :parse_filename
attr_accessor :content_type
attr_accessor :body
attr_accessor :url
attr_accessor :client
def initialize(data, client = nil)
# convert hash keys to strings
data = Hash[data.map { |k, v| [k.to_s, v] }]
@local_filename = data['local_filename'] if data['local_filename']
@parse_filename = data['name'] if data['name']
@parse_filename = data['parse_filename'] if data['parse_filename']
@content_type = data['content_type'] if data['content_type']
@url = data['url'] if data['url']
@body = data['body'] if data['body']
@client = client || Parse.client
end
def eql?(other)
self.class.equal?(other.class) &&
url == other.url
end
alias == eql?
def hash
url.hash
end
def save
uri = Parse::Protocol.file_uri(@local_filename)
resp = @client.request(uri, :post, @body, nil, @content_type)
@parse_filename = resp['name']
@url = resp['url']
resp
end
def to_h(*_a)
{
Protocol::KEY_TYPE => Protocol::TYPE_FILE,
'name' => @parse_filename,
'url' => @url
}
end
alias as_json to_h
def to_json(*a)
to_h.to_json(*a)
end
end
end