Top Level Namespace

Defined Under Namespace

Modules: CarrierWave, ImageKit, Imagekit, Imagekitio Classes: Default, ImageKitFile, ImageKitRequest, URL, Url

Constant Summary collapse

VALID_FILE_OPTIONS =
%w[path fileType tags includeFolder name limit skip]
VALID_FILE_DETAIL_OPTIONS =
["fileID"]
VALID_UPLOAD_OPTIONS =
%w[file file_name use_unique_file_name tags folder is_private_file custom_coordinates response_fields]
MANDATORY_INIT_MISSING =
{
  'message': "Missing public_key or private_key or url_endpoint during ImageKit initialization",
  help: "",
}
INVALID_TRANSFORMATION_POS =
{'message': "Invalid transformationPosition parameter",
help: "",}
INVALID_URL_GENERATION_PARAM =
{'message': "Invalid url parameter", help: ""}
INVALID_TRANSFORMATION_OPTIONS =
{
  'message': "Invalid transformation parameter options",
  help: "",
}
CACHE_PURGE_URL_MISSING =
{'message': "Missing URL parameter for this request",
help: "",}
CACHE_PURGE_STATUS_ID_MISSING =
{'message': "Missing Request ID parameter for this request",
help: "",}
FILE_ID_MISSING =
{'message': "Missing File ID parameter for this request",
help: "",}
UPDATE_DATA_MISSING =
{'message': "Missing file update data for this request",
help: "",}
UPDATE_DATA_TAGS_INVALID =
{'message': "Invalid tags parameter for this request",
help: "tags should be passed as null or an array like ['tag1', 'tag2']",}.freeze
UPDATE_DATA_COORDS_INVALID =
{'message': "Invalid custom_coordinates parameter for this request",
help: "custom_coordinates should be passed as null or a string like 'x,y,width,height'",}
LIST_FILES_INPUT_MISSING =
{
  'message': "Missing options for list files",
  help: "If you do not want to pass any parameter for listing, pass an empty object",
}
MISSING_FILE_URL =
{'message': "Missing file_url for purge_cache", help: ""}
MISSING_UPLOAD_DATA =
{'message': "Missing data for upload", help: ""}
MISSING_UPLOAD_FILE_PARAMETER =
{'message': "Missing file parameter for upload",
'help': "",}
MISSING_UPLOAD_FILENAME_PARAM =
{
  'message': "Missing fileName parameter for upload",
  'help': "",
}
INVALID_PHASH_VALUE =
{'message': "Invalid pHash value",
'help': "Both pHash strings must be valid hexadecimal numbers",}
MISSING_PHASH_VALUE =
{'message': "Missing pHash value",
'help': "Please pass two pHash values",}
UNEQUAL_STRING_LENGTH =
{'message': "Unequal pHash string length",
'help': "For distance calculation, the two pHash strings must have equal length",}
DEFAULT_TIME_DIFF =
60 * 30
SUPPORTED_TRANS =
{
  'height': "h",
  'width': "w",
  'aspect_ratio': "ar",
  'quality': "q",
  'crop': "c",
  'crop_mode': "cm",
  'x': "x",
  'y': "y",
  'focus': "fo",
  'format': "f",
  'radius': "r",
  'background': "bg",
  'border': "bo",
  'rotation': "rt",
  'blur': "bl",
  'named': "n",
  'overlay_image': "oi",
  'overlay_x': "ox",
  'overlay_y': "oy",
  'overlay_focus': "ofo",
  'overlay_height': "oh",
  'overlay_width': "ow",
  'overlay_text': "ot",
  'overlay_text_font_size': "ots",
  'overlay_text_font_family': "otf",
  'overlay_text_color': "otc",
  'overlay_alpha': "oa",
  'overlay_text_typography': "ott",
  'overlay_background': "obg",
  'overlay_image_trim': "oit",
  'progressive': "pr",
  'lossless': "lo",
  'trim': "t",
  'metadata': "md",
  'color_profile': "cp",
  'default_image': "di",
  'dpr': "dpr",
  'effect_sharpen': "e-sharpen",
  'effect_usm': "e-usm",
  'effect_contrast': "e-contrast",
  'effect_gray': "e-grayscale",
  'original': "orig",
}

Instance Method Summary collapse

Instance Method Details

#camel_to_snake(camel_word) ⇒ Object



14
15
16
17
18
19
20
21
# File 'lib/imagekit/utils/formatter.rb', line 14

def camel_to_snake(camel_word)
  # convert camel case to snake case
  camel_word.to_s.gsub(/::/, "/")
    .gsub(/([A-Z]+)([A-Z][a-z])/, '\1_\2')
    .gsub(/([a-z\d])([A-Z])/, '\1_\2')
    .tr("-", "_")
    .downcase
end

#get_authenticated_params(token, expire, private_key) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/imagekit/utils/calculation.rb', line 21

def get_authenticated_params(token, expire, private_key)
  # return authenticated param
  default_expire = DateTime.now.strftime("%s").to_i + DEFAULT_TIME_DIFF
  token ||= SecureRandom.uuid

  auth_params = {'token': token, 'expire': expire, 'signature': ""}
  unless private_key
    return nil
  end

  signature = OpenSSL::HMAC.hexdigest("SHA1", private_key, token.to_s + expire.to_s)
  auth_params[:token] = token
  auth_params[:expire] = expire || default_expire
  auth_params[:signature] = signature
  auth_params
end

#hamming_distance(first, second) ⇒ Object



11
12
13
14
15
16
17
18
19
# File 'lib/imagekit/utils/calculation.rb', line 11

def hamming_distance(first, second)
  # Calculate Hamming distance between to hex string
  unless is_valid_hex(first) && is_valid_hex(second)
    raise ArgumentError, "Both argument should be hexadecimal"
  end
  a = first.to_i(16)
  b = second.to_i(16)
  (a ^ b).to_s(2).count("1")
end

#is_valid_hex(hex_string) ⇒ Object



6
7
8
9
# File 'lib/imagekit/utils/calculation.rb', line 6

def is_valid_hex(hex_string)
  # checks if hexadecimal value is valid or not
  /^[[:xdigit:]]+$/ === hex_string
end

#request_formatter(data) ⇒ Object



23
24
25
26
27
28
29
# File 'lib/imagekit/utils/formatter.rb', line 23

def request_formatter(data)
  result = {}
  data.each do |key, val|
    result[snake_to_camel(key.to_s)] = val
  end
  result
end

#snake_to_camel(word) ⇒ Object



1
2
3
4
5
6
7
8
9
10
11
12
# File 'lib/imagekit/utils/formatter.rb', line 1

def snake_to_camel(word)
  word_list = word.split("_")
  result = []
  word_list&.each { |i|
    if i == word_list[0]
      result.push(i)
    else
      result.push(i.capitalize)
    end
  }
  result.join
end