Skip to content

Commit

Permalink
feat(examples): add examples
Browse files Browse the repository at this point in the history
Add `all`, `latest`, and `random` example.
  • Loading branch information
indiv0 committed Jun 22, 2016
1 parent 01b0962 commit c257d38
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 0 deletions.
29 changes: 29 additions & 0 deletions examples/all.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
// Copyright (c) 2016 Nikita Pekin and the xkcd_rs contributors
// See the README.md file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate hyper;
extern crate xkcd;

fn main() {
let client = hyper::Client::new();

// Retrieve the latest comic ID.
let latest_id = xkcd::comics::latest(&client).unwrap().num;

// Retrieve all the comics.
let mut all_comics = Vec::new();
for i in 1..latest_id + 1 {
// Skip 404 because that's the 404 page and not a comic.
if i == 404 {
continue;
}

all_comics.push(xkcd::comics::get(&client, i).unwrap());
}
}
19 changes: 19 additions & 0 deletions examples/latest.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) 2016 Nikita Pekin and the xkcd_rs contributors
// See the README.md file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate hyper;
extern crate xkcd;

fn main() {
let client = hyper::Client::new();

// Retrieve the latest comic.
let latest_comic = xkcd::comics::latest(&client);
println!("Latest comic: {:?}", latest_comic);
}
23 changes: 23 additions & 0 deletions examples/random.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
// Copyright (c) 2016 Nikita Pekin and the xkcd_rs contributors
// See the README.md file at the top-level directory of this distribution.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

extern crate hyper;
extern crate xkcd;

fn main() {
let client = hyper::Client::new();

// Retrieve a random comic.
let random_comic = xkcd::random::random(&client);
println!("Random comic: {:?}\n", random_comic);

// Retrieve a random comic in the range `start <= x < end`.
let random_comic_in_range = xkcd::random::random_in_range(&client, (1..50));
println!("Random comic in range: {:?}", random_comic_in_range);
}

0 comments on commit c257d38

Please sign in to comment.