Skip to content
This repository was archived by the owner on Oct 7, 2022. It is now read-only.

Commit 6fcefd2

Browse files
authored
Merge pull request #7 from creatubbles/269-warehouse-get-creation-403-retry-mechanism
269 warehouse get creation 403 retry mechanism
2 parents 5e9d759 + 61d24ba commit 6fcefd2

File tree

11 files changed

+167
-3
lines changed

11 files changed

+167
-3
lines changed

Gemfile

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,18 @@
11
source "https://rubygems.org"
22

3+
ruby '2.4.1'
4+
35
gemspec
46

57
group :development do
68
gem 'rake'
79
gem 'rspec'
810
end
11+
12+
group :test do
13+
gem 'rake'
14+
gem 'rspec'
15+
gem 'webmock'
16+
gem 'sinatra'
17+
gem 'pry'
18+
end

Gemfile.lock

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,23 +13,38 @@ GEM
1313
i18n (~> 0.7)
1414
minitest (~> 5.1)
1515
tzinfo (~> 1.1)
16+
addressable (2.5.1)
17+
public_suffix (~> 2.0, >= 2.0.2)
18+
coderay (1.1.1)
1619
concurrent-ruby (1.0.4)
20+
crack (0.4.3)
21+
safe_yaml (~> 1.0.0)
1722
diff-lcs (1.2.5)
1823
faraday (0.10.1)
1924
multipart-post (>= 1.2, < 3)
25+
hashdiff (0.3.4)
2026
i18n (0.8.0)
2127
jwt (1.5.6)
28+
method_source (0.8.2)
2229
minitest (5.10.1)
2330
multi_json (1.12.1)
2431
multi_xml (0.6.0)
2532
multipart-post (2.0.0)
33+
mustermann (1.0.0)
2634
oauth2 (1.3.0)
2735
faraday (>= 0.8, < 0.11)
2836
jwt (~> 1.0)
2937
multi_json (~> 1.3)
3038
multi_xml (~> 0.5)
3139
rack (>= 1.2, < 3)
40+
pry (0.10.4)
41+
coderay (~> 1.1.0)
42+
method_source (~> 0.8.1)
43+
slop (~> 3.4)
44+
public_suffix (2.0.5)
3245
rack (2.0.1)
46+
rack-protection (2.0.0)
47+
rack
3348
rake (11.2.2)
3449
rspec (3.5.0)
3550
rspec-core (~> 3.5.0)
@@ -44,17 +59,35 @@ GEM
4459
diff-lcs (>= 1.2.0, < 2.0)
4560
rspec-support (~> 3.5.0)
4661
rspec-support (3.5.0)
62+
safe_yaml (1.0.4)
63+
sinatra (2.0.0)
64+
mustermann (~> 1.0)
65+
rack (~> 2.0)
66+
rack-protection (= 2.0.0)
67+
tilt (~> 2.0)
68+
slop (3.6.0)
4769
thread_safe (0.3.5)
70+
tilt (2.0.8)
4871
tzinfo (1.2.2)
4972
thread_safe (~> 0.1)
73+
webmock (3.0.1)
74+
addressable (>= 2.3.6)
75+
crack (>= 0.3.2)
76+
hashdiff
5077

5178
PLATFORMS
5279
ruby
5380

5481
DEPENDENCIES
5582
creatubbles!
83+
pry
5684
rake
5785
rspec
86+
sinatra
87+
webmock
88+
89+
RUBY VERSION
90+
ruby 2.4.1p111
5891

5992
BUNDLED WITH
60-
1.14.3
93+
1.15.3

lib/creatubbles.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ def self.instantiate_objects_from_response(response, connection)
4242

4343
end
4444

45+
require "creatubbles/error"
4546
require "creatubbles/utils/auth_details"
4647
require "creatubbles/content"
4748
require "creatubbles/contents"

lib/creatubbles/base_collection.rb

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,18 @@ def initialize(connection)
1616
def find(id)
1717
res = @connection.get("#{self.class.type_name}/#{id}")
1818
Creatubbles.instantiate_object_from_response(res, @connection)
19+
rescue => e
20+
if is_record_not_found_404?(e)
21+
raise Creatubbles::Error::RecordNotFound.new(e), "Not Found! #{self.class.type_name}/#{id}"
22+
else
23+
raise e
24+
end
25+
end
26+
27+
def find_by_id(id)
28+
find(id)
29+
rescue Creatubbles::Error::RecordNotFound
30+
return nil
1931
end
2032

