Sha256: d78fa5e7f95c359c46092b952b6b414405d7464a22843a247a94c0a71d2d71fd

Contents?: true

Size: 1.63 KB

Versions: 6

Compression:

Stored size: 1.63 KB

Contents

module Uploadcare
  module Rails
    class Group < Uploadcare::Api::Group

      # warkaround to build images urls without API request
      # builds array of image urls including operations
      def urls(operations = nil)
        (0..files_count.to_i - 1).to_a.map do |i|
          [
            @api.options[:static_url_base],
            uuid,
            'nth',
            i,
            Operations.new(operations).to_s
          ].join('/')
        end
      end

      def load_data
        load_data! unless is_loaded?
        self
      end

      alias_method :load, :load_data

      def load_data!
        set_data(@api.get("/groups/#{ uuid }/"))
        cache_data
        self
      end

      alias_method :load!, :load_data!

      def cache_data
        return unless UPLOADCARE_SETTINGS.cache_groups
        ::Rails.cache.write(cdn_url, marshal_dump)
        self
      end

      def marshal_dump
        table = @table.deep_dup.stringify_keys!

        if table['files']
          table['files'].map! do |file|
            file.marshal_dump.stringify_keys
          end
        end

        table
      end

      def to_json
        marshal_dump
      end

      def as_json(_options = {})
        marshal_dump
      end

      # override default to string method
      # for group we just will return the uuid
      def to_s
        uuid
      end

      private

      def map_files(data)
        data.stringify_keys!
        data['files'].map! do |file|
          if file.nil?
            file
          else
            Uploadcare::Rails::File.new(@api, file['uuid'], file)
          end
        end

        data
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
uploadcare-rails-1.2.1 lib/uploadcare/rails/objects/group.rb
uploadcare-rails-1.2.0 lib/uploadcare/rails/objects/group.rb
uploadcare-rails-1.2.0.pre.alpha3 lib/uploadcare/rails/objects/group.rb
uploadcare-rails-1.2.0.pre.alpha lib/uploadcare/rails/objects/group.rb
uploadcare-rails-1.1.1 lib/uploadcare/rails/objects/group.rb
uploadcare-rails-1.1.0 lib/uploadcare/rails/objects/group.rb