Skip to content

Commit

Permalink
make scrape_expiries a API method and not an associated func
Browse files Browse the repository at this point in the history
this is to fix uniffi complaint: associated methods not supported
  • Loading branch information
nain-F49FF806 committed Dec 7, 2024
1 parent d8cfd93 commit bef3452
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 18 deletions.
32 changes: 16 additions & 16 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,22 +26,6 @@ impl API {
}
Self { base: url, opts }
}

pub fn scrape_expiries(url: Url) -> PbResult<Vec<String>> {
let client = reqwest::blocking::Client::new();
let response = client.get(url).send()?;
response.error_for_status_ref()?;
let html = response.text()?;
let document = Html::parse_document(&html);
let expiries_selector = Selector::parse("#expiration + ul > li > a").unwrap();
let mut expiries = Vec::new();
for expiry_anchor in document.select(&expiries_selector) {
if let Some(expiry) = expiry_anchor.attr("data-expiration") {
expiries.push(expiry.to_string());
}
}
Ok(expiries)
}
}

impl API {
Expand Down Expand Up @@ -224,6 +208,22 @@ impl API {
}
}

pub fn scrape_expiries(&self) -> PbResult<Vec<String>> {
let client = reqwest::blocking::Client::new();
let response = client.get(self.base()).send()?;
response.error_for_status_ref()?;
let html = response.text()?;
let document = Html::parse_document(&html);
let expiries_selector = Selector::parse("#expiration + ul > li > a").unwrap();
let mut expiries = Vec::new();
for expiry_anchor in document.select(&expiries_selector) {
if let Some(expiry) = expiry_anchor.attr("data-expiration") {
expiries.push(expiry.to_string());
}
}
Ok(expiries)
}

pub fn base(&self) -> Url {
self.base.clone()
}
Expand Down
5 changes: 3 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,9 @@ fn handle_comment(opts: &Opts) -> PbResult<()> {
}

fn handle_scrape(opts: &Opts) -> PbResult<()> {
let url = opts.get_url().clone();
let expiries = API::scrape_expiries(url)?;
let url = opts.get_url();
let api = API::new(url.clone(), opts.clone());
let expiries = api.scrape_expiries()?;
std::io::stdout().write_all(format!("{:?}", expiries).as_bytes())?;
writeln!(std::io::stdout())?;
Ok(())
Expand Down

0 comments on commit bef3452

Please sign in to comment.