mirror-api 0.0.9 → 0.1.0

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,11 @@
1
+ require "hashie/mash"
2
+
3
+ module Mirror
4
+ module Api
5
+ class RequestData < Hashie::Mash
6
+ def convert_key(key) #:nodoc:
7
+ key.to_s.split('_').inject([]){ |buffer,e| buffer.push(buffer.empty? ? e : e.capitalize) }.join
8
+ end
9
+ end
10
+ end
11
+ end
@@ -4,16 +4,18 @@ module Mirror
4
4
  module Api
5
5
  class Resource
6
6
 
7
- def initialize(credentials, resource_name=Request::TIMELINE)
7
+ attr_accessor :options
8
+
9
+ def initialize(credentials, resource_name=Request::TIMELINE, options={})
8
10
  @credentials = credentials
9
11
  @resource_name = resource_name
12
+ self.options = options || {}
10
13
 
11
14
  raise "Invalid credentials #{credentials}" unless @credentials
12
15
  end
13
16
 
14
17
  def list(*args)
15
18
  handle_list(args)
16
-
17
19
  end
18
20
 
19
21
  def create(params)
@@ -40,11 +42,20 @@ module Mirror
40
42
 
41
43
  private
42
44
  def make_options(params=nil, status=200)
43
- {:resource => @resource_name, :params => params, :expected_response => status}
45
+ options.merge({
46
+ :resource => @resource_name,
47
+ :params => params,
48
+ :expected_response => status
49
+ })
44
50
  end
45
51
 
46
52
  def item_options(id, params=nil, status=200)
47
- {:resource => @resource_name, :id => id, :params => params, :expected_response => status}
53
+ options.merge({
54
+ :resource => @resource_name,
55
+ :id => id,
56
+ :params => params,
57
+ :expected_response => status
58
+ })
48
59
  end
49
60
 
50
61
  def handle_list(args)
@@ -0,0 +1,15 @@
1
+ require "hashie/mash"
2
+
3
+ module Mirror
4
+ module Api
5
+ class ResponseData < Hashie::Mash
6
+ def convert_key(key) #:nodoc:
7
+ key.to_s.gsub(/::/, '/').
8
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
9
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
10
+ tr("-", "_").
11
+ downcase
12
+ end
13
+ end
14
+ end
15
+ end
@@ -1,5 +1,5 @@
1
1
  module Mirror
2
2
  module Api
3
- VERSION = "0.0.9"
3
+ VERSION = "0.1.0"
4
4
  end
5
5
  end
data/spec/client_spec.rb CHANGED
@@ -3,481 +3,32 @@ require_relative "spec_helper"
3
3
  describe Mirror::Api::Client do
4
4
  before do
5
5
  @token = "my-token"
6
- @api = Mirror::Api::Client.new(@token)
7
6
  end
8
7
 
9
- describe "timeline" do
10
- describe "create" do
11
- describe "inserting" do
12
-
13
- context "with valid params" do
14
- before do
15
- @msg = "Hello world"
16
- @body = {text: @msg}
17
-
18
- stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/").
19
- with(body: @body,
20
- headers: json_post_request_headers(@body.to_json)).
21
- to_return(status: 200,
22
- body: fixture("timeline_item.json", true),
23
- headers: JSON.parse(fixture("timeline_item_response_headers.json", true)))
24
- end
25
-
26
- it "should insert plain text items" do
27
- item = @api.timeline.create({text: @msg})
28
- item.should_not be_nil
29
- item.created.should == "2012-09-25T23:28:43.192Z" # see fixture
30
- item.text.should == @msg
31
- end
32
- end
33
-
34
- context "with invalid params" do
35
- before do
36
- @msg = "123"
37
- @body = {random: @msg}
38
- stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/").
39
- with(body: @body,
40
- headers: json_post_request_headers(@body.to_json)).
41
- to_return(status: 400, body: {}.to_json,
42
- headers: {})
43
- end
44
-
45
- it "should not insert the item" do
46
-
47
- item = @api.timeline.create(@body)
48
- item.should be_nil
49
- end
50
-
51
- end
52
-
53
- end
8
+ describe "initializing" do
9
+ it "should take the token as a string" do
10
+ @api = Mirror::Api::Client.new(@token)
11
+ @api.should_not be_nil
12
+ @api.credentials[:token].should == @token
54
13
  end
55
- end
56
-
57
- describe "locations" do
58
-
59
- describe "get" do
60
14
 
