| Class | URI::MailTo |
| In: |
lib/uri/mailto.rb
|
| Parent: | Generic |
RFC2368, The mailto URL scheme
| DEFAULT_PORT | = | nil |
| COMPONENT | = | [ :scheme, :to, :headers ].freeze |
| headers | [R] | |
| to | [R] |
Creates a new URI::MailTo object from components of URI::MailTo with check. It is to and headers. It provided by an Array of a Hash. You can provide headers as String like "subject=subscribe&cc=addr" or Array like [["subject", "subscribe"], ["cc", "addr"]]
# File lib/uri/mailto.rb, line 70
70: def self.build(args)
71: tmp = Util::make_components_hash(self, args)
72:
73: if tmp[:to]
74: tmp[:opaque] = tmp[:to]
75: else
76: tmp[:opaque] = ''
77: end
78:
79: if tmp[:headers]
80: tmp[:opaque] << '?'
81:
82: if tmp[:headers].kind_of?(Array)
83: tmp[:opaque] << tmp[:headers].collect { |x|
84: if x.kind_of?(Array)
85: x[0] + '=' + x[1..-1].to_s
86: else
87: x.to_s
88: end
89: }.join('&')
90:
91: elsif tmp[:headers].kind_of?(Hash)
92: tmp[:opaque] << tmp[:headers].collect { |h,v|
93: h + '=' + v
94: }.join('&')
95:
96: else
97: tmp[:opaque] << tmp[:headers].to_s
98: end
99: end
100:
101: return super(tmp)
102: end
Creates a new URI::MailTo object from ``generic’’ components with no check. Because, this method is usually called from URI::parse and the method checks validity of each components.
# File lib/uri/mailto.rb, line 111
111: def initialize(*arg)
112: super(*arg)
113:
114: @to = nil
115: @headers = []
116:
117: if MAILTO_REGEXP =~ @opaque
118: if arg[-1]
119: self.to = $1
120: self.headers = $2
121: else
122: set_to($1)
123: set_headers($2)
124: end
125:
126: else
127: raise InvalidComponentError,
128: "unrecognised opaque part for mailtoURL: #{@opaque}"
129: end
130: end
# File lib/uri/mailto.rb, line 182
182: def headers=(v)
183: check_headers(v)
184: set_headers(v)
185: v
186: end
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 214
214: def to_mailtext
215: to = URI::unescape(@to)
216: head = ''
217: body = ''
218: @headers.each do |x|
219: case x[0]
220: when 'body'
221: body = URI::unescape(x[1])
222: when 'to'
223: to << ', ' + URI::unescape(x[1])
224: else
225: head << URI::unescape(x[0]).capitalize + ': ' +
226: URI::unescape(x[1]) + "\n"
227: end
228: end
229:
230: return "To: #{to}
231: #{head}
232: #{body}
233: "
234: end
# File lib/uri/mailto.rb, line 188
188: def to_s
189: @scheme + ':' +
190: if @to
191: @to
192: else
193: ''
194: end +
195: if @headers.size > 0
196: '?' + @headers.collect{|x| x.join('=')}.join('&')
197: else
198: ''
199: end +
200: if @fragment
201: '#' + @fragment
202: else
203: ''
204: end
205: end
# File lib/uri/mailto.rb, line 172
172: def set_headers(v)
173: @headers = []
174: if v
175: v.scan(HEADER_REGEXP) do |x|
176: @headers << x.split(/=/o, 2)
177: end
178: end
179: end
# File lib/uri/mailto.rb, line 158
158: def check_headers(v)
159: return true unless v
160: return true if v.size == 0
161:
162: if OPAQUE !~ v ||
163: /\A(#{HEADER_PATTERN}(?:\&#{HEADER_PATTERN})*)\z/o !~ v
164: raise InvalidComponentError,
165: "bad component(expected opaque component): #{v}"
166: end
167:
168: return true
169: end
# File lib/uri/mailto.rb, line 134
134: def check_to(v)
135: return true unless v
136: return true if v.size == 0
137:
138: if OPAQUE !~ v || /\A#{MAILBOX_PATTERN}*\z/o !~ v
139: raise InvalidComponentError,
140: "bad component(expected opaque component): #{v}"
141: end
142:
143: return true
144: end