Day twenty: moviemash

October 15, 2015

I like movies, who doesn’t? Along with many other developers I like to spend my time creating kickass applications and when I do get around to watching a movie. I want it to be a great one! This is where moviemash comes in!

live demo

Let’s start with defining the specs of the app:

  • search for movies
  • show the most popular movies

Okay, simple enough. With the specs out of the way. Let’s dive into development.

First I went searching for an API that fits the bill. After looking through the documentation of https://www.themoviedb.org I decided this was the best match. Unfortunately, there wasn’t a reliable package for the API yet. Meaning that I would need to create my own package. No biggie, I have become more comfortable with Meteor’s package system.

meteor create tmdb-api --package
meteor add tmdb-api

These two lines create an empty package and adds it to our main project, Woohoo that was easy! This package has to get data over HTTP and return the result to the package caller. Things couldn’t be easier.

TMDB = {
  search: function (query) {
    return HTTP.get(
      `https://api.themoviedb.org/3/search/movie`, {
        params: {
          api_key: service.apiKey,
          query: query
        }
      }
    );
  },
  popular: function () {
    return HTTP.get(
      `https://api.themoviedb.org/3/movie/popular`, {
        params: {
          api_key: service.apiKey
        }
      }
    );
  }

TMDB is an object that is available everywhere on the server. The client can call a server method which uses the API to return the results. This is an example on how moviemash uses the package:

Meteor.methods({
  search: function (query) {
    try {
      let response = TMDB.search(query);
      return response.data.results;
    } catch (e) {
      console.log(e);
      return [];
    }
  }});

That is all there is to it. To recap:

  1. client calls a server method
  2. server calls the package
  3. package returns the requested data With the API done. The rest of the app was build blazing fast.

thanks for reading

Meteor is becoming more awesome for me everyday! It’s a great time to be a Meteor developer! Thank you for reading and don’t forget to leave a star on the repo.

Comments