Class: Elasticsearch::Git::LiteBlob

Inherits:
Object
  • Object
show all
Includes:
Linguist::BlobHelper
Defined in:
lib/elasticsearch/git/repository.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(repo, raw_blob_hash) ⇒ LiteBlob

Returns a new instance of LiteBlob.



426
427
428
429
430
431
# File 'lib/elasticsearch/git/repository.rb', line 426

def initialize(repo, raw_blob_hash)
  @id = raw_blob_hash[:oid]
  @path = raw_blob_hash[:path]
  @name = @path.split("/").last
  @data = encode!(repo.lookup(@id).content)
end

Instance Attribute Details

#commit_idObject

Returns the value of attribute commit_id.



424
425
426
# File 'lib/elasticsearch/git/repository.rb', line 424

def commit_id
  @commit_id
end

#dataObject

Returns the value of attribute data.



424
425
426
# File 'lib/elasticsearch/git/repository.rb', line 424

def data
  @data
end

#idObject

Returns the value of attribute id.



424
425
426
# File 'lib/elasticsearch/git/repository.rb', line 424

def id
  @id
end

#nameObject

Returns the value of attribute name.



424
425
426
# File 'lib/elasticsearch/git/repository.rb', line 424

def name
  @name
end

#pathObject

Returns the value of attribute path.



424
425
426
# File 'lib/elasticsearch/git/repository.rb', line 424

def path
  @path
end

Instance Method Details

#encode!(message) ⇒ Object



433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
# File 'lib/elasticsearch/git/repository.rb', line 433

def encode!(message)
  return nil unless message.respond_to? :force_encoding

  # if message is utf-8 encoding, just return it
  message.force_encoding("UTF-8")
  return message if message.valid_encoding?

  # return message if message type is binary
  detect = CharlockHolmes::EncodingDetector.detect(message)
  return message.force_encoding("BINARY") if detect && detect[:type] == :binary

  # encoding message to detect encoding
  if detect && detect[:encoding]
    message.force_encoding(detect[:encoding])
  end

  # encode and clean the bad chars
  message.replace clean(message)
rescue
  encoding = detect ? detect[:encoding] : "unknown"
  "--broken encoding: #{encoding}"
end