Consuming the Jamendo API with AngularJS: Services

It took a little while to get back to this little project. After watching some nice stuff from NgConf the other week, it helped get me excited again for AngularJs. Plus, I was stuck on a problem for quite a while that I'll explain in more detail below that really slowed me down. After a bit of a break I got back to it and was able to figure it out.

Services

Before we looked at how to use the $http get method from AngularJS, but all of that was within the Angular controller itself. To separate things out and to help us test easier I moved the $http calls into an Angular service.

To recap, let's take a look at our controller currently:

As mentioned earlier, we have our $http call right inside of our controller. While that definitely works, it's better to separate that out into it's own service. Let's refactor this down a bit...

Now this looks a bit cleaner. We're first passing in the albumsService as a dependency into the controller. Of course, if Angular can't find that the albumsService is defined, it'll throw an error.

Next, we have the function definition of our getAlbums method. In here we extract out the artist from the scope and then call the getAlbums method on our service. In our call to the service we give it a function as a callback if the $http get was a success that we'll see later when we look at our service. This basically just gets the results and puts them into the scope.

Now let's take a look at our service.

Pretty straight forward as it's basically the same as what we had in our controller earlier. We inject in the $http Angular object and we create our getAlbums method while prepending it with this

When we call our success promise from the get method, I just pass in the callback method that we defined in our controller earlier. This makes it a bit easier on our service since the callback method was updating our $scope object and we don't have to worry about that here in our service.

I mentioned earlier that I had a bit of an issue that kept me from finishing this up. I kept getting an $inject error on my service that indicated that it wasn't defined, but I couldn't figure out why that was. After some fiddling around with it, it turns out that I was trying to inject the $scope object into the service and Angular doesn't like that. That's also a reason to use the callback.

Conclusion

Moving the $http get call to an Angular service is mostly just a small refactoring, but it's a good one to do. We will see later when we start to do testing how much easier it is to have that separated out.

Of course, this was just an introduction to Angular services so there's tons more to learn about them. I'll recommend Dan Wahlin's AngularJS in 60ish Minutes to anyone. In fact, I've referenced it a few times to remind myself about services. There's also the WintellectNow video from Jeremy Likness on AngularJS Services that goes into much greater detail of services.

You can play with the demo and, as always, you can play around with the code itself.

First Wintellect Blog Post + A New Series On Design Patterns

Just recently, I published my first blog post to the Wintellect (my awesome new employer) site. May not seem like much, but it's pretty exciting for me.

One of the things to keep yourself learning, I feel, is to tackle the stuff you feel you understand the least. For me, one of those things was the concept and implementation of different design patterns. I'm probably already too late in the game for this, but better than not learning it at all, right?

Head First Design Patterns - Part 1: Observer Pattern

As An ng-conf Virtual Attendee...

This past Thursday and Friday, Google held a public conference for all things AngularJS. I'm sure you've noticed lately that I've been a bit of a fan of it, especially the more I play around with what it all has to offer. 

Luckily, the fine folks who put on the conference decided to stream it as it was going on. Awesome! I got to catch a few of the sessions. Though, it's not the same as actually attending as I couldn't meet all the great people and speakers there, watching the live stream is the next best thing.

I didn't get to see all of the sessions yet (they have all been uploaded), but here are the ones I have seen that seemed to stand out:

In total, I'm glad I was able to catch what I could of the live stream and hope that next time I can be able to join in person. I hope at some point I can be able to contribute something to help make everyone's life easier with AngularJS.

Beginning Ruby for C# Developers

Ruby has become quite popular as a programming language over the past few years. Probably mainly due to the popularity of the Ruby on Rails framework. Because of its popularity, it makes sense to learn at least the basics of it and what all it can do for you as a developer.

Until I have a better solution for embedding Ruby code and output, I'm just embedding the code and results as Github Markdown for now. Even though it doesn't seem to display properly in the embed, it does when viewing the actual gist on GitHub.

Getting Started

One of the easiest ways to get started using Ruby is to just use your browser. Repl.it is a site that supports many dynamic languages to be run in a browser and it's very handy to just play around in.

I've also seen people use Sublime Text to edit their Ruby code (I admit, I enjoy using it as well as a light weight IDE. I've had no problems using the beta of version 3, either). For Windows, you'd just need to install Ruby. If you're on a Mac, you already have it installed. In fact, you can just type "ruby -v" in the terminal to see what version you have.

Since Ruby is a dynamic language, there is no compiling. However, everything in Ruby is considered an object, and because of that everything will include some built in functions with it. With everything in Ruby being an object isn't necessarily a new concept to C# developers since every object in C# derives from System.Object and you do get a few predefined functions, Ruby just seems to have gone that extra step. 

For example, say you want to run a statement three times in a row. How would you do that in Ruby? It's actually easier than just using a for-loop similar to what you would do in C# or even creating a way to use a lambda expression.

Pretty easy and very straight forward, especially for someone who may not even have a programming background.

Let's go on with a few other basics...

Output

Ruby has a couple of ways to display output back to the console. The main way is to use the "print" statement, though you may also see the "puts" (put string) command as well. The only difference in these is the "puts" command will append a new line. You'll probably see these statements quite a bit, especially if you go through online tutorials.

Methods

