Skip to content

Commit b6f084b

Browse files
author
RTLcoil
authored
Add support for metadata parameter in resources Admin APIs
1 parent 42364b3 commit b6f084b

File tree

2 files changed

+73
-5
lines changed

2 files changed

+73
-5
lines changed

lib/cloudinary/api.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -34,25 +34,25 @@ def self.resources(options={})
3434
type = options[:type]
3535
uri = "resources/#{resource_type}"
3636
uri += "/#{type}" unless type.blank?
37-
call_api(:get, uri, only(options, :next_cursor, :max_results, :prefix, :tags, :context, :moderations, :direction, :start_at), options)
37+
call_api(:get, uri, only(options, :next_cursor, :max_results, :prefix, :tags, :context, :moderations, :direction, :start_at, :metadata), options)
3838
end
3939

4040
def self.resources_by_tag(tag, options={})
4141
resource_type = options[:resource_type] || "image"
4242
uri = "resources/#{resource_type}/tags/#{tag}"
43-
call_api(:get, uri, only(options, :next_cursor, :max_results, :tags, :context, :moderations, :direction), options)
43+
call_api(:get, uri, only(options, :next_cursor, :max_results, :tags, :context, :moderations, :direction, :metadata), options)
4444
end
4545

4646
def self.resources_by_moderation(kind, status, options={})
4747
resource_type = options[:resource_type] || "image"
4848
uri = "resources/#{resource_type}/moderations/#{kind}/#{status}"
49-
call_api(:get, uri, only(options, :next_cursor, :max_results, :tags, :context, :moderations, :direction), options)
49+
call_api(:get, uri, only(options, :next_cursor, :max_results, :tags, :context, :moderations, :direction, :metadata), options)
5050
end
5151

5252
def self.resources_by_context(key, value=nil, options={})
5353
resource_type = options[:resource_type] || "image"
5454
uri = "resources/#{resource_type}/context"
55-
params = only(options, :next_cursor, :max_results, :tags, :context, :moderations, :direction,:key,:value)
55+
params = only(options, :next_cursor, :max_results, :tags, :context, :moderations, :direction, :key, :value, :metadata)
5656
params[:key] = key
5757
params[:value] = value
5858
call_api(:get, uri, params, options)

spec/api_spec.rb

Lines changed: 69 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,22 +11,38 @@
1111
TEST_TRANSFOMATION = "c_scale,w_#{TEST_WIDTH}"
1212
PUBLIC_ID_BACKUP_1 = "#{prefix}backup_1#{Time.now.to_i}"
1313
PUBLIC_ID_BACKUP_2 = "#{prefix}backup_2#{Time.now.to_i}"
14+
METADATA_EXTERNAL_ID = "#{prefix}_metadata_external_id_#{UNIQUE_TEST_ID}"
15+
METADATA_DEFAULT_VALUE = "#{prefix}_metadata_default_value_#{UNIQUE_TEST_ID}"
16+
UNIQUE_CONTEXT_KEY = "#{prefix}_context_key_#{UNIQUE_TEST_ID}"
17+
UNIQUE_CONTEXT_VALUE = "#{prefix}_context_value_#{UNIQUE_TEST_ID}"
18+
UNIQUE_CONTEXT = "#{UNIQUE_CONTEXT_KEY}=#{UNIQUE_CONTEXT_VALUE}"
19+
UNIQUE_TEST_TAG_TO_ONE_IMAGE_ASSET = "#{prefix}_unique_tag_to_one_image_asset_#{UNIQUE_TEST_ID}"
1420

1521
test_id_1 = "#{prefix}_1"
1622
test_id_2 = "#{prefix}_2"
1723
test_id_3 = "#{prefix}_3"
1824
test_key = "test_key_#{SUFFIX}"
19-
before(:all) do
2025

