Sha256: d1fc5779353d01dceac2abe9c07dbc68379e6e4905f1e007b347daa6975cc0d3

Contents?: true

Size: 1.24 KB

Versions: 6

Compression:

Stored size: 1.24 KB

Contents

require 'securerandom'

module Centaman
  class Service::CreateSecondaryMember < Centaman::Service
    include Centaman::JsonWrapper

    attr_reader :primary_member_id, :secondary_members

    def after_init(args)
      @primary_member_id = args.fetch(:primary_member_id, nil)
      @secondary_members = args.fetch(:secondary_members, [])
    end

    def endpoint
      '/member_services/Member'
    end

    def object_class
      Centaman::Object::Member
    end

    def build_object(resp)
      return build_objects(resp) if resp.respond_to?(:map)

      return create_error(resp) unless resp.respond_to?(:merge)
      @build_object ||= object_class.new(resp.merge(additional_hash_to_serialize_after_response))
    end

    def options_hash
      request_body = secondary_members.map { |m| build_member(m) }
      request_body.to_json
    end

    private

    def build_member(member)
      {
        'FirstName' => member.first_name,
        'LastName' => member.last_name,
        'Title' => member.title,
        'IsPrimary' => false,
        'PrimaryMemberId' => primary_member_id
      }
    end

    def create_error(resp)
      message = { error: resp.parsed_response || 'Unable to create the member record(s).' }
      raise message[:error]
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
centaman-4.0.1 lib/centaman/service/create_secondary_member.rb
centaman-3.1.2 lib/centaman/service/create_secondary_member.rb
centaman-3.1.1 lib/centaman/service/create_secondary_member.rb
centaman-3.1.0 lib/centaman/service/create_secondary_member.rb
centaman-3.0.0 lib/centaman/service/create_secondary_member.rb
centaman-2.1.0 lib/centaman/service/create_secondary_member.rb