crystal_sdk 1.0.1 → 1.0.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ac7754e0ca7737a3f792fbf72b1a38fc333db61
4
- data.tar.gz: 36981f0bda19928584e1c408afdd0e52faf329ae
3
+ metadata.gz: 1dea48c45c081f3af807b9d3d23f1c67cbc6419b
4
+ data.tar.gz: 4b7c406415da2322c4c11200f2e4fb68389e2754
5
5
  SHA512:
6
- metadata.gz: c4bffe97b1b4cc64e5f76b5e25e15032b58636b96bbb44d102e960dd349c8d351580d4a6485530b0a2fb13875ad1e901a188a2f876c1ca123fdbf78aaeb0104a
7
- data.tar.gz: 6c6579a048f4c7ba398514e52507eaa98a71ff9137e5a3608255df83f5ddd3e36c1b6dc1ca6d01516a3684eaab31b6d794c78b521d6801ec3ab1e0b2d84195d3
6
+ metadata.gz: 9ec2f1e41105835b55dd790e543bb9ba5066f4c57a1faf0817bd578e55884041fc09a51892c61d48b64d0721c10497e56648600a1546a8616171572bc5d0c4c0
7
+ data.tar.gz: 99acff4789f52394971b2e0e9a585c76755fa6de6fae8991611a7431ecfa5e9e5ef174ba03d4a1c6099cab9cfa37bc8578cc6b47a7008067e5b8f420594ff4d0
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- crystal_sdk (1.0.0)
4
+ crystal_sdk (1.0.1)
5
5
  nestful (~> 1.1)
6
6
  recursive-open-struct (~> 1.0)
7
7
 
@@ -1,30 +1,9 @@
1
1
  require 'timeout'
2
+ require_relative 'profile/errors'
2
3
  require_relative 'profile/request'
3
4
 
4
5
  module CrystalSDK
5
6
  class Profile
6
- class NotFoundError < StandardError; end
7
- class RateLimitHitError < StandardError; end
8
- class UnexpectedError < StandardError; end
9
-
10
- class NotAuthedError < StandardError
11
- attr_reader :token
12
-
13
- def initialize(token, msg = 'Organization Token was invalid')
14
- @token = token
15
- super(msg)
16
- end
17
- end
18
-
19
- class NotFoundYetError < StandardError
20
- attr_reader :request
21
-
22
- def initialize(request, msg = 'Profile not found in time')
23
- @request = request
24
- super(msg)
25
- end
26
- end
27
-
28
7
  attr_reader :info, :recommendations
29
8
 
30
9
  def initialize(info, recommendations)
@@ -56,18 +35,8 @@ module CrystalSDK
56
35
  end
57
36
  rescue Timeout::Error
58
37
  raise NotFoundYetError.new(request)
59
-
60
- rescue Nestful::ResponseError => e
61
- check_for_error(e.response)
62
- raise e
63
38
  end
64
39
  end
65
-
66
- def check_for_error(resp)
67
- raise RateLimitHitError if resp.code == '429'
68
- raise NotAuthedError.new(Base.key) if resp.code == '401'
69
- raise NotFoundError if resp.code == '404'
70
- end
71
40
  end
72
41
  end
73
42
  end
@@ -0,0 +1,25 @@
1
+ module CrystalSDK
2
+ class Profile
3
+ class NotFoundError < StandardError; end
4
+ class RateLimitHitError < StandardError; end
5
+ class UnexpectedError < StandardError; end
6
+
7
+ class NotAuthedError < StandardError
8
+ attr_reader :token
9
+
10
+ def initialize(token, msg = 'Organization Token was invalid')
11
+ @token = token
12
+ super(msg)
13
+ end
14
+ end
15
+
16
+ class NotFoundYetError < StandardError
17
+ attr_reader :request
18
+
19
+ def initialize(request, msg = 'Profile not found in time')
20
+ @request = request
21
+ super(msg)
22
+ end
23
+ end
24
+ end
25
+ end
@@ -10,17 +10,36 @@ module CrystalSDK
10
10
  @id = id
