PageRenderTime 27ms CodeModel.GetById 19ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/tools/Ruby/lib/ruby/1.8/xmlrpc/base64.rb

http://github.com/agross/netopenspace
Ruby | 81 lines | 26 code | 13 blank | 42 comment | 0 complexity | ee89ac70fb31911013730ada1ce33ee2 MD5 | raw file
 1=begin
 2= xmlrpc/base64.rb
 3Copyright (C) 2001, 2002, 2003 by Michael Neumann (mneumann@ntecs.de)
 4
 5Released under the same term of license as Ruby.
 6
 7= Classes
 8* ((<XMLRPC::Base64>))
 9
10= XMLRPC::Base64
11== Description
12This class is necessary for (('xmlrpc4r')) to determine that a string should 
13be transmitted base64-encoded and not as a raw-string. 
14You can use (({XMLRPC::Base64})) on the client and server-side as a 
15parameter and/or return-value.
16
17== Class Methods
18--- XMLRPC::Base64.new( str, state = :dec )
19    Creates a new (({XMLRPC::Base64})) instance with string ((|str|)) as the
20    internal string. When ((|state|)) is (({:dec})) it assumes that the 
21    string ((|str|)) is not in base64 format (perhaps already decoded), 
22    otherwise if ((|state|)) is (({:enc})) it decodes ((|str|)) 
23    and stores it as the internal string.
24    
25--- XMLRPC::Base64.decode( str )
26    Decodes string ((|str|)) with base64 and returns that value.
27
28--- XMLRPC::Base64.encode( str )
29    Encodes string ((|str|)) with base64 and returns that value.
30
31== Instance Methods
32--- XMLRPC::Base64#decoded
33    Returns the internal string decoded.
34
35--- XMLRPC::Base64#encoded
36    Returns the internal string encoded with base64.
37
38=end
39
40module XMLRPC
41
42class Base64
43  
44  def initialize(str, state = :dec)
45    case state
46    when :enc
47      @str = Base64.decode(str)
48    when :dec
49      @str = str
50    else
51      raise ArgumentError, "wrong argument; either :enc or :dec"
52    end
53  end
54  
55  def decoded
56    @str  
57  end
58  
59  def encoded
60    Base64.encode(@str)
61  end
62
63
64  def Base64.decode(str)
65    str.gsub(/\s+/, "").unpack("m")[0]
66  end
67
68  def Base64.encode(str)
69    [str].pack("m")
70  end
71
72end
73
74
75end # module XMLRPC
76
77
78=begin
79= History
80    $Id: base64.rb 11708 2007-02-12 23:01:19Z shyouhei $
81=end