Skip to content
This repository has been archived by the owner on May 2, 2023. It is now read-only.

Update deps #12

Merged
merged 4 commits into from
May 2, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 17 additions & 14 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,31 +7,34 @@ addons:
- libcurl4-openssl-dev
- libelf-dev
- libdw-dev
- binutils-dev

rust:
- nightly
- beta
- stable

before_script:
- |
pip install 'travis-cargo<0.2' --user &&
export PATH=$HOME/.local/bin:$PATH
- cargo install cargo-kcov
- cargo kcov --print-install-kcov-sh | $SHELL

script:
- |
travis-cargo build &&
travis-cargo test &&
travis-cargo bench &&
travis-cargo --only stable doc
cargo build &&
cargo kcov --coveralls &&
cargo bench &&
cargo doc

after_success: |
[ "${TRAVIS_BRANCH}" = master ] &&
[ "${TRAVIS_PULL_REQUEST}" == false ] &&
[ "${TRAVIS_RUST_VERSION}" == stable ] &&
cargo install ghp &&
ghp target/doc &&
git config user.name "Travis Documentation" &&
git config user.email "name@example.com" &&
git push -qf https://${GH_TOKEN}@github.com/${TRAVIS_REPO_SLUG}.git gh-pages 2>&1 > /dev/null

after_success:
# upload the documentation from the build with stable (automatically only actually
# runs on the master branch, not individual PRs)
- travis-cargo --only stable doc-upload
# measure code coverage and upload to coveralls.io
- travis-cargo coveralls --no-sudo
env:
global:
- TRAVIS_CARGO_NIGHTLY_FEATURE=""
- secure: "N0VHBpQ8Co81/xVM+MQn2JZIFkJMNYf1nn+lwxUrfotesRQUAR6Wh3KHl/tgCdAFw+WvYk6kfxQ0ODFJd7iRdlZez2nujwxczXJj4pU+vqcFkJhlr1uBl2HrWeboGVv3M/60HWyERRz5nI5lGR5FP5qgPQTE/KRTnrPYADBgKNo="
6 changes: 4 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,7 @@ description = "Unofficial Rust library for the Twitter API."
documentation = "https://gifnksm.github.io/twitter-api-rs"

[dependencies]
oauth-client = "0.1"
rustc-serialize = "0.3"
oauth-client = "0.2"
serde = "1.0"
serde_derive = "1.0"
serde_json = "1.0"
24 changes: 13 additions & 11 deletions examples/tweet.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,19 @@
unused_qualifications, unused_results)]

extern crate twitter_api as twitter;
extern crate rustc_serialize as rustc_serialize;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;
extern crate oauth_client as oauth;

use oauth::Token;
use std::convert::AsRef;
use std::io;
use std::io::prelude::*;
use std::env;
use std::fs::{File, OpenOptions};
use std::io;
use std::io::prelude::*;
use std::path::Path;
use std::path::PathBuf;
use rustc_serialize::Decodable;
use rustc_serialize::json::{self, Json};
use oauth::Token;

const TWITTER_CONF_FILENAME: &'static str = ".twitter.conf";

Expand All @@ -28,7 +28,7 @@ fn get_home_dir() -> PathBuf {
}
}

#[derive(Debug, RustcEncodable, RustcDecodable)]
#[derive(Debug, Serialize, Deserialize)]
pub struct Config {
pub consumer_key: String,
pub consumer_secret: String,
Expand All @@ -42,16 +42,18 @@ impl Config {
Ok(f) => f,
Err(_) => return None,
};
let conf = Json::from_reader(&mut file).unwrap();
Decodable::decode(&mut json::Decoder::new(conf)).ok()
serde_json::from_reader(&mut file).ok()
}

pub fn write(&self, path_file: &Path) {
let mut file = match OpenOptions::new().write(true).create(true).open(path_file) {
let mut file = match OpenOptions::new()
.write(true)
.create(true)
.open(path_file) {
Ok(f) => f,
Err(e) => panic!("{}", e),
};
let _ = write!(&mut file, "{}\n", &json::encode(self).unwrap());
let _ = write!(&mut file, "{}\n", &serde_json::to_string(self).unwrap());
}

pub fn create(path_file: &Path) {
Expand Down
5 changes: 5 additions & 0 deletions rustfmt.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
reorder_imports = true
reorder_imported_names = true
use_try_shorthand = true
write_mode = "overwrite"

29 changes: 9 additions & 20 deletions src/error.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,20 @@
use std::{error, fmt, string};
use oauth;
use rustc_serialize::json;
use serde_json;
use std::{error, fmt, string};

#[derive(Debug)]
pub enum Error {
OAuth(oauth::Error),
FromUtf8(string::FromUtf8Error),
JsonBuilder(json::BuilderError),
JsonDecoder(json::DecoderError),
Json(serde_json::Error),
}

impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match *self {
Error::OAuth(ref err) => write!(f, "OAuth error: {}", err),
Error::FromUtf8(ref err) => write!(f, "String conversion error: {}", err),
Error::JsonBuilder(ref err) => write!(f, "JSON decoding error: {}", err),
Error::JsonDecoder(ref err) => write!(f, "Decoding to struct error: {}", err),
Error::Json(ref err) => write!(f, "JSON decoding error: {}", err),
}
}
}
Expand All @@ -26,17 +24,15 @@ impl error::Error for Error {
match *self {
Error::OAuth(ref err) => err.description(),
Error::FromUtf8(ref err) => err.description(),
Error::JsonBuilder(ref err) => err.description(),
Error::JsonDecoder(ref err) => err.description(),
Error::Json(ref err) => err.description(),
}
}

