Skip to content

Commit ca651ca

Browse files
authored
Merge pull request #1858 from tk0miya/uri_mailto
stdlib: Add types for URI::MailTo
2 parents 82df1eb + eb6c443 commit ca651ca

File tree

2 files changed

+143
-0
lines changed

2 files changed

+143
-0
lines changed

stdlib/uri/0/mailto.rbs

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,89 @@ module URI
44
# RFC6068, the mailto URL scheme.
55
#
66
class MailTo < Generic
7+
EMAIL_REGEXP: Regexp
8+
9+
# <!--
10+
# rdoc-file=lib/uri/mailto.rb
11+
# - build(args)
12+
# -->
13+
# ## Description
14+
#
15+
# Creates a new URI::MailTo object from components, with syntax checking.
16+
#
17+
# Components can be provided as an Array or Hash. If an Array is used, the
18+
# components must be supplied as `[to, headers]`.
19+
#
20+
# If a Hash is used, the keys are the component names preceded by colons.
21+
#
22+
# The headers can be supplied as a pre-encoded string, such as
23+
# `"subject=subscribe&cc=address"`, or as an Array of Arrays like `[['subject',
24+
# 'subscribe'], ['cc', 'address']]`.
25+
#
26+
# Examples:
27+
#
28+
# require 'uri'
29+
#
30+
# m1 = URI::MailTo.build(['[email protected]', 'subject=Ruby'])
31+
# m1.to_s # => "mailto:[email protected]?subject=Ruby"
32+
#
33+
# m2 = URI::MailTo.build(['[email protected]', [['Subject', 'Ruby'], ['Cc', '[email protected]']]])
34+
# m2.to_s # => "mailto:[email protected]?Subject=Ruby&[email protected]"
35+
#
36+
# m3 = URI::MailTo.build({:to => '[email protected]', :headers => [['subject', 'subscribe']]})
37+
# m3.to_s # => "mailto:[email protected]?subject=subscribe"
38+
#
39+
def self.build: (Array[String]) -> instance
40+
| ([String, Array[Array[String]]]) -> instance
41+
| (Hash[Symbol, String | Array[Array[String]]]) -> instance
42+
43+
# <!-- rdoc-file=lib/uri/mailto.rb -->
44+
# E-mail headers set by the URL, as an Array of Arrays.
45+
#
46+
def headers: () -> Array[[String, String]]
47+
48+
# <!--
49+
# rdoc-file=lib/uri/mailto.rb
50+
# - headers=(v)
51+
# -->
52+
# Setter for headers `v`.
53+
#
54+
def headers=: (String) -> String
55+
56+
# <!-- rdoc-file=lib/uri/mailto.rb -->
57+
# The primary e-mail address of the URL, as a String.
58+
#
59+
def to: () -> String
60+
61+
# <!--
62+
# rdoc-file=lib/uri/mailto.rb
63+
# - to=(v)
64+
# -->
65+
# Setter for to `v`.
66+
#
67+
def to=: (String) -> String
68+
69+
# <!--
70+
# rdoc-file=lib/uri/mailto.rb
71+
# - to_mailtext()
72+
# -->
73+
# Returns the RFC822 e-mail text equivalent of the URL, as a String.
74+
#
75+
# Example:
76+
#
77+
# require 'uri'
78+
#
79+
# uri = URI.parse("mailto:[email protected]?Subject=subscribe&cc=myaddr")
80+
# uri.to_mailtext
81+
# # => "To: [email protected]\nSubject: subscribe\nCc: myaddr\n\n\n"
82+
#
83+
def to_mailtext: () -> String
84+
85+
# <!--
86+
# rdoc-file=lib/uri/mailto.rb
87+
# - to_rfc822text()
88+
# -->
89+
#
90+
def to_rfc822text: () -> String
791
end
892
end

test/stdlib/uri/mailto_test.rb

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
require_relative "../test_helper"
2+
require "uri"
3+
4+
class URI::MailToSingletonTest < Test::Unit::TestCase
5+
include TestHelper
6+
7+
library "uri"
8+
testing "singleton(::URI::MailTo)"
9+
10+
def test_EMAIL_REGEXP
11+
assert_const_type "::Regexp", "URI::MailTo::EMAIL_REGEXP"
12+
end
13+
14+
def test_build
15+
assert_send_type "(::Array[ ::String ]) -> ::URI::MailTo",
16+
URI::MailTo, :build, ["[email protected]", "subject=subject"]
17+
assert_send_type "([ ::String, ::Array[ ::Array[ ::String ] ] ]) -> ::URI::MailTo",
18+
URI::MailTo, :build, ["[email protected]", [["subject", "subject"]]]
19+
assert_send_type "(::Hash[ ::Symbol, ::String | ::Array[ ::Array[ ::String ] ] ]) -> ::URI::MailTo",
20+
URI::MailTo, :build, { to: "[email protected]", headers: "subject=subject" }
21+
end
22+
end
23+
24+
class URI::MailToTest < Test::Unit::TestCase
25+
include TestHelper
26+
27+
library "uri"
28+
testing "::URI::MailTo"
29+
30+
def test_headers
31+
assert_send_type "() -> ::Array[[ ::String, ::String ]]",
32+
URI::MailTo.build(["[email protected]", "subject=subject"]), :headers
33+
end
34+
35+
def test_headers=
36+
assert_send_type "(::String) -> ::String",
37+
URI::MailTo.build(["[email protected]", "subject=subject"]), :headers=, "subject=subject2"
38+
end
39+
40+
def test_to
41+
assert_send_type "() -> ::String",
42+
URI::MailTo.build(["[email protected]", "subject=subject"]), :to
43+
end
44+
45+
def test_to=
46+
assert_send_type "(::String) -> ::String",
47+
URI::MailTo.build(["[email protected]", "subject=subject"]), :to=, "[email protected]"
48+
end
49+
50+
def test_to_mailtext
51+
assert_send_type "() -> ::String",
52+
URI::MailTo.build(["[email protected]", "subject=subject"]), :to_mailtext
53+
end
54+
55+
def test_to_rfc822text
56+
assert_send_type "() -> ::String",
57+
URI::MailTo.build(["[email protected]", "subject=subject"]), :to_rfc822text
58+
end
59+
end

0 commit comments

Comments
 (0)