Coding & Stuff

Coding & Stuff

AJAX - CORS with Cloudfront and S3

Just found out that when you want to allow CORS to an AJAX request for a resource from Cloudfront + S3, it’s not enough to just add the permissions on your S3 bucket. You also have to make a called to your resource using an additional parameter in your URL.

For example, for the following values:
Your origin:
The resource from CF (S3):

You would think that enabling CORS on your bucket will be enough… think again. If you want to fetch calculator.js using AJAX you’ll have to make a call to the url:

An AMAZON hidden hack i had to spend half a day on :) If you’re having the same issue i hope you find this. It’ll save you some time.

Node JS - Listen to External IP Address

Good to know !

The SOOMLA Blog: Amazon's Global Coin - Smart Move or Doomed to Fail


Amazon recently announced the launch of a global coin allowing game developers for the kindle fire to recognize it as a form of payment for in app purchases. This is not a new idea, far from it. One might remember that Facebook discontinued a similar service by the name of Facebook credits or…

MongoHQ vs MongoLab: Selecting a Hosted MongoDB Provider - Dan Adams

android tip #2: @JavascriptInterface

If your targetSdkVersion (in AndroidManifest.xml) is 17 and you’re adding a Javascript interface to a Webview than you’ll have to add the annotation @JavascriptInterface above every JS interface function (all functions must also be public).
public void getDeviceId() {
    return "blabla";
(taken from

android tip: allow cross-origin for local files

If you’re on JellyBean and you get this error: ‘Origin null is not allowed by Access-Control-Allow-Origin’ when trying to navigate to a local file when inside a Webview, just add the following:

Great project for publish-subscribe in Android

I found Square’s otto while looking for a way to implement the publish-subscribe pattern in an Android application.

This great project has exactly what you need to have a notification center (like ios’s NSNotificationCenter) in Android.

ios tip: Screen Rotation ios5 vs ios6

Just a small tip for ios devs:

Rotating a UIViewController is done differently in ios5 and ios6. Just copy the relevant code below into your UIViewController. You can copy both code blocks if you want to support both io5 and ios6.


    if ([[StorefrontInfo getInstance] orientationLandscape]){
        return UIInterfaceOrientationIsLandscape(toInterfaceOrientation);

    return UIInterfaceOrientationIsPortrait(toInterfaceOrientation);


- (BOOL)shouldAutorotate
    //returns true if want to allow orientation change
    return YES;

- (NSUInteger)supportedInterfaceOrientations
    if ([[StorefrontInfo getInstance] orientationLandscape]){
        return UIInterfaceOrientationMaskLandscape;

    return UIInterfaceOrientationMaskPortrait;

Rails with AJAX Facebook Connect

Just implemented an option to connect to our Designer using Facebook and i wanted to share. It’s actually quite easy when you get the hang of it.

My requirements:

  1. Let users connect with their Facebook account (duh!).
  2. The login process must be done with AJAX and not like the regular process where you get redirected to facebook and back to the home page when the login process finishes.

Lets get right to work:

Integrating Facebook into your Rails application

Everyone knows about the AMAZING authentication plugin devise. This entire tutorial is based on The ‘devise’ team’s GREAT tutorial that explains how to integrate Facebook into your Rails application. Go over the tutorial and make sure it works. Things i did different:
  1. I’m using MongoDB (with Mongoid) so instead of creating a migration to the User table i just added ‘provider’ and ‘uid’ straight to the User model.
  2. In the function ‘find_for_facebook_oauth’ (in the User model) i added code that checks if the user that just connected with his Facebook account already registered with us (by the user’s email). This is my code:

  def self.find_for_facebook_oauth(auth, signed_in_resource=nil)
    user = User.where(provider: auth.provider, uid: auth.uid).first
    unless user
      # checking if the email from facebook already exists for one of the users
      user = User.where(email:
      if user
        user.provider = auth.provider
        user.uid = auth.uid!
        user = User.create(,

Supporting AJAX

We want to respond to the Facebook-Connect login process with a JSON format response. You’ll see in the next step below that you’ll handle this response and use it to change stuff on the screen.
(look in the code below for [PUT YOUR POST-LOGIN CODE HERE]).

To change the response to a JSON response, go to ‘OmniauthCallbacksController’ and make it look like this:

class Users::OmniauthCallbacksController < Devise::OmniauthCallbacksController
  def facebook
    @user = User.find_for_facebook_oauth(request.env["omniauth.auth"], current_user)

    if @user.persisted?

      render :status => 200, :json => { :user => { :email =>, :name => } }
      render :status => 401, :json => { :errors => alert }

Adding Facebook javascript SDK

If we don’t want to navigate out of the page in order to connect with Facebook and we want to get the Facebook-Connect callback into our Rails application, we have to take the default code from Facebook javascript SDK and change it a bit. This is what you need to add to your application.html.erb:

      <!-- Facebook connect script -->
      <div id="fb-root"></div>
      <script type="text/javascript">
          window.fbAsyncInit = function() {
              // init the FB JS SDK
                  appId      : '[YOUR APP ID HERE]', // App ID from the App Dashboard
                  channelUrl : '//[YOUR DOMAIN HERE]/channel.html', // Channel File for x-domain communication
                  status     : true, // check the login status upon init?
                  cookie     : true, // set sessions cookies to allow your server to access the session?
                  xfbml      : true  // parse XFBML tags on this page?

              // Additional initialization code such as adding Event Listeners goes here


          (function(d, debug){
              var js, id = 'facebook-jssdk', ref = d.getElementsByTagName('script')[0];
              if (d.getElementById(id)) {return;}
              js = d.createElement('script'); = id; js.async = true;
              js.src = "//" + (debug ? "/debug" : "") + ".js";
              ref.parentNode.insertBefore(js, ref);
          }(document, /*debug*/ false));

          $(function() {
              $('#facebook-connect').click(function(e) {

                  FB.login(function(response) {
                      if (response.authResponse) {
                          // since we have cookies enabled, this request will allow omniauth to parse
                          // out the auth code from the signed request in the fbsr_XXX cookie
                          $.getJSON('<%=user_omniauth_callback_path(:facebook)%>', function(data) {
                              // 'data' contains a 'user' object with 'email' and 'name' in it.
                              [PUT YOUR POST-LOGIN CODE HERE]
                  }, { scope: '[ADD THE SCOPE YOU WANT HERE]' });
      <!-- end Facebook connect script -->

That’s it! You got Facebook Connect working with AJAX in your Rails application

IMPORTANT: omniauth-facebook has an issue with CSRF in version 1.4.1 so use 1.4 until they’ll fix it. (thanks Alex Dolgov)

To access url helpers (url_for, etc) from Rails console (Rails 3) - Rails - Snipplr Social Snippet Repository