Sha256: ef5238577aec86ce41ebec46437d48726214130a9bb0e68f6a044dee0379c73c

Contents?: true

Size: 1.7 KB

Versions: 6

Compression:

Stored size: 1.7 KB

Contents

class Zencoder

  cattr_accessor :base_url
  cattr_accessor :api_key

  self.base_url = 'https://app.zencoder.com/api'

  def self.encode(content, format=nil)
    if content.is_a?(String)
      content
    elsif format.to_s == 'xml'
      if content.is_a?(Hash) && content.keys.size == 1
        content[content.keys.first].to_xml(:root => content.keys.first)
      else
        content.to_xml
      end
    else
      content.to_json
    end
  end

  def encode(content, format=nil)
    self.class.encode(content, format)
  end

  def self.decode(content, format=nil)
    if content.is_a?(String)
      if format.to_s == 'xml'
        Hash.from_xml(content)
      else
        ActiveSupport::JSON.decode(content)
      end
    else
      content
    end
  end

  def decode(content, format=nil)
    self.class.decode(content, format)
  end


protected

  def self.apply_api_key(params, format=nil)
    if api_key
      decoded_params = decode(params, format).with_indifferent_access

      if decoded_params[:api_request]
        decoded_params[:api_request] = decoded_params[:api_request].with_indifferent_access
      end

      if format.to_s == 'xml'
        if !(decoded_params[:api_request] && decoded_params[:api_request][:api_key])
          decoded_params[:api_request] ||= {}.with_indifferent_access
          decoded_params[:api_request][:api_key] = api_key
        end
      else
        decoded_params['api_key'] = api_key unless decoded_params['api_key']
      end

      decoded_params
    else
      params
    end
  end

  def self.merge_params(options, params)
    if options[:params]
      options[:params] = options[:params].merge(params)
      options
    else
      options.merge(:params => params)
    end
  end

end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
zencoder-2.1.14 lib/zencoder/zencoder.rb
zencoder-2.1.13 lib/zencoder/zencoder.rb
zencoder-2.1.12 lib/zencoder/zencoder.rb
zencoder-2.1.11 lib/zencoder/zencoder.rb
zencoder-2.1.10 lib/zencoder/zencoder.rb
zencoder-2.1.9 lib/zencoder/zencoder.rb