assert-url 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (4) hide show
  1. checksums.yaml +5 -13
  2. data/assert_url.gemspec +1 -1
  3. data/lib/assert_url.rb +98 -18
  4. metadata +7 -7
checksums.yaml CHANGED
@@ -1,15 +1,7 @@
1
1
  ---
2
- !binary "U0hBMQ==":
3
- metadata.gz: !binary |-
4
- NDY2MTI0ZTRmY2NiYWE1MjA0M2RkYWU4Y2MxZDc3YTQ2ZTgyMDhiYQ==
5
- data.tar.gz: !binary |-
6
- YTRkNDAzMTU3ZjAxMjkyNTQ2YzQ4NWU1ZGE3NDdhMGM3NTIwNzE0Mw==
2
+ SHA1:
3
+ metadata.gz: 081c87760942d9781ee96de1edd14264b124a804
4
+ data.tar.gz: f940ede15f1cabd224276d3ce79215acc3cbbc37
7
5
  SHA512:
8
- metadata.gz: !binary |-
9
- ZDY3OGY4ZTFmOTU4MzBmYmRlOTQxZWRjYzUyMDYwMjQyNjJjOTc0ZmFmNjUy
10
- Y2ZmYzE1MTcxYjQzNmMxOWZmMmMzOGQ4NjUwZDAxOGQ0YTUwYzA5Y2RlZDMx
11
- NDUyMTgxM2QxYTQ4M2U3NGM2M2FjYjYzYzhhOWY1ZGM5MWEyODA=
12
- data.tar.gz: !binary |-
13
- ODZjYThmNzAwM2RlYmNiNzdhZjM2NmIwNTNkZTM3NjZhOTU5Nzg4MDVjODNk
14
- NDZhMjcyNTkxYjQzZDMzNDk0NDgxMGE2ZDZlYmNkMmYyZThjOTIzZWNlZTZl
15
- ZDcyZjc5MDg0NThiZTg0NjIzNmFjYjA4NThhNzUzZGI1NTA4YTI=
6
+ metadata.gz: 1d1bcddc2a8d2db7b23d3a6bcd812b3715c1054b15ec590380d2834c95005e4a068e4af59aac1832f804007068e802624dedec67c2356be75ef760bc900971c3
7
+ data.tar.gz: f8cc98fda94f2c9953f1ef75b5729680e0ad29d3ea11d1dd339a9ac1af3b4c02d885ede89e7c993b3de772253a406bbc5b9b8381e2caa36ef5539ceef291781b
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = "assert-url"
3
- s.version = "1.0.0"
3
+ s.version = "1.0.1"
4
4
  s.summary = "Semantical helpers to test your URLs in Ruby"
5
5
  s.description = s.summary
6
6
  s.authors = ["Lucas Tolchinsky"]
@@ -1,25 +1,112 @@
1
1
  require "uri"
2
2
 
3
3
  module AssertUrl
4
- PARTS = %W[scheme host port path query fragment]
4
+ PARTS = %W[scheme host port path query fragment].each do |part|
5
+ const_set("#{part.capitalize}Error", Class.new(StandardError))
6
+ end
5
7
 
6
- PARTS.each do |part|
7
- error = const_set("#{part.capitalize}Error", Class.new(StandardError))
8
+ # @param [String or Symbol] expected The scheme the url should have.
9
+ # @param [String or URI] value The url you wish to validate.
10
+ #
11
+ # @example
12
+ #
13
+ # assert_scheme_equal(:http, "http://example.org")
14
+ # assert_scheme_equal("http", "http://example.org")
15
+ #
16
+ def assert_scheme_equal(expected, value)
17
+ value = urify(value).scheme
8
18
 
9
- define_method(:"assert_#{part}_equal") do |expected, value|
10
- expected = normalize(part, expected)
11
- value = urify(value).send(part.to_sym)
19
+ expected.to_s == value || raises(SchemeError, expected, value)
20
+ end
12
21
 
13
- expected == value || (raise error, "expected #{expected}, got #{value}")
14
- end
22
+ # @param [String] expected The host the url should have.
23
+ # @param [String or URI] value The url you wish to validate.
24
+ #
25
+ # @example
26
+ #
27
+ # assert_host_equal("example.org", "http://example.org")
28
+ #
29
+ def assert_host_equal(expected, value)
30
+ value = urify(value).host
31
+
32
+ expected == value || raises(HostError, expected, value)
15
33
  end
16
34
 
