| Class | URI::MailTo |
| In: |
lib/uri/mailto.rb
|
| Parent: | Generic |
RFC2368, The mailto URL scheme
| DEFAULT_PORT | = | nil |
| COMPONENT | = | [ :scheme, :to, :headers ].freeze |
Creates a new URI::MailTo object from components, with syntax checking.
Components can be provided as an Array or Hash. If an Array is used, the components must be supplied as [to, headers].
If a Hash is used, the keys are the component names preceded by colons.
The headers can be supplied as a pre-encoded string, such as "subject=subscribe&cc=address", or as an Array of Arrays like [[‘subject’, ‘subscribe’], [‘cc’, ‘address’]]
Examples:
require 'uri'
m1 = URI::MailTo.build(['joe@example.com', 'subject=Ruby'])
puts m1.to_s -> mailto:joe@example.com?subject=Ruby
m2 = URI::MailTo.build(['john@example.com', [['Subject', 'Ruby'], ['Cc', 'jack@example.com']]])
puts m2.to_s -> mailto:john@example.com?Subject=Ruby&Cc=jack@example.com
m3 = URI::MailTo.build({:to => 'listman@example.com', :headers => [['subject', 'subscribe']]})
puts m3.to_s -> mailto:listman@example.com?subject=subscribe
# File lib/uri/mailto.rb, line 88
88: def self.build(args)
89: tmp = Util::make_components_hash(self, args)
90:
91: if tmp[:to]
92: tmp[:opaque] = tmp[:to]
93: else
94: tmp[:opaque] = ''
95: end
96:
97: if tmp[:headers]
98: tmp[:opaque] << '?'
99:
100: if tmp[:headers].kind_of?(Array)
101: tmp[:opaque] << tmp[:headers].collect { |x|
102: if x.kind_of?(Array)
103: x[0] + '=' + x[1..-1].to_s
104: else
105: x.to_s
106: end
107: }.join('&')
108:
109: elsif tmp[:headers].kind_of?(Hash)
110: tmp[:opaque] << tmp[:headers].collect { |h,v|
111: h + '=' + v
112: }.join('&')
113:
114: else
115: tmp[:opaque] << tmp[:headers].to_s
116: end
117: end
118:
119: return super(tmp)
120: end
Creates a new URI::MailTo object from generic URL components with no syntax checking.
This method is usually called from URI::parse, which checks the validity of each component.
# File lib/uri/mailto.rb, line 131
131: def initialize(*arg)
132: super(*arg)
133:
134: @to = nil
135: @headers = []
136:
137: if MAILTO_REGEXP =~ @opaque
138: if arg[-1]
139: self.to = $1
140: self.headers = $2
141: else
142: set_to($1)
143: set_headers($2)
144: end
145:
146: else
147: raise InvalidComponentError,
148: "unrecognised opaque part for mailtoURL: #{@opaque}"
149: end
150: end
# File lib/uri/mailto.rb, line 206
206: def headers=(v)
207: check_headers(v)
208: set_headers(v)
209: v
210: end
Returns the RFC822 e-mail text equivalent of the URL, as a String.
Example:
require 'uri'
uri = URI.parse("mailto:ruby-list@ruby-lang.org?Subject=subscribe&cc=myaddr")
uri.to_mailtext
# => "To: ruby-list@ruby-lang.org\nSubject: subscribe\nCc: myaddr\n\n\n"
# File lib/uri/mailto.rb, line 241
241: def to_mailtext
242: to = URI::unescape(@to)
243: head = ''
244: body = ''
245: @headers.each do |x|
246: case x[0]
247: when 'body'
248: body = URI::unescape(x[1])
249: when 'to'
250: to << ', ' + URI::unescape(x[1])
251: else
252: head << URI::unescape(x[0]).capitalize + ': ' +
253: URI::unescape(x[1]) + "\n"
254: end
255: end
256:
257: return "To: #{to}
258: #{head}
259: #{body}
260: "
261: end
# File lib/uri/mailto.rb, line 212
212: def to_s
213: @scheme + ':' +
214: if @to
215: @to
216: else
217: ''
218: end +
219: if @headers.size > 0
220: '?' + @headers.collect{|x| x.join('=')}.join('&')
221: else
222: ''
223: end +
224: if @fragment
225: '#' + @fragment
226: else
227: ''
228: end
229: end
# File lib/uri/mailto.rb, line 196
196: def set_headers(v)
197: @headers = []
198: if v
199: v.scan(HEADER_REGEXP) do |x|
200: @headers << x.split(/=/o, 2)
201: end
202: end
203: end
# File lib/uri/mailto.rb, line 182
182: def check_headers(v)
183: return true unless v
184: return true if v.size == 0
185:
186: if OPAQUE !~ v ||
187: /\A(#{HEADER_PATTERN}(?:\&#{HEADER_PATTERN})*)\z/o !~ v
188: raise InvalidComponentError,
189: "bad component(expected opaque component): #{v}"
190: end
191:
192: return true
193: end
# File lib/uri/mailto.rb, line 158
158: def check_to(v)
159: return true unless v
160: return true if v.size == 0
161:
162: if OPAQUE !~ v || /\A#{MAILBOX_PATTERN}*\z/o !~ v
163: raise InvalidComponentError,
164: "bad component(expected opaque component): #{v}"
165: end
166:
167: return true
168: end