Sha256: a9fa8ad1f607c7b87aa47bfdf86793635872429cf74ac33c98c84180adb992a3

Contents?: true

Size: 595 Bytes

Versions: 4

Compression:

Stored size: 595 Bytes

Contents

# encoding: UTF-8

require 'uri'

module BEncodr
  module String
    def bencode
      String.bencode(self)
    end
    
    def self.bencode(stringable)
      string = coerce(stringable)
      
      [string.bytesize, ':', string].join
    end
    
    private
    
    def self.coerce(stringable)
      if stringable.respond_to?(:to_s)
        stringable.to_s
      elsif stringable.respond_to?(:to_str)
        stringable.to_str
      else
        raise BEncodeError, "BEncodr::String.bencode can only be called on an object that provides a to_s or to_str method."
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
bencodr-3.0.2 lib/bencodr/string.rb
bencodr-3.0.1 lib/bencodr/string.rb
bencodr-2.0.1 lib/bencodr/string.rb
bencodr-3.0.0 lib/bencodr/string.rb