Permalink
Browse files

Add yam gem

  • Loading branch information...
1 parent 41789d1 commit 0908686daa88e86481709fbc6580d1f81da28b23 Mason Fischer committed Nov 8, 2012
View
@@ -36,6 +36,8 @@ gem 'tddium'
gem 'thin'
gem 'underscore-rails', '~> 1.3'
gem 'zero-clipboard-rails', '~> 1.0'
+gem 'yam', git: "https://#{ENV['GITHUB_TOKEN']}:x-oauth-basic@github.com/yammer/yam.git"
+
# We currently use this forked repo as it allows you to nest links to add associations.
# We can move back to the original gem when the gem fixes a bug where nesting causes
View
@@ -5,6 +5,18 @@ GIT
specs:
cocoon (1.0.20)
+GIT
+ remote: https://dc2c9edbddec893a0b71709d6cd36fab963ee11b:x-oauth-basic@github.com/yammer/yam.git
+ revision: ce49ace5411816c5a4b33952f32330f6cfed94f4
+ specs:
+ yam (0.0.2)
+ faraday (~> 0.8.1)
+ faraday_middleware (~> 0.9.0)
+ hashie (~> 1.2.0)
+ multi_json (~> 1.3)
+ oauth2 (~> 0.8.0)
+ rspec (~> 2.9.0)
+
GEM
remote: https://rubygems.org/
specs:
@@ -104,6 +116,8 @@ GEM
railties (>= 3.0.0)
faraday (0.8.4)
multipart-post (~> 1.1)
+ faraday_middleware (0.9.0)
+ faraday (>= 0.7.4, < 0.9)
ffi (1.1.5)
flutie (1.3.3)
foreman (0.60.2)
@@ -349,4 +363,5 @@ DEPENDENCIES
turnip (~> 1.0)
uglifier (>= 1.0)
underscore-rails (~> 1.3)
+ yam!
zero-clipboard-rails (~> 1.0)
@@ -3,6 +3,7 @@ class ApplicationController < ActionController::Base
before_filter :require_yammer_login
before_filter :check_blank_token
+ before_filter :configure_yammer
hide_action :current_user=
@@ -17,6 +18,25 @@ def current_user=(user)
@current_user = user
end
+ def configure_yammer
+ if signed_in?
+ oauth_token = current_user.access_token
+ staging = current_user.yammer_staging
+ elsif session[:event_id].present?
+ event = Event.find_by_uuid(session[:event_id])
+ oauth_token = event.owner.access_token
+ staging = event.owner.yammer_staging
+ else
+ oauth_token = omniauth_token
+ staging = omniauth_staging?
+ end
+
+ Yam.configure do |config|
+ config.oauth_token = oauth_token
+ config.endpoint = "https://www.#{"staging." if staging}yammer.com/api/v1/"
+ end
+ end
+
def current_user
@current_user ||= CurrentUser.find(
cookies.try(:signed).try(:[], :yammer_user_id),
@@ -32,6 +52,18 @@ def check_blank_token
end
end
+ def omniauth
+ request.env['omniauth.auth']
+ end
+
+ def omniauth_token
+ omniauth.try(:[],:credentials).try(:[],:token)
+ end
+
+ def omniauth_staging?
+ (omniauth.try(:[],:provider) == 'yammer_staging')
+ end
+
def require_yammer_login
unless current_user.yammer_user?
session[:return_to] ||= request.fullpath
@@ -18,6 +18,7 @@ def create
@show_guest_login = show_guest_login?
if @guest.save
+ params[:event_id] = params[:event_id]
log_in_guest
redirect_to previous_page
else
@@ -30,15 +30,9 @@ def find_or_create_with_auth
end
def credentials
- {
- access_token: auth[:credentials][:token],
- yammer_staging: auth[:provider] == 'yammer_staging',
- yammer_user_id: auth[:uid]
- }
- end
-
- def auth
- request.env['omniauth.auth']
+ access_token: omniauth_token,
+ yammer_staging: omniauth_staging?,
+ yammer_user_id: omniauth[:uid]
end
def after_sign_in_path
@@ -10,6 +10,7 @@ def error(job, exception)
end
def perform
+ configure_yammer
invitation.deliver_invitation
end
@@ -18,4 +19,11 @@ def perform
def invitation
Invitation.find(invitation_id)
end
+
+ def configure_yammer
+ Yam.configure do |config|
+ config.oauth_token = invitation.sender.access_token
+ config.endpoint = invitation.sender.yammer_endpoint + "/api/v1"
+ end
+ end
end
@@ -10,6 +10,7 @@ def error(job, exception)
end
def perform
+ configure_yammer
reminder.deliver
end
@@ -18,4 +19,11 @@ def perform
def reminder
Reminder.find(reminder_id)
end
+
+ def configure_yammer
+ Yam.configure do |config|
+ config.oauth_token = reminder.sender.access_token
+ config.endpoint = reminder.sender.yammer_endpoint + "/api/v1"
+ end
+ end
end
@@ -8,24 +8,8 @@ def initialize(user, action, event)
end
def post
- post_activity_json
- rescue RestClient::Unauthorized
- @user.expire_token
- end
-
- private
-
- def post_activity_json
- RestClient.delay.post(rest_client_url, json_payload, json_arguments)
- end
-
- def rest_client_url
- @user.yammer_endpoint +
- "/api/v1/activity.json?access_token=#{@user.access_token}"
- end
-
- def json_payload
- {
+ Yam.delay.post(
+ "/activity",
activity: {
actor: {
name: @user.name,
@@ -41,10 +25,8 @@ def json_payload
},
message: '',
users: @event.invitees_for_json
- }.to_json
- end
-
- def json_arguments
- { content_type: :json, accept: :json }
+ )
+ rescue Faraday::Error::ClientError
+ @user.expire_token
end
end
View
@@ -69,8 +69,15 @@ def existing_scheddo_user
Guest.find_by_email(name_or_email_param)
end
+ def find_user_id_by_email
+ Yam.get(
+ '/users/by_email',
+ email: name_or_email_param
+ ).try(:first).try(:id)
+ end
+
def find_existing_yammer_user
- user_id = YammerUserIdFinder.new(event_creator, name_or_email_param).find
+ user_id = find_user_id_by_email
if user_id
YammerUser.new(
@@ -43,32 +43,6 @@ def deliver
private
- def send_user_message
- post_to_yammer({
- body: @message_body,
- direct_to_id: @recipient.yammer_user_id,
- og_url: event_url(@event)
- })
- end
-
- def send_group_message
- post_to_yammer({
- body: @message_body,
- group_id: @recipient.yammer_group_id,
- og_url: event_url(@event)
- })
- end
-
- def post_to_yammer(message)
- RestClient.post messages_endpoint + '?' +
- message.merge(access_token: @sender.access_token).
- to_query, nil
- end
-
- def messages_endpoint
- @sender.yammer_endpoint + '/api/v1/messages.json'
- end
-
def invitation_message_body
<<-BODY.strip_heredoc
#{@event.owner} created the "#{@event.name}" poll and I want your input.
@@ -97,4 +71,22 @@ def group_invitation_message_body
*This poll was sent using sched.do. Create your own polls for free at #{root_url}
BODY
end
+
+ def send_group_message
+ Yam.post(
+ "/messages",
+ body: @message_body,
+ group_id: @recipient.yammer_group_id,
+ og_url: event_url(@event)
+ )
+ end
+
+ def send_user_message
+ Yam.post(
+ "/messages",
+ body: @message_body,
+ direct_to_id: @recipient.yammer_user_id,
+ og_url: event_url(@event)
+ )
+ end
end
View
@@ -45,11 +45,11 @@ def fetch_yammer_user_data
update_attributes(
{
email: parse_email_from_response(response),
- image: response['mugshot_url'],
- name: response['full_name'],
- nickname: response['name'],
- yammer_profile_url: response['web_url'],
- yammer_network_id: response['network_id'],
+ image: response[:mugshot_url],
+ name: response[:full_name],
+ nickname: response[:name],
+ yammer_profile_url: response[:web_url],
+ yammer_network_id: response[:network_id],
extra: response
},
{ without_protection: true }
@@ -109,15 +109,11 @@ def yammer_group_id
end
def yammer_user_data
- JSON.parse(yammer_user_url)
+ Yam.get("/users/#{yammer_user_id}")
end
private
- def access_token_for_query
- { access_token: access_token }.to_query
- end
-
def associate_each_invitation_with(guest)
guest.invitations.each do |invitation|
self.invitations << invitation
@@ -132,12 +128,4 @@ def parse_email_from_response(response)
response['contact']['email_addresses'].
detect{ |address| address['type'] == 'primary' }['address']
end
-
- def yammer_user_url
- RestClient.get yammer_endpoint +
- '/api/v1/users/' +
- yammer_user_id.to_s +
- '.json?' +
- access_token_for_query
- end
end
@@ -1,34 +0,0 @@
-class YammerUserIdFinder
- SEARCH_PATH = '/api/v1/users/by_email.json'
-
- def initialize(user, email)
- @user = user
- @query = { email: URI.escape(email), access_token: user.access_token }.
- to_query
- end
-
- def find
- get_user_by_email
- get_id_from_json
- end
-
- private
-
- def get_user_by_email
- @response = RestClient.get(api_endpoint)
- end
-
- def api_endpoint
- "#{@user.yammer_endpoint}#{SEARCH_PATH}?#{@query}"
- end
-
- def get_id_from_json
- if @response.present? && parsed_json.present?
- parsed_json['id']
- end
- end
-
- def parsed_json
- JSON.parse(@response)[0]
- end
-end
View
@@ -15,3 +15,7 @@ RACK_ENV=development
# Set as appropriate for environment. Any long string works for the key.
HOSTNAME=localhost:3000
ACCESS_TOKEN_ENCRYPTION_KEY=key
+
+# Used for pulling private gems from GitHub
+# See https://help.github.com/articles/creating-an-oauth-token-for-command-line-use
+GITHUB_TOKEN=key
@@ -7,25 +7,20 @@
step 'a Yammer user exists named :name with email :email' do |name, email|
FakeYammer.yammer_user_name = name
FakeYammer.yammer_email = email
- returned = RestClient.get(EMAIL_SEARCH_URL + email)
- returned.code.should == 200
- json = JSON.parse(returned)
- json[0]['id'].should_not be_nil
+ users = Yam.get("/users/by_email", email: email)
+ users[0]['id'].should be_present
end
step 'no Yammer user exists with email :email' do |email|
FakeYammer.yammer_user_name = 'Wrong Name'
FakeYammer.yammer_email = email + '.fake'
- returned = RestClient.get(EMAIL_SEARCH_URL + email)
- returned.code.should == 200
- returned.should_not be_present
+ users = Yam.get("/users/by_email", email: email)
+ users.first.should_not be_present
end
step ':email signs up for Yammer as :name' do |email, name|
FakeYammer.yammer_user_name = name
FakeYammer.yammer_email = email
- returned = RestClient.get(EMAIL_SEARCH_URL + email)
- returned.code.should == 200
- json = JSON.parse(returned)
- json[0]['id'].should_not be_nil
+ users = Yam.get("/users/by_email", email: email)
+ users[0]['id'].should be_present
end
Oops, something went wrong.

0 comments on commit 0908686

Please sign in to comment.