Skip to content

Instantly share code, notes, and snippets.

View evanjmg's full-sized avatar
👨‍💻
actively contributing

Evan Gillogley evanjmg

👨‍💻
actively contributing
View GitHub Profile
# add pusher to your GemFile
gem 'pusher'
# then in create a config/initializers/pusher.rb
require 'pusher'
Pusher.logger = Rails.logger
Pusher.encrypted = true
Pusher.app_id = ENV['PUSHER_APP_ID']
Pusher.key = ENV['PUSHER_KEY']
class UberService
def run(params)
@current_identity = get_user(params[:meta][:user_id]) # first get the current user in the ride
@current_identity.refresh_token_if_expired('https://login.uber.com')
return 'not accepted status' if params[:meta][:status] != 'accepted'
@ride = current_ride(params[:meta][:status], @current_identity)
{
"event_id": "3a3f3da4-14ac-4056-bbf2-d0b9cdcb0777",
"event_time": 1427343990,
"event_type": "all_trips.status_changed",
"meta": {
"user_id": "d13dff8b",
"resource_id": "2a2f3da4",
"resource_type": "request",
"status": "accepted"
},
class Api::V1::UberController < Api::V1::BaseController
skip_authorization_check
skip_before_filter :check_api_auth!, only: [:webhook]
def webhook
digest = OpenSSL::Digest.new('sha256')
hmac_key = OpenSSL::HMAC.hexdigest(digest, ENV['UBER_CLIENT_SECRET'],request.body.read)
require 'typhoeus'
class Identity < ActiveRecord::Base
belongs_to :user
def refresh_token_if_expired(domain)
if token_expired?
client_id_key = self.provider.upcase + '_CLIENT_ID'
client_secret_key = self.provider.upcase + '_CLIENT_SECRET'
r= 'http://localhost:9000/' + self.provider
def receive_token(object)
create_hash(object)
# find identity if it already exists or create a new one and save.
if @authenticated_user = Identity.find_by(provider: 'uber', user_id: object[:user_id])
if @authenticated_user.update_attributes(@auth_hash)
head :ok
else
render :json => { :errors => @authenticated_user.errors.full_messages }, :status => :unprocessable_entity
class CreateIdentities < ActiveRecord::Migration
def change
create_table :identities do |t|
t.references :user, index: true
t.string :provider
t.text :uid
t.text :access_token
t.datetime :expires_at
t.timestamps
end
def login
response = Typhoeus::Request.new(
"https://login.uber.com/oauth/v2/token",
method: :post,
body: {
'code': params[:code],
'redirect_uri': params[:redirect_uri],
'client_secret': ENV['UBER_CLIENT_SECRET'],
'client_id': ENV['UBER_CLIENT_ID'],
'grant_type': 'authorization_code'
function login () {
// get your specified redirect uri - usually window.location + '/uber'
var redirect_uri = '{your-url}'
// generate login url to get oauth code
var url = 'https://login.uber.com/oauth/v2/authorize?client_id='+ ENV['uber'] +'&response_type=code&scope=request+all_trips+profile&redirect_uri='+ redirect_uri;
// replace current url with new url
window.location.replace(url);
}
@evanjmg
evanjmg / web sockets and rails
Created July 9, 2016 14:31
Webhooks and Sockets with Uber, Angular, and Rails
Integrating new features, such as OAuth and Sockets into a large system can sometimes be a difficult process. However, our experience implementing Uber OAuth and Sockets into our Rails API and Angular app was relatively straight forward. Here are a couple of the topics we'll cover in article:
<ul>
<li><a href="#oauth">Setting up OAuth Authorization</a></li>
<li><a href="#webhook">Setting up an Uber Webhook</a></li>
<li><a href="#pusher-server">Setting up Pusher on the Server (Sockets)</a></li>
<li><a href="#pusher-angular">Setting up Pusher in Angular (Sockets)</a></li>
</ul>