As mentioned earlier everything in Ruby is an object, and since that's true it can have methods on those objects. Of course, depending on the type Ruby recognizes it during runtime is what methods are available. For example, the "length" method can be used on strings but not on numbers.

Methods, like in C#, can also be chained together to give some added syntactic sugar and to reduce the overall code needed to accomplish what you need.

One final note about methods that got me confused early on but I thought was really nice is that there is a bit of a naming convention associated with certain methods. Mainly methods that change the object itself and methods that return a boolean.

Take the "upcase" method we used earlier. There are actually two versions of this method, "upcase" and "upcase!". The first returns a copy of the string being used, the second (indicated with an exclamation point) changes the instance of the string being used. In other words, the first version causes the string to be immutable.

For methods that return a boolean value a question mark is appended to the end of the method name to indicate to the developer that that method can be used for branching. Take the string's "empty?" method, for example. As you can probably tell, it returns if the string is empty or not.

Program Flow

Like most programming languages Ruby supports the if/else block. However, one noticeable difference is that Ruby also has an "unless"' keyword. It will only execute if the given conditional is false. This was included to help make certain branching more readable and a bit more intuitive.

So we can change the structure of an if/else to an unless block like so...

Further Learning

One of the best resources I can recommend to help get you on the path of learning Ruby much easier is Code Wars. This site has quite a few code katas that will help in understanding the syntax and included methods that come in with Ruby.

Codecademy is recommended a lot, as well, by people and for very good reason...they have a lot of good interactive tutorials for learning Ruby.

Conclusion

Of course, I definitely don't know everything about the language or all the tools the community uses. I still haven't gotten my head totally around gem files or rake. I'm not even totally sure of the correct structure of a Ruby application! But, hopefully this will at least spark some interest in you C# people to maybe try something different outside of the .NET world. There is plenty of fun to be had with it and I throughly enjoy learning the language and all it has to offer.

Book Review: Without Their Permission

I'll be honest, I've been excited about this book since I heard about it's release a couple of months ago. I consider Alexis Ohanian to be a true defender of internet freedom and of all the good things that can come from it. Without Their Permission definitely highlights some of the most recent awesome things that have come from the internet being free where anyone can create a site for their passion.

What's Inside?

The book is divided into three basic parts - how Alexis co-founded reddit - one of the most visited web sites today, a few examples of other websites that are changing the world, and how anyone else can do the same.

The story of how reddit was started was itself an interesting tale and perhaps even one that can be its own book since it's also an inspiring read. Who would have thought that one of the most popular web sites may never have existed.

Alexis gives a few examples of other websites that set out to help the world be a better place. With sites like Kickstarter and DonorsChoose, it's hard not to see that people really want to help...and they do. I've participated in both, myself and it is, indeed, very rewarding just knowing that I played a part in helping out.

In fact, with DonorsChoose you can get the added reward of thank you notes directly from the students of the class in which you supported. It gives the reward a bit more of a personal touch. Below is one that I've gotten recently and I can't express what I felt when I read through them.

Thank you note

The third part is where Alexis gives his advice on how anyone can create something without anyone's permission. He gives the advice based on his own experience as well as the experience when he did his investments to other web sites who he feels will help the world. He even gives ideas on how to learn to code if you've never done it before.

Final Thoughts

It didn't take me long at all to finish this book as it was an easy read. It even made me laugh a few times. I'll definitely keep this on the book shelf and will even re-read it several times.

If you're looking to start something this book will definitely be a good kick in the right direction. It even felt like the book made me want to start something at the moment I was reading it. It's not just for people wanting to start a business, it's for people wanting to start anything.

Bonus

Alexis has done quite a bit for the community as a whole. To help introduce you to his work here are a couple of things he's done.

TED Talk about social media and the power of community.

Audio excerpt from the final chapter of Without Their Permission.

Book Review: Masters of Doom

If you were like me and you grew up in the 90s and played any sort of video games then I'm sure you've played Wolfenstein 3D or Doom. Though, regardless if you were into gaming, these games probably marked the beginning of an era in computing in general.

I've heard about this book when I just happened to find it on Amazon, but what wanted me to read it was that it is apparently the book that helped Alexis Ohanian want to start a company so it actually helped make reddit a reality.  After finding that out I thought I just had to read this book.

What just starts out as a few guys who just want to make games that they themselves wanted to play soon turns into an empire. Instantly I can tell this is another nonfiction book that just reads so much like a novel. It starts off with telling the early stories of the Two Johns, John Romero and John Carmack. About their gaming passion and how they got into programming games, and early on you can tell that they were destined to create their own masterpieces. After a little while creating and developing games for a small company they get the itch to create their own company. With Romero's creative ideas and Carmack's programming talent, they definitely have what it takes. They created id Software.

With the help of a few others they recruited, they were able to make their first game Commander Keen. Now I remember playing this back in the Windows 3.1 days and I had no idea it was id who created the game. Their next big title was Wolfenstein 3D. I still have memories of when I first played this and I'm sure, like myself, it the first time others have played a first person shooter. Their next blockbuster is perhaps the one they're most famous for, Doom. I think it was the first time I was hesitant to open a door in a game because I wasn't sure what was behind it.

 

Of course, there were some inner struggles within the small company that the book goes over as well. Eventually a few of them left to pursue other interests, but their initial passion of creating great games never ceased.

This was definitely a joy to read and, if you're a developer as well, you may just get a little interest in graphics or game programming yourself.