Showing posts with label app. Show all posts
Showing posts with label app. Show all posts

Wednesday, December 2, 2015

How to add dynamic text to Rails applications

How can you change your application from this...
To this?
It's simple! Just use embedded ruby!

EDIT: Thanks to Action Dan for some feedback that picks into the niche differences of web application frameworks.

Bear in mind this is a Rails specific post, but I will say that for purposes of building larger applications, the ability to organize and refactor code begins to become a problem



Rails also offers uniform ways to add and remove helper functions through package management, so I'm actually using the current_user function generated by adding the Clearance gem to my project. 

The simple markup syntax is one small reason people like using Ruby on Rails to develop web applications.

Using a helper function (that I didn't even have to write) are two "behind the scenes" benefits of using Rails to develop.

If your confused about the current_user helper I'm using, read more here.

Program on!

Sunday, August 4, 2013

Using Node.js and Non-Blocking Code


One of the largest strengths of Node.js is its great usage of non-blocking code.

In essence, non-blocking code creates a function to handle events as they come, so the program or application will be able to handle new events even if they arise during the last event's execution.


It's the difference between these two bits of code:

var contents = fs.readFileSync('/etc/hosts');
console.log(contents);
console.log('Doing something else');

...and...

fs.readFile('/etc/hosts', function(err, contents) {
  console.log(contents);
});
console.log('Doing something else');

But what's best, is to define a callback function...

var callback = function(err, contents) {
  console.log(contents);
}

...so it can easily be called with one line of code.

fs.readFile('/etc/hosts', callback);



Wednesday, July 31, 2013

Parsing in Python





Let's make sense of this pile of data.

Reading and parsing data are the first two building blocks of a larger process, compiling, so it's always useful to know a couple of good methods.

A quick Google search of "python reading data" got me to http://docs.python.org/2/tutorial/inputoutput.html, for some simple reference material.

Now, testing is always a fun way to find out what's going on behind the code, so let's see if we can just pull out a bit of the important data here so we don't overwhelm ourselves.

Now, continuing from before, lets convert the data object to a useful string/array.

>> theData = str(html.read()).split(',')

Now let's see what this looks like.

>> for i in range(5):


         print(i, theData[i])


This smaller data structure will come in handy when setting up our larger program design.

Here's the result:

0 b'{"Key":"100-og"

1 "Name":"$100 OG"

2 "Category":"Hybrid"

3 "Symbol":"100"

4 "Abstract":"$100 OG is a hybrid cannabis strain that originally became popular in Southern California. It produces large flowers and a potent high."


Now, wait, your probably thinking, what's that b'{ doing there?

This data is meant to be parsed with the use of json modules, not bare bones python split statements, and that should massively simplify the process.


Although; you may have to redesign your parsing algorithm to completely remove all persisting characters and strings.

Monday, July 29, 2013

Introduction to APIs



An important part of almost every web app is reading, parsing, and selecting data.

The process of doing this with a public API, can be a simple task.

Okay, hold on now, what's an API?

First, I's say do a quick read through of http://en.wikipedia.org/wiki/Api, but if TL;DR...

An API is an Application Programming Interface, which in simple terms is both a collection of data and commonly a means of manipulating it.

An easy way to think about APIs is that they are an interface that unifies how applications communicate with other applications.

So let's try accessing Leafly's wonderful API strain collection.

"Let's talk to the data!"

http://www.leafly.com/explore

That's a good view of some of the data that's available to access.

Now here's a view of what it looks like to your code which will process it: http://www.leafly.com/api/strains

Well, that's clearly more convoluted... So whats really going on here?

If you took some of that and nicely spaced it out, these are what you would be left with.
[{
    "Key":"afghan-kush",
    "Name":"Afghan Kush",
    "Category":"Indica",
    "Symbol":"Afg",
    "Abstract":"Originating from the Hindu Kush mountains of Afghanistan...",
    "Url":"http://www.leafly.com/indica/afghan-kush",
    "DetailUrl":"http://www.leafly.com/api/details/afghan-kush",
    "Rating":8,
    "TopEffect":"Lazy",
    "TopMedical":"Migraines",
    "TopActivity":"Relax at home"},
    {
    "Key":"ak-47",
    "Name":"AK-47",
    "Category":"Hybrid",
    "Symbol":"Ak",
    "Abstract":"A great afghani kush cannabis strain that is mostly sativa...",
    "Url":"http://www.leafly.com/hybrid/ak-47",
    "DetailUrl":,"http://www.leafly.com/api/details/ak-47
    "Rating":7,
    "TopEffect":"Lazy",
    "TopMedical":"Migraines",
    "TopActivity":"Relax at home"
}]