Class: Cloudinary::Search
Direct Known Subclasses
Constant Summary collapse
- ENDPOINT =
'resources'
- SORT_BY =
:sort_by
- AGGREGATE =
:aggregate
- WITH_FIELD =
:with_field
- FIELDS =
:fields
- KEYS_WITH_UNIQUE_VALUES =
[SORT_BY, AGGREGATE, WITH_FIELD, FIELDS].freeze
- TTL =
Used for search URLs
300
Class Method Summary collapse
-
.method_missing(method_name, *arguments) ⇒ Object
implicitly generate an instance delegate the method.
Instance Method Summary collapse
-
#aggregate(value) ⇒ Cloudinary::Search
The name of a field (attribute) for which an aggregation count should be calculated and returned in the response.
-
#endpoint(endpoint) ⇒ Cloudinary::Search
Sets the API endpoint.
- #execute(options = {}) ⇒ Object
- #expression(value) ⇒ Object
-
#fields(value) ⇒ Cloudinary::Search
The list of the asset attributes to include for each asset in the response.
-
#initialize ⇒ Search
constructor
A new instance of Search.
- #max_results(value) ⇒ Object
- #next_cursor(value) ⇒ Object
-
#sort_by(field_name, dir = 'desc') ⇒ Cloudinary::Search
Sets the ‘sort_by` field.
-
#to_h ⇒ Hash
Returns the query as an hash.
-
#to_url(ttl = nil, next_cursor = nil, options = {}) ⇒ String
Creates a signed Search URL that can be used on the client side.
-
#ttl(ttl) ⇒ Cloudinary::Search
Sets the time to live of the search URL.
-
#with_field(value) ⇒ Cloudinary::Search
The name of an additional asset attribute to include for each asset in the response.
Constructor Details
#initialize ⇒ Search
Returns a new instance of Search.
12 13 14 15 16 17 18 19 20 21 22 23 |
# File 'lib/cloudinary/search.rb', line 12 def initialize @query_hash = { SORT_BY => {}, AGGREGATE => {}, WITH_FIELD => {}, FIELDS => {}, } @endpoint = self.class::ENDPOINT @ttl = self.class::TTL end |
Class Method Details
.method_missing(method_name, *arguments) ⇒ Object
implicitly generate an instance delegate the method
26 27 28 29 |
# File 'lib/cloudinary/search.rb', line 26 def self.method_missing(method_name, *arguments) instance = new instance.send(method_name, *arguments) end |
Instance Method Details
#aggregate(value) ⇒ Cloudinary::Search
The name of a field (attribute) for which an aggregation count should be calculated and returned in the response.
You can specify more than one aggregate parameter.
67 68 69 70 |
# File 'lib/cloudinary/search.rb', line 67 def aggregate(value) @query_hash[AGGREGATE][value] = value self end |
#endpoint(endpoint) ⇒ Cloudinary::Search
Sets the API endpoint.
154 155 156 157 |
# File 'lib/cloudinary/search.rb', line 154 def endpoint(endpoint) @endpoint = endpoint self end |
#execute(options = {}) ⇒ Object
116 117 118 119 120 |
# File 'lib/cloudinary/search.rb', line 116 def execute( = {}) [:content_type] = :json uri = "#{@endpoint}/search" Cloudinary::Api.call_api(:post, uri, to_h, ) end |
#expression(value) ⇒ Object
31 32 33 34 |
# File 'lib/cloudinary/search.rb', line 31 def expression(value) @query_hash[:expression] = value self end |
#fields(value) ⇒ Cloudinary::Search
The list of the asset attributes to include for each asset in the response.
88 89 90 91 92 93 |
# File 'lib/cloudinary/search.rb', line 88 def fields(value) Cloudinary::Utils.build_array(value).each do |field| @query_hash[FIELDS][field] = field end self end |
#max_results(value) ⇒ Object
36 37 38 39 |
# File 'lib/cloudinary/search.rb', line 36 def max_results(value) @query_hash[:max_results] = value self end |
#next_cursor(value) ⇒ Object
41 42 43 44 |
# File 'lib/cloudinary/search.rb', line 41 def next_cursor(value) @query_hash[:next_cursor] = value self end |
#sort_by(field_name, dir = 'desc') ⇒ Cloudinary::Search
Sets the ‘sort_by` field.
53 54 55 56 |
# File 'lib/cloudinary/search.rb', line 53 def sort_by(field_name, dir = 'desc') @query_hash[SORT_BY][field_name] = { field_name => dir } self end |
#to_h ⇒ Hash
Returns the query as an hash.
108 109 110 111 112 113 114 |
# File 'lib/cloudinary/search.rb', line 108 def to_h @query_hash.sort.each_with_object({}) do |(key, value), query| next if value.nil? || ((value.is_a?(Array) || value.is_a?(Hash)) && value.blank?) query[key] = KEYS_WITH_UNIQUE_VALUES.include?(key) ? value.values : value end end |
#to_url(ttl = nil, next_cursor = nil, options = {}) ⇒ String
Creates a signed Search URL that can be used on the client side.
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 |
# File 'lib/cloudinary/search.rb', line 129 def to_url(ttl = nil, next_cursor = nil, = {}) api_secret = [:api_secret] || Cloudinary.config.api_secret || raise(CloudinaryException, "Must supply api_secret") ttl = ttl || @ttl query = self.to_h _next_cursor = query.delete(:next_cursor) next_cursor = _next_cursor if next_cursor.nil? b64query = Base64.urlsafe_encode64(JSON.generate(query)) prefix = Cloudinary::Utils.build_distribution_domain() signature = Cloudinary::Utils.hash("#{ttl}#{b64query}#{api_secret}", :sha256, :hexdigest) next_cursor = "/#{next_cursor}" if !next_cursor.nil? && !next_cursor.empty? "#{prefix}/search/#{signature}/#{ttl}/#{b64query}#{next_cursor}" end |
#ttl(ttl) ⇒ Cloudinary::Search
Sets the time to live of the search URL.
100 101 102 103 |
# File 'lib/cloudinary/search.rb', line 100 def ttl(ttl) @ttl = ttl self end |
#with_field(value) ⇒ Cloudinary::Search
The name of an additional asset attribute to include for each asset in the response.
77 78 79 80 |
# File 'lib/cloudinary/search.rb', line 77 def with_field(value) @query_hash[WITH_FIELD][value] = value self end |