Skip to content

Commit 96a6593

Browse files
authored
Merge pull request #7 from ernestas-poskus/features/add_method_read_from_response
Add method from_response
2 parents d0351ec + 36c90ec commit 96a6593

File tree

1 file changed

+11
-7
lines changed

1 file changed

+11
-7
lines changed

src/lib.rs

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ use url::Url;
4444
use hyper::Client;
4545
use hyper::header::UserAgent;
4646
use hyper::status::StatusCode;
47+
use hyper::client::Response;
4748

4849
const USER_AGENT: &'static str = "robotparser-rs (https://crates.io/crates/robotparser)";
4950

@@ -247,7 +248,7 @@ impl<'a> RobotFileParser<'a> {
247248
let client = Client::new();
248249
let request = client.get(self.url.clone())
249250
.header(UserAgent(USER_AGENT.to_owned()));
250-
let mut res = match request.send() {
251+
let res = match request.send() {
251252
Ok(res) => res,
252253
Err(_) => {
253254
return;
@@ -260,16 +261,19 @@ impl<'a> RobotFileParser<'a> {
260261
status if status >= StatusCode::BadRequest && status < StatusCode::InternalServerError => {
261262
self.allow_all.set(true);
262263
}
263-
StatusCode::Ok => {
264-
let mut buf = String::new();
265-
res.read_to_string(&mut buf).unwrap();
266-
let lines: Vec<&str> = buf.split('\n').collect();
267-
self.parse(&lines);
268-
}
264+
StatusCode::Ok => self.from_response(res),
269265
_ => {}
270266
}
271267
}
272268

269+
/// Reads the HTTP response and feeds it to the parser.
270+
pub fn from_response(&self, mut response: Response) {
271+
let mut buf = String::new();
272+
response.read_to_string(&mut buf).unwrap();
273+
let lines: Vec<&str> = buf.split('\n').collect();
274+
self.parse(&lines);
275+
}
276+
273277
fn _add_entry(&self, entry: Entry<'a>) {
274278
if entry.has_useragent("*") {
275279
// the default entry is considered last

0 commit comments

Comments
 (0)