Class: OSDb::Movie

Inherits:
Object
  • Object
show all
Defined in:
lib/osdb/movie.rb

Constant Summary collapse

CHUNK_SIZE =

in bytes

64 * 1024

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(path) ⇒ Movie

Returns a new instance of Movie.



6
7
8
# File 'lib/osdb/movie.rb', line 6

def initialize(path)
  @path = path
end

Instance Attribute Details

#pathObject (readonly)

Returns the value of attribute path.



4
5
6
# File 'lib/osdb/movie.rb', line 4

def path
  @path
end

Class Method Details

.compute_hash(path) ⇒ Object



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/osdb/movie.rb', line 21

def self.compute_hash(path)
  filesize = File.size(path)
  hash = filesize
  
  # Read 64 kbytes, divide up into 64 bits and add each
  # to hash. Do for beginning and end of file.
  File.open(path, 'rb') do |f|    
    # Q = unsigned long long = 64 bit
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      hash = hash + n & 0xffffffffffffffff # to remain as 64 bit number
    end
    
    f.seek([0, filesize - CHUNK_SIZE].max, IO::SEEK_SET)
    
    # And again for the end of the file
    f.read(CHUNK_SIZE).unpack("Q*").each do |n|
      hash = hash + n & 0xffffffffffffffff
    end
  end
  
  sprintf("%016x", hash)
end

Instance Method Details

#hashObject



10
11
12
# File 'lib/osdb/movie.rb', line 10

def hash
  @hash ||= self.class.compute_hash(path)
end

#sizeObject



14
15
16
# File 'lib/osdb/movie.rb', line 14

def size
  @size ||= File.size(path)
end