In: |
uri/mailto.rb
|
Parent: | Generic |
RFC2368, The mailto URL scheme
DEFAULT_PORT | = | nil |
COMPONENT | = | [ :scheme, :to, :headers |
HEADER_PATTERN | = | "(?:[^?=&]*=[^?=&]*)".freeze |
hname = *urlc hvalue = *urlc header = hname "=" hvalue | ||
HEADER_REGEXP | = | Regexp.new(HEADER_PATTERN, 'N').freeze |
MAILBOX_PATTERN | = | "(?:#{PATTERN::ESCAPED}|[^(),%?=&])".freeze |
headers = "?" header *( "&" header ) to = mailbox mailtoURL = "mailto:" [ to ] [ headers ] | ||
MAILTO_REGEXP | = | Regexp.new(" \\A (#{MAILBOX_PATTERN}*?) (?# 1: to) (?: \\? (#{HEADER_PATTERN}(?:\\{HEADER_PATTERN})*) (?# 2: headers) )? \\z ", Regexp::EXTENDED, 'N').freeze |
headers | [R] | |
to | [R] |
# File uri/mailto.rb, line 83 def self.build(args) tmp = Util::make_components_hash(self, args) if tmp[:to] tmp[:opaque] = tmp[:to] else tmp[:opaque] = '' end if tmp[:headers] tmp[:opaque] << '?' if tmp[:headers].kind_of?(Array) tmp[:opaque] << tmp[:headers].collect { |x| if x.kind_of?(Array) x[0] + '=' + x[1..-1].to_s else x.to_s end }.join('&') elsif tmp[:headers].kind_of?(Hash) tmp[:opaque] << tmp[:headers].collect { |h,v| h + '=' + v }.join('&') else tmp[:opaque] << tmp[:headers].to_s end end return super(tmp) end
# File uri/mailto.rb, line 117 def initialize(*arg) super(*arg) @to = nil @headers = [] if MAILTO_REGEXP =~ @opaque if arg[-1] self.to = $1 self.headers = $2 else set_to($1) set_headers($2) end else raise InvalidComponentError, "unrecognised opaque part for mailtoURL: #{@opaque}" end end
# File uri/mailto.rb, line 220 def to_s @scheme + ':' + if @to @to else '' end + if @headers.size > 0 '?' + @headers.collect{|x| x.join('=')}.join('&') else '' end end
# File uri/mailto.rb, line 239 def to_mailtext to = URI::unescape(@to) head = '' body = '' @headers.each do |x| case x[0] when 'body' body = URI::unescape(x[1]) when 'to' to << ', ' + URI::unescape(x[1]) else head << URI::unescape(x[0]).capitalize + ': ' + URI::unescape(x[1]) + "\n" end end return "To: #{to} #{head} #{body} " end