Skip to content

Commit

Permalink
Upgrade to Yam 2.0.0
Browse files Browse the repository at this point in the history
* User Yammer::Client instead of Yam
* Use new methods available

https://trello.com/c/XSQvDbXh
  • Loading branch information
jessieay committed Dec 10, 2013
1 parent e8da5b0 commit 6cd735f
Show file tree
Hide file tree
Showing 18 changed files with 85 additions and 76 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ gem 'strong_parameters'
gem 'swfobject-rails'
gem 'tddium'
gem 'thin'
gem 'yam', '0.0.6'
gem 'yam', '~> 2.0.0'
gem 'zclip-rails'

# Gems used only for assets, not required in production environments by default.
Expand Down
35 changes: 17 additions & 18 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ GEM
activesupport (3.2.16)
i18n (~> 0.6, >= 0.6.4)
multi_json (~> 1.0)
addressable (2.3.3)
addressable (2.3.5)
airbrake (3.1.8)
activesupport
builder
Expand Down Expand Up @@ -96,11 +96,9 @@ GEM
factory_girl_rails (4.2.1)
factory_girl (~> 4.2.0)
railties (>= 3.0.0)
faraday (0.8.5)
multipart-post (~> 1.1)
faraday_middleware (0.9.0)
faraday (>= 0.7.4, < 0.9)
ffi (1.9.3)
faraday (0.8.8)
multipart-post (~> 1.2.0)
ffi (1.4.0)
flutie (1.3.3)
foreman (0.61.0)
thor (>= 0.13.6)
Expand Down Expand Up @@ -144,9 +142,9 @@ GEM
jquery-rails (2.2.1)
railties (>= 3.0, < 5.0)
thor (>= 0.14, < 2.0)
json (1.7.7)
jwt (0.1.5)
multi_json (>= 1.0)
json (1.8.1)
jwt (0.1.8)
multi_json (>= 1.5)
launchy (2.2.0)
addressable (~> 2.3)
listen (0.7.3)
Expand All @@ -160,15 +158,16 @@ GEM
mime-types (1.25.1)
multi_json (1.8.2)
multi_xml (0.5.3)
multipart-post (1.1.5)
multipart-post (1.2.0)
newrelic_rpm (3.5.6.55)
nokogiri (1.5.6)
oauth2 (0.8.0)
oauth2 (0.8.1)
faraday (~> 0.8)
httpauth (~> 0.1)
jwt (~> 0.1.4)
multi_json (~> 1.0)
rack (~> 1.2)
oj (2.0.14)
omniauth (1.1.3)
hashie (~> 1.2)
rack
Expand Down Expand Up @@ -221,6 +220,8 @@ GEM
rake (10.1.0)
rdoc (3.12.2)
json (~> 1.4)
rest-client (1.6.7)
mime-types (>= 1.16)
rspec (2.14.1)
rspec-core (~> 2.14.0)
rspec-expectations (~> 2.14.0)
Expand Down Expand Up @@ -305,13 +306,11 @@ GEM
websocket (1.0.7)
xpath (1.0.0)
nokogiri (~> 1.3)
yam (0.0.6)
faraday (~> 0.8.1)
faraday_middleware (~> 0.9.0)
hashie (~> 1.2.0)
json (~> 1.7.6)
yam (2.0.0)
addressable (~> 2.3.3)
multi_json (~> 1.3)
oauth2 (~> 0.8.0)
oj (~> 2.0.10)
rest-client (~> 1.6.7)
zclip-rails (0.0.1)
jquery-rails (>= 1.0.17)
railties (~> 3.1)
Expand Down Expand Up @@ -370,5 +369,5 @@ DEPENDENCIES
timecop
turnip (= 1.0)
uglifier (>= 1.0)
yam (= 0.0.6)
yam (~> 2.0.0)
zclip-rails
2 changes: 1 addition & 1 deletion app/controllers/sessions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def find_or_instantiate_user
end

def refresh_user_data_from_yammer(user)
yammer_user_data = user.yammer_client.get("/users/#{omniauth[:uid]}")
yammer_user_data = user.yammer_client.get_user(omniauth[:uid]).body
YammerUserResponseTranslator.
new(yammer_user_data, user).
translate.
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/yammer_user_invitations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def args
end

