Skip to content

Commit 8eedad9

Browse files
committed
refactored and test fixed
1 parent 6b89402 commit 8eedad9

File tree

4 files changed

+25
-14
lines changed

4 files changed

+25
-14
lines changed

lib/imagekitio/api_service/file.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,19 @@ def upload(file: nil, file_name: nil, **options)
3333
raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file
3434
raise ArgumentError, constants.MISSING_UPLOAD_FILE_PARAMETER unless file_name
3535

36-
content_type = options.delete(:content_type)
36+
content_type = options.delete(:content_type) || ''
3737
options = format_to_json(options, :extensions, Array)
3838
options = format_to_json(options, :custom_metadata, Hash)
3939
options = validate_upload_options(options || {})
4040
if options.is_a?(FalseClass)
4141
raise ArgumentError, "Invalid Upload option"
4242
else
4343
headers = @req_obj.create_headers
44-
if(content_type && image_format?(content_type))
45-
payload = { multipart: true, file: file, fileName: file_name }.merge(options)
46-
else
47-
payload = {
48-
'file' => ::UploadIO.new(file, content_type, file_name),
49-
'fileName' => file_name
50-
}
51-
end
44+
payload = {
45+
multipart: true,
46+
file: file.is_a?(String) ? file : ::UploadIO.new(file, content_type, file_name),
47+
fileName: file_name
48+
}
5249
payload.merge!(options)
5350
url = "#{constants.BASE_URL}#{constants.UPLOAD}"
5451
@req_obj.request("post", url, headers, payload)

lib/imagekitio/request.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ def auth_headers
3434
# request method communicates with server
3535
def request(method, url, headers = create_headers, payload = nil)
3636
headers ||= create_headers
37-
response = {response: nil, error: nil}
37+
response = {}
3838
begin
3939
if(method.downcase.to_sym == :post)
4040
uri = URI.parse(url)
@@ -43,7 +43,7 @@ def request(method, url, headers = create_headers, payload = nil)
4343
req = Net::HTTP::Post::Multipart.new uri.path, payload, headers
4444
resp = http.request(req)
4545
if resp.code.to_i == 400
46-
raise RestClient::ExceptionWithResponse, OpenStruct.new(http_code: 400, body: resp.body)
46+
raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 400, body: resp.body)
4747
end
4848
else
4949
resp = RestClient::Request.new(method: method,
@@ -56,14 +56,14 @@ def request(method, url, headers = create_headers, payload = nil)
5656
if (content_type.include? "application/json")
5757
response[:response] = JSON.parse(resp.body.to_s)
5858
else
59-
raise =RestClient::ExceptionWithResponse
59+
raise RestClient::ExceptionWithResponse, OpenStruct.new(code: 404, body: resp.body)
6060
end
6161
elsif resp.code.to_i == 204
6262
response[:response] = {'success': true}
6363
end
6464

6565
rescue RestClient::ExceptionWithResponse => err
66-
response[:error] = if err.http_code == 404
66+
response[:error] = if err.http_code.to_i == 404
6767
{'message': err.response.to_s}
6868
else
6969
err.response.is_a?(OpenStruct) ? JSON.parse(err.response.body) : JSON.parse(err.response)

test/imagekit/api_service/file_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@
7676
file = open("test/imagekit/dummy_data/sample.jpg", "rb")
7777
upload = SUT.upload(file: file, file_name: "my_file_name")
7878
expect(@ac[:payload].keys).to eq([:multipart, :file, :fileName])
79-
expect(@ac[:payload][:file]).to eq(file)
79+
expect(@ac[:payload][:file].io).to eq(file)
8080
expect(@ac[:payload][:fileName]).to eq('my_file_name')
8181
expect(upload[:code]).to eq(200)
8282

test/imagekit/request_test.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,21 @@
4949

5050
it "test_request_method_204" do
5151
response = @request_obj.request(:get, "https://www.example204.com")
52+
expect(response[:response]).to have_key(:success)
53+
expect(response).to_not have_key(:error)
54+
end
55+
56+
it 'test_request_method_non_JSON_fail_with_post' do
57+
stub_request(:post, 'https://www.exampleservererror/upload').to_return(status: 400, body: '{"message": "Server failed"}', headers: {content_type: 'application/json'})
58+
response = @request_obj.request(:post, 'https://www.exampleservererror/upload', nil, [])
5259
expect(response).to have_key(:error)
5360
end
61+
62+
it 'test_request_method_JSON_success_with_post' do
63+
stub_request(:post, 'https://www.exampleservererror/upload').to_return(status: 200, body: '{"id": "1"}', headers: {content_type: 'application/json'})
64+
response = @request_obj.request(:post, 'https://www.exampleservererror/upload', nil, [])
65+
expect(response).to_not have_key(:error)
66+
expect(response).to have_key(:response)
67+
end
5468
end
5569
end

0 commit comments

Comments
 (0)