UriPattern: UriPattern

UriPatternR Documentation

UriPattern

Description

uri matcher

Public fields

pattern

(character) pattern holder

regex

a logical

query_params

a list, or NULL if empty

partial

bool, default: FALSE

partial_type

a string, default: NULL

Methods

Public 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)

Returns

A new UriPattern object


Method matches()

Match a uri against a pattern

Usage
UriPattern$matches(uri)
Arguments
uri

(character) a uri

Returns

a boolean


Method pattern_matches()

Match a URI

Usage
UriPattern$pattern_matches(uri)
Arguments
uri

(character) a uri

Returns

a boolean


Method query_params_matches()

Match query parameters of a URI

Usage
UriPattern$query_params_matches(uri)
Arguments
uri

(character) a uri

Returns

a boolean


Method extract_query()

Extract query parameters as a named list

Usage
UriPattern$extract_query(uri)
Arguments
uri

(character) a uri

Returns

named list, or NULL if no query parameters


Method add_query_params()

Add query parameters to the URI

Usage
UriPattern$add_query_params(query_params)
Arguments
query_params

(list|character) list or character

Returns

nothing returned, updates uri pattern


Method to_s()

Print pattern for easy human consumption

Usage
UriPattern$to_s()
Returns

a string


Method clone()

The objects of this class are cloneable with this method.

Usage
UriPattern$clone(deep = FALSE)
Arguments
deep

Whether to make a deep clone.

Examples

# trailing slash
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$matches("http://foobar.com") # TRUE
z$matches("http://foobar.com/") # TRUE

# without scheme
## matches http by default: does not match https by default
(z <- UriPattern$new(pattern = "foobar.com"))
z$matches("http://foobar.com") # TRUE
z$matches("http://foobar.com/") # TRUE
z$matches("https://foobar.com") # FALSE
z$matches("https://foobar.com/") # FALSE
## to match https, you'll have to give the complete url
(z <- UriPattern$new(pattern = "https://foobar.com"))
z$matches("https://foobar.com/") # TRUE
z$matches("http://foobar.com/") # FALSE

# default ports
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$matches("http://foobar.com:80") # TRUE
z$matches("http://foobar.com:80/") # TRUE
z$matches("http://foobar.com:443") # TRUE
z$matches("http://foobar.com:443/") # TRUE

# user info - FIXME, not sure we support this yet
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$matches("http://user:pass@foobar.com")

# regex
(z <- UriPattern$new(regex_pattern = ".+ample\\.."))
z$matches("http://sample.org") # TRUE
z$matches("http://example.com") # TRUE
z$matches("http://tramples.net") # FALSE

# add query parameters
(z <- UriPattern$new(pattern = "http://foobar.com"))
z$add_query_params(list(pizza = "cheese", cheese = "cheddar"))
z
z$pattern
z$matches("http://foobar.com?pizza=cheese&cheese=cheddar") # TRUE
z$matches("http://foobar.com?pizza=cheese&cheese=swiss") # FALSE

# query parameters in the uri
(z <- UriPattern$new(pattern = "https://httpbin.org/get?stuff=things"))
z$add_query_params() # have to run this method to gather query params
z$matches("https://httpbin.org/get?stuff=things") # TRUE
z$matches("https://httpbin.org/get?stuff2=things") # FALSE

# regex add query parameters
(z <- UriPattern$new(regex_pattern = "https://foobar.com/.+/order"))
z$add_query_params(list(pizza = "cheese"))
z
z$pattern
z$matches("https://foobar.com/pizzas/order?pizza=cheese") # TRUE
z$matches("https://foobar.com/pizzas?pizza=cheese") # FALSE

# query parameters in the regex uri
(z <- UriPattern$new(regex_pattern = "https://x.com/.+/order\\?fruit=apple"))
z$add_query_params() # have to run this method to gather query params
z$matches("https://x.com/a/order?fruit=apple") # TRUE
z$matches("https://x.com/a?fruit=apple") # FALSE

# any pattern
(z <- UriPattern$new(regex_pattern = "stuff\\.com.+"))
z$regex
z$pattern
z$matches("http://stuff.com") # FALSE
z$matches("https://stuff.com/stff") # TRUE
z$matches("https://stuff.com/apple?bears=brown&bats=grey") # 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
z$matches("http://foobar.com?bye=mars") # 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
z$matches("http://foobar.com?bye=mars") # 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
z$matches("http://foobar.com?hello=stuff") # TRUE
z$matches("http://foobar.com?bye=stuff") # FALSE

webmockr documentation built on April 4, 2025, 12:08 a.m.