Class: Osm::Member::Contact

Inherits:
Osm::Model show all
Defined in:
lib/osm/member.rb

Constant Summary

Constants inherited from Osm::Model

Osm::Model::SORT_BY

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Osm::Model

#<, #<=, #<=>, #>, #>=, #between?, #changed_attributes, configure, #reset_changed_attributes, #to_i

Constructor Details

#initializeObject

Initialize a new Contact

Parameters:

  • attributes (Hash)

    The hash of attributes (see attributes for descriptions, use Symbol of attribute name as the key)



# File 'lib/osm/member.rb', line 660

Instance Attribute Details

#additional_informationDirtyHashy

Returns the additional information (key is OSM’s variable name, value is the data).

Returns:

  • (DirtyHashy)

    the additional information (key is OSM’s variable name, value is the data)



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#additional_information_labelsDirtyHashy

Returns the labels for the additional information (key is OSM’s variable name, value is the label).

Returns:

  • (DirtyHashy)

    the labels for the additional information (key is OSM’s variable name, value is the label)



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_1String

Returns the 1st line of the address.

Returns:

  • (String)

    the 1st line of the address



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_2String

Returns the 2nd line of the address.

Returns:

  • (String)

    the 2nd line of the address



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_3String

Returns the 3rd line of the address.

Returns:

  • (String)

    the 3rd line of the address



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#address_4String

Returns the 4th line of the address.

Returns:

  • (String)

    the 4th line of the address



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#first_nameString

Returns the contact’s first name.

Returns:

  • (String)

    the contact’s first name



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#last_nameString

Returns the contact’s last name.

Returns:

  • (String)

    the contact’s last name



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#phone_1String

Returns the primary phone number.

Returns:

  • (String)

    the primary phone number



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#phone_2String

Returns the secondary phone number.

Returns:

  • (String)

    the secondary phone number



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

#postcodeString

Returns the postcode of the address.

Returns:

  • (String)

    the postcode of the address



641
# File 'lib/osm/member.rb', line 641

attribute :first_name, :type => String

Instance Method Details

#all_phonesArray<String>

Get an array of all phone numbers for the contact

Returns:

  • (Array<String>)


673
674
675
# File 'lib/osm/member.rb', line 673

def all_phones
  [phone_1, phone_2].select{ |n| !n.blank? }.map{ |n| n.gsub(/[^\d\+]/, '') }
end

#name(seperator = ' ') ⇒ String

Get the full name

Parameters:

  • seperator (String) (defaults to: ' ')

    What to split the contact’s first name and last name with

Returns:

  • (String)

    this scout’s full name seperated by the optional seperator



667
668
669
# File 'lib/osm/member.rb', line 667

def name(seperator=' ')
  return [first_name, last_name].select{ |i| !i.blank? }.join(seperator)
end

#update(api, member, force = false) ⇒ Boolean

Update the contact in OSM

Parameters:

  • api (Osm::Api)

    The api to use to make the request

  • section (Osm::Member)

    The member to update the contact for

  • force (Boolean) (defaults to: false)

    Whether to force updates (ie tell OSM every attribute changed even if we don’t think it did)

Returns:

  • (Boolean)

    whether the member was successfully updated or not

Raises:



683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
# File 'lib/osm/member.rb', line 683

def update(api, member, force=false)
  raise Osm::ObjectIsInvalid, 'member is invalid' unless valid?
  require_ability_to(api, :write, :member, member.section_id)

  attribute_map = {
    'first_name' => 'data[firstname]',
    'last_name' => 'data[lastname]',
    'surgery' => 'data[surgery]',
    'address_1' => 'data[address1]',
    'address_2' => 'data[address2]',
    'address_3' => 'data[address3]',
    'address_4' => 'data[address4]',
    'postcode' => 'data[postcode]',
    'phone_1' => 'data[phone1]',
    'receive_phone_1' => 'data[phone1_sms]',
    'phone_2' => 'data[phone2]',
    'receive_phone_2' => 'data[phone2_sms]',
    'email_1' => 'data[email1]',
    'receive_email_1' => 'data[email1_leaders]',
    'email_2' => 'data[email2]',
    'receive_email_2' => 'data[email2_leaders]',
  } # our name => OSM name

  data = {}
  attributes.keys.select{ |a| !['additional_information', 'additional_information_labels'].include?(a) }.select{ |a| force || changed_attributes.include?(a) }.each do |attr|
    value = send(attr)
    value = 'yes' if value.eql?(true)
    data[attribute_map[attr]] = value
  end
  additional_information.keys.select{ |a| force || additional_information.changes.keys.include?(a) }.each do |attr|
    data["data[#{attr}]"] = additional_information[attr]
  end

  updated = true
  unless data.empty?
    result = api.perform_query("ext/customdata/?action=update&section_id=#{member.section_id}", {
      'associated_id' => member.id,
      'associated_type' => 'member',
      'context' => 'members',
      'group_id' => self.class::GROUP_ID,
    }.merge(data))
    updated = result.is_a?(Hash) && result['status'].eql?(true)
  end

  # Finish off
  if updated
    reset_changed_attributes
    additional_information.clean_up!
  end
  return updated
end