lib/ruby-fs-stack/familytree.rb in ruby-fs-stack-0.2.2 vs lib/ruby-fs-stack/familytree.rb in ruby-fs-stack-0.2.3

- old
+ new

@@ -26,34 +26,44 @@ def initialize(fs_communicator) @fs_communicator = fs_communicator end # ===params - # <tt>id</tt> should be a string of the persons identifier. For the 'me' person, use :me or 'me' + # <tt>id_or_ids</tt> should be a string of the persons identifier. For the 'me' person, use :me or 'me'. Can also accept an array of ID strings. # <tt>options</tt> accepts a hash of parameters as documented by the API. # For full parameter documentation, see DevNet[https://devnet.familysearch.org/docs/api-manual-reference-system/familytree-v2/r_api_family_tree_person_read_v2.html] # # ===Example # # communicator is an authenticated FsCommunicator object # # Request a person with no assertions, only the version. # p = communicator.familytree_v2.person :me, :names => 'none', :genders => 'none', :events => 'none' # # p.version # => '90194378772' # p.id # => 'KW3B-NNM' - def person(id, options = {}) - id = id.to_s - if id == 'me' - url = Base + 'person' + def person(id_or_ids, options = {}) + if id_or_ids.kind_of? Array + multiple_ids = true + url = Base + 'person/' + id_or_ids.join(',') else - url = Base + 'person/' + id + multiple_ids = false + id = id_or_ids.to_s + if id == 'me' + url = Base + 'person' + else + url = Base + 'person/' + id + end end url += "?"+FsUtils.querystring_from_hash(options) unless options.empty? response = @fs_communicator.get(url) familytree = Org::Familysearch::Ws::Familytree::V2::Schema::FamilyTree.from_json JSON.parse(response.body) - person = familytree.persons.find{|p| p.requestedId == id } - person ||= familytree.persons.first if id == 'me' - person + if multiple_ids + return familytree.persons + else + person = familytree.persons.find{|p| p.requestedId == id } + person ||= familytree.persons.first if id == 'me' + return person + end end def save_person(person) if person.id.nil? url = Base + 'person' @@ -187,10 +197,26 @@ person = familytree.persons.find{|p|p.id == base_id} return person end end + # Combines person into a new person + # + # ====Params + # * <tt>person_array</tt> - an array of person IDs. + def combine(person_array) + url = Base + 'person' + version_persons = self.person person_array, :genders => 'none', :events => 'none', :names => 'none' + combine_person = Org::Familysearch::Ws::Familytree::V2::Schema::Person.new + combine_person.create_combine(version_persons) + familytree = Org::Familysearch::Ws::Familytree::V2::Schema::FamilyTree.new + familytree.persons = [combine_person] + res = @fs_communicator.post(url,familytree.to_json) + familytree = Org::Familysearch::Ws::Familytree::V2::Schema::FamilyTree.from_json JSON.parse(res.body) + return familytree.persons[0] + end + private #options will either have a :parent, :child, or :spouse key. We need to find which one def get_relationship_type(options) keys = options.keys.collect{|k|k.to_s} key = keys.find{|k| ['parent','child','spouse'].include? k} @@ -782,9 +808,23 @@ def create_relationship(options) raise ArgumentError, ":type option is required" if options[:type].nil? raise ArgumentError, ":with option is required" if options[:with].nil? add_relationships! self.relationships.add_relationship(options) + end + + # This method should only be called from FamilytreeV2::Communicator#combine + # + # ====Params + # * <tt>persons</tt> - an array of person objects. All persons must have an id and version + def create_combine(persons) + self.personas = Org::Familysearch::Ws::Familytree::V2::Schema::PersonPersonas.new + self.personas.personas = persons.map do |person| + persona = Org::Familysearch::Ws::Familytree::V2::Schema::PersonPersona.new + persona.id = person.id + persona.version = person.version + persona + end end private def add_relationships! \ No newline at end of file