Skip to content

Commit

Permalink
Cleaning things up.
Browse files Browse the repository at this point in the history
  • Loading branch information
VisruthSK committed Dec 16, 2024
1 parent 0beaf07 commit c74b117
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 31 deletions.
8 changes: 2 additions & 6 deletions NAMESPACE
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# Generated by roxygen2: do not edit by hand

S3method(html_element,"rvest::rvest_session")
S3method(html_element,LiveHTML)
S3method(html_element,default)
S3method(html_element,rvest_session)
S3method(html_elements,"rvest::rvest_session")
S3method(html_elements,LiveHTML)
S3method(html_elements,default)
S3method(html_elements,rvest_session)
S3method(html_table,"rvest::rvest_session")
S3method(html_table,LiveHTML)
S3method(html_table,xml_document)
Expand Down Expand Up @@ -38,9 +38,6 @@ export(html_session)
export(html_table)
export(html_text)
export(html_text2)
export(httr_cookies)
export(httr_headers)
export(httr_status_code)
export(is.session)
export(jump_to)
export(minimal_html)
Expand All @@ -57,7 +54,6 @@ export(session_submit)
export(set_values)
export(submit_form)
export(url_absolute)
export(xml2_read_html)
export(xml_node)
export(xml_nodes)
export(xml_tag)
Expand Down
24 changes: 6 additions & 18 deletions R/form.R
Original file line number Diff line number Diff line change
Expand Up @@ -37,12 +37,10 @@ html_form <- new_generic("html_form", "x", function(x, base_url = NULL) {
S7_dispatch()
})

#' @export
method(html_form, new_S3_class("xml_document")) <- function(x, base_url = NULL) {
html_form(xml2::xml_find_all(x, ".//form"), base_url = base_url)
}

#' @export
method(html_form, new_S3_class("xml_nodeset")) <- function(x, base_url = NULL) {
lapply(x, html_form, base_url = base_url)
}
Expand All @@ -59,7 +57,6 @@ rvest_form <- new_class(
)
)

#' @export
method(html_form, new_S3_class("xml_node")) <- function(x, base_url = NULL) {
if (xml2::xml_name(x) != "form") {
cli::cli_abort("{.arg x} must be a <form> element.")
Expand Down Expand Up @@ -91,8 +88,8 @@ method(html_form, new_S3_class("xml_node")) <- function(x, base_url = NULL) {
)
}

baseprint <- new_external_generic("base", "print", "x")
method(baseprint, rvest_form) <- function(x, ...) {
print <- new_external_generic("base", "print", "x")
method(print, rvest_form) <- function(x, ...) {
cat("<form> '", x@name, "' (", x@method, " ", x@action, ")\n", sep = "")
cat(format_list(x@fields, indent = 1), "\n", sep = "")
}
Expand Down Expand Up @@ -237,19 +234,11 @@ rvest_field <- new_class(
value = class_character | NULL,
attr = class_list,
options = NULL | class_character
),
constructor = function(type, name, value, attr, options = NULL) {
force(type)
force(name)
force(value)
force(attr)
new_object(S7_object(), type = type, name = name, value = value, attr = attr, options = options)
}
)
)

baseformat <- new_external_generic("base", "format", "x")
#' @export
method(baseformat, rvest_field) <- function(x, ...) {
format <- new_external_generic("base", "format", "x")
method(format, rvest_field) <- function(x, ...) {
if (x@type == "password") {
value <- paste0(rep("*", nchar(x@value %||% "")), collapse = "")
} else {
Expand All @@ -260,8 +249,7 @@ method(baseformat, rvest_field) <- function(x, ...) {
paste0("<field> (", x@type, ") ", x@name, ": ", value)
}

#' @export
method(baseprint, rvest_field) <- function(x, ...) {
method(print, rvest_field) <- function(x, ...) {
cat(format(x, ...), "\n", sep = "")
invisible(x)
}
Expand Down
8 changes: 1 addition & 7 deletions R/session.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,8 +61,7 @@ rvest_session <- new_class(
#' @rdname session
is.session <- function(x) S7_inherits(x, rvest_session)

#' @export
method(baseprint, rvest_session) <- function(x, ...) {
method(print, rvest_session) <- function(x, ...) {
cat("<session> ", x@url, "\n", sep = "")
cat(" Status: ", httr::status_code(x), "\n", sep = "")
cat(" Type: ", httr::headers(x)$`Content-Type`, "\n", sep = "")
Expand Down Expand Up @@ -208,7 +207,6 @@ session_submit <- function(x, form, submit = NULL, ...) {
# xml2 methods ------------------------------------------------------------

#' @importFrom xml2 read_html
#' @export
xml2_read_html <- new_external_generic("xml2", "read_html", "x")
method(xml2_read_html, rvest_session) <- function(x, ...) {
if (!is_html(x@response)) {
Expand All @@ -230,7 +228,6 @@ is_html <- function(x) {

# rvest methods -----------------------------------------------------------------

#' @export
method(html_form, rvest_session) <- function(x, base_url = NULL) {
html_form(read_html(x), base_url = base_url)
}
Expand Down Expand Up @@ -267,21 +264,18 @@ method(html_form, rvest_session) <- function(x, base_url = NULL) {
# httr methods -----------------------------------------------------------------

#' @importFrom httr status_code
#' @export
httr_status_code <- new_external_generic("httr", "status_code", "x")
method(httr_status_code, rvest_session) <- function(x) {
status_code(x@response)
}

#' @importFrom httr headers
#' @export
httr_headers <- new_external_generic("httr", "headers", "x")
method(httr_headers, rvest_session) <- function(x) {
headers(x@response)
}

#' @importFrom httr cookies
#' @export
httr_cookies <- new_external_generic("httr", "cookies", "x")
method(httr_cookies, rvest_session) <- function(x) {
cookies(x@response)
Expand Down

0 comments on commit c74b117

Please sign in to comment.