mirror-api 0.1.0 → 0.1.1
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/README.md +1 -1
- data/lib/mirror-api/bad_request_error.rb +14 -0
- data/lib/mirror-api/errors.rb +6 -3
- data/lib/mirror-api/not_found_error.rb +14 -0
- data/lib/mirror-api/request.rb +23 -7
- data/lib/mirror-api/version.rb +1 -1
- data/spec/fixtures/subscriptions_insert_bad.json +18 -0
- data/spec/spec_helper.rb +5 -0
- data/spec/subscriptions_spec.rb +7 -7
- data/spec/timeline_attachments_spec.rb +1 -1
- metadata +6 -2
data/README.md
CHANGED
@@ -34,7 +34,7 @@ require "mirror-api"
|
|
34
34
|
```
|
35
35
|
### Authenticating your client
|
36
36
|
```ruby
|
37
|
-
token = Mirror::OAuth.new(client_id, client_secret, refresh_token)
|
37
|
+
token = Mirror::Api::OAuth.new(client_id, client_secret, refresh_token)
|
38
38
|
|
39
39
|
api = Mirror::Api::Client.new(token)
|
40
40
|
```
|
data/lib/mirror-api/errors.rb
CHANGED
@@ -1,9 +1,12 @@
|
|
1
|
+
require_relative "not_found_error"
|
2
|
+
require_relative "bad_request_error"
|
3
|
+
|
1
4
|
module Mirror
|
2
5
|
module Api
|
3
6
|
class Errors
|
4
|
-
ERROR_400 = {code: 1, :message => "Mirror API returned a status code 400 for this call"}
|
5
|
-
ERROR_404 = {code: 2, :message => "Mirror API returned a status code 404 for this call"}
|
6
|
-
|
7
|
+
ERROR_400 = {code: 1, :message => "Mirror API returned a status code 400 for this call", :ex_class => Mirror::Api::BadRequestError}
|
8
|
+
ERROR_404 = {code: 2, :message => "Mirror API returned a status code 404 for this call", :ex_class => Mirror::Api::NotFoundError}
|
9
|
+
ERROR = {:code => 100, :message => "Mirror API returned an unexpected status code"}
|
7
10
|
TOKEN_REFRESH_ERROR = {:code => 200, :message => "Could not refresh your API access token"}
|
8
11
|
end
|
9
12
|
end
|
data/lib/mirror-api/request.rb
CHANGED
@@ -37,7 +37,7 @@ module Mirror
|
|
37
37
|
|
38
38
|
def logger=(value)
|
39
39
|
if value
|
40
|
-
if value.
|
40
|
+
if value.respond_to?(:warn) && value.respond_to?(:error)
|
41
41
|
@logger = value
|
42
42
|
else
|
43
43
|
raise "Invalid object given as logger #{value.inspect}"
|
@@ -96,10 +96,12 @@ module Mirror
|
|
96
96
|
def handle_http_response(response, request, result, &block)
|
97
97
|
@request = request
|
98
98
|
case response.code
|
99
|
-
when 404
|
100
|
-
|
99
|
+
when 404, 400
|
100
|
+
@error_response = response.body
|
101
|
+
@error_code = response.code
|
102
|
+
@chosen_error = @error_code == 400 ? Mirror::Api::Errors::ERROR_400 : Mirror::Api::Errors::ERROR_404
|
101
103
|
if @logger
|
102
|
-
msg = "ERROR - #{
|
104
|
+
msg = "ERROR - #{@error_code} #{request.inspect} to #{self.invoke_url} with params #{self.params}. Response is #{@error_response}"
|
103
105
|
@logger.error(msg)
|
104
106
|
end
|
105
107
|
response
|
@@ -124,7 +126,7 @@ module Mirror
|
|
124
126
|
if successful_response?
|
125
127
|
ret_val
|
126
128
|
else
|
127
|
-
send_error
|
129
|
+
@chosen_error ? send_chosen_error : send_error
|
128
130
|
end
|
129
131
|
end
|
130
132
|
|
@@ -134,7 +136,13 @@ module Mirror
|
|
134
136
|
@last_error[:validation_error] = validation_error if validation_error
|
135
137
|
msg += " with params #{params}"
|
136
138
|
@logger.warn(msg) if @logger
|
137
|
-
|
139
|
+
if throw_on_fail
|
140
|
+
if error_desc[:ex_class]
|
141
|
+
raise error_desc[:ex_class].new(@last_error, params)
|
142
|
+
else
|
143
|
+
raise error_desc[:message]
|
144
|
+
end
|
145
|
+
end
|
138
146
|
end
|
139
147
|
|
140
148
|
def set_data
|
@@ -185,11 +193,19 @@ module Mirror
|
|
185
193
|
|
186
194
|
def send_error
|
187
195
|
return handle_error(
|
188
|
-
Mirror::Api::Errors::
|
196
|
+
Mirror::Api::Errors::ERROR,
|
189
197
|
"Error making a request for #{@resource}",
|
190
198
|
@data
|
191
199
|
)
|
192
200
|
end
|
201
|
+
|
202
|
+
def send_chosen_error
|
203
|
+
return handle_error(
|
204
|
+
@chosen_error,
|
205
|
+
"Error making a request for #{@resource}",
|
206
|
+
{:response => @error_response, :code => @error_code}
|
207
|
+
)
|
208
|
+
end
|
193
209
|
end
|
194
210
|
end
|
195
211
|
end
|
data/lib/mirror-api/version.rb
CHANGED
@@ -0,0 +1,18 @@
|
|
1
|
+
{
|
2
|
+
"error": {
|
3
|
+
"errors": [
|
4
|
+
{
|
5
|
+
"domain": "global",
|
6
|
+
"reason": "required",
|
7
|
+
"message": "collection field is required for Subscription"
|
8
|
+
},
|
9
|
+
{
|
10
|
+
"domain": "global",
|
11
|
+
"reason": "required",
|
12
|
+
"message": "callbackUrl field is required for Subscription"
|
13
|
+
}
|
14
|
+
],
|
15
|
+
"code": 400,
|
16
|
+
"message": "collection field is required for Subscription"
|
17
|
+
}
|
18
|
+
}
|
data/spec/spec_helper.rb
CHANGED
@@ -18,6 +18,11 @@ def fixture(file, read=false)
|
|
18
18
|
file
|
19
19
|
end
|
20
20
|
|
21
|
+
RSpec.configure do |config|
|
22
|
+
config.filter_run :focus => true
|
23
|
+
config.run_all_when_everything_filtered = true
|
24
|
+
end
|
25
|
+
|
21
26
|
def json_post_request_headers(token, body)
|
22
27
|
{
|
23
28
|
'Accept'=>'application/json',
|
data/spec/subscriptions_spec.rb
CHANGED
@@ -41,8 +41,8 @@ describe "Subscriptions" do
|
|
41
41
|
|
42
42
|
context "bubbling errors" do
|
43
43
|
it "should raise ex" do
|
44
|
-
@api = Mirror::Api::Client.new(@token, true)
|
45
|
-
expect{@api.subscriptions.delete(@id)}.to raise_error
|
44
|
+
@api = Mirror::Api::Client.new(@token, raise_errors: true)
|
45
|
+
expect{@api.subscriptions.delete(@id)}.to raise_error(Mirror::Api::BadRequestError)
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -75,20 +75,20 @@ describe "Subscriptions" do
|
|
75
75
|
stub_request(:post, "https://www.googleapis.com/mirror/v1/subscriptions/").
|
76
76
|
with(body: @body2,
|
77
77
|
headers: json_post_request_headers(@token, @body2.to_json)).
|
78
|
-
to_return(status:
|
79
|
-
body:
|
78
|
+
to_return(status: 400,
|
79
|
+
body: fixture("subscriptions_insert_bad.json", true),
|
80
80
|
headers: {})
|
81
81
|
end
|
82
82
|
it "should return nil" do
|
83
83
|
@api = Mirror::Api::Client.new(@token)
|
84
|
-
subscription = @api.subscriptions.insert(
|
84
|
+
subscription = @api.subscriptions.insert(@body2)
|
85
85
|
subscription.should == nil
|
86
86
|
end
|
87
87
|
|
88
88
|
context "bubbling errors" do
|
89
89
|
it "should raise ex" do
|
90
90
|
@api = Mirror::Api::Client.new(@token, {:raise_errors => true})
|
91
|
-
expect{@api.subscriptions.insert(@
|
91
|
+
expect{@api.subscriptions.insert(@body2)}.to raise_error(Mirror::Api::BadRequestError)
|
92
92
|
end
|
93
93
|
end
|
94
94
|
end
|
@@ -161,7 +161,7 @@ describe "Subscriptions" do
|
|
161
161
|
|
162
162
|
it "should raise an exception" do
|
163
163
|
@api = Mirror::Api::Client.new(@token, {:raise_errors => true})
|
164
|
-
expect{@api.subscriptions.update(@id, @body)}.to raise_error
|
164
|
+
expect{@api.subscriptions.update(@id, @body)}.to raise_error(Mirror::Api::BadRequestError)
|
165
165
|
end
|
166
166
|
end
|
167
167
|
end
|
@@ -130,7 +130,7 @@ describe "Timeline Attachments" do
|
|
130
130
|
headers: {})
|
131
131
|
end
|
132
132
|
|
133
|
-
it "should return a list of contacts"
|
133
|
+
it "should return a list of contacts" do
|
134
134
|
attachments = @api.timeline.list(@timeline_id, {attachments: {} })
|
135
135
|
attachments.should_not be_nil
|
136
136
|
attachments.items.count.should == 2 # see fixture
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: mirror-api
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.1.
|
4
|
+
version: 0.1.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date: 2013-
|
13
|
+
date: 2013-09-04 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: rake
|
@@ -205,8 +205,10 @@ files:
|
|
205
205
|
- Rakefile
|
206
206
|
- example/.gitkeep
|
207
207
|
- lib/mirror-api.rb
|
208
|
+
- lib/mirror-api/bad_request_error.rb
|
208
209
|
- lib/mirror-api/client.rb
|
209
210
|
- lib/mirror-api/errors.rb
|
211
|
+
- lib/mirror-api/not_found_error.rb
|
210
212
|
- lib/mirror-api/oauth.rb
|
211
213
|
- lib/mirror-api/request.rb
|
212
214
|
- lib/mirror-api/request_data.rb
|
@@ -222,6 +224,7 @@ files:
|
|
222
224
|
- spec/fixtures/locations_item.json
|
223
225
|
- spec/fixtures/locations_list.json
|
224
226
|
- spec/fixtures/oauth_response.json
|
227
|
+
- spec/fixtures/subscriptions_insert_bad.json
|
225
228
|
- spec/fixtures/subscriptions_item.json
|
226
229
|
- spec/fixtures/subscriptions_list.json
|
227
230
|
- spec/fixtures/timeline_item.json
|
@@ -270,6 +273,7 @@ test_files:
|
|
270
273
|
- spec/fixtures/locations_item.json
|
271
274
|
- spec/fixtures/locations_list.json
|
272
275
|
- spec/fixtures/oauth_response.json
|
276
|
+
- spec/fixtures/subscriptions_insert_bad.json
|
273
277
|
- spec/fixtures/subscriptions_item.json
|
274
278
|
- spec/fixtures/subscriptions_list.json
|
275
279
|
- spec/fixtures/timeline_item.json
|