Skip to content

Commit

Permalink
Merge pull request #5 from libellis/feature/user-domain-model
Browse files Browse the repository at this point in the history
Add User Domain Model that Maps Soundcloud API response.
  • Loading branch information
kaedub authored Apr 13, 2020
2 parents 3e37e81 + ebdfecd commit c370018
Show file tree
Hide file tree
Showing 3 changed files with 66 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,6 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
serde = "1.0.106"
serde_json = "1.0.51"
serde_derive = "1.0.106"
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
#[macro_use]
extern crate serde_derive;

pub mod user;
pub use user::*;

#[cfg(test)]
mod tests {
#[test]
Expand Down
57 changes: 57 additions & 0 deletions src/user.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
use serde::Deserialize;

#[derive(Deserialize)]
struct User {
id: u32,
username: String,

// TODO: Map these two to valid URL type rather than String.
avatar_url: String,
permalink_url: String,

city: String,
// TODO: Map this to country code to better utilize type system.
country: String,
track_count: u32,
private_tracks_count: u32,
}

#[cfg(test)]
mod tests {
use super::User;

#[test]
fn mapping_from_api_response_works() {
let mock_api_response = r#"
{
"id": 3207,
"permalink": "jwagener",
"username": "Johannes Wagener",
"uri": "https://api.soundcloud.com/users/3207",
"permalink_url": "https://soundcloud.com/jwagener",
"avatar_url": "https://i1.sndcdn.com/avatars-000001552142-pbw8yd-large.jpg?142a848",
"country": "Germany",
"full_name": "Johannes Wagener",
"city": "Berlin",
"description": "<b>Hacker at SoundCloud</b>\r\n\r\nSome of my recent Hacks:\r\n\r\nsoundiverse.com \r\nbrowse recordings with the FiRe app by artwork\r\n\r\ntopbillin.com \r\nfind people to follow on SoundCloud\r\n\r\nchatter.fm \r\nget your account hooked up with a voicebox\r\n\r\nrecbutton.com \r\nrecord straight to your soundcloud account",
"discogs_name": null,
"myspace_name": null,
"website": "http://johannes.wagener.cc",
"website_title": "johannes.wagener.cc",
"online": true,
"track_count": 12,
"playlist_count": 1,
"followers_count": 416,
"followings_count": 174,
"public_favorites_count": 26,
"plan": "Pro Plus",
"private_tracks_count": 63,
"private_playlists_count": 3,
"primary_email_confirmed": true
}"#;

let u: User = serde_json::from_str(mock_api_response).unwrap();

assert_eq!(u.id, 3207);
}
}

0 comments on commit c370018

Please sign in to comment.