diff options
author | James Maki <jmaki@multitech.com> | 2010-05-10 14:26:36 -0500 |
---|---|---|
committer | James Maki <jmaki@multitech.com> | 2010-05-10 14:26:36 -0500 |
commit | 9ba8adf164b316a3d6914b3b187f371f6e19066d (patch) | |
tree | cc4579c4141b42ed51cd25a4beb1ae733d8f03ac /contrib/smsweb/smswsc/utils.rb | |
parent | 088fee0dd65a2d11dfac58afec8871b3a3cfc376 (diff) | |
download | sms-utils-9ba8adf164b316a3d6914b3b187f371f6e19066d.tar.gz sms-utils-9ba8adf164b316a3d6914b3b187f371f6e19066d.tar.bz2 sms-utils-9ba8adf164b316a3d6914b3b187f371f6e19066d.zip |
Add sms web service example0.0.3
Diffstat (limited to 'contrib/smsweb/smswsc/utils.rb')
-rw-r--r-- | contrib/smsweb/smswsc/utils.rb | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/contrib/smsweb/smswsc/utils.rb b/contrib/smsweb/smswsc/utils.rb new file mode 100644 index 0000000..fa07efd --- /dev/null +++ b/contrib/smsweb/smswsc/utils.rb @@ -0,0 +1,63 @@ +require 'cgi' + +class Time + # try rfc3339 + # try sql + # try rfc822 + def self.rfcdate(str) + if str =~ /\A(\d{4})-(\d{2})-(\d{2})[T ](\d{2})\:(\d{2})\:(\d{2})/ + return Time.utc($1.to_i, $2.to_i, $3.to_i, $4.to_i, $5.to_i, $6.to_i).localtime + elsif str =~ /\A\w{3}, (\d{1,2}) (\w{3}) (\d{4}) (\d{2})\:(\d{2})\:(\d{2})/ + return Time.utc($3.to_i, $2, $1.to_i, $4.to_i, $5.to_i, $6.to_i).localtime + else + return nil + end + end +end + +class Array + def urlpath + args, atoms = self.flatten.partition { |a| a.is_a?(Hash) } + args = args.flatten.inject { |s, v| s.merge!(v) } + + front = atoms.join('/').squeeze('/') + + if args + rear = args.inject('?') { |s, (k, v)| + s << CGI::escape(k.to_s) + "=" + CGI::escape(v.to_s) + ";" + } [0 .. -2] + + front + rear + else + front + end + end +end + +module SMSWSC + module Utils + + # Assemble a reference from arguments. + # + # ref("/path", "to", "it", {:q => "test"}) => "/path/to/it?q=test" + + def ref(*atoms) + args, atoms = atoms.flatten.partition{|a| a.is_a?(Hash) } + args = args.flatten.inject { |s, v| s.merge!(v) } + + front = atoms.join('/').squeeze('/') + + if args + rear = args.inject('?') { |s, (k, v)| + s << CGI::escape(k.to_s) + "=" + CGI::escape(v.to_s) + ";" + } [0 .. -2] + + front + rear + else + front + end + end + module_function :ref + + end +end |