How to use Twitter API using Node.js


Today We will be learning How to talk to ‘Twitter’ using Twitter API. With ‘Twit Package’ of NPM(Node Package Manager), It got easier to directly talk to Twitter i.e Post tweets, read tweets, post multimedia and do other stuff, Sounds interesting to you, but wondering How to do it?
Come let’s do it.

The very first thing you need to install is Node into your system.
You can download node from here Click here to download Node
Once you have downloaded Node, check the version you are using by typing ‘node –version’

Now to start a new project we will make a new folder called ‘Twit’ or any other of your choice.This will be the project folder which will have all the files. Now using the command line we will go the folder and type ‘npm init’. The moment you say npm init , It will ask a few questions like
name
version
description
entry point
test command
git repository
keywords
author
license
Once it is done You will have a package.json file inside the project folder. A package.json file is a json file which is a settings or config file and contains “dependencies”.

After this, we will install the ‘Twit’ package from npm using the following command ‘npm install twit – -save’.Using — save as an argument to npm install means save this as a reference to your package.json file ie. The dependency will be added to your package.json file

Below is my package.json file which has ‘twit’ dependency.

package-json

Now we are done with installing the dependency.

Let’s set up our project to use Twitter API.Now we will make a js file inside our project
For example twitter-example.js

Inside our js file, we will write the code to connect to the Twitter API. Now how to run this file All you need to do is type ‘node twitter-example.js’. The moment you write this the code inside the js file will start to execute. You can also run your js file like this ‘npm start’ but for that, you will have to make changes to your package.json

Add a line
“start” : “node twitter-example.js” inside script like this

package-json

Alright, Now we are done setting up the dependencies. Now we will go to dev.twitter.com to generate
1.consumer_key
2.consumer_secret
3.access_token
4.access_token_secret
If you are wondering what are these keys and tokens for, they are secret keys and access tokens which give access to use the twitter API and let you play around with your account through your application/project. So now we will generate them first. Let’s see How it is done.

Step 1. Goto dev.twitter.com
Step2. Scroll down to the bottom and find Tools under this find ‘Manage my apps’
Step 3. On top right you will find ‘Create new app’
Step 4. Fill in your details Name, Description, website and agree to the Agreement
Step5 . Create your twitter Application
Step 6. Goto keys and access token, find your keys there
Step 7. Hit Create my access token and generate access token
If you are having trouble creating one please follow the link below
Generate Keys and tokens
Now you will have all the above-said keys and tokens with you. We will make use of those keys in out js file.

1.Search Post using the Twitter API.
Now that we have everything set up we will search for the particular post. Now, How do we do that? Continue reading

Posted in Scala | Leave a comment

A Simple Example using elm


In this blog, we will see how to build a simple application using elm. Elm is a functional language that compiles into javascript with following features:-

1)  Great Performance
2)  No run-time exception
3)  Javascript Interop
4)  Friendly error message
5)  Ability to write functional code

Elm is getting popular day by day. According to Slant community, it is the 2nd most popular language, which compiles into Javascript. I have just started learning Elm and am still exploring this.

This is a very simple example to get country code of any location.  I have taken help from Elm tutorial.

Every Elm program in divided into 3 parts

1) Model

model

2)  Update

update

3)  View

view

For complete source code, go to A Simple Application using Elm.

KNOLDUS-advt-sticker

Posted in AJAX, Functional Programming, HTML, JavaScript, knoldus, Web, web application | Tagged , , , , , | Leave a comment

Mailchimp and Mandrill- The ‘ Hominidae’ kingdom


Hi all,

Knoldus had organized an hours session on 16th December 2016 at 4:00 PM. Topic was Mailchimp and Mandrill- The ‘ Hominidae’ kingdom in Scala . Many enthusiasts  joined and learned from the session. I am  sharing the slides of the session here. Please let me know if you have any question related to linked slides.

You can also watch the video below…

Posted in Scala | Leave a comment

Handling HTTPS requests with Akka-HTTPS Server


Hi guys,

In my last blogs I explained how one can create a self-signed certificate and KeyStore in PKCS12. You can go through the previous blog, as we’ll be needing certificate and keystore  for handling HTTPS requests.

  1. https://blog.knoldus.com/2016/10/18/create-a-self-signed-ssl-certificate-using-openssl/
  2. https://blog.knoldus.com/2016/10/26/how-to-create-a-keystore-in-pkcs12-format/

