Skip to content

Commit

Permalink
persist capture_id after sync
Browse files Browse the repository at this point in the history
  • Loading branch information
peppyheppy committed Dec 30, 2011
1 parent 064befe commit d252372
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 13 deletions.
2 changes: 1 addition & 1 deletion README.md
Expand Up @@ -114,7 +114,7 @@ Janrain::Authentication

# TODO:

* add flag for capture update failures, etc
* add flag for capture update failures, etc *flags*
* save the janrain entity attributes in model as cache @user.some_field
* create configuration for capture (split out resource/application configs from environment keys and secrets, etc) "resource_name"
* capture status for model (contains time left, etc)
Expand Down
4 changes: 2 additions & 2 deletions lib/janrain/capture/client/entity.rb
Expand Up @@ -36,9 +36,9 @@ def self.create(attrs={})
# https://janraincapture.com/docs/api_entity.bulkCreate.html
# returns an array of ids and/or errors
def self.bulk_create(new_entities=[], overrides={})
post("/entity.create", query: defaults.
post("/entity.bulkCreate", query: defaults.
merge(overrides).
merge(all_attributes: new_entites.to_json))
merge(all_attributes: new_entities.to_json))
end

# https://janraincapture.com/docs/api_entity.delete.html
Expand Down
45 changes: 39 additions & 6 deletions lib/janrain/capture/user.rb
Expand Up @@ -42,16 +42,49 @@ def to_capture(only_changes=false)

def persist_to_capture(only_changes = false)
params = self.to_capture(only_changes)
if not params.blank?
if params.present?
response = {}
if capture_id.blank?
response = Janrain::Capture::Client::Entity.create(params) unless params.blank?
else
existing_user = nil
email_existing = Janrain::Capture::Client::Entity.find("email = '#{email}'")['results'].first

# new user in local system will update remote capture and set capture_id if remote
# - capture_id is nil; existing is set; existing_user is nil
#
# new user in both systems will create new capture and set capture_id
# - capture_id is nil; existing is nil; existing_user is nil
#
# existing in both systems will update capture
# - capture_id is set; existing is set; existing_user is set

if self.capture_id or email_existing
if email_existing and not self.capture_id
# verify
if existing_user = User.find_by_capture_id(email_existing['id']) and
self.id != existing_user.id
then
raise "Janrain::Capture (user inconsistancy) an attempt to create a duplicate user was made."
end
self.capture_id = email_existing['id']
end
# update
response = Janrain::Capture::Client::Entity.update(capture_id, params)
else
# create
response = Janrain::Capture::Client::Entity.create(params)
self.capture_id = response['id']
end

if response['stat'] == 'ok'
if persisted?
update_attribute(:capture_id, self.capture_id)
else
self[:capture_id] = self.capture_id
end
end
response['stat'] == 'ok'
# return capture id
self.capture_id
else
true # nothing to update
self.capture_id # nothing to update
end
end

Expand Down
14 changes: 10 additions & 4 deletions spec/lib/janrain/capture/user_spec.rb
Expand Up @@ -29,28 +29,34 @@

it "should send all new attributes to capture" do
user = TestUser.new(@user_params.merge(capture_id: nil, email: 'newuser@valid.com'))
user.persist_to_capture.should be_true
user.persist_to_capture.should == 978
end

it "should send all new attributes to capture" do
user = TestUser.new(@user_params.merge(capture_id: nil, email: 'newuser@invalid.com'))
user.persist_to_capture.should be_false
user.persist_to_capture.should be_nil
end

end

context "update existing user" do

it "should not update capture if there are not any changes" do
user = TestUser.create(@user_params)
# Note: no changes have been made
user.persist_to_capture(only_changes = true).should == 1
end

it "should send changed attributes to capture VALID" do
user = TestUser.create(@user_params)
user.email = 'existinguser@valid.com'
user.persist_to_capture(only_changes = true).should be_true
user.persist_to_capture(only_changes = true).should == 1
end

it "should send changed attributes to capture INVALID" do
user = TestUser.create(@user_params)
user.email = 'existinguser@invalid.com'
user.persist_to_capture(only_changes = true).should be_false
user.persist_to_capture(only_changes = true).should == 1
end

end
Expand Down
4 changes: 4 additions & 0 deletions spec/support/janrain_api.rb
Expand Up @@ -9,6 +9,10 @@ def call(env)
when /\/oauth\/token.*code=a_valid_code/
[200, {"Content-Type" => "application/json"}, ['{"access_token":"a_valid_token","expires_in":3600,"refresh_token":"ajwym5c4f3c3bj6w92sb","stat":"ok","transaction_state":{"capture":{"action":"token_url_2","session_expires":null},"engage":{"identifier":"https://www.google.com/profiles/104030799970740279255","providerName":"Google"}}}']]
# /entity | with auth_token header
when /\/entity.find.*newuser%40(in)?valid.*/
[200, {"Content-Type" => "application/json"}, ['{"result_count":0,"results":[],"stat":"ok"}']]
when /\/entity.find.*existinguser%40(in)?valid.*/
[200, {"Content-Type" => "application/json"}, ['{"result_count":1,"results":[{"bio":null,"birthDate":"1900-01-01","birthday":null,"country":null,"created":"2011-12-29 19:27:11.436142 +0000","display":null,"displayName":null,"email":"existinguser@valid.com","firstName":null,"gender":"male","id":308099,"permissions":4,"preferences":0,"upperGenreBounds":null,"uuid":"995d75c2-1118-4b6e-bd63-b46d735346c4"}],"stat":"ok"}']]
when /\/entity.update.*existinguser%40valid.*/
[200, {"Content-Type" => "application/json"}, ['{"stat":"ok"}']]
when /\/entity.update.*existinguser%40invalid.*/ #
Expand Down

0 comments on commit d252372

Please sign in to comment.