Sha256: 49eee233e8b2f4d01448cb6918c235d740a13de70a632a9b2bc20860055b2ff3

Contents?: true

Size: 593 Bytes

Versions: 1

Compression:

Stored size: 593 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.length, ':', 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

1 entries across 1 versions & 1 rubygems

Version Path
bencodr-2.0.0 lib/bencodr/string.rb