Sha256: 4aea5e54205c8c0c2576fae62c1535bce5bef189ecfb58f68d84080f72defd81

Contents?: true

Size: 1.87 KB

Versions: 20

Compression:

Stored size: 1.87 KB

Contents

module Geminabox
  module Proxy
    class FileHandler

      attr_reader :file_name

      def initialize(file_name)
        @file_name = file_name
        ensure_destination_exists
      end

      def local_path
        File.expand_path(file_name, root_path)
      end

      def root_path
        Geminabox.data
      end

      def local_file_exists?
        file_exists? local_path
      end

      def proxy_file_exists?
        file_exists? proxy_path
      end

      def proxy_path
        File.expand_path(file_name, proxy_folder_path)
      end

      def file_exists?(path)
        File.exist? path
      end

      def proxy_folder_path
        File.join(root_path, proxy_folder_name)
      end

      def proxy_folder_name
        'proxy'
      end

      def remote_content
        Geminabox.http_adapter.get_content(remote_url).force_encoding(encoding)
      rescue
        return nil if Geminabox.allow_remote_failure
        raise GemStoreError.new(500, "Unable to get content from #{remote_url}")
      end

      def remote_url
        URI.join(Geminabox.ruby_gems_url, file_name)
      end

      def local_content
        File.read(local_path).force_encoding(encoding)
      end

      private
      def encoding
        "UTF-8"
      end

      def ensure_destination_exists
        create_local_folder unless local_folder_exists?
        create_proxy_folder unless proxy_folder_exists?
      end

      def proxy_file_folder
        File.dirname proxy_path
      end

      def proxy_folder_exists?
        Dir.exist?(proxy_file_folder)
      end

      def create_proxy_folder
        FileUtils.mkdir_p(proxy_file_folder)
      end

      def local_file_folder
        File.dirname local_path
      end

      def local_folder_exists?
        Dir.exist?(local_file_folder)
      end

      def create_local_folder
        FileUtils.mkdir_p(local_file_folder)
      end

    end
  end
end

Version data entries

20 entries across 20 versions & 1 rubygems

Version Path
geminabox-1.4.1 lib/geminabox/proxy/file_handler.rb
geminabox-1.4.0 lib/geminabox/proxy/file_handler.rb
geminabox-1.3.1 lib/geminabox/proxy/file_handler.rb
geminabox-1.3.0 lib/geminabox/proxy/file_handler.rb
geminabox-1.2.0 lib/geminabox/proxy/file_handler.rb
geminabox-1.1.1 lib/geminabox/proxy/file_handler.rb
geminabox-1.1.0 lib/geminabox/proxy/file_handler.rb
geminabox-1.0.1 lib/geminabox/proxy/file_handler.rb
geminabox-1.0.0 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.15 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.14 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.13 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.11 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.10 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.9 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.8 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.7 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.6 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.5 lib/geminabox/proxy/file_handler.rb
geminabox-0.13.4 lib/geminabox/proxy/file_handler.rb