This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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'] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{ | |
"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" | |
}, |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> | |