11
11
  end
12
12
 
13
- def self.from_search(query)
14
- resp = Api.make_request(:post, 'profile_search/async', params: query)
15
- body = JSON.parse(resp.body || '{}', symbolize_names: true)
13
+ class << self
14
+ def from_search(query)
15
+ begin
16
+ resp = Api.make_request(:post, 'profile_search/async', params: query)
17
+ rescue Nestful::ResponseError => e
18
+ check_for_error(e.response)
19
+ raise e
20
+ end
16
21
 
17
- Profile::Request.new(body[:request_id])
22
+ body = JSON.parse(resp.body || '{}', symbolize_names: true)
23
+ Profile::Request.new(body[:request_id])
24
+ end
25
+
26
+ def check_for_error(resp)
27
+ raise Profile::RateLimitHitError if resp.code == '429'
28
+ raise Profile::NotAuthedError.new(Base.key) if resp.code == '401'
29
+ raise Profile::NotFoundError if resp.code == '404'
30
+ end
18
31
  end
19
32
 
20
33
  def fetch_request_info
21
34
  return @cached_data if @cached_data
22
35
 
23
- resp = Api.make_request(:get, "results/#{@id}")
36
+ begin
37
+ resp = Api.make_request(:get, "results/#{@id}")
38
+ rescue Nestful::ResponseError => e
39
+ Request.check_for_error(e.response)
40
+ raise e
41
+ end
42
+
24
43
  body = resp.body ? JSON.parse(resp.body, symbolize_names: true) : nil
25
44
 
26
45
  if body[:status] == 'complete' || body[:status] == 'error'
@@ -1,3 +1,3 @@
1
1
  module CrystalSDK
2
- VERSION = '1.0.1'.freeze
2
+ VERSION = '1.0.2'.freeze
3
3
  end
@@ -22,6 +22,17 @@ describe CrystalSDK::Profile::Request do
22
22
 
23
23
  expect { subject }.to raise_error('SomeRandomError')
24
24
  end
25
+
26
+ it 'should pass Nestful exceptions to check_for_error' do
27
+ allow(CrystalSDK::Api).to receive(:make_request)
28
+ .with(request_type, endpoint, params: query)
29
+ .and_raise(Nestful::ResponseError.new(nil, nil))
30
+
31
+ expect(CrystalSDK::Profile::Request).to receive(:check_for_error)
32
+ .and_raise('CheckForErrorCalled')
33
+
34
+ expect { subject }.to raise_error('CheckForErrorCalled')
35
+ end
25
36
  end
26
37
 
27
38
  context 'CrystalSDK::Api returns invalid json body' do
@@ -249,4 +260,65 @@ describe CrystalSDK::Profile::Request do
249
260
  end
250
261
  end
251
262
  end
263
+
264
+ describe '.check_for_error' do
265
+ subject { CrystalSDK::Profile::Request.check_for_error(resp) }
266
+
267
+ context '200' do
268
+ let(:resp) do
269
+ double(code: '200')
270
+ end
271
+
272
+ it 'should raise no error' do
273
+ expect { subject }.to_not raise_error
274
+ end
275
+ end
276
+
277
+ context '202' do
278
+ let(:resp) { double(code: '202') }
279
+
280
+ it 'should raise no error' do
281
+ expect { subject }.to_not raise_error
282
+ end
283
+ end
284
+
285
+ context '401' do
286
+ let(:resp) { double(code: '401') }
287
+
288
+ before(:each) do
289
+ @orig_key = CrystalSDK.key
290
+ CrystalSDK.key = 'SomeKey'
291
+ end
292
+
293
+ after(:each) do
294
+ CrystalSDK.key = @orig_key
295
+ end
296
+
297
+ it 'should raise NotAuthedError' do
298
+ expect { subject }.to raise_error(CrystalSDK::Profile::NotAuthedError)
299
+
300
+ begin
301
+ subject
302
+ rescue CrystalSDK::Profile::NotAuthedError => e
303
+ expect(e.token).to eql('SomeKey')
304
+ end
305
+ end
306
+ end
307
+
308
+ context '404' do
309
+ let(:resp) { double(code: '404') }
310
+
311
+ it 'should raise NotFoundError' do
312
+ expect { subject }.to raise_error(CrystalSDK::Profile::NotFoundError)
313
+ end
314
+ end
315
+
316
+ context '429' do
317
+ let(:resp) { double(code: '429') }
318
+
319
+ it 'should raise RateLimitHitError' do
320
+ expect { subject }.to raise_error(CrystalSDK::Profile::RateLimitHitError)
321
+ end
322
+ end
323
+ end
252
324
  end
