Skip to content

Commit

Permalink
Add support for visual_search Admin API
Browse files Browse the repository at this point in the history
  • Loading branch information
const-cloudinary committed Jul 9, 2023
1 parent 960dd60 commit 7082738
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 2 deletions.
13 changes: 13 additions & 0 deletions lib/cloudinary/api.rb
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,19 @@ def self.resources_by_asset_folder(asset_folder, options={})
call_api(:get, uri, params, options)
end

# Find images based on their visual content.
#
# @param [Hash] options The optional parameters.
#
# @return [Cloudinary::Api::Response]
#
# @raise [Cloudinary::Api::Error]
def self.visual_search(options = {})
uri = "resources/visual_search"
params = only(options, :image_url, :image_asset_id, :text)
call_api(:get, uri, params, options)
end

# Returns the details of the specified asset and all its derived assets.
#
# Note that if you only need details about the original asset,
Expand Down
1 change: 1 addition & 0 deletions lib/cloudinary/uploader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,7 @@ def self.build_upload_params(options)
:responsive_breakpoints => Cloudinary::Utils.generate_responsive_breakpoints_string(options[:responsive_breakpoints]),
:return_delete_token => Cloudinary::Utils.as_safe_bool(options[:return_delete_token]),
:similarity_search => options[:similarity_search],
:visual_search => Cloudinary::Utils.as_safe_bool(options[:visual_search]),
:tags => options[:tags] && Cloudinary::Utils.build_array(options[:tags]).join(","),
:timestamp => (options[:timestamp] || Time.now.to_i),
:transformation => Cloudinary::Utils.generate_transformation_string(options.clone),
Expand Down
10 changes: 10 additions & 0 deletions spec/api_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@
@api.resources(:type=>"upload", :start_at=>start_at, :direction => "asc")
end

it "should allow visual search" do
expected = {
:url => /.*\/resources\/visual_search$/,
:method => :get,
:payload => { :image_url => TEST_IMAGE_URL, :image_asset_id => test_asset_id, :text => "sample image" },
}
expect(RestClient::Request).to receive(:execute).with(deep_hash_value(expected))
@api.visual_search({ "image_url" => TEST_IMAGE_URL, "image_asset_id" => test_asset_id, "text" => "sample image" })
end

describe "structured metadata" do
matcher :have_metadata do
match do |expected|
Expand Down
7 changes: 5 additions & 2 deletions spec/utils_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -807,15 +807,18 @@

it "build_upload_params canonize booleans" do
options = { :backup => true, :use_filename => false, :colors => :true,
:image_metadata => :false, :media_metadata => true, :invalidate => 1 }
:image_metadata => :false, :media_metadata => true, :invalidate => 1,
:visual_search => true,
}
params = Cloudinary::Uploader.build_upload_params(options)
expect(Cloudinary::Api.only(params, *options.keys))
.to eq(:backup => 1,
:use_filename => 0,
:colors => 1,
:image_metadata => 0,
:media_metadata => 1,
:invalidate => 1
:invalidate => 1,
:visual_search => 1,
)
options = { :colors => "true", :exif => "false", :eager_async => "1", :media_metadata => true }
params = Cloudinary::Uploader.build_upload_params(options)
Expand Down

0 comments on commit 7082738

Please sign in to comment.