Sha256: 53da7d4c021b5840b86d4ca212b6e59c6fa5cc1436ddabc9427b5803953d4ed4

Contents?: true

Size: 1.98 KB

Versions: 1

Compression:

Stored size: 1.98 KB

Contents

require 'pry'
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

1 entries across 1 versions & 1 rubygems

Version Path
bitmovin-ruby-0.1.0 lib/bitmovin/resource.rb