26+
before(:all) do
2127
@api = Cloudinary::Api
28+
29+
@api.add_metadata_field(
30+
:external_id => METADATA_EXTERNAL_ID,
31+
:label => METADATA_EXTERNAL_ID,
32+
:type => "string",
33+
:default_value => METADATA_DEFAULT_VALUE
34+
)
35+
2236
Cloudinary::Uploader.upload(TEST_IMG, :public_id => test_id_1, :tags => [TEST_TAG, TIMESTAMP_TAG], :context => "key=value", :eager =>[:width =>TEST_WIDTH, :crop =>:scale])
2337
Cloudinary::Uploader.upload(TEST_IMG, :public_id => test_id_2, :tags => [TEST_TAG, TIMESTAMP_TAG], :context => "key=value", :eager =>[:width =>TEST_WIDTH, :crop =>:scale])
2438
Cloudinary::Uploader.upload(TEST_IMG, :public_id => test_id_3, :tags => [TEST_TAG, TIMESTAMP_TAG], :context => "key=value", :eager =>[:width =>TEST_WIDTH, :crop =>:scale])
2539
Cloudinary::Uploader.upload(TEST_IMG, :public_id => test_id_1, :tags => [TEST_TAG, TIMESTAMP_TAG], :context => "#{test_key}=test", :eager =>[:width =>TEST_WIDTH, :crop =>:scale])
2640
Cloudinary::Uploader.upload(TEST_IMG, :public_id => test_id_3, :tags => [TEST_TAG, TIMESTAMP_TAG], :context => "#{test_key}=tasty", :eager =>[:width =>TEST_WIDTH, :crop =>:scale])
41+
Cloudinary::Uploader.upload(TEST_IMG, :tags => [UNIQUE_TEST_TAG_TO_ONE_IMAGE_ASSET, TEST_TAG, TIMESTAMP_TAG], :context => UNIQUE_CONTEXT, :moderation => :manual)
2742
end
2843

2944
after(:all) do
45+
@api.delete_metadata_field(METADATA_EXTERNAL_ID)
3046
# in addition to "cleanup" context
3147
unless Cloudinary.config.keep_test_products
3248
up = Cloudinary::Api.upload_presets max_results: 500
@@ -114,6 +130,58 @@
114130
@api.resources(:type=>"upload", :start_at=>start_at, :direction => "asc")
115131
end
116132

133+
describe "structured metadata" do
134+
matcher :have_metadata do
135+
match do |expected|
136+
expect(expected["resources"]).to be_present
137+
138+
expected["resources"].each do |resource|
139+
expect(resource).to have_key("metadata")
140+
end
141+
end
142+
143+
match_when_negated do |expected|
144+
expect(expected["resources"]).to be_present
145+
146+
expected["resources"].each do |resource|
147+
expect(resource).to_not have_key("metadata")
148+
end
149+
end
150+
end
151+
152+
it "should return structured metadata in the response of the resources API response" do
153+
result = @api.resources(:prefix => test_id_1, :type => "upload", :metadata => true)
154+
expect(result).to have_metadata
155+
156+
result = @api.resources(:prefix => test_id_1, :type => "upload", :metadata => false)
157+
expect(result).to_not have_metadata
158+
end
159+
160+
it "should return structured metadata in the response of the resources by tag API" do
161+
result = @api.resources_by_tag(UNIQUE_TEST_TAG_TO_ONE_IMAGE_ASSET, :metadata => true)
162+
expect(result).to have_metadata
163+
164+
result = @api.resources_by_tag(UNIQUE_TEST_TAG_TO_ONE_IMAGE_ASSET, :metadata => false)
165+
expect(result).to_not have_metadata
166+
end
167+
168+
it "should return structured metadata in the response of the resources by context API" do
169+
result = @api.resources_by_context(UNIQUE_CONTEXT_KEY, UNIQUE_CONTEXT_VALUE, :metadata => true)
170+
expect(result).to have_metadata
171+
172+
result = @api.resources_by_context(UNIQUE_CONTEXT_KEY, UNIQUE_CONTEXT_VALUE, :metadata => false)
173+
expect(result).to_not have_metadata
174+
end
175+
176+
it "should return structured metadata in the response of the resources by moderation API" do
177+
result = @api.resources_by_moderation(:manual, :pending, :metadata => true)
178+
expect(result).to have_metadata
179+
180+
result = @api.resources_by_moderation(:manual, :pending, :metadata => false)
181+
expect(result).to_not have_metadata
182+
end
183+
end
184+
117185
describe ":direction" do
118186

119187
it "should accept a string 'desc' and 'asc'" do

0 commit comments

Comments
 (0)