Author Archives: yogesh.mali@gmail.com

Custom Twitter Client vs Spring Boot Twitter Plugin

To use twitter data in my saas application, I was going to write my own custom Twitter client by doing a rest call. However, I found out Spring Boot offers a Twitter plugin that can be used to fetch Twitter data. Neat.

In this post, I will show some comparison of these two approaches and why one can choose over another:

Custom Twitter Client

So custom twitter client will be a standalone client which will build an HTTP entity with client secrets that are needed to authenticate with Twitter API. In this client, we will use restOperations to call API endpoint passing HTTP entity and the REST call will respond with Twitter Data Model.

This will look like below:

public TwitterDataModel getTwitterData(long accountId)
{
    String url = buildRestUrl(accountId);
    ParameterizedTypeReference<HashMap<Long, TwitterDataModel>> responseType = new ParameterizedTypeReference<HashMap<Long, TwitterDataModel>>(){};
    HttpEntity entity = buildHttpEntity(CLIENT_ID, CLIENT_SECRET);
    Map<Long, TwitterDataModel> twitterDataModelMap = restOperations.exchange(url, HttpMethod.GET, entity, responseType).getBody();

    Long keyForData = new Long(accountId);
    TwitterDataModel twitterDataModel = twitterDataModelMap.get(keyForData);

    return twitterDataModel;
}

public String buildRestUrl(long accountId)
{
    return TWITTER_REST_ENDPOINT + accountId + TWITTER_REST_API;
}

There is nothing much wrong with this approach, except the fact that we will have to write an extra TwitterDataModel business object. Also, this business model should be created before we do the actual REST call.

Spring Boot Twitter Plugin

To use this plugin, first, we need to add the plugin in Gradle or maven like below:

compile('org.springframework.social:spring-social-twitter')

Once we have this plugin, we can add an object of type Twitter in our code to call REST APIs.

This will look like below:

private final Twitter twitter;

public TwitterDataModel getTwitterData(long accountId)
    {
        String url = buildRestUrl(accountId);
        ParameterizedTypeReference<HashMap<Long, TwitterDataModel>> responseType = new ParameterizedTypeReference<HashMap<Long, TwitterDataModel>>(){};
        HttpEntity entity = buildHttpEntity(CLIENT_ID,CLIENT_SECRET);

        Map<Long, TwitterDataModel> twitterDataModelMap = twitter.restOperations().exchange(url, HttpMethod.GET, entity, responseType).getBody();

        Long keyForData = new Long(accountId);
        TwitterDataModel twitterDataModel = twitterDataModelMap.get(keyForData);

        return twitterDataModel;
    }

    public String buildRestUrl(long accountId)
    {
        return TWITTER_REST_ENDPOINT + accountId + TWITTER_REST_API;
    }

The major advantage of this plugin is that we can get the data in Twitter Data Model that twitter offers. An then we can go on to use to handle our data.

Conclusion

In this post, I showed how we can use a Spring Boot Twitter social plugin to gather Twitter data.

SaaS Application Design Discussion – Part IV

In the previous post, I discussed database design for saas application. To continue design discussion for our social pie saas application, in this post, we will discuss a few more ideas about how a user and user’s company will sign up for application. This will be a user story. We are building a SAAS application. To make it more viable, this application will use the freemium and pay model.

  1. In the freemium model – Any company can join and review what reports it will be able to see and what kind of marketing strategies it can design using those reports.
    1. 5 Reports
    2. Free Marketing Strategies
    3. Up to 3 users
    4. Limited usage of twitter and Instagram APIs
  2. In pay model – If a company opts to join a pay subscription, it will be able to get more advance reports, will be able to see reports in a different format, and can also get consultation about strategies for marketing.
    1. N number of reports – Your data, your freedom
    2. Marketing Consultation
    3. KPI tracker and notification
    4. Up to N users (won’t be implemented in first version)

User Flow

Once the user lands on the home page, he can opt for either model and sign up. An automated email will be sent to the user for a demo or sign up. Upon sign up, where the user will be entering details about himself and his company. This user will be an administrator and he can add other users with custom roles. The same user can go to reports tab and click on sync data. This will get the latest data from social media and update it in the database. Every new request will compare newly fetched data with current data in the database. If the new request has brought changes, it will be updated in the database. When generating reports, this data from the database will be cached.

We will not be fetching any on-the-fly data from Twitter and Instagram. Administrator users will have an option to send reports to other people from the company. There will be an email/download option.

There are some nitty-gritty details that I have not covered in this post. But with this post, we will be starting to develop a Saas application using java and spring-boot.

 

Database and design discussion – Part III

To continue the development of a spring-based web application, this post will discuss using of Twitter API in saas application. If you want to understand, what we are building, you can read the first two posts of this series where we discussed the design of the application we are building:

  1. Database design and discussion – Part I
  2. Database design and discussion – Part II

In the previous post, we discussed the Instagram API that we will be using. With recent events around Facebook, I have decided not to use Facebook API for application development. We will still use Instagram and Twitter API.

Using Twitter API in SAAS Application

Firstly, Twitter offers different APIs for developers to build applications. We will be using Engagement API. You can find more details Twitter API.

Our goal is to use this Twitter API to collect engagement metrics in the Saas application.

Secondly, Engagement API offers us details about account engagement metrics which can help us to design marketing strategy. Sample response of this API looks like the below:

{
  "Tweet metrics": {
    "902301386880286721": {
      "engagements": "433",
      "favorites": "21",
      "impressions": "72218"
    },
    "902731270274166784": {
      "engagements": "61",
      "favorites": "27",
      "impressions": "7827"
    },
    "907022936564838401": {
      "engagements": "187",
      "favorites": "37",
      "impressions": "1916"
    }
  }
}