fn cause(&self) -> Option<&error::Error> {
match *self {
Error::OAuth(ref err) => Some(err),
Error::FromUtf8(ref err) => Some(err),
Error::JsonBuilder(ref err) => Some(err),
Error::JsonDecoder(ref err) => Some(err),
Error::Json(ref err) => Some(err),
}
}
}
Expand All @@ -53,15 +49,8 @@ impl From<string::FromUtf8Error> for Error {
}
}

impl From<json::BuilderError> for Error {
fn from(err: json::BuilderError) -> Error {
Error::JsonBuilder(err)
}
}


impl From<json::DecoderError> for Error {
fn from(err: json::DecoderError) -> Error {
Error::JsonDecoder(err)
impl From<serde_json::Error> for Error {
fn from(err: serde_json::Error) -> Error {
Error::Json(err)
}
}
52 changes: 25 additions & 27 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,14 @@
#![warn(unused_results)]

extern crate oauth_client as oauth;
extern crate rustc_serialize as rustc_serialize;
#[macro_use]
extern crate serde_derive;
extern crate serde_json;

pub use error::Error;
use oauth::Token;
use std::borrow::Cow;
use std::collections::HashMap;
use oauth::Token;
use rustc_serialize::Decodable;
use rustc_serialize::json::{self, Json};

pub use error::Error;

mod error;

Expand All @@ -31,17 +30,16 @@ mod api_twitter_soft {
json";
}

#[derive(Clone, Debug, RustcEncodable, RustcDecodable)]
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct Tweet {
pub created_at: String,
pub text: String,
}

impl Tweet {
pub fn parse_timeline(json_string: String) -> Result<Vec<Tweet>, Error> {
let conf = try!(Json::from_str(&json_string));
let d = try!(Decodable::decode(&mut json::Decoder::new(conf)));
Ok(d)
let conf = serde_json::from_str(&json_string)?;
Ok(conf)
}
}

Expand All @@ -57,8 +55,8 @@ fn split_query<'a>(query: &'a str) -> HashMap<Cow<'a, str>, Cow<'a, str>> {
}

pub fn get_request_token(consumer: &Token) -> Result<Token<'static>, Error> {
let bytes = try!(oauth::get(api_twitter_oauth::REQUEST_TOKEN, consumer, None, None));
let resp = try!(String::from_utf8(bytes));
let bytes = oauth::get(api_twitter_oauth::REQUEST_TOKEN, consumer, None, None)?;
let resp = String::from_utf8(bytes)?;
let param = split_query(&resp);
let token = Token::new(param.get("oauth_token").unwrap().to_string(),
param.get("oauth_token_secret").unwrap().to_string());
Expand All @@ -77,11 +75,11 @@ pub fn get_access_token(consumer: &Token,
-> Result<Token<'static>, Error> {
let mut param = HashMap::new();
let _ = param.insert("oauth_verifier".into(), pin.into());
let bytes = try!(oauth::get(api_twitter_oauth::ACCESS_TOKEN,
consumer,
Some(request),
Some(&param)));
let resp = try!(String::from_utf8(bytes));
let bytes = oauth::get(api_twitter_oauth::ACCESS_TOKEN,
consumer,
Some(request),
Some(&param))?;
let resp = String::from_utf8(bytes)?;
let param = split_query(&resp);
let token = Token::new(param.get("oauth_token").unwrap().to_string(),
param.get("oauth_token_secret").unwrap().to_string());
Expand All @@ -93,19 +91,19 @@ pub fn get_access_token(consumer: &Token,
pub fn update_status(consumer: &Token, access: &Token, status: &str) -> Result<(), Error> {
let mut param = HashMap::new();
let _ = param.insert("status".into(), status.into());
let _ = try!(oauth::post(api_twitter_soft::UPDATE_STATUS,
consumer,
Some(access),
Some(&param)));
let _ = oauth::post(api_twitter_soft::UPDATE_STATUS,
consumer,
Some(access),
Some(&param))?;
Ok(())
}

pub fn get_last_tweets(consumer: &Token, access: &Token) -> Result<Vec<Tweet>, Error> {
let bytes = try!(oauth::get(api_twitter_soft::HOME_TIMELINE,
consumer,
Some(access),
None));
let last_tweets_json = try!(String::from_utf8(bytes));
let ts = try!(Tweet::parse_timeline(last_tweets_json));
let bytes = oauth::get(api_twitter_soft::HOME_TIMELINE,
consumer,
Some(access),
None)?;
let last_tweets_json = String::from_utf8(bytes)?;
let ts = Tweet::parse_timeline(last_tweets_json)?;
Ok(ts)
}