def create_user_from(invitee_id)
invitee_data = owner_yammer_client.get("/users/#{invitee_id}")
invitee_data = owner_yammer_client.get_user(invitee_id).body
User.new.tap do |user|
YammerUserResponseTranslator.
new(invitee_data, user).
Expand Down
2 changes: 1 addition & 1 deletion app/jobs/activity_creator_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ def user
end

def post_yammer_activity
user.yammer_client.post('/activity', json_payload)
user.yammer_client.create_activity(json_payload)
end

def json_payload
Expand Down
5 changes: 1 addition & 4 deletions app/models/invitee_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,7 @@ def get_user_from_yammer
end

def get_yammer_user_by_email
@event.owner.
yammer_client.
get('/users/by_email', email: @email).
try(:first)
@event.owner.yammer_client.get_user_by_email(@email).try(:body).try(:first)
end

def create_guest
Expand Down
16 changes: 3 additions & 13 deletions app/models/user.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
require 'yammer'

class User < ActiveRecord::Base
attr_encrypted :access_token, key: ENV['ACCESS_TOKEN_ENCRYPTION_KEY']

Expand Down Expand Up @@ -90,7 +92,7 @@ def yammer_client
raise 'Yammer client requires an access_token!'
end

@yam ||= Yam.new(access_token, yammer_endpoint)
@yam ||= Yammer::Client.new(access_token: access_token)
end

private
Expand All @@ -107,18 +109,6 @@ def messenger(sender)
end
end

def yammer_endpoint
"#{yammer_host}/api/v1/"
end

def yammer_host
if yammer_staging
Rails.configuration.yammer_staging_host
else
Rails.configuration.yammer_host
end
end

def strip_email_whitespace
self.email = email.try(:strip)
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/yammer_messenger.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ def notify(event, message)
private

def deliver
@sender.yammer_client.post('/messages', request_params)
@sender.yammer_client.create_message(message_body, request_params)
end

def request_params
Expand Down
31 changes: 27 additions & 4 deletions app/models/yammer_user_response_translator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,11 +19,34 @@ def translate

private

# def parse_email_from_response
# if @response && @response[:contact] && @response[:contact][:email_addresses]
# primary_email = @response[:contact][:email_addresses].
# detect{ |address| address[:type] == 'primary' }
# primary_email && primary_email[:address]
# end
# end


def parse_email_from_response
if @response && @response['contact'] && @response['contact']['email_addresses']
primary_email = @response['contact']['email_addresses'].
detect{ |address| address['type'] == 'primary' }
primary_email && primary_email['address']
if valid_response?
primary_email && primary_email_address
end
end

def valid_response?
@response && @response[:contact] && @response[:contact][:email_addresses]
end

def primary_email
all_email_addresses.detect{ |address| address[:type] == 'primary' }
end

def all_email_addresses
@response[:contact][:email_addresses]
end

def primary_email_address
primary_email[:address]
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ Feature: Multiple invitations
And I invite the Yammer user "Joe Smith" to "Clown party" from the multiple invite page
And I remove "Joe Smith" from the list of invited users
When I press the "Invite" button for the "Clown party" event
Then "Joe Smith" should receive 0 private message
Then "Joe Smith" should receive 0 private messages
And I should not see "Joe Smith" in the list of invitees

@javascript
Expand Down
10 changes: 5 additions & 5 deletions spec/acceptance/step_definitions/invitation_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,14 +56,14 @@

step 'someone invites :email to :event_name' do |email, event_name|
event = Event.find_by_name!(event_name)
invitation = Invitation.new(event: event)
InviteeBuilder.new.find_user_by_email_or_create_guest(email, event).save
user = InviteeBuilder.new(email, event).find_user_by_email_or_create_guest
create(:invitation, event: event, invitee: user)
end

step ':guest_email was invited to the event :event_name' do |guest_email, event_name|
event = Event.find_by_name!(event_name)
invitation = Invitation.new(event: event)
InviteeBuilder.new(guest_email, event).find_user_by_email_or_create_guest.save
user = InviteeBuilder.new(guest_email, event).find_user_by_email_or_create_guest
create(:invitation, event: event, invitee: user)
end

# Multi-invite page
Expand Down Expand Up @@ -114,7 +114,7 @@
end