Therefore this API provides metrics for tweets, which tweet generated more traffic. The key decision that can be devised based on these metrics is what kind of tweet, moment or incident generates the traffic.

What fields we will use in our database?

In conclusion, we will be using the following fields in our database table TwitterData

  • tweet id
  • engagements
  • impressions
  • tweet

Twitter is a viable medium. This data will provide small businesses with a key metric about what tweets have worked with their followers and how they can leverage that pattern. Once the small businesses sort out these patterns, they will be able to create a number of tweets to engage with customers. Eventually, the goal here is to help small businesses to attract customers, and repeat customers.

References

  1. Twitter API documentation – Twitter API

 

Smart Contracts in Blockchain – Part II

In this post, we will show how to write a simple smart contract. If you do a quick google search, you will find numerous articles on writing smart contracts. In my previous post Blockchain, I explained blockchain. But I didn’t talk about the smart contracts in that post. That’s why a separate post.

Smart Contract

In layman’s terms, the contract is nothing but an agreement between two parties, witnessed by a third party to hold both parties accountable for playing out the contract. So what is a Smart Contract then?

In Nick Szabo’s words

Smart Contracts are a set of promises, specified in digital form, including protocols within which the parties perform on these promises.”

In Web Developer’s terms, a Smart contract is like an application API, but there are few exceptions. Like an API can call another external API, a smart contract can not call external API. A smart contract can call another smart contract. A smart contract comprised of a number of private functions and variables to implement the agreement.

More formal definition of a smart contract is a method of using Bitcoin to form agreements with people via the blockchain.

Ethereum

So how do we write these smart contracts? Ethereum is one such a platform that is used primarily for building and publishing distributed applications. It is a Turing-complete virtual machine built for the purpose of cryptocurrency. It is the fundamental underlying infrastructure platform that can run all blockchains and protocols. Each node in Ethereum runs an Ethereum Virtual Machine. This EVM hosts distributed programs (smart contracts) which get executed seamlessly.

Implementation of smart contracts

To address some basic questions like “How does a smart contract look?”, “What do you use for programming a smart contract?”, I will go over some simple concepts.

There are currently two programming languages that can be used to write a smart contract.

  • Solidity – A javascript look-alike language with file extensions .sol
  • Serpent – A python look-alike language with file extensions .se

Once a contract is written in either language, you can compile it using a solc compiler. A compiled contract then posted on the network. You can call this contract in your web app by using web3.js Javascript API.

Conclusion

In this post, I tried to explain one of the key concepts of blockchain, a smart contract. This can be used further in building decentralized applications. In the next post, I will show how to write a simple smart contract and run on a node with EVM.

References

  1. Building a smart contract – Smart Contract Ethereum
  2. Blockchain for web developers – Blockchain

 

Blockchain for Web Developers

Yes, there might be a plethora of articles about blockchain and how web developers can use to build applications. And this might not be a much different article either. In this post, I describe the basics of blockchain and crypto technology.

Introduction

Blockchain has been the underlying technology for cryptocurrencies like bitcoin.

Firstly, this is a basic understanding of blockchain. We will cover the rest of the basics of blockchain soon. In most banking or financial systems, all bank accounts track through a ledger that keeps track of incomes and expenses.

Secondly, in current times, our centralized financial systems follow certain rules and regulations. A central authority defined these rules and that’s how trust was built. But blockchain is a decentralized system of the ledger where a peer-to-peer network is involved. Based on the peer-to-peer network, miners involved in the process, build trust in a decentralized form. All transactions are recorded on ledger and ledger is verified by nodes in the network. These nodes communicate with each other cryptographically for verification of transactions. When new transactions are added, there is a consensus formed in the network, this consensus is nothing but a block.

Proof-of-work

One reason why blockchain is popular is that it solves a double-spending problem that has been there for a long time in computer science. When it comes to a distributed system, there is no way to correctly verify the integrity of transactions. In relational database systems, we use referential integrity to verify integrity.

This is the foundational algorithm in the blockchain. In the mining process, miners compete with each other in the network to verify transactions and produce new blocks. For this work, miners get cryptocurrency.

In network, transactions happen all the time between users. A decentralized ledger will keep track of all these transactions. Miners will verify these transactions through proof of work algorithm.

Person A sends $10 to person B and not to person C. How do we verify that money went to Person B and not to Person C? This is a double-spending problem. Proof of work helps to solve this problem. There are other aspects to this algorithm like how to avoid any security threats, faster block generation(Power of network), storage capabilities. We will not be discussing those aspects here.

Drawbacks

There are few drawbacks to this algorithm and one of the major one is 51% attack. The idea of 51% is when a user or a group of users control the majority of mining power. If this happens, the group can monopolize generating new blocks and this will lose the advantage of the decentralization principal.

Blocks

Blocks form the ledger which forms the basis of blockchain. Each block contains transaction information which we can call as a fact. A block is nothing but the arrangement of all these facts and each block will have a reference to the next block.

Blockchain for web developers

Before these facts get added to blocks, they are pending and as miners continue to work, they verify these facts to confirm.

Conclusion

In conclusion, I introduced blockchain with some basics of blockchain for the web developers. But this is just the tip of the iceberg, there is a lot to learn and bigger things to do in the cryptocurrency world. If you enjoyed this post, subscribe to my blog.

References

  1. Blockchain: A blueprint for the new economy by Melanie Swann – Blockchain
  2. Proof of work – Proof of work
  3. Ethereum for Web developers – Ethereum for web developers