Monday, January 9, 2017

Do the Right Thing and Document Your Rails API with Swagger

Maybe, if you are working on a small project and your team consists of just one member and that's you. But even then you won't be there to maintain that little project of yours forever. If someday, someone walks in and tries to understand how it all works in order to implement another feature or fix a bug, it might take much much longer to just get started. Besides, we all know that lost feeling when we encounter a large, undocumented code base and try to fix a bug. "Code as documentation" doesn't always (or ever?) work.

For APIs, this is far more important as the system you are developing is going to be used by others through endpoints you expose. Consider a scenario when you have to design a back-end solution for a business problem, which will be used by a front-end or mobile teams. It would be inconvenient to share Postman collections every time an API is changed or implemented. This will slow down the development pace and cause unnecessary friction. Thankfully, we have lots of tools available to implement documentation without having to write a lot of code and example responses.

Today I will walk you through the process of creating APIs with documentation via Swagger. Swagger has two parts: swagger-docs which generates docs for your API, and swagger-ui which uses JSON generated by swagger-docs to build a UI used to consume and interact with those APIs.

Continue reading %Do the Right Thing and Document Your Rails API with Swagger%


by Parth Modi via SitePoint

Integrate External Libraries in OpenCart Using Composer

Almost every framework nowadays has built-in support of Composer, an awesome dependency management tool in PHP, and OpenCart is no exception. In this tutorial, you'll learn how to use Composer to integrate external libraries in OpenCart.

The Role of Composer in OpenCart

Since the introduction of OpenCart 2.2, the Composer-based workflow is supported. So go ahead and grab the latest version of OpenCart; as of writing this, it's 2.3.0.2. Make sure that you install and configure the latest version that you've downloaded as that will be useful later in the article.

Explore the directory structure and you'll notice certain differences compared to earlier versions of OpenCart. In the context of this article, the interesting candidates are the composer.json file and the vendor directory. 

Let's quickly go through the composer.json file.

Although a discussion of Composer syntax is out of the scope of this article, let's quickly go through what it says in layman terms.

First, the OpenCart project itself is now available as a library, so you could install it using Composer itself without manually downloading it from the site. Further, it also requires other third-party libraries to work properly, like divido, leafo, etc. Of course, you don't need to worry about it as that will be handled automatically when you run related Composer commands.

When you install a new library, the related entry will be added to the composer.json file. The related library files are placed under the vendor directory at the same level. Just explore that directory and you should see the libraries are installed already!

The vendor directory also contains autoload.php, generated by Composer itself, which makes sure that the libraries are loaded automatically in OpenCart, so you could use it straight away. Of course, OpenCart includes autoload.php while bootstrapping the project.

So that's a quick introduction of how Composer works with OpenCart. For demonstration purposes, we'll install the popular PHPMailer library using Composer.

Install PHPMailer Using Composer

The PHPMailer is a popular PHP library that's used to send emails. We'll install it in OpenCart using Composer. So go to your terminal and change the directory so that you are at the same level where the vendor directory and composer.json file reside.

Now, run the command composer require phpmailer/phpmailer and press enter! Assuming that everything goes fine, it should look like the following.

So that's it! PHPMailer is downloaded and installed successfully, and that's the beauty of Composer! Verify that by looking into the vendor directory, and you'll find it installed in the phpmailer/phpmailer directory.

Also, let's open composer.json to see what it looks like.

As you can see, the entry "phpmailer/phpmailer": "^5.2" is added into the require section. So it means that your project requires PHPMailer to work properly.

Let's assume that you're working with other developers and need to share your work regularly. In that case, you just need to share your composer.json file with them and the rest will be handled by Composer itself! They just need to run the composer update command, and that should take care of installing the required dependencies in their copy!

Now, we've installed PHPMailer using Composer, but how to use it? Don't worry, I won't leave you that soon—that's exactly the recipe of our next section!

How to Use the PHPMailer Library?

You've already done yourself a favor by using Composer to install the PHPMailer library, and you'll witness it in this section as we explore how straightforward it is to use in the code.

For example purposes, we'll build a pretty simple custom controller file that you could call to send an email.

Open your favorite text editor and create example/email.php under the catalog/controller directory with the following contents.

You could test it by accessing your site via http://your-opencart-site-url/index.php?route=example/email.

