uploadcare-ruby 1.2.2 → 3.0.5

Sign up to get free protection for your applications and to get access to all the features.
Files changed (98) hide show
  1. checksums.yaml +4 -4
  2. data/.github/workflows/gem-push.yml +20 -0
  3. data/.github/workflows/ruby.yml +52 -0
  4. data/.gitignore +13 -5
  5. data/.rspec +3 -0
  6. data/.rubocop.yml +20 -0
  7. data/.yardopts +4 -0
  8. data/CHANGELOG.md +26 -26
  9. data/DEVELOPMENT.md +18 -0
  10. data/Gemfile +2 -0
  11. data/LICENSE +1 -1
  12. data/README.md +191 -519
  13. data/Rakefile +6 -4
  14. data/bin/console +15 -0
  15. data/bin/setup +8 -0
  16. data/lib/uploadcare.rb +36 -31
  17. data/lib/uploadcare/api/api.rb +25 -0
  18. data/lib/uploadcare/client/file_client.rb +44 -0
  19. data/lib/uploadcare/client/file_list_client.rb +46 -0
  20. data/lib/uploadcare/client/group_client.rb +45 -0
  21. data/lib/uploadcare/client/multipart_upload/chunks_client.rb +46 -0
  22. data/lib/uploadcare/client/multipart_upload_client.rb +62 -0
  23. data/lib/uploadcare/client/project_client.rb +18 -0
  24. data/lib/uploadcare/client/rest_client.rb +73 -0
  25. data/lib/uploadcare/client/rest_group_client.rb +23 -0
  26. data/lib/uploadcare/client/upload_client.rb +35 -0
  27. data/lib/uploadcare/client/uploader_client.rb +93 -0
  28. data/lib/uploadcare/client/webhook_client.rb +43 -0
  29. data/lib/uploadcare/concern/error_handler.rb +54 -0
  30. data/lib/uploadcare/concern/throttle_handler.rb +25 -0
  31. data/lib/uploadcare/concern/upload_error_handler.rb +32 -0
  32. data/lib/uploadcare/entity/decorator/paginator.rb +81 -0
  33. data/lib/uploadcare/entity/entity.rb +18 -0
  34. data/lib/uploadcare/entity/file.rb +81 -0
  35. data/lib/uploadcare/entity/file_list.rb +30 -0
  36. data/lib/uploadcare/entity/group.rb +41 -0
  37. data/lib/uploadcare/entity/group_list.rb +24 -0
  38. data/lib/uploadcare/entity/project.rb +13 -0
  39. data/lib/uploadcare/entity/uploader.rb +73 -0
  40. data/lib/uploadcare/entity/webhook.rb +14 -0
  41. data/lib/uploadcare/exception/request_error.rb +9 -0
  42. data/lib/uploadcare/exception/throttle_error.rb +14 -0
  43. data/lib/uploadcare/param/authentication_header.rb +25 -0
  44. data/lib/uploadcare/param/param.rb +10 -0
  45. data/lib/uploadcare/param/secure_auth_header.rb +37 -0
  46. data/lib/uploadcare/param/simple_auth_header.rb +14 -0
  47. data/lib/uploadcare/param/upload/signature_generator.rb +24 -0
  48. data/lib/uploadcare/param/upload/upload_params_generator.rb +23 -0
  49. data/lib/uploadcare/param/user_agent.rb +21 -0
  50. data/lib/uploadcare/ruby/version.rb +5 -0
  51. data/uploadcare-ruby.gemspec +50 -36
  52. metadata +112 -96
  53. data/.travis.yml +0 -12
  54. data/lib/uploadcare/api.rb +0 -24
  55. data/lib/uploadcare/api/file_api.rb +0 -7
  56. data/lib/uploadcare/api/file_list_api.rb +0 -8
  57. data/lib/uploadcare/api/group_api.rb +0 -38
  58. data/lib/uploadcare/api/group_list_api.rb +0 -8
  59. data/lib/uploadcare/api/project_api.rb +0 -9
  60. data/lib/uploadcare/api/raw_api.rb +0 -44
  61. data/lib/uploadcare/api/uploading_api.rb +0 -110
  62. data/lib/uploadcare/errors/errors.rb +0 -64
  63. data/lib/uploadcare/resources/file.rb +0 -164
  64. data/lib/uploadcare/resources/file_list.rb +0 -41
  65. data/lib/uploadcare/resources/group.rb +0 -115
  66. data/lib/uploadcare/resources/group_list.rb +0 -31
  67. data/lib/uploadcare/resources/project.rb +0 -13
  68. data/lib/uploadcare/rest/auth/auth.rb +0 -31
  69. data/lib/uploadcare/rest/auth/secure.rb +0 -43
  70. data/lib/uploadcare/rest/auth/simple.rb +0 -16
  71. data/lib/uploadcare/rest/connections/api_connection.rb +0 -32
  72. data/lib/uploadcare/rest/connections/upload_connection.rb +0 -21
  73. data/lib/uploadcare/rest/middlewares/auth_middleware.rb +0 -24
  74. data/lib/uploadcare/rest/middlewares/parse_json_middleware.rb +0 -33
  75. data/lib/uploadcare/rest/middlewares/raise_error_middleware.rb +0 -21
  76. data/lib/uploadcare/utils/parser.rb +0 -71
  77. data/lib/uploadcare/utils/user_agent.rb +0 -44
  78. data/lib/uploadcare/version.rb +0 -3
  79. data/spec/api/raw_api_spec.rb +0 -25
  80. data/spec/resources/file_list_spec.rb +0 -64
  81. data/spec/resources/file_spec.rb +0 -222
  82. data/spec/resources/group_list_spec.rb +0 -30
  83. data/spec/resources/group_spec.rb +0 -101
  84. data/spec/resources/operations_spec.rb +0 -59
  85. data/spec/resources/project_spec.rb +0 -21
  86. data/spec/rest/api_connection_spec.rb +0 -68
  87. data/spec/rest/auth/secure_spec.rb +0 -66
  88. data/spec/rest/auth/simple_spec.rb +0 -31
  89. data/spec/rest/errors_spec.rb +0 -75
  90. data/spec/rest/upload_connection_spec.rb +0 -19
  91. data/spec/spec_helper.rb +0 -41
  92. data/spec/uploadcare_spec.rb +0 -43
  93. data/spec/uploading/uploading_multiple_spec.rb +0 -43
  94. data/spec/uploading/uploading_spec.rb +0 -40
  95. data/spec/utils/parser_spec.rb +0 -87
  96. data/spec/utils/user_agent_spec.rb +0 -46
  97. data/spec/view.png +0 -0
  98. data/spec/view2.jpg +0 -0
