Sha256: 83c571df80a9acd25d321c57831abaf613691c3e5a0be21aaa144ffa65a9e9b4

Contents?: true

Size: 1.49 KB

Versions: 10

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true
require 'imgix'

module Dato
  module Local
    module FieldType
      class File
        attr_reader :path, :format, :size, :width, :height, :title, :alt

        def self.parse(upload_id, repo)
          if upload_id
            upload = repo.entities_repo.find_entity("upload", upload_id)
            new(
              upload.path,
              upload.format,
              upload.size,
              upload.width,
              upload.height,
              upload.alt,
              upload.title,
              repo.site.entity.imgix_host
            )
          end
        end

        def initialize(
          path,
          format,
          size,
          width,
          height,
          alt,
          title,
          imgix_host
        )
          @path = path
          @format = format
          @size = size
          @imgix_host = imgix_host
          @width = width
          @height = height
          @alt = alt
          @title = title
        end

        def file
          Imgix::Client.new(
            host: @imgix_host,
            secure: true,
            include_library_param: false
          ).path(path)
        end

        def url(opts = {})
          file.to_url(opts)
        end

        def to_hash(*_args)
          {
            format: format,
            size: size,
            width: width,
            height: height,
            alt: alt,
            title: title,
            url: url
          }
        end
      end
    end
  end
end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
dato-0.6.7 lib/dato/local/field_type/file.rb
dato-0.6.6 lib/dato/local/field_type/file.rb
dato-0.6.5 lib/dato/local/field_type/file.rb
dato-0.6.3 lib/dato/local/field_type/file.rb
dato-0.6.3.beta lib/dato/local/field_type/file.rb
dato-0.6.2 lib/dato/local/field_type/file.rb
dato-0.6.1 lib/dato/local/field_type/file.rb
dato-0.6.0 lib/dato/local/field_type/file.rb
dato-0.5.1 lib/dato/local/field_type/file.rb
dato-0.5.0 lib/dato/local/field_type/file.rb