module Bitmovin::Helpers
Constants
- S3_BUCKET_IN_URL_REGEX
- S3_BUCKET_SUBDOMAIN_REGEX
- S3_OBJECT_KEY_IN_URL_REGEX
- S3_OBJECT_KEY_SUBDOMAIN_REGEX
Public Instance Methods
Converts hash keys to string in camelCase @param subject [Hash] Hash to be converted @param first_letter_in_uppercase [Boolean] Is first letter should be uppercased
@return [Hash] converted hash with stringified keys in camelCase
# File lib/bitmovin/helpers.rb, line 49 def deep_camelize_keys(subject, first_letter_in_uppercase = false) init_value = Hash.new subject.inject init_value do |acc, props| acc[camelize(props.first.to_s, first_letter_in_uppercase).to_sym] = case props.last when Hash then deep_camelize_keys(props.last, first_letter_in_uppercase) when Array then props.last.map { |i| i.is_a?(Hash) ? deep_camelize_keys(i, first_letter_in_uppercase) : i } else props.last end acc end end
Converts hash keys to symbols in snake_case @param subject [Hash] Hash to be converted
@return [Hash] converted hash with symbolized keys in snake_case
# File lib/bitmovin/helpers.rb, line 66 def deep_underscore_keys(subject) init_value = subject.class.new if subject.is_a?(Hash) || subject.is_a?(Array) subject.inject init_value do |acc, props| acc[underscore(props.first.to_s).to_sym] = case props.last when Hash then deep_underscore_keys(props.last) when Array then props.last.map { |i| i.is_a?(Hash) ? deep_underscore_keys(i) : i } else props.last end acc end end
Extracts AWS S3 bucket name from url
@param url [String] url to AWS S3 file
@return [String] name of bucket
# File lib/bitmovin/helpers.rb, line 86 def extract_bucket(url) unescaped = URI.unescape(url) bucket = unescaped.match(S3_BUCKET_SUBDOMAIN_REGEX)[1] rescue nil bucket ||= unescaped.match(S3_BUCKET_IN_URL_REGEX)[1] rescue nil bucket end
Extracts AWS S3 file name from url
@param url [String] url to AWS S3 file
@return [String] name of file with containing folders
# File lib/bitmovin/helpers.rb, line 102 def extract_object_key(url) unescaped = URI.unescape(url) file = unescaped.match(S3_OBJECT_KEY_SUBDOMAIN_REGEX)[1] rescue nil file ||= unescaped.match(S3_OBJECT_KEY_IN_URL_REGEX)[1] rescue nil file end
Converting passed hash to be acceptable by bitmivin api
@param hash [Hash] the hash to be converted
@return [String] valid json string
# File lib/bitmovin/helpers.rb, line 21 def prepare_request_json(hash) json = deep_camelize_keys(hash) JSON.generate(json) end
Converting bitmovin api response json to ruby hash
@param json [String] json string with camelcased keys
@return [Hash] parsed api response with snakecased keys
# File lib/bitmovin/helpers.rb, line 33 def prepare_response_json(json) json = JSON.parse json if json.is_a?(Hash) deep_underscore_keys json elsif json.is_a?(Array) json.map { |ji| deep_underscore_keys(ji) } end end
Protected Instance Methods
# File lib/bitmovin/helpers.rb, line 122 def camelize(lower_case_and_underscored_word, first_letter_in_uppercase = true) if first_letter_in_uppercase lower_case_and_underscored_word.to_s.gsub(/\/(.?)/) { "::" + $1.upcase }.gsub(/(^|_)(.)/) { $2.upcase } else lower_case_and_underscored_word[0] + camelize(lower_case_and_underscored_word)[1..-1] end end
# File lib/bitmovin/helpers.rb, line 113 def headers @headers ||= { 'Content-Type' => "application/json", 'bitcodin-api-version' => 'v1', 'bitcodin-api-key' => Bitmovin.api_key } end
# File lib/bitmovin/helpers.rb, line 130 def underscore(str) str.gsub(/[A-Z\s]/) { "_#{$&.downcase}" } end