@@ -1,66 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Uploadcare::Connections::Auth::Secure do
4
- describe '#apply' do
5
- let(:env){ Faraday::Env.new(:get, nil, URI::HTTP.build(host: 'example.com'), nil, {}) }
6
- subject{ env.request_headers }
7
-
8
- before(:each) do
9
- described_class.new(public_key: 'pub', private_key: 'priv').apply(env)
10
- end
11
-
12
- it "adds Authorization header to env's request_headers" do
13
- expect(subject).to include('Authorization')
14
- end
15
-
16
- it "adds Date header to env's request_headers" do
17
- expect(subject).to include('Date')
18
- end
19
-
20
- it "uses secure authorization" do
21
- expect(subject['Authorization']).to match /Uploadcare pub:.+/
22
- end
23
- end
24
-
25
- describe 'signature' do
26
- let(:uri){ URI::HTTP.build(host: 'example.com', path: '/path', query: 'test=1') }
27
- let(:headers){ {'Content-Type' => 'application/x-www-form-urlencoded'} }
28
- let(:env){ Faraday::Env.new(:post, 'url=encoded&test=body', uri, nil, headers) }
29
-
30
- subject{ env.request_headers }
31
-
32
- before(:each) do
33
- allow(Time).to receive(:now).and_return(Time.parse('2017.02.02 12:58:50 +0000'))
34
- described_class.new(public_key: 'pub', private_key: 'priv').apply(env)
35
- end
36
-
37
- it "counts signature correctly" do
38
- expected = '71b61ed67d16f48d2e46a4ee72ca12025aeb8d1f'
39
- expect(subject['Authorization']).to eq "Uploadcare pub:#{expected}"
40
- end
41
- end
42
-
43
-
44
- describe 'integration' do
45
- let(:api){ Uploadcare::Api.new(auth_scheme: :secure) }
46
- let(:file){ api.upload IMAGE_URL }
47
-
48
- before(:each) do
49
- # ensure that secure auth is being used
50
- expect_any_instance_of(described_class).to receive(:apply).at_least(4).times.and_call_original
51
- end
52
-
53
- it 'auth works with real requests' do
54
- # request with url params
55
- expect{ api.get('/files/', limit: 1) }.not_to raise_error
56
- # request with an url-encoded body
57
- expect{
58
- retry_if(Uploadcare::Error::RequestError::BadRequest) do
59
- api.post('/files/', source: file.uuid)
60
- end
61
- }.not_to raise_error
62
- # request with an empty body and a redirect
63
- expect{ api.delete("/files/#{file.uuid}/storage/") }.not_to raise_error
64
- end
65
- end
66
- end
@@ -1,31 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Uploadcare::Connections::Auth::Simple do
4
- let(:env){ Faraday::Env.new(nil, nil, nil, nil, {}) }
5
- subject{ described_class.new(public_key: 'pub', private_key: 'priv') }
6
-
7
- describe 'apply' do
8
- it "adds Authorization header to env's request_headers" do
9
- subject.apply(env)
10
- expect(env.request_headers).to include('Authorization')
11
- end
12
-
13
- it "sets Authorization header's value correctly" do
14
- subject.apply(env)
15
- expect(env.request_headers['Authorization']).to eq "Uploadcare.Simple pub:priv"
16
- end
17
- end
18
-
19
- describe 'integration' do
20
- let(:api){ Uploadcare::Api.new(auth_scheme: :simple) }
21
-
22
- before(:each) do
23
- # ensure that simple auth is being used
24
- expect_any_instance_of(described_class).to receive(:apply).and_call_original
25
- end
26
-
27
- it 'auth works with real requests' do
28
- expect{ api.get('/files/') }.not_to raise_error
29
- end
30
- end
31
- end
@@ -1,75 +0,0 @@
1
- require 'spec_helper'
2
- require 'uri'
3
- require 'socket'
4
-
5
- describe Uploadcare::Error do
6
- before(:all) do
7
- @settings = Uploadcare.default_settings
8
- @codes = [400,401,403,404,406,408,422,429,500,502,503,504]
9
- @connection = Uploadcare::Connections::ApiConnection.new(@settings)
10
- end
11
-
12
- it 'Error codes should be accesbile' do
13
- Uploadcare::Error.errors.keys.should == @codes
14
- end
15
-
16
- it 'Errors should be kind of requested codes' do
17
- not_found = Uploadcare::Error.errors[404]
18
- not_found.new('File not found').should be_kind_of(Uploadcare::Error::RequestError::NotFound)
19
- end
20
-
21
- it 'errors should have meaningfull messages' do
22
- not_found = Uploadcare::Error.errors[404]
23
- error = not_found.new
24
- error.message.should == "HTTP 404 - the requested resource could not be found."
25
- end
26
-
27
- it 'Should raise an error' do
28
- error = Uploadcare::Error::RequestError::NotFound
29
- expect{ @connection.send :get, '/random_url/', {} }.to raise_error(error)
30
- end
31
-
32
- it "should escape particular error" do
33
- error = Uploadcare::Error::RequestError::NotFound
34
- expect do
35
- begin
36
- @connection.send :get, '/random_url/', {}
37
- rescue error => e
38
- nil
39
- end
40
- end.to_not raise_error
41
- end
42
-
43
- it 'should escape common request error' do
44
- error = Uploadcare::Error::RequestError
45
- expect do
46
- begin
47
- @connection.send :get, '/random_url/', {}
48
- rescue error => e
49
- nil
50
- end
51
- end.to_not raise_error
52
- end
53
-
54
- it 'should escape generic uploadcare service error' do
55
- error = Uploadcare::Error
56
- expect do
57
- begin
58
- @connection.send :get, '/random_url/', {}
59
- rescue error => e
60
- nil
61
- end
62
- end.to_not raise_error
63
- end
64
-
65
- it 'should escape generic uploadcare service error' do
66
- error = StandardError
67
- expect do
68
- begin
69
- @connection.send :get, '/random_url/', {}
70
- rescue error => e
71
- nil
72
- end
73
- end.to_not raise_error
74
- end
75
- end
@@ -1,19 +0,0 @@
1
- require 'spec_helper'
2
- require 'uri'
3
- require 'socket'
4
-
5
- describe Uploadcare::Connections::UploadConnection do
6
- before(:all) do
7
- @settings = Uploadcare.default_settings
8
- end
9
-
10
- it 'should initialize upload connection' do
11
- expect {Uploadcare::Connections::UploadConnection.new(@settings)}.to_not raise_error
12
- end
13
-
14
- it 'should use ParseJson and RaiseError middleware' do
15
- connection = Uploadcare::Connections::UploadConnection.new(@settings)
16
- connection.builder.handlers.include?(Uploadcare::Connections::Response::ParseJson).should == true
17
- connection.builder.handlers.include?(Uploadcare::Connections::Response::RaiseError).should == true
18
- end
19
- end
data/spec/spec_helper.rb DELETED
@@ -1,41 +0,0 @@
1
- $LOAD_PATH << File.join(File.dirname(__FILE__), '..', 'lib')
2
- $LOAD_PATH << File.join(File.dirname(__FILE__))
3
-
4
- require 'rubygems'
5
- require 'pry'
6
- require 'rspec'
7
- require 'uploadcare'
8
- require 'yaml'
9
-
10
- CONFIG = Uploadcare.default_settings.merge!(
11
- public_key: ENV['UPLOADCARE_PUBLIC_KEY'] || 'demopublickey',
12
- private_key: ENV['UPLOADCARE_SECRET_KEY'] || 'demoprivatekey',
13
- )
14
- UUID_REGEX = /[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}/
15
-
16
- API = Uploadcare::Api.new(CONFIG)
17
- IMAGE_URL = "http://macaw.co/images/macaw-logo.png"
18
-
19
- FILE1 = File.open(File.join(File.dirname(__FILE__), 'view.png'))
20
- FILE2 = File.open(File.join(File.dirname(__FILE__), 'view2.jpg'))
21
- FILES_ARY = [FILE1, FILE2]
22
-
23
-
24
- config_file = File.join(File.dirname(__FILE__), 'config.yml')
25
- if File.exists?(config_file)
26
- CONFIG.update Hash[YAML.parse_file(config_file).to_ruby.map{|a, b| [a.to_sym, b]}]
27
- end
28
-
29
- if CONFIG[:public_key] == 'demopublickey'
30
- RSpec.configure do |c|
31
- c.before(:example, :payed_feature){ skip "Unavailable for demo account" }
32
- end
33
- end
34
-
35
- def retry_if(error, retries=5, &block)
36
- block.call
37
- rescue error
38
- raise if retries <= 0
39
- sleep 0.2
40
- retry_if(error, retries-1, &block)
41
- end
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Uploadcare do
4
- around do |example|
5
- orig_stderr = $stderr
6
- $stderr = StringIO.new
7
-
8
- example.call
9
-
10
- $stderr = orig_stderr
11
- end
12
-
13
- describe '::user_agent' do
14
- subject(:user_agent) { described_class.user_agent(options) }
15
- let(:options) { {user_agent: 'user/agent'} }
16
- let(:user_agent_builder) { instance_double('Uploadcare::UserAgent') }
17
-
18
- it 'returns user agent string' do
19
- allow(Uploadcare::UserAgent).to receive(:new) { user_agent_builder }
20
- expect(user_agent_builder).to receive(:call).with(options) { 'user/agent' }
21
-
22
- expect(user_agent).to eq('user/agent')
23
- end
24
-
25
- it 'is deprecated' do
26
- user_agent
27
-
28
- $stderr.rewind
29
- expect($stderr.string).to start_with('[DEPRECATION] `Uploadcare::user_agent`')
30
- end
31
- end
32
-
33
- describe '::USER_AGENT' do
34
- it { expect(described_class::USER_AGENT).not_to be_nil }
35
-
36
- it 'is deprecated' do
37
- described_class::USER_AGENT
38
-
39
- $stderr.rewind
40
- expect($stderr.string).to start_with('[DEPRECATION] `Uploadcare::USER_AGENT`')
41
- end
42
- end
43
- end
@@ -1,43 +0,0 @@
1
- require 'spec_helper'
2
- require 'uri'
3
- require 'socket'
4
-
5
- describe Uploadcare::Api do
6
- before :all do
7
- @api = API
8
- @files_ary = FILES_ARY
9
- end
10
-
11
- it "it should upload multiple files" do
12
- expect {files = @api.upload @files_ary}.to_not raise_error
13
- end
14
-
15
- it "should retunrn an array of files" do
16
- files = @api.upload @files_ary
17
- files.should be_kind_of(Array)
18
- end
19
-
20
- it "each in array should be UC file object" do
21
- files = @api.upload @files_ary
22
- files.each do |f|
23
- f.should be_kind_of(Uploadcare::Api::File)
24
- end
25
- end
26
-
27
- it "each in array should have valid UUID" do
28
- files = @api.upload @files_ary
29
- files.each do |f|
30
- f.should respond_to(:uuid)
31
- f.uuid.should match(UUID_REGEX)
32
- end
33
- end
34
-
35
- it "each in array should load data for uploaded file" do
36
- files = @api.upload @files_ary
37
- files.each do |f|
38
- f.should respond_to(:load_data)
39
- expect {f.load_data}.to_not raise_error
40
- end
41
- end
42
- end
43
-
@@ -1,40 +0,0 @@
1
- require 'spec_helper'
2
- require 'uri'
3
- require 'socket'
4
-
5
- describe Uploadcare::Api do
6
- before :all do
7
- @api = API
8
- @file = FILE1
9
- @url = IMAGE_URL
10
- end
11
-
12
- it 'should upload file or url' do
13
- file = @api.upload @file
14
- file.should be_an_instance_of Uploadcare::Api::File
15
- end
16
-
17
- it 'should raise an error when neither file nor url given' do
18
- expect { @api.upload 12 }.to raise_error
19
- end
20
-
21
- it 'should upload file' do
22
- file = @api.upload @file
23
- file.should be_an_instance_of Uploadcare::Api::File
24
- end
25
-
26
- it 'should upload from url' do
27
- file = @api.upload @url
28
- file.should be_an_instance_of Uploadcare::Api::File
29
- end
30
-
31
- it 'should not upload from invalid url' do
32
- expect { @api.upload 'not.url.' }.to raise_error
33
- end
34
-
35
- it 'uploaded file should have valid UUID' do
36
- file = @api.upload @url
37
- file.should be_an_instance_of Uploadcare::Api::File
38
- file.uuid.should match UUID_REGEX
39
- end
40
- end
@@ -1,87 +0,0 @@
1
- require 'spec_helper'
2
-
3
- describe Uploadcare::Parser do
4
- before :all do
5
- # https://ucarecdn.com/be4e24fb-2cad-476f-9417-ba95e3fefbf2~3/-/crop/123/-/fromat/png/
6
- @uuid = "be4e24fb-2cad-476f-9417-ba95e3fefbf2"
7
- @count = "12"
8
- @operations = "-/crop/123/-/fromat/png/"
9
- end
10
-
11
- it "should parse file uuid string" do
12
- string = "#{@uuid}"
13
-
14
- parsed = Uploadcare::Parser.parse(string)
15
-
16
- parsed.should be_kind_of(Uploadcare::Parser::File)
17
- parsed.uuid.should == @uuid
18
- parsed.count.should be_nil
19
- parsed.operations.should be_kind_of(Array)
20
- parsed.operations.should be_empty
21
- end
22
-
23
- it "should parse file cdn string without operations string" do
24
- string = "https://ucarecdn.com/#{@uuid}/"
25
-
26
- parsed = Uploadcare::Parser.parse(string)
27
-
28
- parsed.should be_kind_of(Uploadcare::Parser::File)
29
- parsed.uuid.should == @uuid
30
- parsed.count.should be_nil
31
- parsed.operations.should be_kind_of(Array)
32
- parsed.operations.should be_empty
33
- end
34
-
35
- it "should parse file cdn string with operations string" do
36
- string = "https://ucarecdn.com/#{@uuid}/#{@operations}"
37
-
38
- parsed = Uploadcare::Parser.parse(string)
39
-
40
- parsed.should be_kind_of(Uploadcare::Parser::File)
41
- parsed.uuid.should == @uuid
42
- parsed.count.should be_nil
43
- parsed.operations.should be_kind_of(Array)
44
- parsed.operations.should_not be_empty
45
- end
46
-
47
- it "should parse group uuid string" do
48
- string = "#{@uuid}~#{@count}"
49
-
50
- parsed = Uploadcare::Parser.parse(string)
51
-
52
- parsed.should be_kind_of(Uploadcare::Parser::Group)
53
- parsed.uuid.should == "#{@uuid}~#{@count}"
54
- parsed.count.should_not be_nil
55
- parsed.count.should == @count
56
- parsed.operations.should be_kind_of(Array)
57
- parsed.operations.should be_empty
58
- end
59
-
60
- it "should parse file cdn string without operations string" do
61
- string = "https://ucarecdn.com/#{@uuid}~#{@count}/"
62
-
63
- parsed = Uploadcare::Parser.parse(string)
64
-
65
- parsed.should be_kind_of(Uploadcare::Parser::Group)
66
- parsed.uuid.should == "#{@uuid}~#{@count}"
67
- parsed.count.should_not be_nil
68
- parsed.count.should == @count
69
- parsed.operations.should be_kind_of(Array)
70
- parsed.operations.should be_empty
71
- end
72
-
73
- it "should parse file cdn string with operations string" do
74
- string = "https://ucarecdn.com/#{@uuid}~#{@count}/#{@operations}"
75
-
76
- parsed = Uploadcare::Parser.parse(string)
77
-
78
- parsed.should be_kind_of(Uploadcare::Parser::Group)
79
- parsed.uuid.should == "#{@uuid}~#{@count}"
80
- parsed.count.should_not be_nil
81
- parsed.count.should == @count
82
- parsed.operations.should be_kind_of(Array)
83
- parsed.operations.should_not be_empty
84
- end
85
-
86
-
87
- end