Category: Coding

  • Learning MongoDB on Windows 7 (Shell Edition)

    I’ve worked on a high-volume site that processed thousands of telecom network subscribers everyday. Me and the rest of the team managed to handle the challenges using MySQl and query optimizations, but now, I wonder how easier the tasks would have been if we had used NoSQL / MongoDB back then.

    My environment and methods:

    As I wrote this tutorial, I was using Windows 7. I will be using MongoDB Shell edition on my Git Bash.

    I actually haven’t used MongoDB yet, but I’m trying to learn it now. Let me share with you the steps I’ve been taking.

    MongoDB: Beginner’s guide

    For the MongoDB terms such as document, collection, shards, query routes, All out beginner’s guide to MongoDB from Analytics Vidhya provides good explanation.

    Download for Windows 7

    Download the latest version of signed .msi file from MongoDB Win-32 downloads page. As of writing this was the version I downloaded.

    Getting Started with MongoDB (MongoDB Shell Edition)

    Let’s follow the instructions from the MongoDB website on getting started with MongoDB using Shell.

    You may choose other editions such as Java, Python, Node.JS, and others in this page.

    Common hiccups

    MongoDB error: SyntaxError: missing ; before statement @(shell)

    While running the mongoimport command, if you get this kind of error in your shell for MongoDB:

    E QUERY [thread1] SyntaxError: missing ; before statement @(shell)

    Try to open a new command prompt / Git Bash window (or whatever you’re using), navigate to the directory where your mongo.exe is (e.g. C:/MongoDB/bin/) and run your mongoimport command there.

    MongoDB Improper shutdown

    When I press Ctrl + C it leads to “improper shutdown” error. Refer to this doc to repair / recover from improper shutdown of MongoDB.

  • Laravel 5: create route group that is only available during testing (Middleware)


    Today I needed to protect a group of routes from everything except my testing environment. I need to run PHPUnit tests that depend on certain routes, but I don’t want those routes to be available anywhere else. I’ll share in this blog post what I did to get what I need.

    Assumptions in this tutorial:

    You already have Laravel 5 installed on your computer and you are running the app in “testing” mode (e.g. PHPUnit testing)

    Introduction

    In Laravel, Middleware filters requests that are coming into our application. One of the pre-made Middleware in Laravel 5.2 is Authenticate, which looks like this:

    Inside the handle() method, it checks if the request is using AJAX -or- is asking for JSON, and if either condition returns TRUE, it blocks access and returns ‘Unauthorized’ as response. Otherwise, client is redirected to the ‘login’ route, which should happen when client is requesting via web browser.

    What I did to make my grouped routes available only in testing environment (in my case, when running PHPUnit) was create a new Middlewave. I’ll show you how. 🙂

    Create a new file under Http/Middleware and name it TestingEnvOnly.php

    In the handle method, we check if app environment is set to testing, and if so, we allow the request to access the requested route. Otherwise, the request should fail and repond with ‘Unavailable’.

    Now, let’s register this Middleware to Http/Middleware. Add the following to the $routeMiddleware array:

    It’s time to add a testing-only route:

    If you use PHPUnit like I do, you can test our new Middleware by creating this test method:

    I’m only using var_dump so that we can easily see the printed string response which should say “This should be visible only for testing environment” when you run PHPUnit.

    Now, in your web browser, visit the route your set for /api/v1/testing_env_only. It should say ‘Unauthorized’.

    Here’s how the files I edited in Laravel look like now:

    I’m using Laravel 5.2 and these are codes I uploaded at Gist

  • Laravel 5: activate API token guard and print “Unauthorized” instead of login redirect

    In Laravel 5, I wrap my API routes in a route group like this:

    Notice that I use auth:api and throttle on my middleware array.

    auth:api turns on the auth middleware with API token as guard, while throttle activates rate limiter for APIs.

    To get started with API token authentication, you may read https://gistlog.co/JacobBennett/090369fbab0b31130b51.

    I followed the same tutorial to set mine up, but had this issue: Upon using invalid api_token or no token at all, I am redirected to the /login route, which in my case is a currently nonexistent route.

    As instructed by the guide, I already edited my app/Http/Middleware/Authenticate.php file to add the || $request->wantsJson() condition, but my API routes still redirected to /login route upon detection of invalid api_token.

    The solution

    What I did was add another condition: || $guard == "api"

    And now, my app/Http/Middleware/Authenticate.php file looks like this:

  • Android app dev: ShareActionProvider in Activity & Fragment

    I’ve been taking the Developing Android Apps course on Udacity, and yesterday I encountered a difficulty in solving the Quiz: Share Intent.

    One of the course’s goal is to build the Sunshine App – an Android app with weather forecast.

    In that quiz I had to implement ShareActionProvider on my DetailsFragment so that a share icon becomes available on my app’s ActionBar.

    One of the main difficulties was the error that stated ActionProvider cannot be cast into ShareActionProvider. And at some point there was also an error saying that the methods/classes I’m using require API 14, while my minimum supported API is 9. The Udacity course required that I set it to 9 so I couldn’t change that.

    Now, the sharing in my app is working file, and I will share how I did it via Gist. I actually ended up taking a peek at the code provided by Developing Android Apps course on Udacity because I didn’t want to waste any more time doing trial-and-error (I have another project to work on! lol), but I still had to edit their code for ShareActionProvider here and there to make it bug-free.

    And so, take a look at how my Sunshine app codes ended up:

  • Mac OS X – Sublime Text 2 key bindings suddenly stopped working (SOLVED)

    https://www.sublimetext.com/docs/2/revert.html

    I’m using a Mac Mini with OS X El Capitan (v. 10.11) with Sublime Text v.2.0.2.

    Today, Sublime Text shortcuts like Cmd + / (line comment), Cmd + D (multiple selection), Cmd + F (find) and others stopped working. I had no problem with those key bindings before until today.

    Based on research, I found that it could be caused by packages installed to Sublime Text. Aha! It could be the GoToClass package that I just installed today! I uninstalled it since I was desperate and the package didn’t work as I need it to anyway. Still no luck, my favorite Sublime Text shortcuts still didn’t work.

    I decided to revert my Sublime Text into a clean install. That fixed my key bindings that stopped working.

    I’ll show you how.

    First, quit Sublime Text 2.

    Then remove the “Sublime Text 2” folder that’s located at ~/Library/Application Support/. You may try to do so by running this command in the terminal:

    FYI: The backslashes are required to escape the white spaces in file/folder names. rm is the command for removing files/folders. The -rf flag means that we want to remove the folder we specified and all its contents recursively.

    Once deleted, run Sublime Text 2 again. You’ll notice that it’s back to its freshly installed state. 🙂

  • Laravel 5 RESTful API with Basic Auth: PHPUnit Testing

    I’ve been using PHPUnit to test my RESTful API. PHPUnit is ready to use in Laravel.

    In my Laravel-based RESTful API, I have an endpoint /api/users which returns JSON data when you send a GET request. The API will only return such data if the client sends the corrent username and password (Basic Authentication). The JSON data looks like this:

    I’m gonna show you how to create a PHPUnit test for this kind of JSON API data in Laravel 5.

    To create a test case, use this command:

    This will create a new test case named ApiUsersTest.php in your directory /tests

    Paste the following code in it.

    Edit the test case as needed.

  • Android M runtime permissions utility class


    Hello world, I prepared a PermissionsUtil.java class and it’s on GitHub.

    Check it out: AndroidPermissionsUtil

    Why use it? It will help by letting you write less code as you add conditions to your Android app that supports Android 6.0 (Marshmallow). The utility class defines helper methods that were used in the following example.

    Sample usage of Android M PermissionUtils

    On the activity where you need to check for runtime permissions, add these:

    And add the callback function to the same activity:

    Feel free to use in on any project. And please let me know if you have suggestions on how to improve that Android M runtime permissions utility class.