In the index method, you can see that we've instantiated the PHPMailer object without any include statements that would have included the required PHPMailer classes had we not used a Composer-based workflow. You've got it right, it's auto-loaded by OpenCart itself. Recall that autoload.php in the vendor directory does all the magic!

Following that is some pretty standard stuff required by PHPMailer to send an email. Of course, I've tried to keep the example as simple as possible since the discussion of PHPMailer requires a separate article!

So, that was a quick and simple introduction of how you could use Composer with OpenCart to integrate external libraries.

Conclusion

In this article, we've just scratched the surface of a Composer-based workflow in OpenCart to use third-party libraries in your project. Not to mention that Composer is the future of dependency management tools in PHP. So it's always good to get your hands dirty with that as it's becoming the standard in all popular frameworks.

Queries and suggestions are always appreciated!


by Sajal Soni via Envato Tuts+ Code

What Are Scrum Rituals?

The following is an extract from our book, Scrum: Novice to Ninja, written by M. David Green. Copies are sold in stores worldwide, or you can buy it in ebook form here. The practice of working on a scrum team is organized into a series of rituals. The rituals mark key events in the process […]

Continue reading %What Are Scrum Rituals?%


by M. David Green via SitePoint

Mathematical Modules in Python: Math and Cmath

When writing programs in our day-to-day life, we usually come across situations where we need to use a little maths to get the task done. Like other programming languages, Python provides various operators to perform basic calculations like * for multiplication, % for modulus, and // for floor division.

If you are writing a program to perform specific tasks like studying periodic motion or simulating electric circuits, you will need to work with trigonometric functions as well as complex numbers. While you can't use these functions directly, you can access them by including two mathematical modules first. These modules are math and cmath

The first one gives you access to hyperbolic, trigonometric, and logarithmic functions for real numbers, while the latter allows you to work with complex numbers. In this tutorial, I will go over all the important functions offered by these modules. Unless explicitly mentioned, all the values returned are floats.

Arithmetic Functions

These functions perform various arithmetic operations like calculating the floor, ceiling, or absolute value of a number using the floor(x), ceil(x), and fabs(x) functions respectively. The function ceil(x) will return the smallest integer that is greater than or equal to x. Similarly, floor(x) returns the largest integer less than or equal to x. The fabs(x) function returns the absolute value of x.

You can also perform non-trivial operations like calculating the factorial of a number using factorial(x). A factorial is the product of an integer and all the positive integers smaller than it. It is used extensively when dealing with combinations and permutations. It can also be used to calculate the value of sine and cosine functions. 

Another useful function in the math module is gcd(x,y), which gives you the greatest common divisor (GCD) of two numbers x and y. When x and y are both not zero, this function returns the largest positive integer that divides both x and y. You can use it indirectly to calculate the lowest common multiple of two numbers using the following formula:

Here are a few of the arithmetic functions that Python offers:

Trigonometric Functions

These functions relate the angles of a triangle to its sides. They have a lot of applications, including the study of triangles and the modeling of periodic phenomena like sound and light waves. Keep in mind that the angle you provide is in radians.

You can calculate sin(x), cos(x), and tan(x) directly using this module. However, there is no direct formula to calculate cosec(x), sec(x), and cot(x), but their value is equal to the reciprocal of the value returned by sin(x), cos(x), and tan(x) respectively.

Instead of calculating the value of trigonometric functions at a certain angle, you can also do the inverse and calculate the angle at which they have a given value by using asin(x), acos(x), and atan(x)

Are you familiar with the Pythagorean theorem? It states that that the square of the hypotenuse (the side opposite the right angle) is equal to the sum of the squares of the other two sides. The hypotenuse is also the largest side of a right-angled triangle. The math module provides the hypot(a, b) function to calculate the length of the hypotenuse.

Hyperbolic Functions

Hyperbolic functions are analogs of trigonometric functions that are based on a hyperbola instead of a circle. In trigonometry, the points (cos b, sin b) represent the points of a unit circle. In the case of hyperbolic functions, the points (cosh b, sinh b) represent the points that form the right half of an equilateral hyperbola. 

Just like the trigonometric functions, you can calculate the value of sinh(x), cosh(x), and tanh(x) directly. The rest of the values can be calculated using various relations among these three values. There are also other functions like asinh(x), acosh(x), and atanh(x), which can be used to calculate the inverse of the corresponding hyperbolic values.

Since math.pi is equal to about 3.141592653589793, when we used asinh() on the value returned by sinh(math.pi), we got our π back.

