Skip to content

Commit

Permalink
Add thepasteb.in as gist host (fix #10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Xion committed May 4, 2017
1 parent 992b3bc commit 2a4cebc
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/hosts/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod mozilla;
mod paste;
mod pastebin;
mod sprunge;
mod thepasteb_in;


use std::collections::HashMap;
Expand Down Expand Up @@ -108,6 +109,7 @@ lazy_static! {
mibpaste::ID => Arc::new(mibpaste::Mibpaste::new()) as Arc<Host>,
sprunge::ID => Arc::new(sprunge::Sprunge::new()) as Arc<Host>,
dpaste_de::ID => Arc::new(dpaste_de::create()) as Arc<Host>,
thepasteb_in::ID => Arc::new(thepasteb_in::create()) as Arc<Host>,
};
}
#[cfg(not(test))]
Expand Down
58 changes: 58 additions & 0 deletions src/hosts/thepasteb_in.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
//! Module implementing thepasteb.in as gist host.

use regex::Regex;

use hosts::common::Basic;


/// thepasteb.in host iD.
pub const ID: &'static str = "tpb";

/// Craate thepasteb.in host implementation.
pub fn create() -> Basic {
Basic::new(ID, "thepasteb.in",
"https://thepasteb.in/raw/${id}",
"https://thepasteb.in/p/${id}",
Regex::new("[0-9a-zA-Z]+").unwrap()).unwrap()
}


#[cfg(test)]
mod tests {
use super::create;

#[test]
fn html_url_regex() {
let host = create();
let html_url: String = host.html_url_origin();

let valid_html_urls: Vec<(/* URL */ String,
/* ID */ &'static str)> = vec![
(html_url.clone() + "/p/abc", "abc"), // short
(html_url.clone() + "/p/a1b2c3d4e5", "a1b2c3d4e5"), // long
(html_url.clone() + "/p/43ffg", "43ffg"), // starts with digit
(html_url.clone() + "/p/46417247", "46417247"), // only digits
(html_url.clone() + "/p/MfgT45f", "MfgT45f"), // mixed case
];
let invalid_html_urls: Vec<String> = vec![
html_url.clone() + "/a/b/c", // too many path segments
html_url.clone() + "/p/abc/", // trailing slash
html_url.clone() + "/p//", // ID must not be empty
html_url.clone() + "/p", // no ID at all
html_url.clone() + "/", // no path at all
"http://example.com/fhdFG36ok".into(), // wrong thepasteb.in domain
"foobar".into(), // not even an URL
];

let html_url_re = host.html_url_regex();
for (ref valid_url, id) in valid_html_urls {
let captures = html_url_re.captures(valid_url)
.expect(&format!("Paste's HTML URL was incorrectly deemed invalid: {}", valid_url));
assert_eq!(id, captures.name("id").unwrap());
}
for ref invalid_url in invalid_html_urls {
assert!(!html_url_re.is_match(invalid_url),
"URL was incorrectly deemed a valid gist HTML URL: {}", invalid_url);
}
}
}

0 comments on commit 2a4cebc

Please sign in to comment.