35
+ # @param [Integer] expected The port the url should have.
36
+ # @param [String or URI] value The url you wish to validate.
37
+ #
38
+ # @example
39
+ #
40
+ # assert_port_equal(80, "http://example.org")
41
+ #
42
+ def assert_port_equal(expected, value)
43
+ value = urify(value).port
44
+
45
+ expected == value || raises(PortError, expected, value)
46
+ end
47
+
48
+ # @param [String] expected The path the url should have.
49
+ # @param [String or URI] value The url you wish to validate.
50
+ #
51
+ # @example
52
+ #
53
+ # assert_path_equal("/path", "http://example.org/path")
54
+ #
55
+ def assert_path_equal(expected, value)
56
+ value = urify(value).path
57
+
58
+ expected == value || raises(PathError, expected, value)
59
+ end
60
+
61
+ # @param [Hash] expected The query the url should have.
62
+ # @param [String or URI] value The url you wish to validate.
63
+ #
64
+ # @example
65
+ #
66
+ # assert_query_equal({foo: "bar"}, "http://example.org/?foo=bar")
67
+ #
68
+ def assert_query_equal(expected, value)
69
+ value = urify(value).query
70
+ expected = (URI.encode_www_form(expected) rescue expected)
71
+
72
+ expected == value || raises(QueryError, expected, value)
73
+ end
74
+
75
+ # @param [String] expected The fragment the url should have.
76
+ # @param [String or URI] value The url you wish to validate.
77
+ #
78
+ # @example
79
+ #
80
+ # assert_fragment_equal("fragment", "http://example.org/path#fragment")
81
+ #
82
+ def assert_fragment_equal(expected, value)
83
+ value = urify(value).fragment
84
+
85
+ expected == value || raises(FragmentError, expected, value)
86
+ end
87
+
88
+ # {#assert_url_equal} runs all the validations above. It's not a String comparison.
89
+ #
90
+ # @param [String or URI] expected The url you wish to have.
91
+ # @param [String or URI] value The url you wish to validate.
92
+ #
93
+ # @example
94
+ #
95
+ # assert_url_equal(URI("http://example.org"), "http://example.org")
96
+ #
17
97
  def assert_url_equal(expected, value)
18
98
  expected, value = urify(expected), urify(value)
19
99
 
20
100
  PARTS.map { |part| send(:"assert_#{part}_equal", expected.send(part.to_sym), value) }.reduce(:&)
21
101
  end
22
102
 
103
+ # @param [String or Symbol] expected The key-value pairs that the url must include.
104
+ # @param [String or URI] value The url you wish to validate.
105
+ #
106
+ # @example
107
+ #
108
+ # assert_query_include({foo: "bar"}, "http://example.org/?foo=bar&baz=wat")
109
+ #
23
110
  def assert_query_include(expected, value)
24
111
  value = Hash[URI.decode_www_form(urify(value).query)]
25
112
 
@@ -39,16 +126,9 @@ module AssertUrl
39
126
  end
40
127
  private :urify
41
128
 
42
- def normalize(part, expected)
43
- case part
44
- when "scheme"
45
- expected.kind_of?(Symbol) ? expected.to_s : expected
46
- when "query"
47
- URI.encode_www_form(expected) rescue expected
48
- else
49
- expected
50
- end
129
+ def raises(error, expected, value)
130
+ raise error, "expected #{expected}, got #{value}"
51
131
  end
52
- private :normalize
132
+ private :raises
53
133
  end
54
134
 
metadata CHANGED
@@ -1,27 +1,27 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: assert-url
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Lucas Tolchinsky
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-23 00:00:00.000000000 Z
11
+ date: 2014-08-24 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: cutest
15
15
  requirement: !ruby/object:Gem::Requirement
16
16
  requirements:
17
- - - ! '>='
17
+ - - ">="
18
18
  - !ruby/object:Gem::Version
19
19
  version: '0'
20
20
  type: :development
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
- - - ! '>='
24
+ - - ">="
25
25
  - !ruby/object:Gem::Version
26
26
  version: '0'
27
27
  description: Semantical helpers to test your URLs in Ruby
@@ -46,17 +46,17 @@ require_paths:
46
46
  - lib
47
47
  required_ruby_version: !ruby/object:Gem::Requirement
48
48
  requirements:
49
- - - ! '>='
49
+ - - ">="
50
50
  - !ruby/object:Gem::Version
51
51
  version: '0'
52
52
  required_rubygems_version: !ruby/object:Gem::Requirement
53
53
  requirements:
54
- - - ! '>='
54
+ - - ">="
55
55
  - !ruby/object:Gem::Version
56
56
  version: '0'
57
57
  requirements: []
58
58
  rubyforge_project:
59
- rubygems_version: 2.2.2
59
+ rubygems_version: 2.2.0
60
60
  signing_key:
61
61
  specification_version: 4
62
62
  summary: Semantical helpers to test your URLs in Ruby