# Best Time to Buy and Sell Stock II

Here’s another algorithm style coding challenge that I worked on LeetCode. The problem is rated as easy, and I would agree with that rating. Of course, the easy problems are great for aligning the logical thinking muscles necessary for efficient coding, and I find that the solutions to those problems are often more useful in real-world situations.

In this exercise, we begin with an array of prices. These values correspond to the prices of an unnamed stock over a consecutive number of days. For example, the array`[7,1,5,3,6,4]` represents the stock price over six days. …

# Revisiting CRUD with SQL

Nowadays, if you want to communicate with a database you have several options. After working with the Object Relational Mapping system in Ruby on Rails, I found myself a little rusty on basic SQL implementation. I decided to help myself by recording some of the basics in this article, meaning the post is more for me than it is for the readers!

# A Brief History

The Structured Query Language, or SQL, is a language used for handling data in relational database management systems. It was developed in the 1970s by Donald D. Chamberlin and Raymond F. Boyce of IBM. …

# Yet Another HackerRank Problem — 2D Array Hourglass in Javascript

As a developer, it’s important to keep the skills sharp with algorithm practice. The details for the problem detailed in this post can be found at https://www.hackerrank.com/challenges/2d-array/problem.

The problem begins with a 6 x 6 array, like this one:

`1  1  1  0  0 00  1  0  0  0 01  1  1  0  0 00  9  2 -4 -4 00  0  0 -2  0 00  0 -1 -2 -4 0`

We’ll be looking for subsets of this array that follow this pattern of 7 numbers:

`a b c  de f g`

This hourglass pattern…

# Merging Pre-Sorted Arrays in Ruby

Ruby is a magnificent programming language. There is so much maturity in its syntax and methods that are all designed to make the programmer’s life easier.

I’m a big fan of the array method in Ruby. The way they abstract the logic necessary to massage and mutate array data is amazing.

I was tasked with creating an API route that returned an array containing potentially up to 4 different objects, all sorted in descending order of date created. In addition to these requirements, the number of items in the merged array would be limited.

As it turns out, the fact…

# Serialize with Fast_jsonapi

So you’ve created a great API using Ruby on Rails, and now its time to prepare the data object (probably a hash) to be consumed by your frontend. Your frontend expects JSON, and that means the data has to be converted to a string, or serialized. The render method has a json: option to handle this easily.

# Render with to_json

If we’re working with a User model, maybe the show method of the users_controller.rb has a line that looks like this:

`render json: @user.to_json(only: [:id, :email, :name, :registered_at], methods: :average_rating)`

Here’s what /users/1 will produce with sample a little included sample data:

`{…`

# Ruby Serializer in Action

Ruby has been in use now for quite a while. Even though that is the case, there is still a need to address well-used issues. In this particular case, I am talking about active serializer.

One of the most important steps in developing a JSON API using Ruby on Rails is serializing the rendered JSON. That means structuring the data and selecting the attributes from the model.

Let’s look at an example of a basic route:`/users` . This route is intended to serve an array of objects containing data for all users in the database. In the `users_controller.rb`

# Adding Swagger Docs to a Rails 6 API Using Rswag

Swagger is a powerful set of tools for developing, testing, deploying, maintaining, and documenting APIs. The tools are found at swagger.io. When I first happened upon an API with Swagger documentation in place, I was amazed. The tools allowed navigating and testing the API in the browser with ease.

I recently began adding Swagger documentation to one of my projects, and I wanted to share the basics of that process.

This article assumes that a working Ruby on Rails API exists with models and controllers in place.

Begin with the project Gemfile. The rswag gem contains what is needed to…

# Set up the Node.js Application

Setting up a Node.js server to access data from a Postgresql database is simple.

Start with the basic Node.js setup. We’ll create an app called myDatabaseApp.

`mkdir myDatabaseAppcd myDatabaseAppyarn init`

Since this application will use Postgresql, we’ll need the appropriate node module.

`yarn add pg`

Create index.js. The code will connect to a database called node_test, and output records from the users table.

`const pg = require('pg')const config = {    database: 'node_test'}const pool = new pg.Pool(config);pool.connect((err, client, done) => {    if (err) throw err;    client.query('SELECT  name, age…`

# What’s So Great About Node.js?

When designing backend applications, there are several choices. Ruby on Rails gained prominence due to its pillar of “Convention over Configuration”. In recent years, Node.js has risen to become one of the most popular development environments used today.

As a matter of fact, you might say it’s become a standard for large scale enterprise applications.

Node.js is a runtime. It is built on Google’s V8 Javascript engine. The advantages of this architecture are found in optimized performance, feature support, and security.

Node JS is single-threaded and even-driven. It conserves RAM and CPU by utilizing the event loop and callbacks to…

# GraphQL Basic Scenario using Node.js and Express

While we’re all locked down and flattening the COVID-19 curve, I decided to expand my experience level a little.

# Node.js Setup

This example runs on Node.js. The dependencies necessary to execute are express, expressGraphQL, graphql, axios, lodash, and json-server.

Once the dependencies have been added by way of npm or yarn, utilize GraphQL middleware in server.js with the .use() method.

This will set up our server to listen for requests on port 4000. Our GraphQL server requires a schema object to define its actions.

# Schema

The data we retrieve using GraphQL queries is defined using a set of types. In this project…

## Clark Johnson

Full stack software engineer seeking projects using React, Ruby on Rails, Javascript, when I’m not at a baseball game

Get the Medium app