Using the Bluemix Insights for Twitter Service with a Rails App

by Brian Cohen FalahMarch 23, 2016
The article guides you through creating the IBM Bluemix Insights for Twitter service and binding it to a Rails app.

The tutorial also provides sample code for a proof of concept.

 

A step-by-step tutorial

Today, I will show how to use one of the many services provided by Bluemix in a Rails application.

It is a really easy process once you get the idea. Here is my working code at GitHub. Now, let’s go through the main steps.

  1. Create a Ruby web application from the Bluemix dashboard.
  2. Create the Insights for Twitter service and bind it to your application.
  3. ibm-bluemix-twitter-insights-v1
  4. Click your application on the Bluemix dashboard, go to the Start Coding section, and follow the instructions for downloading your application.
  5. Run bundle install.
  6. Install the Figaro gem following these instructions and add the necessary environment variables to the config/application.yml file.

    To find these variables, go to your application summary on the Bluemix dashboard and click Show Credentials on the Insights for Twitter service tile. Once there, the needed variables are the ones inside the credentials key. All environment information related to services bound to your application is stored in the VCAP_SERVICES environment variable.

    Here are these values in your Bluemix application console.

    ibm-bluemix-insights-for-twitter-vcap_services-v1

    I added the variables required for connecting to the Insights for Twitter service, and here is my sample file application.yml.sample. The variables from this file will be automatically set as environment variables. The file should be ignored by Git, but it will be uploaded to your Bluemix instance unless you create a .cfignore file with the files and folders you don’t want to be uploaded.

    If you don’t want to use the file, you can configure the same environment variables inside the Bluemix user interface. For doing so, click your application tile, go to Environment Variables on the left navigation pane, and click USER-DEFINED.

    You will be able to access these variables with Figaro.env.key_name or with ENV["key_name"].

  7. Create a class for communicating with the Twitter service.

    I decided to create it inside the services folder here. Remember to add the services folder to application.rb for autoloading it:

    config.autoload_paths << Rails.root.join('app/services')

    Here is the code for the controller: searches_controller.rb.

  8. For the front end, use the example provided in the service documentation and available at https://cdetestapp.mybluemix.net/.

    You can download the .html, .css, .js, and image files with your browser or from the code I provide.

    Then, create a new controller (for example, SearchesController), create an action inside it (for example, query), its view (for example, query.html.erb), and fill it in with the downloaded HTML.

    You can copy the images, .css, and .js files to the assets folder and include them properly in the view.

    Make that action the root of your application by changing routes.rb:

    root :to => 'search#query'
  9. Add another endpoint to SearchesController called count_tweets and the corresponding route.

    This endpoint will use our class service and return a JSON response. Using a class service is the suggested way of making requests. If you make them directly from the browser as in the example given, you will probably have problems with CORS, and you will be exposing sensitive information.

    Finally, you will need to change the URL used in the countTweets() function inside the JavaScript file to work with our previously created endpoint (count_tweets).

    You won’t be able to use the URL helpers directly inside the JavaScript file, so you can define a JavaScript variable with that URL in your html.erb and then use it in the .js file.

    Here are the HTML and JS views:

    Also, you could use the gem from https://github.com/gazay/gon for solving the problem of sending Rails variables to JS files or whatever you want.

  10. Run cf push app-name.

It should work!

 

Conclusion

I think there’s no big difference between using a service provided by Bluemix and services from other vendors when what you are doing is a simple call to an API.

As for me, the most important advantages of such services are the easiness of subscription, the fact that they provide you with credentials in an organized and standardized way, and a possible discount because of buying them from Bluemix.

 

Design Your Custom Kubernetes Training

Related reading

  •  
  •  
  •