Sha256: cbff59d29ebac2cec2db8bbf14aabdbf50689186564a6ad3890a0cc3069354ae

Contents?: true

Size: 1.84 KB

Versions: 4

Compression:

Stored size: 1.84 KB

Contents

module Zencoder
  class Base

    def self.api_key
      Zencoder.api_key
    end

    def self.base_url
      Zencoder.base_url
    end

    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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
zencoder-2.3.3 lib/zencoder/base.rb
zencoder-2.3.2 lib/zencoder/base.rb
zencoder-2.3.1 lib/zencoder/base.rb
zencoder-2.3.0 lib/zencoder/base.rb