chute 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,45 @@
1
+ module Chute
2
+ class GCParcel < GCResource
3
+ attr_accessor :id,
4
+ :status,
5
+ :share_url,
6
+ :device_id,
7
+ :asset_count
8
+
9
+ def initialize(attributes = {})
10
+ super
11
+ @id = attributes[:id]
12
+ @status = attributes[:status]
13
+ @share_url = attributes[:share_url]
14
+ @device_id = attributes[:device_id]
15
+ @asset_count = attributes[:asset_count]
16
+ end
17
+
18
+ # Public: Returns pluralized name of the resource.
19
+ def resource_name
20
+ "parcels"
21
+ end
22
+
23
+ # Public: Returns pluralized name of the resource.
24
+ def class_path
25
+ "parcels"
26
+ end
27
+
28
+ # Public: Fetch parcel from its id.
29
+ # id - Integer: identifier.
30
+ #
31
+ # Example
32
+ #
33
+ # Chute::GCParcel.find_by_id(23)
34
+ # # => parcel
35
+ # # => false if not found
36
+ # Returns GCParcel
37
+
38
+ def self.find_by_id(id)
39
+ parcel = Chute::GCParcel.new
40
+ parcel.perform(get("/parcels/#{id}"))
41
+ parcel
42
+ end
43
+
44
+ end
45
+ end
@@ -0,0 +1,354 @@
1
+ module Chute
2
+ class GCResource
3
+ attr_accessor :attributes, :errors, :meta, :prefix_options
4
+
5
+ def initialize(attributes = {})
6
+ @errors = []
7
+ @attributes = attributes
8
+ @prefix_options = {}
9
+ end
10
+
11
+ # Public: Save the resource instance
12
+ #
13
+ # Examples
14
+ #
15
+ # chute = GCChute.new(:name => "Test Chute")
16
+ # # => GCChute instance
17
+ #
18
+ # For valid sttributes
19
+ # chute.save
20
+ # # => true
21
+ # chute.id
22
+ # # => 23
23
+ # For invalid attributes
24
+ # chute.save
25
+ # # => false
26
+ # chute.id
27
+ # # => nil
28
+ # chute.errors
29
+ # # => {"name already taken"}
30
+ def save
31
+ options = {}
32
+ options["#{resource_name.singularize}"] = attributes
33
+ options.merge!(prefix_options)
34
+ perform(self.class.post("/#{resource_name}", options))
35
+ end
36
+
37
+ # Public: Update the resource instance
38
+ #
39
+ # Examples
40
+ #
41
+ # chute = GCChute.new(:name => "Test Chute")
42
+ # # => GCChute instance
43
+ # chute.save
44
+ # # => true
45
+ # chute.name = "Name Changed"
46
+ # chute.update
47
+ # # => true
48
+ # Return errors in case the request failed
49
+
50
+ def update
51
+ options = {}
52
+ options["#{resource_name.singularize}"] = attributes
53
+ options.merge!(prefix_options)
54
+ perform(self.class.put("/#{resource_name}/#{id}", options))
55
+ end
56
+
57
+ # Public: Delete a resource instance
58
+ #
59
+ # Examples
60
+ #
61
+ # chute = GCChute.new(:name => "Test Chute")
62
+ # # => GCChute instance
63
+ # chute.save
64
+ # # => true
65
+ # chute.destroy
66
+ # # => true
67
+ # Return false in case the request failed or chute not found
68
+
69
+ def destroy
70
+ response = self.class.delete("/#{resource_name}/#{id}", prefix_options)
71
+ response.is_success
72
+ end
73
+
74
+ # Public: Pluralized name of the resource.
75
+ def resource_name
76
+ raise NotImplementedError
77
+ end
78
+
79
+ # Public: Relative path to the resource path.
80
+ def resource_path
81
+ "#{resource_name}/#{id}"
82
+ end
83
+
84
+ # Public: Returns if the resource is valid.
85
+ def valid?
86
+ errors.size == 0
87
+ end
88
+
89
+ # Public: Returns if the resource is new.
90
+ def new?
91
+ !self.id.blank?
92
+ end
93
+
94
+ # Public: Fetch collection of resources.
95
+ # Implemented in the inherited classes.
96
+
97
+ def self.all
98
+ raise NotImplementedError
99
+ end
100
+
101
+ # Public: Fetch resource from its id.
102
+ # Implemented in the inherited classes.
103
+ # id - Identifier.
104
+
105
+ def self.find_by_id(id)
106
+ raise NotImplementedError
107
+ end
108
+
109
+ # Public: Search all chutes with specified key.
110
+ #
111
+ # key - key to be searched.
112
+ # value - Optional, value of the key.
113
+ #
114
+ # Example
115
+ #
116
+ # Chute::GCChute.search("title")
117
+ # # => GCollection of the chutes with a key "title" in meta data
118
+ #
119
+ # Chute::GCChute.search("title", "Testing")
120
+ # # => GCollection of the chutes with a key "title" and its value "Testing" in meta data
121
+
122
+ def self.search(key, value=nil)
123
+ self.perform(get("/#{class_path}/meta/#{key}/#{value}"))
124
+ end
125
+
126
+ # Public: Set meta data for one or more keys for the resource.
127
+ #
128
+ # data - Hash or String.
129
+ # Hash of key/value pair to be added to the meta data.
130
+ # String value for a key specified as the next parameter.
131
+ #
132
+ # key - Optional. String if a specific key value to be set.
133
+ #
134
+ # Examples
135
+ #
136
+ # chute = GCChute.find_by_id(23)
137
+ # # => GCChute instance
138
+ #
139
+ # For a key.
140
+ # chute.set_meta_data("Testing", :title)
141
+ # # => true
142
+ # chute.get_meta_data(:title)
143
+ # # => "Testing"
144
+ #
145
+ # Add a Hash to meta data.
146
+ # chute.set_meta_data({:details => "Testing the methods", :source => "Mobile"})
147
+ # # => true
148
+ # chute.get_meta_data
149
+ # # => {:title => "Testing", :details => "Testing the methods", :source => "Mobile"}
150
+
151
+ def set_meta_data(data, key=nil)
152
+ response = (key.blank? and Hash === data) ? (self.class.post("/#{resource_path}/meta", data.to_json)) : (self.class.post("/#{resource_path}/meta/#{key}", data))
153
+ if response.is_success
154
+ Hash === data ? (self.meta = data) : (self.meta[key] = data)
155
+ true
156
+ else
157
+ false
158
+ end
159
+ end
160
+
161
+ # Public: Get complete meta data or a specific key for the resource.
162
+ #
163
+ # key - Optional. String if a specific key value to be fetched.
164
+ #
165
+ # Examples
166
+ #
167
+ # chute = GCChute.find_by_id(23)
168
+ # # => GCChute instance
169
+ #
170
+ # With a key.
171
+ # chute.get_meta_data(:title)
172
+ # # => "Testing"
173
+ #
174
+ # Returns String value of meta data for a key
175
+ #
176
+ # Without a key.
177
+ # chute.get_meta_data
178
+ # # => {:title => "Testing", :source => "Mobile"}
179
+ #
180
+ # Returns meta data Hash without a key
181
+
182
+ def get_meta_data(key=nil)
183
+ if key.blank?
184
+ response = self.class.get("/#{resource_path}/meta")
185
+ else
186
+ response = self.class.get("/#{resource_path}/meta/#{key}")
187
+ end
188
+ if response.is_success
189
+ Hash === response.data ? (self.meta = response.data) : (self.meta[key] = response.data)
190
+ else
191
+ false
192
+ end
193
+ end
194
+
195
+ # Public: Deletes complete meta data or a specific key for the resource.
196
+ #
197
+ # key - Optional. String if a specific key to be deleted.
198
+ #
199
+ # Examples
200
+ #
201
+ # chute = GCChute.find_by_id(23)
202
+ # # => GCChute instance
203
+ #
204
+ # chute.get_meta_data
205
+ # # => {:title => "Testing", :source => "Mobile"}
206
+ #
207
+ # With a key.
208
+ # chute.delete_meta_data(:title)
209
+ # # => "Testing"
210
+ # chute.get_meta_data
211
+ # # => {:source => "Mobile"}
212
+ #
213
+ # Without a key.
214
+ # chute.delete_meta_data
215
+ # # => {}
216
+ # chute.get_meta_data
217
+ # # => {}
218
+
219
+ def delete_meta_data(key = nil)
220
+ if key.blank?
221
+ response = self.class.delete("/#{resource_path}/meta")
222
+ response.is_success ? self.meta = {} : false
223
+ else
224
+ response = self.class.delete("/#{resource_path}/meta/#{key}")
225
+ response.is_success ? self.meta.delete(key) : false
226
+ end
227
+ end
228
+
229
+ # Protected: Handles GCResponse object and updates chute instance with the updated values
230
+ #
231
+ # response - GCResponse object
232
+ #
233
+ # Returns a resource instance
234
+ def perform(response)
235
+ if response.is_success
236
+ response.data.each do |key, value|
237
+ attributes[key.to_sym] = response.data[key] if (self.instance_variable_defined?("@#{key}") rescue nil)
238
+ end
239
+ return true
240
+ else
241
+ self.errors = response.errors
242
+ return false
243
+ end
244
+ end
245
+
246
+ # Protected: Handles GCResponse object.
247
+ #
248
+ # response - GCResponse object
249
+ #
250
+ # Returns a collection of the resources
251
+
252
+ def self.perform(response)
253
+ collection = Chute::GCCollection.new
254
+ if response.is_success
255
+ data = Hash === response.data ? response.data["#{class_path}"] : response.data
256
+ data = [] unless (data and !data.empty?)
257
+ data.each do |item|
258
+ new_resource = self.new
259
+ item.each do |key, value|
260
+ new_resource.attributes[key.to_sym] = value if (new_resource.instance_variable_defined?("@#{key}") rescue nil)
261
+ end
262
+ collection << new_resource
263
+ end
264
+ return collection
265
+ else
266
+ debugger
267
+ raise Chute::Exceptions::InValidResponse, response.errors
268
+ end
269
+ end
270
+
271
+ protected
272
+
273
+ # Protected: Overriding attr_accessor.
274
+ # It keeps attributes hash in sync with class attributes.
275
+
276
+ class << self
277
+ def attr_accessor(*names)
278
+ super
279
+ unless name.match(/^x_/)
280
+ names.each do |name|
281
+ self.class_eval do
282
+ define_method name do
283
+ attributes[name]
284
+ end
285
+
286
+ define_method("#{name}=") do |value|
287
+ attributes[name] = value
288
+ end
289
+ end
290
+ end
291
+ end
292
+ end
293
+ end
294
+
295
+ # Protected: Custom get request to a specific URL.
296
+ #
297
+ # url - relative url.
298
+ # params - parameters hash.
299
+ # headers - headers hash.
300
+ #
301
+ # Returns a GCResponse.
302
+
303
+ def self.get(url, params=nil, headers=nil)
304
+ @request = GCRequest.new()
305
+ @request.get(url, params)
306
+ end
307
+
308
+ # Protected: Custom post request to a specific URL.
309
+ #
310
+ # url - relative url.
311
+ # params - parameters hash.
312
+ # headers - headers hash.
313
+ #
314
+ # Returns a GCResponse.
315
+
316
+ def self.post(url, params=nil, headers=nil)
317
+ @request = GCRequest.new()
318
+ @request.post(url, params)
319
+ end
320
+
321
+ # Protected: Custom put request to a specific URL.
322
+ #
323
+ # url - relative url.
324
+ # params - parameters hash.
325
+ # headers - headers hash.
326
+ #
327
+ # Returns a GCResponse.
328
+
329
+ def self.put(url, params=nil, headers=nil)
330
+ @request = GCRequest.new()
331
+ @request.put(url, params)
332
+ end
333
+
334
+ # Protected: Custom delete request to a specific URL.
335
+ #
336
+ # url - relative url.
337
+ # params - parameters hash.
338
+ # headers - headers hash.
339
+ #
340
+ # Returns a GCResponse.
341
+
342
+ def self.delete(url, params=nil, headers=nil)
343
+ @request = GCRequest.new()
344
+ @request.delete(url, params)
345
+ end
346
+
347
+ # Protected: Set whether the resource has meta data or not
348
+
349
+ def has_meta?
350
+ true
351
+ end
352
+
353
+ end
354
+ end
@@ -0,0 +1,113 @@
1
+ module Chute
2
+ class GCUser < GCResource
3
+ attr_accessor :id,
4
+ :name,
5
+ :email,
6
+ :login,
7
+ :avatar,
8
+ :notification_photos,
9
+ :notification_comments,
10
+ :notification_invites
11
+
12
+ def initialize(attributes = {})
13
+ super
14
+ @id = attributes[:id]
15
+ @name = attributes[:name]
16
+ @email = attributes[:email]
17
+ @login = attributes[:login]
18
+ @notification_photos = attributes[:notification_photos]
19
+ @notification_invites = attributes[:notification_invites]
20
+ @notification_comments = attributes[:notification_comments]
21
+ end
22
+
23
+ # Public: Returns pluralized name of the resource.
24
+ def resource_name
25
+ "me"
26
+ end
27
+
28
+ # Public: Returns resource path.
29
+ def resource_path
30
+ "#{resource_name}"
31
+ end
32
+
33
+ # Public: Collection of accounts associated with user.
34
+ #
35
+ # Returns GCCollection of GCAccount.
36
+ def accounts
37
+ Chute::GCAccount.perform(self.class.get("/#{id}/accounts"))
38
+ end
39
+
40
+ # Public: Collection of assets associated with user.
41
+ #
42
+ # Returns GCCollection of GCAsset.
43
+ def assets
44
+ Chute::GCAsset.perform(self.class.get("/#{id}/assets"))
45
+ end
46
+
47
+ # Public: Collection of bundles associated with user.
48
+ #
49
+ # Returns GCCollection of GCBundle.
50
+ def bundles
51
+ Chute::GCBundle.perform(self.class.get("/#{id}/bundles"))
52
+ end
53
+
54
+ # Public: Collection of chutes associated with user.
55
+ #
56
+ # Returns GCCollection of GCChute.
57
+ def chutes
58
+ Chute::GCChute.perform(self.class.get("/#{id}/chutes"))
59
+ end
60
+
61
+ # Public: Collection of parcels associated with user.
62
+ #
63
+ # Returns GCCollection of GCParcel.
64
+ def parcels
65
+ Chute::GCParcel.perform(self.class.get("/#{id}/parcels"))
66
+ end
67
+
68
+ # Public: Collection of favorite assets associated with user.
69
+ #
70
+ # Returns GCCollection of GCChute.
71
+ def hearts
72
+ Chute::GCChute.perform(self.class.get("/#{id}/hearts"))
73
+ end
74
+
75
+ #below collections aren't working yet, there classes still ned to be implemented.
76
+ def devices
77
+ Chute::GCAsset.perform(self.class.get("/#{id}/devices"))
78
+ end
79
+
80
+ def notices
81
+ Chute::GCAsset.perform(self.class.get("/#{id}/notices"))
82
+ end
83
+
84
+
85
+ # Public: Generate single use token for logging into Chute.
86
+ #
87
+ # Returns single use token String.
88
+ def single_use_token
89
+ response = self.class.get("/oauth/single_use_token", nil, {'X_USER_IDENTIFIER' => id})
90
+ response.data
91
+ end
92
+
93
+ # Public: Returns pluralized name of the resource.
94
+ def class_path
95
+ "me"
96
+ end
97
+
98
+ # Public: Fetch user from the API key.
99
+ #
100
+ # Example
101
+ #
102
+ # Chute::GCUser.me
103
+ # # => user
104
+ # # => false if not found
105
+ # Returns GCUser.
106
+
107
+ def self.me
108
+ user = Chute::GCUser.new
109
+ user.perform(get("/info")) ? user : false
110
+ end
111
+
112
+ end
113
+ end