summaryrefslogtreecommitdiff
path: root/contrib/smsweb/smswsc/utils.rb
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/smsweb/smswsc/utils.rb')
-rw-r--r--contrib/smsweb/smswsc/utils.rb63
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