Sha256: f43b3ec6b27f7ab3158067f2ea2fb5b93c613c9b577657f3dfac250a16d3b2b5

Contents?: true

Size: 1.69 KB

Versions: 9

Compression:

Stored size: 1.69 KB

Contents

module Zencoder
  class Resource

    include Zencoder::Serializer

    def self.api_key
      Zencoder.api_key
    end

    def self.base_url
      Zencoder.base_url
    end

    def self.post(path, params={}, options={})
      options = options.dup
      url     = url_for(path, options)
      body    = encode(params)
      options = add_api_key_header(options)
      HTTP.post(url, body, options)
    end

    def self.put(path, params={}, options={})
      options = options.dup
      url     = url_for(path, options)
      body    = encode(params)
      options = add_api_key_header(options)
      HTTP.put(url, body, options)
    end

    def self.get(path, options={})
      options = options.dup
      url     = url_for(path, options)
      options = add_api_key_header(options)
      HTTP.get(url, options)
    end

    def self.delete(path, options={})
      options = options.dup
      url     = url_for(path)
      options = add_api_key_header(options)
      HTTP.delete(url, options)
    end


  protected

    def self.url_for(path, options={})
      File.join((options[:base_url] || base_url).to_s, path.to_s)
    end

    def self.add_api_key_header(options)
      effective_api_key = options.delete(:api_key) || api_key

      if effective_api_key
        if options[:headers]
          options[:headers] = options[:headers].dup
        else
          options[:headers] = {}
        end
        options[:headers]["Zencoder-Api-Key"] = effective_api_key
      end

      options
    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

9 entries across 9 versions & 1 rubygems

Version Path
zencoder-2.5.1 lib/zencoder/resource.rb
zencoder-2.5.0 lib/zencoder/resource.rb
zencoder-2.4.6 lib/zencoder/resource.rb
zencoder-2.4.5 lib/zencoder/resource.rb
zencoder-2.4.4 lib/zencoder/resource.rb
zencoder-2.4.3 lib/zencoder/resource.rb
zencoder-2.4.2 lib/zencoder/resource.rb
zencoder-2.4.1 lib/zencoder/resource.rb
zencoder-2.4.0 lib/zencoder/resource.rb