First Loklak plugin added to WordPress directory

Recently first Loklak plugin was accepted in WordPress directory. We are planning to add more and more plugins with loklak integration and it is an awesome start. The plugin Tweets Widget is a minimalist tweet feed which can render tweets from Loklak.org API or Twitter API v1.1 .

The widget uses Twitter Auth WordPress API (wp-twitter-api) by _timwhitlock_ and Loklak’s PHP API for tweet rendering purposes. Both the APIs are added as submodules.

Tweets Widget shows tweet feed on your wordpress website. You can add its shortcode or just add it as a Widget using wordpress’s drag and drop feature.

It is compatible with both Twitter as well as Loklak API. The settings page allows you to select one of the two modes for your current feed (as shown below).

screenshot-2

After you provide the default settings, you have to provide a tweet title, handle, number of tweets and some more custom feature

screenshot-3

See the screenshot of a sample feed below.

screenshot-1

It uses loklak search API (code sample shown below).

Selection_020

The main widget code can be seen here.

First Loklak plugin added to WordPress directory

Testing localhost-only Loklak APIs

Loklak provides several APIs which are localhost-only! i.e. only localhost clients are granted access. For example:

This feature makes it difficult to integrate testing of such APIs with the main test-suite. Since we use Travis-CI for online testing, we would have to create an extra connection setup for online testing and offline (localhost) testing.

So in this dilemma, I had to come up with an approach which would allow the test-suite to recognise Travis-CI and take necessary actions to handle such conditions. After some reading, I came across PHP’s getenv() function which provides information of current environment variables.

So, in order to write tests for localhost-only APIs, I used getenv() as a check to differentiate between Travis and local tests.

Selection_013

Using this approach, tests for Settings and Accounts API were added. Code samples shown below!

Selection_014

This approach improves the tested code-coverage and integrates all unit tests into one file!

Testing localhost-only Loklak APIs

Improving Code Coverage for Loklak PHP API

Tests


This week, I added tests for suggest, map, markdown, push and susi APIs and fixed tests for user topology related queries. PHPUnit was used as the testing framework since it provides code-coverage support.

Loklak PHP API now has a good test-suite support and associated examples to help you use our services

Below are the Tests that were added.

testPushto test Push API.Selection_001

testSusito test newly added Susi API. Selection_003

Refer to this and this for more info about Susi

testMap – to test map API.

testMarkdown – to test markdown API.Selection_002

For more detailed information regarding the entire Loklak PHP API test-suite, refer to this.

In above process, code-coverage was increased from 33% to 61%. Test-suite continuously updated as new APIs are added to Loklak.

Source support for Search API


Apart from that, since Loklak is scaling beyond Twitter. Source argument has been added to the search API to define the source of search results. As far as wordpress plugins are concerned, since they only require twitter results for now, source has been added as a default argument. See below code: Selection_004

Improving Code Coverage for Loklak PHP API

Susi support for Loklak APIs

Here at Loklak, we are striving continuously for innovation. Continuing with this trend, we recently launched ‘Susi – The chat bot’. Please refer to this previous blog post by Damini.

Along with the chat bot, Susi query support was added to Loklak Python and PHP APIs. Susi can be queried from localhost as well as other online loklak peers.

Susi API function added to Python API(as shown below). See full implementation here.

def susi(self, query=None):
   """Hits Susi with the required query and returns back the susi response"""
   susi_application = 'api/susi.json'
   url_to_give = self.baseUrl + susi_application
   self.query = query
   if query:
      params = {}
      params['q'] = self.query
      return_to_user = requests.get(url_to_give, params=params)
      if return_to_user.status_code == 200:
          return return_to_user.json()
      else:
          return_to_user = {}
          return_to_user['error'] = ('Looks like there is a problem in susi replying.')
          return json.dumps(return_to_user)
      else:
          return_to_user = {}
          return_to_user['error'] = ('Please ask susi something.')
          return json.dumps(return_to_user)

A sample usage of Susi API in python could be:

from loklak import Loklak
query = "Hi I am Zeus"
l = Loklak()
result = l.susi(query)
print result

Susi integration with PHP API(see below). See full implementation here.

public function susi($query=null) {
	$this->requestURL = $this->baseUrl . '/api/susi.json';
	$this->query = $query;
	if($query) {
		$params = array('q'=>$this->query);
		$request = Requests::request($this->requestURL, array('Accept' => 'application.json'), $params);
		if ($request->status_code == 200) {
			return json_encode($request, true);
		}
		else {
			$request = array();
			$error = "Looks like Susi is not replying.";
			$request['error'] = array_push($request, $error);
			return json_encode($request, true);
		}
	}
	else {
		$request = array();
		$error = "Please ask Susi something.";
		$request['error'] = array_push($request, $error);
		return json_encode($request, true);
	}
}

Sample usage of Susi API in PHP:

include('loklak.php');
$loklak = new Loklak(); 
$result = $loklak->susi('Hi I am Zeus');
$susiResponse = json_decode($result);
$susiResponse = $susiResponse->body;
$susiResponse = json_decode($susiResponse, true);
var_dump($susiResponse);

Tests for above-mentioned functions have been added to the respective API suite. Refer to this and this.

Try Social Universe Super Intelligence!

Ask questions, interact with it. I am pretty sure that you would like it!

Susi support for Loklak APIs

Using Heroku-WordPress Buildpack to test Loklak integration in WordPress plugins

Loklak support was added to several wordpress plugins this summer. In order to properly handle and structure further development in this area, a common repository has been created to host the incumbent plugins. Please refer to this repository. To ensure rigorous testing in the internet environment, all plugins have been installed (loklak_wordpress_plugins/wp-content/plugins) in a sample WordPress site deployed on Heroku.

Heroku is a platform as a service (PaaS) that enables developers to build, run, and operate applications entirely in the cloud.

Heroku provides several buildpacks for different deployment patterns. After careful research I found a useful WordPress+Heroku buildpack here. To create your own wordpress instance to test the plugins follow the below instructions:

Deploy from github directly:

  1. Click on Deploy to Heroku button in the Readme file of your fork of loklak_wordpress_plugins repo. This step would install wordpress on Heroku and setup your database.
  2. Give your website a name and input your time-zone and add authentication information for .htpasswd to access wp-admin page (admin privileges). See the screenshot below. blog2_1
  3. Click on Deploy for Free. Once the app is deployed, Click on ‘Manage App’. Go to ‘Deploy’ tab and choose ‘Deployment method’ as Github
  4. Connect your forked loklak_wordpress_plugins repo to Heroku. blog2_2
  5. To automate the deployment process when the github repo is updated, enable automatic deploys. Now deploy the master branch of your repo and you are good to go. blog2_3

Deploy using Heroku Toolbelt:

To deploy using Heroku-git toolbelt, please refer to this.

After we are done with the deployment, we need to setup our plugins for using and testing:

  1. Follow the steps to install WordPress (as they appear on your computer screen) on your chosen app/domain name.
  2. Once WP is installed, change the language, time-zone and preferred date-time format in ‘general settings’.blog2_4
  3. Now you can activate plugins from ‘plugins’ as per your need and test their functionality!blog2_5
Using Heroku-WordPress Buildpack to test Loklak integration in WordPress plugins