lib/bencode.rb in bencode-0.3.1 vs lib/bencode.rb in bencode-0.3.2

- old
+ new

@@ -3,27 +3,27 @@ # Bencode is a Ruby implementation of the Bencode data serialization # format used in the BitTorrent protocol. # # == Synopsis # -# Bencoding (pronounced _bee-encode_) is a simple protocol, consiting of +# Bencoding (pronounced <i>bee-encode</i>) is a simple protocol, consisting of # only 4 value types. # # === Integers # -# An integer is encoded an _i_ followed by the numeral itself, followed +# An integer is encoded as an _i_ followed by the numeral itself, followed # by an _e_. Leading zeros are not allowed. Negative values are prefixed # with a minus sign. # # 42.bencode #=> "i42e" # -2.bencode #=> "i-2e" # 0.bencode #=> "i0e" # # === Strings # -# Strings are a sequence of zero or more bytes. It is encoded as -# _<length>:<contents>_, where _length_ is the lenth of _contents_. _length_ +# Strings are sequences of zero or more bytes. They are encoded as +# <i><length>:<contents></i>, where _length_ is the length of _contents_. _length_ # must be non-negative. # # "".bencode #=> "0:" # "foo".bencode #=> "3:foo" # @@ -34,13 +34,13 @@ # # [1, 2, 3].bencode #=> "li1ei2ei3ee" # # === Dictionaries # -# Dictionaries are encoded as _d<contents>e_, where _contents_ is a sequence -# of keys and values. Each value is immediately preceded by a key. Keys must -# be strings, and will appear in lexicographical order. +# Dictionaries are encoded as _d_ followed by a sequence of key-value pairs, followed by _e_. +# Each value must be immediately preceded by a key. Keys must be strings, and must appear in +# lexicographical order. # # {"foo" => 3, "bar" => 1, "baz" => 2}.bencode # #=> "d3:bari1e3:bazi2e3:fooi3ee" # # @@ -65,18 +65,20 @@ # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR # A PARTICULAR PURPOSE. See the GNU General Public License for more details. # class Object + # # Raises an exception. Subclasses of Object must themselves # define meaningful #bencode methods. def bencode raise BencodeError, self.class end end class Integer + # # Bencodes the Integer object. Bencoded integers are represented # as +ixe+, where +x+ is the integer with an optional # hyphen prepended, indicating negativity. # # 42.bencode #=> "i42e" @@ -85,30 +87,33 @@ "i#{self}e" end end class String + # # Bencodes the String object. Bencoded strings are represented - # as +x:y+, where +y+ is the string and x is the length of the + # as +x+:+y+, where +y+ is the string and +x+ is the length of the # string. # # "foo".bencode #=> "3:foo" # "".bencode #=> "0:" # def bencode "#{length}:#{self}" end + # # Bdecodes the String object and returns the data serialized # through bencoding. # # "li1ei2ei3ee".bdecode #=> [1, 2, 3] # def bdecode Bencode.load(self) end + # # Tests whether the String object is a valid bencoded string. def bencoded? bdecode rescue BdecodeError false @@ -116,46 +121,48 @@ true end end class Array + # # Bencodes the Array object. Bencoded arrays are represented as - # +lxe+, where x is zero or more bencoded objects. + # +lxe+, where +x+ is zero or more bencoded objects. # # [1, "foo"].bencode #=> "li1e3:fooe" # def bencode "l#{map{|obj| obj.bencode}.join('') }e" end end class Hash + # # Bencodes the Hash object. Bencoded hashes are represented as - # +dxe+, where x is zero or a power of two bencoded objects. + # +dxe+, where +x+ is zero or a power of two bencoded objects. # each key is immediately followed by its associated value. # All keys must be strings. The keys of the bencoded hash will # be in lexicographical order. def bencode pairs = sort.map{|key, val| [key.to_str.bencode, val.bencode]} "d#{pairs.join('')}e" end end class IO - def self.bdecode(*args) - new(*args).bdecode + def self.bdecode(filename) + open(filename, 'r').bdecode end - def self.bencode(*args) - new(*args).bencode + def self.bencode(filename) + open(filename, 'r').bencode end def bdecode read.chomp.bdecode end def bencode - read.chomp.bdecode + read.chomp.bencode end end class BencodeError < StandardError def initialize(object_class = nil) # :nodoc: