Sha256: 085119d87805eb89976b63d7baa3b00de70ff341fbcefb9e013510cebd81fe75

Contents?: true

Size: 1.73 KB

Versions: 16

Compression:

Stored size: 1.73 KB

Contents

module Dbox
  module Utils
    def times_equal?(t1, t2)
      time_to_s(t1) == time_to_s(t2)
    end

    def time_to_s(t)
      case t
      when Time
        # matches dropbox time format
        t.utc.strftime("%a, %d %b %Y %H:%M:%S +0000")
      when String
        t
      end
    end

    def parse_time(t)
      case t
      when Time
        t
      when String
        Time.parse(t)
      end
    end

    # assumes local_path is defined
    def local_to_relative_path(path)
      if path.include?(local_path)
        path.sub(local_path, "").sub(/^\//, "")
      else
        raise BadPath, "Not a local path: #{path}"
      end
    end

    # assumes remote_path is defined
    def remote_to_relative_path(path)
      if path.include?(remote_path)
        path.sub(remote_path, "").sub(/^\//, "")
      else
        raise BadPath, "Not a remote path: #{path}"
      end
    end

    # assumes local_path is defined
    def relative_to_local_path(path)
      if path && path.length > 0
        File.join(local_path, path)
      else
        local_path
      end
    end

    # assumes remote_path is defined
    def relative_to_remote_path(path)
      if path && path.length > 0
        File.join(remote_path, path)
      else
        remote_path
      end
    end

    def calculate_hash(filepath)
      begin
        Digest::MD5.file(filepath).to_s
      rescue Errno::EISDIR
        nil
      rescue Errno::ENOENT
        nil
      end
    end

    def find_nonconflicting_path(filepath)
      proposed = filepath
      while File.exists?(proposed)
        dir, p = File.split(proposed)
        p = p.sub(/^(.*?)( \((\d+)\))?(\..*?)?$/) { "#{$1} (#{$3 ? $3.to_i + 1 : 1})#{$4}" }
        proposed = File.join(dir, p)
      end
      proposed
    end
  end
end

Version data entries

16 entries across 16 versions & 1 rubygems

Version Path
dbox-0.6.15 lib/dbox/utils.rb
dbox-0.6.14 lib/dbox/utils.rb
dbox-0.6.13 lib/dbox/utils.rb
dbox-0.6.12 lib/dbox/utils.rb
dbox-0.6.11 lib/dbox/utils.rb
dbox-0.6.10 lib/dbox/utils.rb
dbox-0.6.9 lib/dbox/utils.rb
dbox-0.6.8 lib/dbox/utils.rb
dbox-0.6.7 lib/dbox/utils.rb
dbox-0.6.6 lib/dbox/utils.rb
dbox-0.6.5 lib/dbox/utils.rb
dbox-0.6.4 lib/dbox/utils.rb
dbox-0.6.3 lib/dbox/utils.rb
dbox-0.6.2 lib/dbox/utils.rb
dbox-0.6.1 lib/dbox/utils.rb
dbox-0.6.0 lib/dbox/utils.rb