1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
//This file is part of rubygem_api
//
//ruygem_api is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.
//
//rubygem_api is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.
//
//You should have received a copy of the GNU General Public License
//along with Foobar.  If not, see <http://www.gnu.org/licenses/>.

extern crate reqwest;
#[macro_use]
extern crate serde_derive;
extern crate serde;
extern crate serde_json;
#[macro_use]
extern crate failure;
#[macro_use]
extern crate log;

use reqwest::{StatusCode, Url};
use serde::de::DeserializeOwned;

#[derive(Fail, Debug)]
pub enum Error {
    #[fail(display = "{}", _0)]
    Http(reqwest::Error),
    #[fail(display = "{}", _0)]
    Url(url::ParseError),
    #[fail(display = "Not found")]
    NotFound,
}

impl From<reqwest::Error> for Error {
    fn from(e: reqwest::Error) -> Self {
        Error::Http(e)
    }
}

impl From<url::ParseError> for Error {
    fn from(e: url::ParseError) -> Self {
        Error::Url(e)
    }
}

pub struct SyncClient {
    client: reqwest::Client,
    base_url: Url,
}

#[derive(Deserialize)]
pub struct GemDeps {
    pub development: Option<Vec<String>>,
    pub runtime: Option<Vec<String>>,
}

#[derive(Deserialize)]
pub struct GemInfo {
    pub name: String,
    pub authors: String,
    pub version: String,
    pub info: Option<String>,
    pub licenses: Option<String>,
    pub project_uri: String,
    pub gem_uri: String,
    pub homepage_uri: Option<String>,
    pub wiki_uri: Option<String>,
    pub documentation_uri: Option<String>,
    pub dependencies: GemDeps,
}

impl SyncClient {
    /// Instantiate a new synchronous API client.
    ///
    /// This will fail if the underlying http client could not be created.
    pub fn new() -> Self {
        SyncClient {
            client: reqwest::Client::new(),
            base_url: Url::parse("https://rubygems.org/api/v1/gems/").unwrap(),
        }
    }

    fn get<T: DeserializeOwned>(&self, url: Url) -> Result<T, Error> {
        info!("GET {}", url);

        let mut res = {
            let res = self.client.get(url).send()?;

            if res.status() == StatusCode::NOT_FOUND {
                return Err(Error::NotFound);
            }
            res.error_for_status()?
        };

        let data: T = res.json()?;
        Ok(data)
    }

    pub fn gem_info(&self, name: &str) -> Result<GemInfo, Error> {
        let url = self.base_url.join(&format!("{}.json", &name))?;
        let data: GemInfo = self.get(url)?;

        let deserialized_gemdeps = GemDeps {
            development: data.dependencies.development,
            runtime: data.dependencies.runtime,
        };

        let deserialized_geminfo = GemInfo {
            name: data.name,
            version: data.version,
            authors: data.authors,
            info: data.info,
            licenses: data.licenses,
            project_uri: data.project_uri,
            gem_uri: data.gem_uri,
            homepage_uri: data.homepage_uri,
            wiki_uri: data.wiki_uri,
            documentation_uri: data.documentation_uri,
            dependencies: deserialized_gemdeps,
        };

        Ok(deserialized_geminfo)
    }
}

#[cfg(test)]
mod test {
    use SyncClient;

    #[test]
    fn test_client() {
        let client = SyncClient::new();
        let gem_info = client.gem_info("ruby-json").unwrap();
        assert!(gem_info.name.len() > 0);
    }
}