Akka-HTTP provides both Server-Side and Client-Side HTTPS support.

In this blog I’ll be covering the Server-Side HTTPS support.

Let’s start with “why do we need server-side HTTPS support?”

If we want the communication between the browser and the server to be encrypted we need to handle HTTPS request.  HTTPS is often used to protect highly confidential online transactions like online banking and online shopping order forms.

Akka-HTTP supports TLS(Transport Layer Security).

For handling the HTTPS request we need to have the SSL certificate and the KeyStore. Once you have generated both you can go through the example.

In this example, you will see how easily you can handle HTTPS  request. Akka HTTP provides support for low level and high level HTTP server APIs. I’ve used high level API in this example.

Add following dependencies in your build.sbt file:

libraryDependencies ++= {
  val AkkaHttpVersion   = "2.4.8"
  Seq(
    "com.typesafe.akka" %% "akka-http-testkit" % "2.4.8",
    "com.typesafe.akka" %% "akka-http-experimental" % "2.4.8", 
    "org.scalatest" %% "scalatest" % "2.2.6" % "test"
  )
}

You can add the generated KeyStore in the resource package of your project and pass the name of the keyStore in Boot.scala.

val keystore: InputStream = getClass.getClassLoader
.getResourceAsStream("keystore.pkcs12")

You would also  need to provide the password which you have used for generating the KeyStore.

val password: Array[Char] = "akka-https".toCharArray
ks.load(keystore, password)

It is recommended not to store passwords in code, you can read it from somewhere safe like application.conf .

In my Boot.scala you’ll find following line:

val keyManagerFactory: KeyManagerFactory = KeyManagerFactory.
getInstance("SunX509")
val tmf: TrustManagerFactory = TrustManagerFactory.getInstance("SunX509")

We need KeyManagerFactory and TrustManagerFactory to initialize  SSLContext.

TrustManager determines whether remote connection should be trusted or not i.e. whether remote party is who it claims to and KeyManager decides which authentication credentials should be sent to the remote host for authentication during SSL handshake.

You can download Postman or any other rest client for making the request on your server.

You can find the complete example  here .

Enjoy 🙂

Reference:

http://doc.akka.io/docs/akka/2.4.8/scala/http/server-side-https-support.html

KNOLDUS-advt-sticker

Posted in Akka, akka-http, Scala, Security, Security Audit, Security Checklist, Security Controls, Security Guidelines | Tagged , , , , , , , , , , , , , , , , , , , | 3 Comments

AWS | Cleaning up your Amazon ECS resources


In my previous blog posts on AWS (Introduction to Amazon ECS | Launch Amazon ECS cluster | Scaling with Amazon ECS | Deploy updated Task definition/Docker image), I had given an overview about what is Amazon ECS with a walk-through on how to launch Amazon ECS and then deploy sample app by creating a task definition, scheduling tasks and configuring a cluster and to scale in / scale out the same on Amazon ECS and we have also gained the knowledge on how to create a new revision for the existing task definition to deploy the latest updated docker image.

In this we will have a look on cleaning up your Amazon ECS resources that we have created so far. Once you have launched the Amazon ECS cluster and try to terminate container instances in order to clean up the resources then you won’t be able to do so because of cloud formation.

Below are the step that you have to follow in order to clean up Amazon ECS recourses :

1. Scale Down Services :  Firstly, we have to scale down the desired count of tasks to ‘0’ of all the services running in a cluster, so that Amazon ECS does not try to start new tasks on our container instances while we are cleaning up.

1-copy

1.png

Continue reading

Posted in Amazon, Amazon EC2, AWS, AWS Services, Scala | Tagged , , , , | 3 Comments

Learning Markdown


Markdown is a lightweight markup language with plain text formatting syntax designed so that it can be converted to HTML and many other formats using a tool by the same name. Markdown is often used to format readme, for writing messages in online discussion forums, and to create rich text using a plain text editor.

So, here is a quick introduction to some of the elements of markdown.

  • Headers

Markdown provides 6 headers(H1-H6) similar to HTML.