2133
def init_objects(response)
@@ -26,4 +38,15 @@ def handle_params(params_hash={}, allowed_params=['query','filter'])
2638
params_hash.stringify_keys!.slice!(allowed_params).to_param
2739
end
2840

41+
private
42+
43+
def is_record_not_found_404?(e)
44+
return false unless e.is_a?(OAuth2::Error)
45+
body = e&.response&.body
46+
return false unless body
47+
resp = JSON.parse(e.response.body) rescue nil
48+
return false unless resp
49+
resp['errors'] && resp['errors'].any? { |err| err['status'].to_i == 404 }
50+
end
51+
2952
end

lib/creatubbles/error.rb

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
module Creatubbles
2+
module Error
3+
#Creatubbles::Error::RecordNotFound
4+
class RecordNotFound < StandardError
5+
attr_reader :original
6+
def initialize original
7+
@original = original
8+
end
9+
end
10+
end
11+
end
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
require 'spec_helper'
2+
3+
RSpec.describe Creatubbles::BaseCollection do
4+
5+
it "ignores deleted creation" do
6+
client = Creatubbles::Client.dummy
7+
8+
expect{client.test_objects.find('non-existing-object-id')}.
9+
to raise_error(Creatubbles::Error::RecordNotFound)
10+
11+
begin
12+
client.test_objects.find('non-existing-object-id')
13+
rescue => e
14+
expect(e.original).to be_a(OAuth2::Error)
15+
expect(e.message).to include('non-existing-object-id')
16+
end
17+
18+
expect(client.test_objects.find_by_id('non-existing-object-id')).
19+
to be_nil
20+
end
21+
end

spec/creatubbles/creation_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'helper'
1+
require 'spec_helper'
22

33
describe Creatubbles::Creation do
44
let(:client) { Creatubbles::Client.new }

spec/creatubbles/user_spec.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
require 'helper'
1+
require 'spec_helper'
22

33
describe Creatubbles::User do
44
let(:client) { Creatubbles::Client.new }

spec/helper.rb renamed to spec/spec_helper.rb

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,21 @@
11
require 'oauth2'
22
require 'rspec'
33
require 'creatubbles'
4+
require 'sinatra/base'
5+
require 'webmock/rspec'
6+
require 'pry'
47

58
Dir['spec/support/**/*.rb'].each do |f|
69
require f.gsub('spec/', '').gsub('.rb', '')
710
end
811

12+
WebMock.disable_net_connect!(allow_localhost: true)
13+
914
RSpec.configure do |config|
15+
config.before(:each) do
16+
stub_request(:any, /api.creatubbles.com/).to_rack(Creatubbles::MockAPI)
17+
end
18+
1019
config.expect_with :rspec do |c|
1120
c.syntax = :expect
1221
end

spec/support/creatubbles_mock_api.rb

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'spec_helper'
2+
3+
module Creatubbles
4+
class MockAPI < Sinatra::Base
5+
set :port, 3000
6+
7+
# put class variables here
8+
# you cannot use @var in Sinatra::Base
9+
configure do
10+
end
11+
12+
# return a preset access token
13+
post '/oauth/token' do
14+
headers "Content-Type" => 'application/json'
15+
{"access_token" => "a8c814aa3cee2d5cdca997a314dca206335c119e7121e954a7c6f17592caa33d", "token_type" => "bearer", "created_at" => 1479918313}.to_json
16+
end
17+
18+
post '/v2/oauth/token' do
19+
headers "Content-Type" => 'application/json'
20+
{"access_token" => "a8c814aa3cee2d5cdca997a314dca206335c119e7121e954a7c6f17592caa33d", "token_type" => "bearer", "created_at" => 1479918313}.to_json
21+
end
22+
23+
# get a non existing object
24+
get '/v2/test_objects/non-existing-object-id' do
25+
status 404
26+
headers "Content-Type" => 'application/json'
27+
"{\"errors\":[{\"status\":\"404\",\"code\":\"not_found\",\"source\":\"https://api.creatubbles.com/v2/objects/non-existing-creation-id\",\"title\":\"Not found\",\"detail\":\"Record not found\"}]}"
28+
end
29+
30+
end
31+
end
32+
33+
# To run the moch as a separate process:
34+
# Creatubbles::MockAPI.run!

0 commit comments

Comments
 (0)