Skip to content
Brian Simon edited this page May 22, 2014 · 25 revisions

Online Resources

  1. Mongoid: http://railscasts.com/episodes/238-mongoid-revised

  2. Mongoid installation: http://mongoid.org/en/mongoid/docs/installation.html Note: Instructions to get rid of Active Record are provided in mongoid installation steps.

  3. Google Contacts API: https://developers.google.com/google-apps/contacts/v3/ Bonus: import Outlook contacts and iPhone contacts

  4. Sorcery External Authentication: https://github.com/NoamB/sorcery/wiki/External

Misc unrelated links found but not used:

http://stackoverflow.com/questions/7996342/mongomapper-run-simple-mongodb-queries

Roadblocks:

  1. Handling duplicate contacts
  2. Offline contacts vs. Online synced contacts

Gmaps4Rails

You don't need Google API key when using gmaps4rails because it uses Google Maps Javascript API V3

http://stackoverflow.com/questions/5042829/google-maps-api-key-in-gmaps4rails

Mongoid

Note1: Don't call a field :class in MongoDB or application will crash

Note2: Updating instance variable in controller also updates Mongodb database

Example - Search function for contacts - code below will modify user -> contacts

def show
  @user = User.find(params[:id])
  
  @user.contacts = if params[:search] && params[:search] != ""
    @user.contacts.where(first_name: params[:search])
  else
    @user.contacts
  end  
end

Bourbon, Bitters, Neat

Neat grid system reference: http://neat.bourbon.io/examples/

Gemfile: gem 'bourbon' gem 'bitters' gem 'neat'

application.css.scss // normalize @import "normalize";

// bourbon (needed to install bitters from vendor stylesheets directory to create bitters/bitters)
@import "bourbon";
@import "bitters/bitters";
@import "grid-settings";
@import "neat-helpers";
@import "neat";

// bourbon refills
@import "refills-bourbon-cards";

OAuth2 and starting a SSL server

Note: Required for Google API

Starting Thin server instead of WEBBRICK

thin start --ssl

This is needed if you want to communicate through google API's and fetch their data.

How to setup a connection to a google API

Generally there are 2 steps to do:

  1. Register your app in google
  2. Add the script.

Few things to watch out when you work with API's

  • Make sure where the callback URL goes to, have different ones for different tools (e.g. one for sorcery, one for the API)
  • Check what request the API exactly needs or you might get an error from the API that you did something wrong (very hard to debug)

Here is an example script for fetching google contacts via the google API:

class ImportController < ApplicationController require 'net/http' require 'net/https' require 'uri' require 'rexml/document'

    def authenticate
      @title = "Google Authetication"

      client_id = Figaro.env.google_key
      google_root_url = "https://accounts.google.com/o/oauth2/auth?state=profile&redirect_uri="+googleauth_url+"&response_type=code&client_id="+client_id.to_s+"&approval_prompt=force&scope=https://www.google.com/m8/feeds/"
      redirect_to google_root_url
    end

    def authorise
      begin
        @title = "Google Authetication"
        token = params[:code]
        client_id = Figaro.env.google_key
        client_secret = Figaro.env.google_secret
        uri = URI('https://accounts.google.com/o/oauth2/token')
        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        request = Net::HTTP::Post.new(uri.request_uri)

        request.set_form_data('code' => token, 'client_id' => client_id, 'client_secret' => client_secret, 'redirect_uri' => googleauth_url, 'grant_type' => 'authorization_code')
        response = http.request(request)
        response.code
        access_keys = ActiveSupport::JSON.decode(response.body)

        uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?oauth_token="+access_keys['access_token'].to_s+"&max-results=50000&alt=json")

        http = Net::HTTP.new(uri.host, uri.port)
        http.use_ssl = true
        http.verify_mode = OpenSSL::SSL::VERIFY_NONE
        request = Net::HTTP::Get.new(uri.request_uri)
        response = http.request(request)


        contacts = ActiveSupport::JSON.decode(response.body)

        contacts['feed']['entry'].each_with_index do |contact,index|

           name = contact['title']['$t']
           # address = contact['gd$postalAddress'][0]['$t']
           contact['gd$email'].to_a.each do |email|
            email_address = email['address']
            current_user.contacts.create(:first_name => name, :email => email_address)  # for testing I'm pushing it into database..
          end
        end  
        rescue Exception => ex
        ex.message
      end
      redirect_to user_path(current_user), :notice => "Invite or follow your Google contacts."
    end
end