step ':first_item should appear before :second_item' do |first_item, second_item|
expect(page.body).to match /#{first_item}.*#{second_item}/m
expect(page.body).to match(/#{first_item}.*#{second_item}/m)
end

step ':name should receive a private message' do |name|
Expand Down
18 changes: 9 additions & 9 deletions spec/acceptance/step_definitions/yammer_user_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,25 +3,25 @@
end

step 'a Yammer user exists named :name with email :email' do |name, email|
yammer = Yam.new('123', 'https://www.yammer.com/api/v1')
yammer = Yammer::Client.new(access_token: '123')
FakeYammer.yammer_user_name = name
FakeYammer.yammer_email = email
users = yammer.get('/users/by_email', email: email)
expect(users[0]['id']).to be_present
users = yammer.get_user_by_email(email).body
expect(users[0][:id]).to be_present
end

step 'no Yammer user exists with email :email' do |email|
yammer = Yam.new('123', 'https://www.yammer.com/api/v1')
yammer = Yammer::Client.new(access_token: '123')
FakeYammer.yammer_user_name = 'Wrong Name'
FakeYammer.yammer_email = email + '.fake'
users = yammer.get('/users/by_email', email: email)
expect(users.first).to_not be_present
users = yammer.get_user_by_email(email).body
expect(users).to_not be_present
end

step ':email signs up for Yammer as :name' do |email, name|
yammer = Yam.new('123', 'https://www.yammer.com/api/v1')
yammer = Yammer::Client.new(access_token: '123')
FakeYammer.yammer_user_name = name
FakeYammer.yammer_email = email
users = yammer.get('/users/by_email', email: email)
expect(users[0]['id']).to be_present
users = yammer.get_user_by_email(email).body
expect(users[0][:id]).to be_present
end
2 changes: 1 addition & 1 deletion spec/controllers/invitations_controller_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@
it 'creates multiple invitations for the correct users' do
event_creator = create_user_and_sign_in
event = create(:event, owner: event_creator)
invitation = double(invite: nil, valid?: true)
invitation = double(invite: nil, invite: nil, valid?: true)
emails = '[email protected], [email protected]'
Invitation.stub(new: invitation)
invitee = double
Expand Down
8 changes: 4 additions & 4 deletions spec/jobs/activity_creator_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,13 @@
event = build_stubbed(:event)
User.stub(find: user)
Event.stub(find: event)
yam_session_stub = double(post: nil)
Yam.stub(new: yam_session_stub)
yam_session = double(create_activity: nil)
Yammer::Client.stub(new: yam_session)

ActivityCreatorJob.new(user, action, event).perform

expect(yam_session_stub).to have_received(:post).
with('/activity', expected_json(event))
expect(yam_session).to have_received(:create_activity).
with(expected_json(event))
end

private
Expand Down
2 changes: 1 addition & 1 deletion spec/jobs/vote_created_job_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@

VoteCreatedJob.new.perform

expect(voter.yammer_client.oauth_token).to eq voter.access_token
expect(voter.yammer_client.access_token).to eq voter.access_token
end
end

Expand Down
6 changes: 3 additions & 3 deletions spec/models/invitee_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,13 +27,13 @@
it 'searches for existing Yammer users' do
invitation = create(:invitation)
invitee_email = '[email protected]'
yam_client = double(get: nil)
Yam.stub(new: yam_client)
yam_client = double(get_user_by_email: nil)
Yammer::Client.stub(new: yam_client)

InviteeBuilder.new(invitee_email, invitation.event).
find_user_by_email_or_create_guest

expect(yam_client).to have_received(:get)
expect(yam_client).to have_received(:get_user_by_email)
end

it 'creates a User if it finds an existing Yammer user' do
Expand Down
12 changes: 7 additions & 5 deletions spec/models/yammer_user_response_translator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,13 @@
web_url: 'http://web_url',
network_id: 2,
network_name: 'network',
'contact' => {
'email_addresses' => [{
'type' => 'primary',
'address' => '[email protected]'
}]
contact: {
email_addresses: [
{
type: 'primary',
address: '[email protected]'
}
]
}
}
user = User.new
Expand Down
4 changes: 1 addition & 3 deletions spec/support/fake_yammer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def self.reset
end

post '/api/v1/messages' do
self.access_token = params[:access_token]
self.access_token = self.request.env["HTTP_AUTHORIZATION"].split.last
self.group_id = params[:group_id]
self.message = params[:body]
self.messages_endpoint_hits += 1
Expand All @@ -37,8 +37,6 @@ def self.reset

if params[:email] == self.yammer_email
json_for_user_email_search
else
"[ ]"
end
end

Expand Down

0 comments on commit 6cd735f

Please sign in to comment.