uri matcher
Public fields
pattern
(character) pattern holder
regex
a logical
query_params
a list, or
NULL
if emptypartial
bool, default:
FALSE
partial_type
a string, default: NULL
Methods
Method new()
Create a new UriPattern
object
Usage
UriPattern$new(pattern = NULL, regex_pattern = NULL)
Arguments
pattern
(character) a uri, as a character string. if scheme is missing, it is added (we assume http)
regex_pattern
(character) a uri as a regex character string, see base::regex. if scheme is missing, it is added (we assume http)
Method matches()
Match a uri against a pattern
Examples
# trailing slash
(z <- UriPattern$new(pattern = "http://foobar.com"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: http://foobar.com
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$matches("http://foobar.com") # TRUE
#> [1] TRUE
z$matches("http://foobar.com/") # TRUE
#> [1] TRUE
# without scheme
## matches http by default: does not match https by default
(z <- UriPattern$new(pattern = "foobar.com"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: http://foobar.com
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$matches("http://foobar.com") # TRUE
#> [1] TRUE
z$matches("http://foobar.com/") # TRUE
#> [1] TRUE
z$matches("https://foobar.com") # FALSE
#> [1] FALSE
z$matches("https://foobar.com/") # FALSE
#> [1] FALSE
## to match https, you'll have to give the complete url
(z <- UriPattern$new(pattern = "https://foobar.com"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: https://foobar.com
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$matches("https://foobar.com/") # TRUE
#> [1] TRUE
z$matches("http://foobar.com/") # FALSE
#> [1] FALSE
# default ports
(z <- UriPattern$new(pattern = "http://foobar.com"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: http://foobar.com
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$matches("http://foobar.com:80") # TRUE
#> [1] TRUE
z$matches("http://foobar.com:80/") # TRUE
#> [1] TRUE
z$matches("http://foobar.com:443") # TRUE
#> [1] TRUE
z$matches("http://foobar.com:443/") # TRUE
#> [1] TRUE
# user info - FIXME, not sure we support this yet
(z <- UriPattern$new(pattern = "http://foobar.com"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: http://foobar.com
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$matches("http://user:pass@foobar.com")
#> [1] TRUE
# regex
(z <- UriPattern$new(regex_pattern = ".+ample\\.."))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: https?://.+ample\..
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: TRUE
#> to_s: function ()
z$matches("http://sample.org") # TRUE
#> [1] TRUE
z$matches("http://example.com") # TRUE
#> [1] TRUE
z$matches("http://tramples.net") # FALSE
#> [1] FALSE
# add query parameters
(z <- UriPattern$new(pattern = "http://foobar.com"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: http://foobar.com
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$add_query_params(list(pizza = "cheese", cheese = "cheddar"))
z
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: http://foobar.com?pizza=cheese&cheese=cheddar
#> pattern_matches: function (uri)
#> query_params: list
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$pattern
#> [1] "http://foobar.com?pizza=cheese&cheese=cheddar"
z$matches("http://foobar.com?pizza=cheese&cheese=cheddar") # TRUE
#> [1] TRUE
z$matches("http://foobar.com?pizza=cheese&cheese=swiss") # FALSE
#> [1] FALSE
# query parameters in the uri
(z <- UriPattern$new(pattern = "https://httpbin.org/get?stuff=things"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: https://httpbin.org/get?stuff=things
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: FALSE
#> to_s: function ()
z$add_query_params() # have to run this method to gather query params
z$matches("https://httpbin.org/get?stuff=things") # TRUE
#> [1] TRUE
z$matches("https://httpbin.org/get?stuff2=things") # FALSE
#> [1] FALSE
# regex add query parameters
(z <- UriPattern$new(regex_pattern = "https://foobar.com/.+/order"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: https://foobar.com/.+/order
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: TRUE
#> to_s: function ()
z$add_query_params(list(pizza = "cheese"))
#> NULL
z
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: https://foobar.com/.+/order
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: TRUE
#> to_s: function ()
z$pattern
#> [1] "https://foobar.com/.+/order"
z$matches("https://foobar.com/pizzas/order?pizza=cheese") # TRUE
#> [1] TRUE
z$matches("https://foobar.com/pizzas?pizza=cheese") # FALSE
#> [1] FALSE
# query parameters in the regex uri
(z <- UriPattern$new(regex_pattern = "https://x.com/.+/order\\?fruit=apple"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: https://x.com/.+/order\?fruit=apple
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: TRUE
#> to_s: function ()
z$add_query_params() # have to run this method to gather query params
#> NULL
z$matches("https://x.com/a/order?fruit=apple") # TRUE
#> [1] TRUE
z$matches("https://x.com/a?fruit=apple") # FALSE
#> [1] FALSE
# any pattern
(z <- UriPattern$new(regex_pattern = "stuff\\.com.+"))
#> <UriPattern>
#> Public:
#> add_query_params: function (query_params)
#> clone: function (deep = FALSE)
#> extract_query: function (uri)
#> initialize: function (pattern = NULL, regex_pattern = NULL)
#> matches: function (uri)
#> partial: FALSE
#> partial_type: NULL
#> pattern: https?://stuff\.com.+
#> pattern_matches: function (uri)
#> query_params: NULL
#> query_params_matches: function (uri)
#> regex: TRUE
#> to_s: function ()
z$regex
#> [1] TRUE
z$pattern
#> [1] "https?://stuff\\.com.+"
z$matches("http://stuff.com") # FALSE
#> [1] FALSE
z$matches("https://stuff.com/stff") # TRUE
#> [1] TRUE
z$matches("https://stuff.com/apple?bears=brown&bats=grey") # TRUE
#> [1] TRUE
# partial matching
## including
z <- UriPattern$new(pattern = "http://foobar.com")
z$add_query_params(including(list(hello = "world")))
z$matches(uri = "http://foobar.com?hello=world&bye=mars") # TRUE
#> [1] TRUE
z$matches("http://foobar.com?bye=mars") # FALSE
#> [1] FALSE
## excluding
z <- UriPattern$new(pattern = "http://foobar.com")
z$add_query_params(excluding(list(hello = "world")))
z$matches(uri = "http://foobar.com?hello=world&bye=mars") # FALSE
#> [1] FALSE
z$matches("http://foobar.com?bye=mars") # TRUE
#> [1] TRUE
## match on list keys (aka: names) only, ignore values 0
z <- UriPattern$new(pattern = "http://foobar.com")
z$add_query_params(including(list(hello = NULL)))
z$matches(uri = "http://foobar.com?hello=world&bye=mars") # TRUE
#> [1] TRUE
z$matches("http://foobar.com?hello=stuff") # TRUE
#> [1] TRUE
z$matches("http://foobar.com?bye=stuff") # FALSE
#> [1] FALSE