Sha256: d6ac64590f0bb71bffdea31ce0081da2bd7a19fec40ab3717cb7ba54be0795f4

Contents?: true

Size: 1.75 KB

Versions: 3

Compression:

Stored size: 1.75 KB

Contents

module Economic
  module Proxies
    module Actions
      class FindByName
        attr_reader :name

        def initialize(caller, name)
          @caller = caller
          @name = name
          @session = caller.session
        end

        def call
          contacts = build_partial_contact_entities(handles_from_endpoint)
          scope_to_owner(contacts)
        end

        private

        def build(*options)
          @caller.build(options)
        end

        def build_partial_contact_entities(handles)
          handles.collect do |handle|
            contact = build
            contact.partial = true
            contact.persisted = true
            contact.handle = handle
            contact.id = handle[:id]
            contact.number = handle[:number]
            contact
          end
        end

        def handles_from_endpoint
          [response[handle_key]].flatten.reject(&:blank?)
        end

        def handle_key
          (Support::String.underscore(@caller.class.entity_class_name) + "_handle").to_sym
        end

        def owner
          @caller.owner
        end

        def request(action, data)
          @session.request(
            Endpoint.new.soap_action_name(@caller.entity_class, action),
            data
          )
        end

        def response
          request("FindByName", {"name" => name})
        end

        def scope_to_owner(contacts)
          if owner.is_a?(Session)
            contacts
          else
            owner_type = Support::String.underscore(
              Support::String.demodulize(owner.class.name)
            )
            contacts.select do |contact|
              contact.get_data
              contact.send(owner_type) == owner
            end
          end
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
rconomic-0.7.0 lib/economic/proxies/actions/find_by_name.rb
rconomic-0.6.1 lib/economic/proxies/actions/find_by_name.rb
rconomic-0.6.0 lib/economic/proxies/actions/find_by_name.rb