Add doc and tests

master
Rémi BERTHO 3 years ago
parent 4bd5e1eb5e
commit 3ace078024
Signed by: dalan
GPG Key ID: EE3B917931C07B64
  1. 12
      src/parser.rs
  2. 9
      src/parser/darty.rs
  3. 14
      src/price_checker.rs
  4. 4
      src/price_result.rs

@ -9,23 +9,35 @@ use arraygen::Arraygen;
use url::Url;
use anyhow::Result;
/// Trait needed to get price from a specific website
pub trait PriceParser{
/// Create a new parser
fn new() -> Result<Self> where Self :Sized;
/// Indicate if it can parse this URL
fn can_parse(&self, url : &Url) -> bool;
/// Parse the html into a price
fn parse(&self, html : &Html) -> Result<PriceResult>;
}
#[derive(Arraygen, Debug)]
#[gen_array(pub fn get_price: & dyn PriceParser)]
/// Represent the list of all the parser
pub struct List {
#[in_array(get_price)]
darty: darty::Darty
}
impl List {
/// Create the list
pub fn new() -> Result<Self> {
Ok(List {
darty: darty::Darty::new()?
})
}
}
#[test]
fn test_parser_list() {
let parser_list = List::new().unwrap();
assert_eq!(parser_list.get_price().len(), 1);
}

@ -5,6 +5,7 @@ use url::Url;
use anyhow::{Result, anyhow};
#[derive(Debug)]
/// Parser for the darty website
pub struct Darty {
price_selector: Selector,
name_selector: Selector,
@ -46,4 +47,12 @@ impl PriceParser for Darty {
price
})
}
}
#[test]
fn test_parser_darty() {
let darty_parser = Darty::new().unwrap();
assert!(darty_parser.can_parse(&Url::parse("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap()));
assert!(darty_parser.can_parse(&Url::parse("http://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap()));
assert!(darty_parser.can_parse(&Url::parse("https://www.fnace.com").unwrap()) == false);
}

@ -10,12 +10,14 @@ use anyhow::{Result, anyhow};
const USER_AGENT: &str = "Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Firefox/78.0";
/// Struct used to get price from a website
pub struct PriceChecker {
client: Client,
parser_list: parser::List
}
impl PriceChecker {
/// Create a new PriceChecker
pub fn new() -> Result<Self> {
let client = reqwest::blocking::Client::builder().user_agent(USER_AGENT).build()?;
Ok(PriceChecker {
@ -24,6 +26,7 @@ impl PriceChecker {
})
}
/// Get a price from an URL
pub fn get_price(&self, url : Url) -> Result<PriceResult> {
let response = self.client.get(url.clone()).send()?;
let text = response.text()?;
@ -31,4 +34,15 @@ impl PriceChecker {
let parser = *self.parser_list.get_price().iter().find(|p| p.can_parse(&url)).ok_or(anyhow!("No parser can parse {}", url))?;
Ok(parser.parse(&document)?)
}
}
#[test]
fn test_price_checker() {
let price_checker = PriceChecker::new().unwrap();
// Test darty
let price_result = price_checker.get_price(Url::parse("https://www.darty.com/nav/achat/gros_electromenager/refrigerateur-congelateur-refrigerateur-cong/refrigerateur-congelateur_bas/samsung_rb33n300nsa_ef.html").unwrap()).unwrap();
assert!(price_result.name != "");
assert!(price_result.price != 0.);
assert!(price_result.product != "");
}

@ -1,9 +1,13 @@
use std::fmt;
#[derive(PartialEq, Clone, Debug)]
/// Price result
pub struct PriceResult {
/// The name of object
pub name: String,
/// The product type
pub product: String,
/// The price
pub price: f64
}

Loading…
Cancel
Save