@@ -43,20 +43,10 @@ describe CrystalSDK::Profile do
43
43
  .and_return(req)
44
44
 
45
45
  allow(req).to receive(:did_finish?)
46
- .and_raise(Nestful::ResponseError.new(nil, double()))
46
+ .and_raise(StandardError.new)
47
47
  end
48
48
 
49
- it 'should pass it off to check_for_error' do
50
- expect(CrystalSDK::Profile).to receive(:check_for_error)
51
- .and_raise('CheckForErrorCalled')
52
-
53
- expect { subject }.to raise_error('CheckForErrorCalled')
54
- end
55
-
56
- it 'should still raise exception if passed check_for_error' do
57
- expect(CrystalSDK::Profile).to receive(:check_for_error)
58
- .and_return(nil)
59
-
49
+ it 'should raise exception' do
60
50
  expect { subject }.to raise_error
61
51
  end
62
52
  end
@@ -78,7 +68,7 @@ describe CrystalSDK::Profile do
78
68
  end
79
69
 
80
70
  context 'request did not finish before timeout expired' do
81
- subject { CrystalSDK::Profile.search(query, timeout: 1) }
71
+ subject { CrystalSDK::Profile.search(query, timeout: 0.01) }
82
72
 
83
73
  before(:each) do
84
74
  allow(CrystalSDK::Profile::Request).to receive(:from_search)
@@ -100,66 +90,85 @@ describe CrystalSDK::Profile do
100
90
  end
101
91
  end
102
92
  end
103
- end
104
93
 
105
- describe '.check_for_error' do
106
- subject { CrystalSDK::Profile.check_for_error(resp) }
94
+ context 'ApiSDK threw errors during initial request creation' do
95
+ context '401 error' do
96
+ before(:each) do
97
+ allow(CrystalSDK::Api).to receive(:make_request)
98
+ .and_raise(Nestful::ResponseError.new(nil, resp))
99
+ end
100
+ let(:resp) { OpenStruct.new(code: '401') }
107
101
 
108
- context '200' do
109
- let(:resp) do
110
- double(code: '200')
102
+ it 'should raise NotAuthedError' do
103
+ expect { subject }.to raise_error(CrystalSDK::Profile::NotAuthedError)
104
+ end
111
105
  end
112
106
 
113
- it 'should raise no error' do
114
- expect { subject }.to_not raise_error
107
+ context '404 error' do
108
+ before(:each) do
109
+ allow(CrystalSDK::Api).to receive(:make_request)
110
+ .and_raise(Nestful::ResponseError.new(nil, resp))
111
+ end
112
+ let(:resp) { OpenStruct.new(code: '404') }
113
+
114
+ it 'should raise NotFoundError' do
115
+ expect { subject }.to raise_error(CrystalSDK::Profile::NotFoundError)
116
+ end
115
117
  end
116
- end
117
118
 