61
- context "with valid params" do
62
- before do
63
- @id = "0987"
64
-
65
- stub_request(:get, "https://www.googleapis.com/mirror/v1/locations/#{@id}").
66
- with(headers: json_get_request_headers).
67
- to_return(status: 200,
68
- body: fixture("locations_item.json", true),
69
- headers: {})
70
- end
71
-
72
- it "should get the location for @id" do
73
- location = @api.locations.get(@id)
74
- location.should_not be_nil
75
- location.displayName.should == "Home" # see fixture
76
- end
77
- end
78
-
79
- context "with invalid params" do
80
- before do
81
- @id = "0987asdasds"
82
-
83
- stub_request(:get, "https://www.googleapis.com/mirror/v1/locations/").
84
- with(headers: json_get_request_headers).
85
- to_return(status: 404, body: {}.to_json,
86
- headers: {})
87
-
88
- end
89
-
90
- it "should not get the item" do
91
-
92
- item = @api.timeline.get(@id)
93
- item.should be_nil
94
- end
95
- end
15
+ it "should take the credentials hash" do
16
+ hash = {:token => @token}
17
+ @api = Mirror::Api::Client.new(hash)
18
+ @api.should_not be_nil
19
+ @api.credentials.should == hash
96
20
  end
97
21
 
98
- describe "list" do
99
-
100
- context "with valid params" do
101
- before do
102
-
103
- stub_request(:get, "https://www.googleapis.com/mirror/v1/locations/").
104
- with(headers: json_get_request_headers).
105
- to_return(status: 200,
106
- body: fixture("locations_list.json", true),
107
- headers: {})
108
- end
109
-
110
- it "should return a list of locations" do
111
- locations = @api.locations.list()
112
- locations.should_not be_nil
113
- locations.items.count.should == 2 # see fixture
114
- end
115
- end
22
+ it "should raise an error if invalid params" do
23
+ expect{@api = Mirror::Api::Client.new(nil)}.to raise_error "Invalid credentials. Missing token"
116
24
  end
117
25
  end
118
26
 
