lib/cloudinary/uploader.rb in cloudinary-1.6.0 vs lib/cloudinary/uploader.rb in cloudinary-1.7.0

- old
+ new

@@ -2,10 +2,12 @@ require 'rest_client' require 'json' class Cloudinary::Uploader + REMOTE_URL_REGEX = %r(^ftp:|^https?:|^s3:|^data:[^;]*;base64,([a-zA-Z0-9\/+\n=]+)$) + # @deprecated use {Cloudinary::Utils.build_eager} instead def self.build_eager(eager) Cloudinary::Utils.build_eager(eager) end @@ -23,11 +25,11 @@ :background_removal => options[:background_removal], :backup => Cloudinary::Utils.as_safe_bool(options[:backup]), :callback => options[:callback], :categorization => options[:categorization], :colors => Cloudinary::Utils.as_safe_bool(options[:colors]), - :context => Cloudinary::Utils.encode_hash(options[:context]), + :context => Cloudinary::Utils.encode_context(options[:context]), :custom_coordinates => Cloudinary::Utils.encode_double_array(options[:custom_coordinates]), :detection => options[:detection], :discard_original_filename => Cloudinary::Utils.as_safe_bool(options[:discard_original_filename]), :eager => Cloudinary::Utils.build_eager(options[:eager]), :eager_async => Cloudinary::Utils.as_safe_bool(options[:eager_async]), @@ -69,11 +71,11 @@ def self.upload(file, options={}) call_api("upload", options) do params = build_upload_params(options) if file.is_a?(Pathname) params[:file] = File.open(file, "rb") - elsif file.respond_to?(:read) || file =~ /^ftp:|^https?:|^s3:|^data:[^;]*;base64,([a-zA-Z0-9\/+\n=]+)$/ + elsif file.respond_to?(:read) || file.match(REMOTE_URL_REGEX) params[:file] = file else params[:file] = File.open(file, "rb") end [params, [:file]] @@ -87,11 +89,13 @@ public_id = options[:public_id] else public_id = public_id_or_options options = old_options end - if file.is_a?(Pathname) || !file.respond_to?(:read) + if file.match(REMOTE_URL_REGEX) + return upload(file, options.merge(:public_id => public_id)) + elsif file.is_a?(Pathname) || !file.respond_to?(:read) filename = file file = File.open(file, "rb") else filename = "cloudinaryfile" end @@ -253,16 +257,42 @@ def self.replace_tag(tag, public_ids = [], options = {}) return self.call_tags_api(tag, "replace", public_ids, options) end + def self.remove_all_tags(public_ids = [], options = {}) + return self.call_tags_api(nil, "remove_all", public_ids, options) + end + private def self.call_tags_api(tag, command, public_ids = [], options = {}) return call_api("tags", options) do { :timestamp => (options[:timestamp] || Time.now.to_i), :tag => tag, + :public_ids => Cloudinary::Utils.build_array(public_ids), + :command => command, + :type => options[:type] + } + end + end + + def self.add_context(context, public_ids = [], options = {}) + return self.call_context_api(context, "add", public_ids, options) + end + + def self.remove_all_context(public_ids = [], options = {}) + return self.call_context_api(nil, "remove_all", public_ids, options) + end + + private + + def self.call_context_api(context, command, public_ids = [], options = {}) + return call_api("context", options) do + { + :timestamp => (options[:timestamp] || Time.now.to_i), + :context => Cloudinary::Utils.encode_hash(context), :public_ids => Cloudinary::Utils.build_array(public_ids), :command => command, :type => options[:type] } end