Module: Bitmovin::Helpers

Included in:
EncodingProfile, EncodingProfile, Input, Input, Job, Job, Output, Output, TransferJob, TransferJob
Defined in:
lib/bitmovin/helpers.rb

Constant Summary collapse

S3_BUCKET_SUBDOMAIN_REGEX =
/^https?:\/\/(.*)\.s3/.freeze
S3_BUCKET_IN_URL_REGEX =
/^https?:\/\/s3\.amazonaws\.com\/([\w\-\_]+)\//.freeze
S3_OBJECT_KEY_SUBDOMAIN_REGEX =
/^https?:\/\/(?:.*)\.s3\.amazonaws\.com\/(.*)/.freeze
S3_OBJECT_KEY_IN_URL_REGEX =
/^https?:\/\/s3\.amazonaws\.com\/[\w\-\_]+\/(.*)/.freeze

Instance Method Summary collapse

Instance Method Details

#deep_camelize_keys(subject, first_letter_in_uppercase = false) ⇒ Hash

Converts hash keys to string in camelCase

Parameters:

  • subject (Hash)

    Hash to be converted

  • first_letter_in_uppercase (Boolean) (defaults to: false)

    Is first letter should be uppercased

Returns:

  • (Hash)

    converted hash with stringified keys in camelCase



49
50
51
52
53
54
55
56
57
58
59
# 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

#deep_underscore_keys(subject) ⇒ Hash

Converts hash keys to symbols in snake_case

Parameters:

  • subject (Hash)

    Hash to be converted

Returns:

  • (Hash)

    converted hash with symbolized keys in snake_case



66
67
68
69
70
71
72
73
74
75
76
# 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

#extract_bucket(url) ⇒ String

Extracts AWS S3 bucket name from url

Parameters:

  • url (String)

    url to AWS S3 file

Returns:

  • (String)

    name of bucket



86
87
88
89
90
91
92
93
# 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

#extract_object_key(url) ⇒ String

Extracts AWS S3 file name from url

Parameters:

  • url (String)

    url to AWS S3 file

Returns:

  • (String)

    name of file with containing folders



102
103
104
105
106
107
108
109
# 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

#prepare_request_json(hash) ⇒ String

Converting passed hash to be acceptable by bitmivin api

Parameters:

  • hash (Hash)

    the hash to be converted

Returns:

  • (String)

    valid json string



21
22
23
24
# File 'lib/bitmovin/helpers.rb', line 21

def prepare_request_json(hash)
  json = deep_camelize_keys(hash)
  JSON.generate(json)
end

#prepare_response_json(json) ⇒ Hash

Converting bitmovin api response json to ruby hash

Parameters:

  • json (String)

    json string with camelcased keys

Returns:

  • (Hash)

    parsed api response with snakecased keys



33
34
35
36
37
38
39
40
41
# 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