118
- context '202' do
119
- let(:resp) { double(code: '202') }
119
+ context '429 error' do
120
+ before(:each) do
121
+ allow(CrystalSDK::Api).to receive(:make_request)
122
+ .and_raise(Nestful::ResponseError.new(nil, resp))
123
+ end
124
+ let(:resp) { OpenStruct.new(code: '429') }
120
125
 
121
- it 'should raise no error' do
122
- expect { subject }.to_not raise_error
126
+ it 'should raise RateLimitHitError' do
127
+ expect { subject }.to raise_error(CrystalSDK::Profile::RateLimitHitError)
128
+ end
123
129
  end
124
130
  end
125
131
 
126
- context '401' do
127
- let(:resp) { double(code: '401') }
132
+ context 'ApiSDK threw errors during polling' do
133
+ context '401 error' do
134
+ before(:each) do
135
+ allow(CrystalSDK::Profile::Request).to receive(:from_search)
136
+ .and_return(CrystalSDK::Profile::Request.new('some_id'))
128
137
 
129
- before(:each) do
130
- @orig_key = CrystalSDK.key
131
- CrystalSDK.key = 'SomeKey'
132
- end
138
+ allow(CrystalSDK::Api).to receive(:make_request)
139
+ .and_raise(Nestful::ResponseError.new(nil, resp))
140
+ end
141
+ let(:resp) { OpenStruct.new(code: '401') }
133
142
 
134
- after(:each) do
135
- CrystalSDK.key = @orig_key
143
+ it 'should raise NotAuthedError' do
144
+ expect { subject }.to raise_error(CrystalSDK::Profile::NotAuthedError)
145
+ end
136
146
  end
137
147
 
138
- it 'should raise NotAuthedError' do
139
- expect { subject }.to raise_error(CrystalSDK::Profile::NotAuthedError)
148
+ context '404 error' do
149
+ before(:each) do
150
+ allow(CrystalSDK::Api).to receive(:make_request)
151
+ .and_raise(Nestful::ResponseError.new(nil, resp))
152
+ end
153
+ let(:resp) { OpenStruct.new(code: '404') }
140
154
 
141
- begin
142
- subject
143
- rescue CrystalSDK::Profile::NotAuthedError => e
144
- expect(e.token).to eql('SomeKey')
155
+ it 'should raise NotFoundError' do
156
+ expect { subject }.to raise_error(CrystalSDK::Profile::NotFoundError)
145
157
  end
146
158
  end
147
- end
148
159
 
149
- context '404' do
150
- let(:resp) { double(code: '404') }
160
+ context '429 error' do
161
+ before(:each) do
162
+ allow(CrystalSDK::Api).to receive(:make_request)
163
+ .and_raise(Nestful::ResponseError.new(nil, resp))
164
+ end
165
+ let(:resp) { OpenStruct.new(code: '429') }
151
166
 
152
- it 'should raise NotFoundError' do
153
- expect { subject }.to raise_error(CrystalSDK::Profile::NotFoundError)
167
+ it 'should raise RateLimitHitError' do
168
+ expect { subject }.to raise_error(CrystalSDK::Profile::RateLimitHitError)
169
+ end
154
170
  end
155
171
  end
156
172
 
157
- context '429' do
158
- let(:resp) { double(code: '429') }
159
-
160
- it 'should raise RateLimitHitError' do
161
- expect { subject }.to raise_error(CrystalSDK::Profile::RateLimitHitError)
162
- end
163
- end
164
173
  end
165
174
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: crystal_sdk
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.1
4
+ version: 1.0.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Cory Finger
@@ -86,6 +86,7 @@ files:
86
86
  - lib/crystal_sdk/api.rb
87
87
  - lib/crystal_sdk/base.rb
88
88
  - lib/crystal_sdk/profile.rb
89
+ - lib/crystal_sdk/profile/errors.rb
89
90
  - lib/crystal_sdk/profile/request.rb
90
91
  - lib/crystal_sdk/version.rb
91
92
  - spec/crystal_sdk/api_spec.rb