class SpikesController < ApplicationController

  def facebook_invite

  end


  def gmail_contacts
    if current_user.nil? 
      render :nothing => true, :status => 404
      return
    end
    authorization = current_user.user_authorizations.where(:provider => 'google_login')
    if authorization.empty?
      render :nothing => true, :status => 404
      return
    end
    token = authorization.first.token
    uri = URI.parse("https://www.google.com/m8/feeds/contacts/default/full?oauth_token=#{token}&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)
    ret_contacts = []
    contacts['feed']['entry'].each_with_index do |contact,index|
      name = contact['title']['$t']
      contact['gd$email'].to_a.each do |email|
        email_address = email['address']
        ret_contacts.push(email_address)
      end
    end  

    render :json => ret_contacts
  end

end
