Sha256: cab52fb082bc12ee6fb0357b6defd8642654799390ed8d7c613e44ac4d6341c1

Contents?: true

Size: 965 Bytes

Versions: 4

Compression:

Stored size: 965 Bytes

Contents

class Findr::Encoder

  class String
    def initialize( other_coding )
      @other_coding = other_coding.split(',').map {|coding| Encoding.find(coding)}
    end

    # Encodes given +string+ from +@other_coding+ to utf8.
    def decode( string )
      coding = nil
      have_valid_coding = @other_coding.any? do |c|
        string.force_encoding(c)
        coding = c
        string.valid_encoding?
      end
      fail Error.new("No valid coding given.") unless have_valid_coding
      return [string.encode('UTF-8'), coding.to_s]
    rescue
      raise Error, "Error when decoding from '#{@other_coding}' into 'UTF-8': #{$!}"
    end

    # Encodes given utf8 +string+ into +coding+.
    def encode( string, coding )
      return string.encode(coding)
    rescue
      raise Error, "Error when encoding from 'UTF-8' into '#{coding}'."
    end

    # Returns a list of valid encodings
    def self.list
      return Encoding.list.map(&:to_s)
    end
  end

end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
findr-0.0.8.pre3 lib/findr/encoder/string.rb
findr-0.0.8.pre2 lib/findr/encoder/string.rb
findr-0.0.8.pre1 lib/findr/encoder/string.rb
findr-0.0.7 lib/findr/encoder/string.rb