Sha256: eab8214095033ebb30bf893b7df5bd720318daaaafe2b7731b0a538d809ad3e0

Contents?: true

Size: 1.96 KB

Versions: 2

Compression:

Stored size: 1.96 KB

Contents

module Bitmovin
  class Resource
    include Bitmovin::Helpers

    class << self
      def init(path)
        @resource_path = path
      end
      attr_reader :resource_path


      def list(limit = 100, offset = 0)
        response = Bitmovin.client.get @resource_path, limit: limit, offset: offset
        Bitmovin::Helpers.result(response)['items'].map do |item|
          new(item)
        end
      end

      def find(id)
        response = Bitmovin.client.get File.join(@resource_path, id)
        new(Bitmovin::Helpers.result(response))
      end
    end

    attr_accessor :id, :name, :description, :created_at, :modified_at


    def initialize(hash = {})
      init_from_hash(hash)
    end

    def save!
      if @id
        raise BitmovinError.new(self), "Cannot save already persisted resource"
      end

      response = Bitmovin.client.post do |post|
        post.url self.class.resource_path
        post.body = collect_attributes
      end
      init_from_hash(result(response))
      self
    end

    def persisted?
      !@id.nil?
    end

    def delete!
      Bitmovin.client.delete File.join(self.class.resource_path, @id)
    end

    def inspect
      "#{self.class.name}(id: #{@id}, name: #{@name})"
    end

    private

    def init_from_hash(hash = {})
      hash.each do |name, value|
        instance_variable_set("@#{ActiveSupport::Inflector.underscore(name)}", value)
      end
    end

    def collect_attributes
      val = Hash.new
      ignored_variables = []
      if (self.respond_to?(:ignore_fields))
        ignored_variables = self.ignore_fields
      end
      instance_variables.each do |name|
        if ignored_variables.include?(name)
          next
        end
        if name == :@max_ctu_size
          val['maxCTUSize'] = instance_variable_get(name)
        else
          json_name = ActiveSupport::Inflector.camelize(name.to_s.gsub(/@/, ''), false)
          val[json_name] = instance_variable_get(name)
        end
      end
      val
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
bitmovin-ruby-0.2.0 lib/bitmovin/resource.rb
bitmovin-ruby-0.1.1 lib/bitmovin/resource.rb