Skip to content

FasTracks/FasTracks-BE

Repository files navigation

FasTracks README (BE)

FasTracks provides a streamlined approach to workout playlist generation. Once the user selects genre preference and workout type, they are provided with an optimized playlist using metadata from Spotify, and criteria held in the FasTracks backend.

The project follows service-oriented architecture (SOA) and is divided into two different repositories:

FasTracks BE API fits into our projects SOA by providing the endpoint and backend logic that allows us to easily connect to any given front end service as a way of being able to quickly adapt.

FasTracks BE

This application consists of two repositories that utilize Spotify's API to create playlists for workouts. I don’t like working out, but it’s more fun and I have a better workout when I have a good playlist. But I don’t always like making playlists either.

This is the backend repository that interacts with the Spotify API and delivers playlist data to the front end application. FasTracks-FE provides the backend with authentication and user playlist preferences. FasTracks-BE takes that input and, based on the parameters, sends requests to Spotify to generate a playlist inside the user's Spotify account. FasTracks-BE returns the important playlist details to FasTracks-FE in JSON format so the user can enjoy their new playlist.

Getting Started

The fastest way to start working out using FasTracks is to visit our deployed app here.

If you would like to run the application locally, you will need Rails 7.X.X and to clone both FasTracks-FE and FasTracks-BE, since the application follows service-oriented architecture. Within each repo, follow the steps below.

  1. bundle install
  2. rails db:{drop,create}
  3. bundle exec rspec -- to see TDD in action
  4. rails server

Then, in your browser, visit localhost:3000 and follow the prompts on screen.

Due to development constraints from Spotify, FasTracks is currently limited to invite-only users. Please message one of our contributors with to be added to our list of approved users. Please be sure to include the email address associated with your Spotify account. Alternatively, users may create their own app through Spotify's developer portal and reconfigure the Client ID and Client Secret in their local copy of the FE repo.

Prerequisites

For the fastest startup, we recommend visiting the deployed app here.

However, if you prefer to get your hands dirty and understand our app, feel free to fork and clone this repo as well as FasTracks-BE. We recommend a basic understanding of the following concepts before diving in to our code:

  • Ruby on Rails Applications
  • Service oriented architecture
  • Oauth 2.0
  • Spotify API

Requirements for the software and other tools to build, test and push

  • Rails 7.X
  • Ruby 3.2.2
  • Spotify API V1

Notes on Data Flow

FasTracks FE x FasTracks FE

Only one endpoint is exposed for FasTracks BE: /api/v1/playlists. The BE receives proper authentication and playlist criteria, an BE makes a POST request to the Spotify API and a playlist JSON is returned.

FasTracks FE sends the user's auth token and playlist preferences in the parameters of the POST request to the FasTracks BE endpoint /api/v1/playlists

Example Request: HTTP://<backendurl/path>?code=<USER_ACCESS_TOKEN>&genre=<SELECTED_GENRE>&workout=<SELECTED_WORKOUT>

FasTracks BE then returns playlist details to FasTracks FE in JSON Format including:

  • Playlist name
  • Playlist Spotify URL
  • Songs Album Artwork URL
  • Songs track title
  • Songs Artist
  • Playlist song count

Visit this link for a sample return

FasTracks FE x Spotify API

A POST request to the FasTracks BE playlist endpoint results in four calls from FasTracks BE to Spotify:

  • GET /recommendations sends the playlist's song preferences including BPM, Genre, and count. Among other data it returns each track's unique URI.
  • GET /me retrieves the current user's details, including user ID
  • POST /users/<USER_ID>/playlists creates a new, empty playlist for the user
  • POST /playlists/<PLAYLIST_ID>/tracks the track URI's are sent as an array, and the playlist details are returned.

Each of these requests includes the Auth code in the header. For examples of the returns of each of these requests, visit this folder of the FasTracks BE repo.

Installing

bundle install
rails db:{create,migrate}

Gems Included:

  • gem "pry-rails"
  • gem "rspec-rails"
  • gem "factory_bot_rails"
  • gem "faker"
  • gem "shoulda-matchers"
  • gem "capybara"
  • gem "faraday"
  • gem "webmock"
  • gem "vcr"

Note on schema: YAGNI. The Backend API has intentionally been desinged to not require a database.

Running the tests

Follow commands below to run the app test suite.

bundle exec rspec

Sample Tests

See below for an example test from our suite. This is part of the spotify_facade_spec that can be found under the spec/facades directory in the repo.

Tests below include many of the pivotal operations of our application, including:

  • Retrieving User ID
  • Creating a playlist for the user
  • Finding songs that match the workout
  • Populating the playlist
    describe "::generate_spotify_playlist" do
      before(:each) do
        user_response = File.read("spec/fixtures/user/user.json")
        # Get user id
        stub_request(:get, "https://api.spotify.com/v1/me")
          .with(headers: {"Authorization" => "Bearer 1234"})
          .to_return(status: 200, body: {id: "12345"}.to_json, headers: {})
        # Create playlist
        stub_request(:post, "https://api.spotify.com/v1/users/12345/playlists")
          .with(
            headers: {"Authorization" => "Bearer 1234", "Content-Type" => "application/json"},
            body: "{\"name\":\"FT HIIT Pop\",\"public\":true,\"description\":\"Playlist created by FasTracks on Spotify API\"}"
          )
          .to_return(status: 201, body: {id: "testID"}.to_json, headers: {})
        # Add tracks to playlist
        stub_request(:post, "https://api.spotify.com/v1/playlists/testID/tracks")
          .with(
            headers: {"Authorization" => "Bearer 1234", "Content-Type" => "application/json"},
            body: "{\"uris\":[\"spotify:track:4iV5W9uYEdYUVa79Axb7Rh\"]}"
          )
          .to_return(status: 201, body: "".to_json, headers: {})
      end

      it "gets a user_id, creates a playlist, and adds tracks to it" do
        playlist = File.read("spec/fixtures/playlists/get_playlist.json")
        
        stub_request(:get, "https://api.spotify.com/v1/playlists/testID")
        .with(
          headers: {"Authorization" => "Bearer 1234"}
          )
        .to_return(status: 200, body: playlist, headers: {})

        results = SpotifyFacade.generate_spotify_playlist("1234", ["spotify:track:4iV5W9uYEdYUVa79Axb7Rh"], "FT HIIT Pop")

        expect(results[:status]).to eq(200)
        expect(results[:data]).to be_a(Hash)
        expect(results[:data]).to have_key(:id)
      end
    end

Deployment

This project is deployed using heroku here

drawing

Built With

  • Spotify API
  • Ruby on Rails
  • Faraday
  • Webmock

drawing

Contributing

Contributions are welcome and can be submitted by pull request.

Versioning

The current version (V1) of our application is live here on github.

Authors

License

This project is not licensed and is open source.

Acknowledgments

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Contributors 4

  •  
  •  
  •  
  •  

Languages