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.
data/spec/spec_helper.rb CHANGED
@@ -16,4 +16,25 @@ def fixture(file, read=false)
16
16
  file = File.new(fixture_path + '/' + file)
17
17
  return File.read(file) if read
18
18
  file
19
+ end
20
+
21
+ def json_post_request_headers(token, body)
22
+ {
23
+ 'Accept'=>'application/json',
24
+ 'Accept-Encoding'=>'gzip, deflate',
25
+ 'Authorization'=>"Bearer #{token}",
26
+ 'Content-Length'=>body.length.to_s,
27
+ 'Content-Type'=>'application/json',
28
+ 'User-Agent'=>'Ruby'
29
+ }
30
+ end
31
+
32
+ def json_get_request_headers(token)
33
+ {
34
+ 'Accept'=>'application/json',
35
+ 'Accept-Encoding'=>'gzip, deflate',
36
+ 'Authorization'=>"Bearer #{token}",
37
+ 'Content-Type'=>'application/json',
38
+ 'User-Agent'=>'Ruby'
39
+ }
19
40
  end
@@ -0,0 +1,170 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe "Subscriptions" do
4
+ before do
5
+ @token = "my-token"
6
+ end
7
+
8
+ describe "delete" do
9
+ context "with valid params" do
10
+ before do
11
+ @id = "timeline"
12
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/subscriptions/#{@id}").
13
+ with(headers: json_get_request_headers(@token)).
14
+ to_return(status: 200,
15
+ body: {},
16
+ headers: {})
17
+ end
18
+ it "should return true" do
19
+ @api = Mirror::Api::Client.new(@token)
20
+ @api.subscriptions.delete(@id).should be_true
21
+ end
22
+ end
23
+
24
+ context "with invalid params" do
25
+ before do
26
+ @id = "blah"
27
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/subscriptions/#{@id}").
28
+ with(headers: json_get_request_headers(@token)).
29
+ to_return(status: 400,
30
+ body: {},
31
+ headers: {})
32
+ end
33
+
34
+ context "without bubbling errors" do
35
+ it "should return nil" do
36
+ @api = Mirror::Api::Client.new(@token)
37
+ subscription = @api.subscriptions.delete(@id)
38
+ subscription.should == nil
39
+ end
40
+ end
41
+
42
+ context "bubbling errors" do
43
+ it "should raise ex" do
44
+ @api = Mirror::Api::Client.new(@token, true)
45
+ expect{@api.subscriptions.delete(@id)}.to raise_error
46
+ end
47
+ end
48
+ end
49
+
50
+ end
51
+
52
+ describe "insert" do
53
+
54
+ context "with valid params" do
55
+ before do
56
+ @body = {collection: "timeline", userToken:"user_1", operation: ["UPDATE"], callbackUrl: "https://yourawesomewebsite.com/callback"}
57
+
58
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/subscriptions/").
59
+ with(body: @body,
60
+ headers: json_post_request_headers(@token, @body.to_json)).
61
+ to_return(status: 200,
62
+ body: fixture("subscriptions_item.json", true),
63
+ headers: {})
64
+ end
65
+ it "should return a subscription with .kind == 'mirror#subscription'" do
66
+ @api = Mirror::Api::Client.new(@token)
67
+ subscription = @api.subscriptions.insert(@body)
68
+ subscription.kind.should == 'mirror#subscription'
69
+ end
70
+ end
71
+
72
+ context "with invalid params" do
73
+ before do
74
+ @body2 = {:notCoolDude => "Really you thought that was valid?!"}
75
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/subscriptions/").
76
+ with(body: @body2,
77
+ headers: json_post_request_headers(@token, @body2.to_json)).
78
+ to_return(status: 404,
79
+ body: {},
80
+ headers: {})
81
+ end
82
+ it "should return nil" do
83
+ @api = Mirror::Api::Client.new(@token)
84
+ subscription = @api.subscriptions.insert({not_cool_dude: "Really you thought that was valid?!"})
85
+ subscription.should == nil
86
+ end
87
+
88
+ context "bubbling errors" do
89
+ it "should raise ex" do
90
+ @api = Mirror::Api::Client.new(@token, {:raise_errors => true})
91
+ expect{@api.subscriptions.insert(@body)}.to raise_error
92
+ end
93
+ end
94
+ end
95
+
96
+ end
97
+
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/subscriptions/").
104
+ with(headers: json_get_request_headers(@token)).
105
+ to_return(status: 200,
106
+ body: fixture("subscriptions_list.json", true),
107
+ headers: {})
108
+ end
109
+
110
+ it "should return a list of subscriptions" do
111
+ @api = Mirror::Api::Client.new(@token)
112
+ subscriptions = @api.subscriptions.list
113
+ subscriptions.should_not be_nil
114
+ subscriptions.items.count.should == 1 # see fixture
115
+ end
116
+ end
117
+
118
+ end
119
+
120
+ describe "update" do
121
+
122
+ context "with valid params" do
123
+ before do
124
+ @id = 'timeline'
125
+ @body = {collection: 'timeline', callbackUrl: 'https://fullscreen.net/callback', operation:["INSERT", "UPDATE"]}
126
+
127
+ stub_request(:put, "https://www.googleapis.com/mirror/v1/subscriptions/#{@id}").
128
+ with(body: @body,
129
+ headers: json_post_request_headers(@token, @body.to_json)).
130
+ to_return(status: 200,
131
+ body: fixture("subscriptions_item.json", true),
132
+ headers: {})
133
+ end
134
+ it "should return a subscription with .kind == 'mirror#subscription'" do
135
+ @api = Mirror::Api::Client.new(@token)
136
+ subscription = @api.subscriptions.update(@id, @body)
137
+ subscription.kind.should == 'mirror#subscription'
138
+ end
139
+ end
140
+
141
+ context "with invalid params" do
142
+ before do
143
+ @id = 'timeline'
144
+ @body = {derp: 'troll'}
145
+
146
+ stub_request(:put, "https://www.googleapis.com/mirror/v1/subscriptions/#{@id}").
147
+ with(body: @body,
148
+ headers: json_post_request_headers(@token, @body.to_json)).
149
+ to_return(status: 400,
150
+ body: {},
151
+ headers: {})
152
+ end
153
+
154
+ it "should return nil" do
155
+ @api = Mirror::Api::Client.new(@token)
156
+ subscription = @api.subscriptions.update(@id, @body)
157
+ subscription.should == nil
158
+ end
159
+
160
+ context "when bubbling errors" do
161
+
162
+ it "should raise an exception" do
163
+ @api = Mirror::Api::Client.new(@token, {:raise_errors => true})
164
+ expect{@api.subscriptions.update(@id, @body)}.to raise_error
165
+ end
166
+ end
167
+ end
168
+
169
+ end
170
+ end
@@ -0,0 +1,141 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe "Timeline Attachments" do
4
+
5
+ before do
6
+ @token = "my-token"
7
+ @api = Mirror::Api::Client.new(@token)
8
+ end
9
+
10
+ context "delete" do
11
+ context "with valid params" do
12
+ before do
13
+ @timeline_id = "1234"
14
+ @attachment_id = "123123312"
15
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
16
+ with(headers: json_get_request_headers(@token)).
17
+ to_return(status: 200,
18
+ body: "",
19
+ headers: {})
20
+ end
21
+ it "should return true" do
22
+ @api.timeline.delete(@timeline_id, {attachments:{id: @attachment_id}}).should be_true
23
+ end
24
+ end
25
+
26
+ context "with invalid params" do
27
+ before do
28
+ @timeline_id = "1234"
29
+ @attachment_id = "blah"
30
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
31
+ with(headers: json_get_request_headers(@token)).
32
+ to_return(status: 400,
33
+ body: "",
34
+ headers: {})
35
+ end
36
+ it "should return nil" do
37
+ timeline_attachment = @api.timeline.delete(@timeline_id, {attachments:{id: @attachment_id}})
38
+ timeline_attachment.should == nil
39
+ end
40
+ end
41
+ end
42
+
43
+ context "get" do
44
+ context "with valid params" do
45
+ before do
46
+ @timeline_id = "1234"
47
+ @attachment_id = "123123312"
48
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
49
+ with(headers: json_get_request_headers(@token)).
50
+ to_return(status: 200,
51
+ body: fixture("timeline_item_attachments_item.json", true),
52
+ headers: {})
53
+ end
54
+ it "should return timeline_attachment with id '1234'" do
55
+ timeline_attachment = @api.timeline.get(@timeline_id, {attachments:{id: @attachment_id}})
56
+ timeline_attachment.id == '1234'
57
+ end
58
+
59
+ it "should return isProcessingContent with boolean false" do
60
+ timeline_attachment = @api.timeline.get(@timeline_id, {attachments:{id: @attachment_id}})
61
+ timeline_attachment.is_processing_content == false
62
+ end
63
+ end
64
+
65
+ context "with invalid params" do
66
+ before do
67
+ @timeline_id = "1234"
68
+ @attachment_id = "blah"
69
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/#{@attachment_id}").
70
+ with(headers: json_get_request_headers(@token)).
71
+ to_return(status: 404,
72
+ body: "",
73
+ headers: {})
74
+ end
75
+ it "should return nil" do
76
+ timeline_attachment = @api.timeline.get(@timeline_id, {attachments:{id: @attachment_id}})
77
+ timeline_attachment.should == nil
78
+ end
79
+ end
80
+ end
81
+
82
+ #TODO: Support file upload
83
+ # context "insert" do
84
+ # context "with valid params" do
85
+ # before do
86
+ # @timeline_id = "1234"
87
+ # @file = fixture_file_upload('files/fry.png', 'image/png')
88
+ # @params = {uploadType: 'media'}
89
+
90
+ # stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments?uploadType=media").
91
+ # with(
92
+ # headers: json_post_request_headers(@token, @body.to_json)).
93
+ # to_return(status: 200,
94
+ # body: fixture("timeline_item_attachments_item.json", true),
95
+ # headers: {})
96
+ # end
97
+ # it "should return timeline_attachment with id '1234'" do
98
+ # timeline_attachment = @api.timeline.delete(@timeline_id, {attachments:{id: @attachment_id}})
99
+ # timeline_attachment.id == '1234ß'
100
+ # end
101
+ # end
102
+
103
+ # context "with invalid params" do
104
+ # before do
105
+ # @body = {canIHazContact: "Really you thought that was valid?!"}
106
+
107
+ # stub_request(:post, "https://www.googleapis.com/mirror/v1/contacts/").
108
+ # with(body: @body,
109
+ # headers: json_post_request_headers(@token, @body.to_json)).
110
+ # to_return(status: 404,
111
+ # body: "",
112
+ # headers: {})
113
+ # end
114
+ # it "should return nil" do
115
+ # contact = @api.contacts.insert(@body)
116
+ # contact.should == nil
117
+ # end
118
+ # end
119
+ # end
120
+
121
+ # TODO correct resource#list method to handle attachments
122
+ context "list" do
123
+ context "with valid params" do
124
+ before do
125
+ @timeline_id = "1234"
126
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@timeline_id}/attachments/").
127
+ with(headers: json_get_request_headers(@token)).
128
+ to_return(status: 200,
129
+ body: fixture("timeline_item_attachments_list.json", true),
130
+ headers: {})
131
+ end
132
+
133
+ it "should return a list of contacts", :focus => true do
134
+ attachments = @api.timeline.list(@timeline_id, {attachments: {} })
135
+ attachments.should_not be_nil
136
+ attachments.items.count.should == 2 # see fixture
137
+ end
138
+ end
139
+ end
140
+
141
+ end
@@ -0,0 +1,239 @@
1
+ require_relative "spec_helper"
2
+
3
+ describe "Timeline" do
4
+ before do
5
+ @token = "my-token"
6
+ @api = Mirror::Api::Client.new(@token)
7
+ end
8
+
9
+ describe "create" do
10
+ describe "inserting" do
11
+
12
+ context "with valid params" do
13
+ before do
14
+ @msg = "Hello world"
15
+ @body = {text: @msg}
16
+
17
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/").
18
+ with(body: @body,
19
+ headers: json_post_request_headers(@token, @body.to_json)).
20
+ to_return(status: 200,
21
+ body: fixture("timeline_item.json", true),
22
+ headers: JSON.parse(fixture("timeline_item_response_headers.json", true)))
23
+ end
24
+
25
+ it "should insert plain text items" do
26
+ item = @api.timeline.create({text: @msg})
27
+ item.should_not be_nil
28
+ item.created.should == "2012-09-25T23:28:43.192Z" # see fixture
29
+ item.text.should == @msg
30
+ end
31
+
32
+ it "should insert plain text items passing request data" do
33
+ data = Mirror::Api::RequestData.new({text: @msg})
34
+ item = @api.timeline.create(data)
35
+ item.should_not be_nil
36
+ item.created.should == "2012-09-25T23:28:43.192Z" # see fixture
37
+ item.text.should == @msg
38
+ end
39
+ end
40
+
41
+ context "with invalid params" do
42
+ before do
43
+ @msg = "123"
44
+ @body = {random: @msg}
45
+ stub_request(:post, "https://www.googleapis.com/mirror/v1/timeline/").
46
+ with(body: @body,
47
+ headers: json_post_request_headers(@token, @body.to_json)).
48
+ to_return(status: 400, body: "",
49
+ headers: {})
50
+ end
51
+
52
+ it "should not insert the item" do
53
+
54
+ item = @api.timeline.create(@body)
55
+ item.should be_nil
56
+ end
57
+
58
+ end
59
+
60
+ end
61
+ end
62
+
63
+ describe "delete" do
64
+ context "with valid params" do
65
+ before do
66
+ @id = "123123312"
67
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
68
+ with(headers: json_get_request_headers(@token)).
69
+ to_return(status: 200,
70
+ body: "",
71
+ headers: {})
72
+ end
73
+ it "should return true" do
74
+ @api.timeline.delete(@id).should be_true
75
+ end
76
+ end
77
+
78
+ context "with invalid params" do
79
+ before do
80
+ @id = "blah"
81
+ stub_request(:delete, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
82
+ with(headers: json_get_request_headers(@token)).
83
+ to_return(status: 400,
84
+ body: "",
85
+ headers: {})
86
+ end
87
+ it "should return nil" do
88
+ @api.timeline.delete(@id).should == nil
89
+ end
90
+
91
+ it "log the error" do
92
+ logger = Logger.new(STDOUT)
93
+ @api.timeline.options[:logger] = logger
94
+ logger.should_receive(:warn)
95
+ @api.timeline.delete(@id).should == nil
96
+ end
97
+ end
98
+
99
+ end
100
+
101
+ describe "get" do
102
+
103
+ context "with valid params" do
104
+ before do
105
+ @id = "0987"
106
+
107
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
108
+ with(headers: json_get_request_headers(@token)).
109
+ to_return(status: 200,
110
+ body: fixture("timeline_item.json", true),
111
+ headers: {})
112
+ end
113
+ it "should return a timeline with .kind == 'mirror#timelineItem'" do
114
+ timeline_item = @api.timeline.get(@id)
115
+ timeline_item.kind.should == 'mirror#timelineItem'
116
+ end
117
+ end
118
+
119
+ context "with invalid params" do
120
+ before do
121
+ @id = "bad_id"
122
+
123
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
124
+ with(headers: json_get_request_headers(@token)).
125
+ to_return(status: 404,
126
+ body: fixture("timeline_item.json", true),
127
+ headers: {})
128
+ end
129
+ it "should return nil" do
130
+ timeline_item = @api.timeline.get(@id)
131
+ timeline_item.should == nil
132
+ end
133
+ end
134
+
135
+ end
136
+
137
+ describe "list" do
138
+
139
+ context "with valid params" do
140
+ before do
141
+
142
+ stub_request(:get, "https://www.googleapis.com/mirror/v1/timeline/").
143
+ with(headers: json_get_request_headers(@token)).
144
+ to_return(status: 200,
145
+ body: fixture("timeline_list.json", true),
146
+ headers: {})
147
+ end
148
+
149
+ it "should return a list of timelines" do
150
+ timeline = @api.timeline.list()
151
+ timeline.should_not be_nil
152
+ timeline.items.count.should == 1 # see fixture
153
+ end
154
+ end
155
+
156
+ end
157
+
158
+ describe "patch" do
159
+
160
+ context "with valid params" do
161
+ before do
162
+ @id = '1234'
163
+ @body = {text: "You realize you are a bad friend right?", menu_items:[{action: "REPLY"}]}
164
+ @body2 = {text: "You realize you are a bad friend right?", menuItems:[{action: "REPLY"}]}
165
+
166
+ stub_request(:patch, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
167
+ with(body: @body2,
168
+ headers: json_post_request_headers(@token, @body2.to_json)).
169
+ to_return(status: 200,
170
+ body: fixture("timeline_item.json", true),
171
+ headers: {})
172
+ end
173
+ it "should return a timeline item with .kind == 'mirror#timelineItem'" do
174
+ timeline = @api.timeline.patch(@id, @body)
175
+ timeline.kind.should == 'mirror#timelineItem'
176
+ end
177
+ end
178
+
179
+ context "with invalid params" do
180
+ before do
181
+ @id = '1234'
182
+ @body = {derp: 'troll'}
183
+
184
+ stub_request(:patch, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
185
+ with(body: @body,
186
+ headers: json_post_request_headers(@token, @body.to_json)).
187
+ to_return(status: 400,
188
+ body: "",
189
+ headers: {})
190
+ end
191
+ it "should return nil" do
192
+ timeline = @api.timeline.patch(@id, @body)
193
+ timeline.should == nil
194
+ end
195
+ end
196
+
197
+ end
198
+
199
+ describe "update" do
200
+
201
+ context "with valid params" do
202
+ before do
203
+ @id = '1234'
204
+ @body = {displayName: 'Demo App'}
205
+
206
+ stub_request(:put, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
207
+ with(body: @body,
208
+ headers: json_post_request_headers(@token, @body.to_json)).
209
+ to_return(status: 200,
210
+ body: fixture("timeline_item.json", true),
211
+ headers: {})
212
+ end
213
+ it "should return a timeline item with .kind == 'mirror#timelineItem'" do
214
+ timeline = @api.timeline.update(@id, @body)
215
+ timeline.kind.should == 'mirror#timelineItem'
216
+ end
217
+ end
218
+
219
+ context "with invalid params" do
220
+ before do
221
+ @id = '1234'
222
+ @body = {derp: 'troll'}
223
+
224
+ stub_request(:put, "https://www.googleapis.com/mirror/v1/timeline/#{@id}").
225
+ with(body: @body,
226
+ headers: json_post_request_headers(@token, @body.to_json)).
227
+ to_return(status: 400,
228
+ body: "",
229
+ headers: {})
230
+ end
231
+ it "should return nil" do
232
+ timeline = @api.timeline.update(@id, @body)
233
+ timeline.should == nil
234
+ end
235
+ end
236
+
237
+ end
238
+
239
+ end