To insert an H1 heading, you need to use a single #. For H2 use ## and so on till H6.

Eg: # This is a Header

  • Italics

If you want to write some content in italics, you can use * or _ (single hyphen).

Eg: This *word* is in italics or This _word _ is in italics.

  • Bold

To bold some content, you can use ** or __ (2 hyphens).

Eg: This **word** is in bold or This __word__ is in bold

  • Lists

To create an ordered list use two spaces and then the item number.

Eg: Here Spaces are determined using . (dot)

..1. Item1

..2. Item2 

To create an unordered list use two spaces and then *, – or +.

Eg: Here Spaces are determined using . (dot)

..* Item1

..* Item2

  • Links

To create a link, you can use the following syntax

1. [some text](link)

Eg:  [This is a link to google](https://www.google.com)

You can also provide title to a link.

Eg: [This is a link to google](http://www.google.com "Google")

2. You can also link to a header in the same file. Spaces in the heading are substituted by – (hyphen).

Eg: # Heading 1

To create a link to the above header :

[This is a link to Heading 1](#heading-1)

3. URLs in angular brackets acts as a link.

Eg:<http://www.google.com>

4. To create a reference style link:

[This is a link][reference]

[reference]: http://www.google.com

  • Images

Images can be used in two ways:

1. Inline style

![alt text](http://media02.hongkiat.com/ww-flower-wallpapers/roundflower.jpg "Image title")

2. Reference style

![alt text][img]

[img]: http://media02.hongkiat.com/ww-flower-wallpapers/roundflower.jpg "Image title"

  • Code Highlighting

For inline code-highlighting use ` (backtick).

Eg: This is `code`.

Block of code can be written using “` (three backticks) or indenting the code with four spaces.

Eg:

```
This is code
```
  • Tables

To create table in markdown, you can use | (pipes) and – (hyphens). Each column must be separated using — (3 hyphens)

Eg:

|Col 1|Col 2|
|—|—|
|data1|data2|

The outer pipes are optional.

  • Blockquotes

To add blockquotes, you can use >.

Eg: >This is a blockquote

  • Inline Html

You can also write raw Html in your markdown

Eg:

<dl><dt>Term</dt><dd>Description</dd></dl>

  •  Horizontal rule

You can use 3 or more * (astericks), – (hyphens) or _ (underscores ) to create an horizontal rule.

Eg: *** or ---

Here is the link for a sample markdown file:

Markdown File

IDE specifications:

You can use Intellij Idea to edit your md files. It provides the following plugins named:

Markdown Navigator and Markdown Support that helps to view styling of your md files.

Posted in git, github, Scala | Leave a comment

Tableau: Getting into Tableau Public


graph1Big Data visualization and Business Intelligence got so easy using Tableau, millions and billions of records can be analyzed in just one go whether your data format is excel, csv, text or database, Tableau make it easy for you. So finally you have make your mind to generate visualizations using Tableau and want to know what are the heights of Tableau in visualizations?. You are in right place. I will cover the major questions/doubts/scenarios related to Tableau here.

  • So before investing your amount/money you want to use/test Tableau?
  • Want to generate your own graph, using your own data?
  • Want to embed your generated graph in your own website with user interactions?
  • Want to check the heights of Tableau as per the data limits?

First thing that you have to do is to download and install Tableau Public, so prerequisite are:

  •  You must have windows or mac system with some basic standard configurations.
  • Tableau Public Desktop software. (Download from here)
  • One data file (you can download it from here)

Continue reading

Posted in apache spark, big data, Scala, Spark, Tableau | Tagged , , , , , , , | Leave a comment

HTML, CSS and JavaScript coding conventions


Knoldus organized a session on “HTML, CSS and JavaScript style guide and coding conventions”. It covered the best standards to be followed while writing codes in HTML and JavaScript and also,styling using CSS. The basic conventions that we forget while coding are discussed in this session.

Hope it will help you in writing a clean and understandable code.

Slides for this session –

Video for this session –

Any related questions are acceptable 🙂

Thanks

Posted in Scala | Tagged , , , , | Leave a comment

Prototype & Prototype chaining with Object Oriented JavaScript


We have always seen that every object in JavaScript has some pre-defined methods.  To know about it we have find answers of these questions.
What is prototype ?
What is prototype chaining ?

What does hasOwnProperty do ?
What is __proto__ ?

We will have a look into it and we will come to know where do all these methods come from.
Nearly all objects in JavaScript are instances of Object. That means all the objects in JavaScript, inherit the properties and methods from Object.prototype. This is called Prototype chaining. This is a very powerful and potentially dangerous mechanism to override or extend object behavior.

Objects created using new keyword, inherit from a prototype called Object.prototype.

For example: If a date object [new Date()] is created with new keyword, then it inherits the Date.prototype.
We have Date, Array, Function, RegExp in the list for the same, All these objects inherit from the Object.prototype.

We should never alter or change the predefined methods or properties of prototype, It may cause inappropriate failure, which may be difficult to debug.

To check whether the object has a property or not, we use hasOwnProperty() method. For example if we have age property in any object, we can check it with help of hasOwnProperty() method. we will come to know more about it in this blog.

Let’s take a look at a quick example of prototype chaining
In this example, we will create a class and add prototype object in the same. After instantiation of the object, we will be able to use prototype of the class. Which is simply object prototype chaining.

> function Person(firstName, lastName, age){
this.firstName = firstName;
this.lastName = lastName;
this.age = age;
}
//Person class created

Person.prototype.getFullName = function(){
return this.firstName + ” ” + this.lastName;
}
// we have added getFullName method in Person’s prototype.
> var person = new Person(“pushpendu”, “purkait”, 25);
// It will create an instance of the Person class

> person.hasOwnProperty(“firstName”);  // true
> person.hasOwnProperty(“getFullName”);  // false

> person.getFullName(); // pushpendu purkait

In the above code, we used hasOwnProperty() method to check whether we have getFullName method as a property of the object. It returned false, that means there is no such property. but when we used getFullName method, it returned the actual full name but the property was not there.
How can that be possible ??

To know more we have to dig into it. As the getFullName() method was not there in the object but still we were able to use it. we should not forget that we added the method in class Person’s prototype. The method went to the object through prototype chaining. it means the class had the method in it’s prototype, which went to it’s instance (person object) through prototype chaining. To prove that we will check it in the object.

Whenever we create an instance of any class, the prototype of the class is created in object as well. we have to check it in  __proto__  property. this property contains the prototype of it’s class. For example: Person’s getFullName() method will be created in object’s __proto__ property.

Let’s take a look into the code:

person.__proto__.hasOwnProperty(‘getFullName’);  //true

As we can see __proto__ has the getFullName() method, which is taken from class Person.
we can see more methods and properties like
__defineGetter__, __defineSetter__, __lookupGetter__, __lookupSetter__, constructor etc. which are there because of prototype chaining.

I hope you enjoyed and learned something new with the blog.

Posted in JavaScript, Scala | Leave a comment

Business Intelligence-Data Visualization: Tableau


Image result for tableauSpark, Bigdata, NoSQL, Hadoop are some of the most using and top in charts technologies that we frequently use in Knoldus, when these terms used than one thing comes into picture is ‘Huge Data, millions/billions of records’ Knoldus developers use these terms frequently, managing (and managing means here- storing data, rectifying data, normalizing it, cleaning it and much more) such amount of data is really not at all an easy task.

But user do no understand what they are talking about they just need to know the real essence of whole matter/data/story/facts. From here the term ‘visualization’ comes into picture, so Data Visualization/Intelligence is as important & vast as handling it.

Data visualization brings Business Intelligence Tools for accomplishing visualization goals and the market of BI tools is really huge, there are number of tools with different features, pricing, capabilities etc., if we start comparing them than there is no limit but there are some major Tools that do everything for you and your selection in search of best BI tool is totally based on your business requirements. Somehow as our title appended Tableau so we will talk about it here.

Tableau is groundbreaking data visualization software created by Tableau Software. Tableau connects easily to nearly any data source, be it corporate Data Warehouse, Microsoft Excel or web-based data. Tableau allows for instantaneous insight by transforming data into visually appealing, interactive visualizations called dashboards. This process takes only seconds or minutes rather than months or years, and is achieved through the use of an easy to use drag-and-drop interface.

Continue reading

Posted in Scala, Tableau | Tagged , , , , , , , , | 1 Comment