Skip to content

Instantly share code, notes, and snippets.

@jeffdeville
Created August 11, 2011 16:58
Show Gist options
  • Select an option

  • Save jeffdeville/1140180 to your computer and use it in GitHub Desktop.

Select an option

Save jeffdeville/1140180 to your computer and use it in GitHub Desktop.
class ConsumerToken
def self.make_facebook_public_profile_request(ids)
begin
response = Typhoeus::Request.get("https://graph.facebook.com/?fields=id,name,picture&ids=#{ids.join(',')}")
JSON.parse(response.body)
rescue
HoptoadNotifier.notify(
:error_class => "JSON Parse Error",
:error_message => "JSON Parsing Error: ",
:parameters => params
)
{}
end
end
end
# just including this, with the retryable module above, to give you some thoughts on
# how you can handle retries for 3rd party services. It's not related to your
# question, just your intent.
module Fb
include Retryable
extend self
def load_from_fb(ids, token)
retryable(:tries => 3, :on => NoMethodError, :hoptoad => {:friend_ids => ids, :token => token}) do
Mogli::User.find(ids, client, FIELDS)
end
end
end
require 'spec_helper'
require 'vcr_setup'
include UserFactory::Fb
describe "UserFactory" do
describe "Fb" do
use_vcr_cassette "fb", :record => :new_episodes
describe("#extract_userid_from_token") do
Scenario "extract_userid_from_token for unencoded token" do
When(:user_id) { UserFactory::Fb.extract_userid_from_token(ACCESS_TOKEN) }
Then "it should be able to pull the user id" do
user_id.should == "502884401"
end
end
Scenario "extract_userid_from_token for url-encoded token" do
When(:user_id) { UserFactory::Fb.extract_userid_from_token(ACCESS_TOKEN) }
Then "it should be able to pull the user id" do
user_id.should == "502884401"
end
end
end
shared_examples_for "jeff's persistent record should be complete" do
Then("it should map jeff's name") { jeff.name.should == "Jeff Deville" }
Then("it should map the access token") { jeff.access_token.should == ACCESS_TOKEN }
Then("it should map jeff's email") { jeff.email.should == "[email protected]" }
Then("it should map all of jeff's profile fields") do
expected_jeff_profile = Profile.new({"timezone"=>"-4", "third_party_id"=>"RKIgCGL1vk8jHMry99x9gDoJ8Qw",
"gender"=>"male", "id"=>"502884401", "birthday"=>"07/05/1978", "last_name"=>"Deville",
"updated_time"=>"2011-07-08T03:24:10+0000", "verified"=>"true", "locale"=>"en_US",
"first_name"=>"Jeff"}
)
%w[timezone third_party_id gender last_name birthday updated_time locale first_name].each do |field|
jeff.profile.send(field).should == expected_jeff_profile.send(field)
end
end
Then "should map the profile interests" do
jeff.profile.interests.count.should == 41
jeff.profile.interests.first.fb_id.should == "136550053041283"
jeff.profile.interests.first.name.should == "Reading"
end
Then "should map the profile activities" do
jeff.profile.activities.count.should == 14
jeff.profile.activities.first.fb_id.should == "103112876395671"
jeff.profile.activities.first.name.should == "Drew Brees"
end
Then "should map the profile books" do
jeff.profile.books.count.should == 14
jeff.profile.books.first.fb_id.should == "105512439482413"
jeff.profile.books.first.name.should == "The Dresden Files"
end
Then "should map the profile movies" do
jeff.profile.movies.count.should == 12
jeff.profile.movies.first.fb_id.should == "118107951542269"
jeff.profile.movies.first.name.should == "Harry Potter and the Deathly Hallows: Part II"
end
Then "should map the profile television" do
jeff.profile.television.count.should == 10
jeff.profile.television.first.fb_id.should == "30566281895"
jeff.profile.television.first.name.should == "Lost"
end
Then "should map the profile likes" do
jeff.profile.likes.count.should == 151
jeff.profile.likes.first.fb_id.should == "118107951542269"
jeff.profile.likes.first.name.should == "Harry Potter and the Deathly Hallows: Part II"
end
end
describe "#get" do
Given { User2.remove }
Given(:jeff_id) { "502884401" }
When(:imported_users) do
UserFactory::Fb.get(user_ids, ACCESS_TOKEN)
end
Scenario("one id is provided") do
Given(:jeff) { imported_users } #singular in this case
Given(:user_ids) { jeff_id }
Then("a single user is returned") do
imported_users.should be_a(User2)
end
it_should_behave_like "jeff's persistent record should be complete"
end
Scenario("multiple ids are provided") do
Given(:jeff) { imported_users.find { |u| u.fb_id == jeff_id } }
Given(:user_ids) { [jeff_id, "840748768"]}
Then("two users are returned") do
imported_users.should be_a(Array)
end
it_should_behave_like "jeff's persistent record should be complete"
end
end
end
end
module Retryable
def self.included(base)
base.extend(self)
end
# Options:
# * :tries - Number of retries to perform. Defaults to 1.
# * :on - The Exception on which a retry will be performed. Defaults to Exception, which retries on any Exception.
# * :hoptoad - a hash of options that will be sent to Hoptoad along w/ the exception on errors
# Example
# =======
# retryable(:tries => 1, :on => OpenURI::HTTPError) do
# # your code here
# end
def retryable(options = {}, &block)
opts = {:tries => 1, :on => Exception, :hoptoad => {}}.merge(options)
retry_exception, retries, hoptoad = opts[:on], opts[:tries], opts[:hoptoad]
begin
return yield
rescue retry_exception
HoptoadNotifier.notify($!, hoptoad)
retry if (retries -= 1) > 0
end
yield
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment