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
//! Represent Google Trend Related Topics list.
//!
//! Users searching for your keywords also searched for these topics.
//! You can view by the following metrics:
//! - Top - The most popular topics.
//! Scoring is on a relative scale where a value of 100 is the most commonly searched topic and a value of 50 is a topic searched half as often as the most popular term, and so on.
//! - Rising
//! Related topics with the biggest increase in search frequency since the last time period.
//! Results marked "Breakout" had a tremendous increase, probably because these topics are new and had few (if any) prior searches.

use crate::client::*;
use crate::request_handler::Query;
use serde_json::Value;


#[derive(Clone, Debug, Default)]
pub struct RelatedTopics {
    pub client: Client,
}

impl RelatedTopics {
    pub fn new(client: Client) -> RelatedTopics {
        RelatedTopics { client }
    }

    /// Retrieve Topics data for all keywords.
    ///
    /// Retrieve data for all keywords set within the client.
    ///
    /// Returns a JSON serde Value (serde_json::Value).
    ///
    /// # Example
    /// ```
    /// # use rtrend::{Country, Keywords, Client, RelatedTopics};
    /// let keywords = Keywords::new(vec!["Github vs Gitlab"]);
    /// let country = Country::new("ALL");
    /// let client = Client::new(keywords, country).build();
    ///
    /// let related_topics = RelatedTopics::new(client).get();
    ///
    /// println!("{}", related_topics);
    /// ```
    ///
    /// # Panics
    /// Panic if the client have not been built.
    ///
    /// ```should_panic
    /// # use rtrend::{Country, Keywords, Client, RelatedTopics};
    /// let keywords = Keywords::new(vec!["hacker"]);
    /// let country = Country::new("US");
    ///
    /// // Client not built
    /// let client = Client::new(keywords, country);
    ///
    /// let related_topics = RelatedTopics::new(client).get();
    /// ```
    pub fn get(&self) -> Value {
        let value = self
            .send_request()
            .into_iter()
            .map(|x| x.to_string())
            .collect::<Vec<String>>();
        let joined = value.join(",");
        let form: String = format!("[{}]", joined);

        serde_json::from_str(form.as_str()).unwrap()
    }

    /// Retrieve Topics data for a specific keywords.
    ///
    /// Retrieve data for a specific keyword set within the client.
    ///
    /// Returns a JSON serde Value (serde_json::Value).
    ///
    /// ```rust
    /// # use rtrend::{Country, Keywords, Client, RelatedTopics};
    /// let keywords = Keywords::new(vec!["Github", "Gitlab"]);
    /// let country = Country::new("ALL");
    /// let client = Client::new(keywords, country).build();
    ///
    /// let related_topics = RelatedTopics::new(client).get_for("Gitlab");
    ///
    /// println!("{}", related_topics);
    /// ```
    pub fn get_for(&self, keyword: &str) -> Value {
        let index = self
            .client
            .keywords
            .keywords
            .iter()
            .position(|&x| x == keyword);
        let keyword_index = match index {
            Some(k) => k,
            None => panic!("The keyword {} is not set with the client", keyword),
        };

        self.send_request()[keyword_index].clone()
    }
}