-
Notifications
You must be signed in to change notification settings - Fork 16
Add image upload support (/image API)
#27
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
tanysheng
wants to merge
7
commits into
master
Choose a base branch
from
implement-image-api
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+137
−12
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
36a0512
Implement /image api
tanysheng 06770e5
Update readme
tanysheng a195a4d
Update spec
tanysheng de1459d
Update error message
tanysheng f48ad50
Rewind IO before upload
tanysheng d83cf60
Merge remote-tracking branch 'origin/master' into implement-image-api
tanysheng a550066
Update README
tanysheng File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| require 'spec_helper' | ||
| require 'stringio' | ||
| require 'tempfile' | ||
|
|
||
| describe 'Image API' do | ||
| let(:response) do | ||
| double( | ||
| status: 200, | ||
| body: '{"message":"Image uploaded successfully.","image_id":"test-image-id"}' | ||
| ) | ||
| end | ||
| let(:socket) { double } | ||
| let(:client) { SerpApi::Client.new(api_key: api_key) } | ||
|
|
||
| before do | ||
| allow(HTTP).to receive(:persistent).and_return(socket) | ||
| allow(response).to receive(:flush) | ||
| end | ||
|
|
||
| it 'uploads an image from a file path' do | ||
| Tempfile.create(['image', '.png']) do |image| | ||
| expect(socket).to receive(:post) do |endpoint, options| | ||
| uploaded_image = options[:form][:image] | ||
|
|
||
| expect(endpoint).to eq('/image') | ||
| expect(uploaded_image.filename).to end_with('.png') | ||
| response | ||
| end | ||
|
tanysheng marked this conversation as resolved.
|
||
|
|
||
| result = client.upload_image(image.path) | ||
|
|
||
| expect(result[:message]).to eq('Image uploaded successfully.') | ||
| expect(result[:image_id]).to eq('test-image-id') | ||
| end | ||
| end | ||
|
|
||
| it 'rewinds and uploads an image from an IO object' do | ||
| image = StringIO.new('image data') | ||
| image.read(5) | ||
|
|
||
| expect(socket).to receive(:post) do |endpoint, options| | ||
| expect(endpoint).to eq('/image') | ||
| expect(image.pos).to eq(0) | ||
| response | ||
| end | ||
|
|
||
| result = client.upload_image(image) | ||
|
|
||
| expect(result[:image_id]).to eq('test-image-id') | ||
| end | ||
|
|
||
| it 'raises an error when image is rejected' do | ||
| error_response = double( | ||
| status: 400, | ||
| body: '{"error":"Invalid image format. Supported format: jpg, jpeg, png, webp"}' | ||
| ) | ||
| allow(socket).to receive(:post).with('/image', anything).and_return(error_response) | ||
|
|
||
| expect { | ||
| client.upload_image(StringIO.new('invalid image data')) | ||
| }.to raise_error( | ||
| SerpApi::SerpApiError, | ||
| /Invalid image format\. Supported format: jpg, jpeg, png, webp/ | ||
| ) | ||
| end | ||
| end | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I originally kept a local link so when you work with the repo or it's hosted somewhere it works as usual. The
.erbversion has a full link because it's on the website. Can you explain more?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It was generated from the
.erbversion. We are actually using just the.mdon the website, it is broken atm.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, okay. Thanks for noticing.