Power and Logarithmic Functions

You will probably be dealing with powers and logarithms more often than hyperbolic or trigonometric functions. Fortunately, the math module provides a lot of functions to help us calculate logarithms.

You can use log(x,[base]) to calculate the log of a given number x to the given base. If you leave out the optional base argument, the log of x is calculated to the base e. Here, e is a mathematical constant whose value is 2.71828182.... and it can be accessed using math.e. By the way, Python also allows you to access another constant π using math.pi.

If you want to calculate the base-2 or base-10 logarithm values, using log2(x) and log10(x) will return more accurate results than log(x, 2) and log(x, 10). Keep in mind that there is no log3(x) function, so you will have to keep using log(x, 3) for calculating base-3 logarithm values. The same goes for all other bases.

If the value whose logarithm you are calculating is very close to 1, you can use log1p(x). The 1p in log1p signifies 1 plus. Therefore, log1p(x) calculates log(1+x) where x is close to zero. However, the results are more accurate with log1p(x).

You can also calculate the value of a number x raised to the power y by using pow(x, y). Before computing the powers, this function converts both the arguments to type float. If you want the final result to be computed in exact integer powers, you should use the built-in pow() function or the ** operator.

You can also compute the square root of any given number x by using sqrt(x), but the same thing can also be accomplished by using pow(x, 0.5).

Complex Numbers

Complex numbers are stored internally using rectangular or Cartesian coordinates. A complex number z will be represented in Cartesian coordinates as z = x + iy, where x represents the real part and y represents the imaginary part. Another way to represent them is by using polar coordinates. 

In this case, the complex number z would be defined a combination of the modulus r and phase angle phi. The modulus r is the distance between the complex number z and the origin. The angle phi is the counterclockwise angle measured in radians from the positive x-axis to the line segment joining z and the origin.

While dealing with complex numbers, the cmath module can be of great help. The modulus of a complex number can be calculated using the built-in abs() function, and its phase can be calculated using the phase(z) function available in the cmath module. You can convert a complex number in rectangular form to polar form using polar(z), which will return a pair (r, phi), where r is abs(z) and phi is phase(z)

Similarly, you can convert a complex number in polar form to rectangular form using rect(r, phi). The complex number returned by this function is r * (math.cos(phi) + math.sin(phi)*1j).

The cmath module also allows us to use regular mathematical functions with complex numbers. For example, you can calculate the square root of a complex number using sqrt(z) or its cosine using cos(z)

Complex numbers have a lot of applications like modelling electric circuits, fluid dynamics, and signal analysis. If you need to work on any of those things, the cmath module won't disappoint you. 

Final Thoughts

All of these functions we discussed above have their specific applications. For example, you can use the factorial(x) function to solve permutation and combination problems. You can use the trigonometric functions to resolve a vector into Cartesian coordinates. You can also use trigonometric functions to simulate periodic functions like sound and light waves. 

Similarly, the curve of a rope hanging between two poles can be determined using a hyperbolic function. Since all these functions are directly available in the math module, it makes it very easy to create little programs that perform all these tasks.

I hope you enjoyed this tutorial. If you have any questions, let me know in the comments.


by Monty Shokeen via Envato Tuts+ Code

How to Create a Facebook Messenger Chatbot

Does your business want to do more with Facebook Messenger? Interested in using a chatbot for customer service and marketing? Facebook Messenger chatbots can help your followers get answers to frequently asked questions and more. In this article, you’ll discover how to set up a Facebook Messenger chatbot for your business. Why a Chatbot for [...]

This post How to Create a Facebook Messenger Chatbot first appeared on .
- Your Guide to the Social Media Jungle


by Sally Hendrick via

Intangible Matter

Discover Intangible Matter. An interactive experience by Lucy Hardcastle and Stinkdigital that was inspired by the physical and virtual element of the new fragrance, CHANEL Nº5 L’Eau.
by via Awwwards - Sites of the day

Sunday, January 8, 2017

How Do Infographics Perform in a Mobile-First Strategy?

Infographics: those visual, easy to digest pieces of graphic content that you’ve found all over the web are more popular than ever. Usually seen in a simple JPG or PNG format, they can be also animated or interactive but are very often static, meaning users must pinch the screen to view them on...

[ This is a content summary only. Visit our website http://ift.tt/1b4YgHQ for full links, other content, and more! ]

by Web Desk via Digital Information World