diff --git a/README.md b/README.md index 8fc0fbf..cc0fc57 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/lib/janrain/capture/client/entity.rb b/lib/janrain/capture/client/entity.rb index 79211cc..d6518c3 100644 --- a/lib/janrain/capture/client/entity.rb +++ b/lib/janrain/capture/client/entity.rb @@ -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 diff --git a/lib/janrain/capture/user.rb b/lib/janrain/capture/user.rb index acd37a3..179537b 100644 --- a/lib/janrain/capture/user.rb +++ b/lib/janrain/capture/user.rb @@ -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 diff --git a/spec/lib/janrain/capture/user_spec.rb b/spec/lib/janrain/capture/user_spec.rb index ceb84ec..40347c3 100644 --- a/spec/lib/janrain/capture/user_spec.rb +++ b/spec/lib/janrain/capture/user_spec.rb @@ -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 diff --git a/spec/support/janrain_api.rb b/spec/support/janrain_api.rb index 68cea9f..ae7e736 100644 --- a/spec/support/janrain_api.rb +++ b/spec/support/janrain_api.rb @@ -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.*/ #