119
- describe "contacts" do
120
-
121
- describe "delete" do
122
- context "with valid params" do
123
- before do
124
- @id = "123123312"
125
- stub_request(:delete, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
126
- with(headers: json_get_request_headers).
127
- to_return(status: 200,
128
- body: {},
129
- headers: {})
130
- end
131
- it "should return nil" do
132
- contact = @api.contacts.delete(@id)
133
- contact.should == nil
134
- end
135
- end
136
-
137
- context "with invalid params" do
138
- before do
139
- @id = "blah"
140
- stub_request(:delete, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
141
- with(headers: json_get_request_headers).
142
- to_return(status: 400,
143
- body: {},
144
- headers: {})
145
- end
146
- it "should return nil" do
147
- contact = @api.contacts.delete(@id)
148
- contact.should == nil
149
- end
150
- end
151
-
152
- end
153
-
154
- describe "get" do
155
-
156
- context "with valid params" do
157
- before do
158
- @id = "0987"
159
-
160
- stub_request(:get, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
161
- with(headers: json_get_request_headers).
162
- to_return(status: 200,
163
- body: fixture("contacts_item.json", true),
164
- headers: {})
165
- end
166
- it "should return a contact with .kind == 'mirror#contact'" do
167
- contact = @api.contacts.get(@id)
168
- contact.kind.should == 'mirror#contact'
169
- end
170
- end
171
-
172
- context "with invalid params" do
173
- before do
174
- @id = "bad_id"
175
-
176
- stub_request(:get, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
177
- with(headers: json_get_request_headers).
178
- to_return(status: 404,
179
- body: fixture("contacts_item.json", true),
180
- headers: {})
181
- end
182
- it "should return nil" do
183
- contact = @api.contacts.get(@id)
184
- contact.should == nil
185
- end
186
- end
187
-
27
+ describe "methods" do
28
+ it "should have methods that match the mirror api" do
29
+ @api = Mirror::Api::Client.new(@token)
30
+ @api.methods.should include(:timeline, :locations, :subscriptions, :contacts)
188
31
  end
189
-
190
- describe "insert" do
191
-
192
- context "with valid params" do
193
- before do
194
- @body = {id: '1234', displayName: 'Demo App', imageUrls: ["http://pixelr3ap3r.com/wp-content/uploads/2012/08/357c6328ee4b11e1bfbf22000a1c91a7_7.jpg"]}
195
-
196
- stub_request(:post, "https://www.googleapis.com/mirror/v1/contacts/").
197
- with(body: @body,
198
- headers: json_post_request_headers(@body.to_json)).
199
- to_return(status: 200,
200
- body: fixture("contacts_item.json", true),
201
- headers: {})
202
- end
203
- it "should return a contact with .kind == 'mirror#contact'" do
204
- contact = @api.contacts.insert(@body)
205
- contact.kind.should == 'mirror#contact'
206
- end
207
- end
208
-
209
- context "with invalid params" do
210
- before do
211
- @body = {canIHazContact: "Really you thought that was valid?!"}
212
-
213
- stub_request(:post, "https://www.googleapis.com/mirror/v1/contacts/").
214
- with(body: @body,
215
- headers: json_post_request_headers(@body.to_json)).
216
- to_return(status: 404,
217
- body: {},
218
- headers: {})
219
- end
220
- it "should return nil" do
221
- contact = @api.contacts.insert(@body)
222
- contact.should == nil
223
- end
224
- end
225
-
226
- end
227
-
228
- describe "list" do
229
-
230
- context "with valid params" do
231
- before do
232
-
233
- stub_request(:get, "https://www.googleapis.com/mirror/v1/contacts/").
234
- with(headers: json_get_request_headers).
235
- to_return(status: 200,
236
- body: fixture("contacts_list.json", true),
237
- headers: {})
238
- end
239
-
240
- it "should return a list of contacts" do
241
- contacts = @api.contacts.list()
242
- contacts.should_not be_nil
243
- contacts.items.count.should == 2 # see fixture
244
- end
245
- end
246
-
247
- end
248
-
249
- describe "patch" do
250
-
251
- context "with valid params" do
252
- before do
253
- @id = '1234'
254
- @body = {displayName: 'Demo App'}
255
-
256
- stub_request(:patch, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
257
- with(body: @body,
258
- headers: json_post_request_headers(@body.to_json)).
259
- to_return(status: 200,
260
- body: fixture("contacts_item.json", true),
261
- headers: {})
262
- end
263
- it "should return a contact with .kind == 'mirror#contact'" do
264
- contact = @api.contacts.patch(@id, @body)
265
- contact.kind.should == 'mirror#contact'
266
- end
267
- end
268
-
269
- context "with invalid params" do
270
- before do
271
- @id = '1234'
272
- @body = {derp: 'troll'}
273
-
274
- stub_request(:patch, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
275
- with(body: @body,
276
- headers: json_post_request_headers(@body.to_json)).
277
- to_return(status: 400,
278
- body: {},
279
- headers: {})
280
- end
281
- it "should return nil" do
282
- contact = @api.contacts.patch(@id, @body)
283
- contact.should == nil
284
- end
285
- end
286
-
287
- end
288
-
289
- describe "update" do
290
-
291
- context "with valid params" do
292
- before do
293
- @id = '1234'
294
- @body = {displayName: 'Demo App'}
295
-
296
- stub_request(:put, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
297
- with(body: @body,
298
- headers: json_post_request_headers(@body.to_json)).
299
- to_return(status: 200,
300
- body: fixture("contacts_item.json", true),
301
- headers: {})
302
- end
303
- it "should return a contact with .kind == 'mirror#contact'" do
304
- contact = @api.contacts.update(@id, @body)
305
- contact.kind.should == 'mirror#contact'
306
- end
307
- end
308
-
309
- context "with invalid params" do
310
- before do
311
- @id = '1234'
312
- @body = {derp: 'troll'}
313
-
314
- stub_request(:put, "https://www.googleapis.com/mirror/v1/contacts/#{@id}").
315
- with(body: @body,
316
- headers: json_post_request_headers(@body.to_json)).
317
- to_return(status: 400,
318
- body: {},
319
- headers: {})
320
- end
321
- it "should return nil" do
322
- contact = @api.contacts.update(@id, @body)
323
- contact.should == nil
324
- end
325
- end
326
-
327
- end
328
-
329
- end
330
-
331
- describe "timeline attachments" do
332
-
333
- context "delete" do
334
- context "with valid params" do
335
- before do
336
- @timeline_id = "1234"
337
- @attachment_id = "123123312"
338
- stub_request(:delete, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
339
- with(headers: json_get_request_headers).
340
- to_return(status: 200,
341
- body: {},
342
- headers: {})
343
- end
344
- it "should return nil" do
345
- timeline_attachment = @api.timeline.delete(@timeline_id, {attachments:{id: @attachment_id}})
346
- timeline_attachment.should == nil
347
- end
348
- end
349
-
350
- context "with invalid params" do
351
- before do
352
- @timeline_id = "1234"
353
- @attachment_id = "blah"
354
- stub_request(:delete, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
355
- with(headers: json_get_request_headers).
356
- to_return(status: 400,
357
- body: {},
358
- headers: {})
359
- end
360
- it "should return nil" do
361
- timeline_attachment = @api.timeline.delete(@timeline_id, {attachments:{id: @attachment_id}})
362
- timeline_attachment.should == nil
363
- end
364
- end
365
- end
366
-
367
- context "get" do
368
- context "with valid params" do
369
- before do
370
- @timeline_id = "1234"
371
- @attachment_id = "123123312"
372
- stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
373
- with(headers: json_get_request_headers).
374
- to_return(status: 200,
375
- body: fixture("timeline_item_attachments_item.json", true),
376
- headers: {})
377
- end
378
- it "should return timeline_attachment with id '1234'" do
379
- timeline_attachment = @api.timeline.get(@timeline_id, {attachments:{id: @attachment_id}})
380
- timeline_attachment.id == '1234'
381
- end
382
- end
383
-
384
- context "with invalid params" do
385
- before do
386
- @timeline_id = "1234"
387
- @attachment_id = "blah"
388
- stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
389
- with(headers: json_get_request_headers).
390
- to_return(status: 404,
391
- body: {},
392
- headers: {})
393
- end
394
- it "should return nil" do
395
- timeline_attachment = @api.timeline.get(@timeline_id, {attachments:{id: @attachment_id}})
396
- timeline_attachment.should == nil
397
- end
398
- end
399
- end
400
-
401
- #TODO: Support file upload
402
- # context "insert" do
403
- # context "with valid params" do
404
- # before do
405
- # @timeline_id = "1234"
406
- # @file = fixture_file_upload('files/fry.png', 'image/png')
407
- # @params = {uploadType: 'media'}
408
-
409
- # stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments?uploadType=media").
410
- # with(
411
- # headers: json_post_request_headers(@body.to_json)).
412
- # to_return(status: 200,
413
- # body: fixture("timeline_item_attachments_item.json", true),
414
- # headers: {})
415
- # end
416
- # it "should return timeline_attachment with id '1234'" do
417
- # timeline_attachment = @api.timeline.delete(@timeline_id, {attachments:{id: @attachment_id}})
418
- # timeline_attachment.id == '1234ß'
419
- # end
420
- # end
421
-
422
- # context "with invalid params" do
423
- # before do
424
- # @body = {canIHazContact: "Really you thought that was valid?!"}
425
-
426
- # stub_request(:post, "https://www.googleapis.com/mirror/v1/contacts/").
427
- # with(body: @body,
428
- # headers: json_post_request_headers(@body.to_json)).
429
- # to_return(status: 404,
430
- # body: {},
431
- # headers: {})
432
- # end
433
- # it "should return nil" do
434
- # contact = @api.contacts.insert(@body)
435
- # contact.should == nil
436
- # end
437
- # end
438
- # end
439
-
440
- # TODO correct resource#list method to handle attachments
441
- context "list" do
442
- context "with valid params" do
443
- before do
444
- @timeline_id = "1234"
445
- stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/").
446
- with(headers: json_get_request_headers).
447
- to_return(status: 200,
448
- body: fixture("timeline_item_attachments_list.json", true),
449
- headers: {})
450
- end
451
-
452
- it "should return a list of contacts", :focus => true do
453
- attachments = @api.timeline.list(@timeline_id, {attachments: {} })
454
- attachments.should_not be_nil
455
- attachments.items.count.should == 2 # see fixture
456
- end
457
- end
458
- end
459
-
460
- end
461
-
462
- def json_post_request_headers(body)
463
- {
464
- 'Accept'=>'application/json',
465
- 'Accept-Encoding'=>'gzip, deflate',
466
- 'Authorization'=>"Bearer #{@token}",
467
- 'Content-Length'=>body.length.to_s,
468
- 'Content-Type'=>'application/json',
469
- 'User-Agent'=>'Ruby'
470
- }
471
- end
472
-
473
- def json_get_request_headers
474
- {
475
- 'Accept'=>'application/json',
476
- 'Accept-Encoding'=>'gzip, deflate',
477
- 'Authorization'=>"Bearer #{@token}",
478
- 'Content-Type'=>'application/json',
479
- 'User-Agent'=>'Ruby'
480
- }
481
32
  end
482
33
 
483
34
  end