Add doc and tests

This commit is contained in:
Rémi BERTHO 2020-05-24 18:16:32 +02:00
parent 4bd5e1eb5e
commit 3ace078024
Signed by: dalan
GPG key ID: EE3B917931C07B64
4 changed files with 39 additions and 0 deletions

View file

@ -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);
}

View file

@ -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,
@ -47,3 +48,11 @@ impl PriceParser for Darty {
})
}
}
#[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);
}

View file

@ -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()?;
@ -32,3 +35,14 @@ impl PriceChecker {
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 != "");
}

View file

@ -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
}