Sha256: ba9d8145665ca6f93b7c0573aec6e52540e334aeb266531cefcf30a748758358

Contents?: true

Size: 1.86 KB

Versions: 1

Compression:

Stored size: 1.86 KB

Contents

module Outreach
  module Service
    class Prospect
      def initialize(client)
        @request = client.request
      end

      def find(id)
        response = @request.get("#{api_url}/#{id}")
        collection_class.build_from_attributes_hash(response['data'])
      end

      def find_all(attrs = {})
        response = @request.get(api_url, filter_attribute_mapping(attrs))
        response['data'].map do |attrs|
          collection_class.build_from_attributes_hash(attrs)
        end
      end

      def update(id, attrs)
        mapped_attrs = update_attribute_mapping(attrs)
        @request.patch(api_url + '/' + id.to_s, mapped_attrs)
      end

      protected

      def api_url
        'https://api.outreach.io/api/v2/prospects?include=account,stage,sequenceStates&'
      end

      def collection_class
        Outreach::Prospect
      end

      def filter_attribute_mapping(attrs)
        if attrs[:first_name]
          attrs['filter[lastName]'] = attrs.delete(:first_name)
        end
        if attrs[:last_name]
          attrs['filter[firstName]'] = attrs.delete(:last_name)
        end
        attrs['filter[emails]'] = attrs.delete(:email) if attrs[:email]
        attrs['filter[id]'] = attrs.delete(:id) if attrs[:id]
        attrs
      end

      def update_attribute_mapping(attrs)
        result = {
          'data' => {
            'attributes' => {
              'personal' => {},
              'contact' => {}
            }
          }
        }

        if attrs['first_name']
          result['data']['attributes']['personal']['name']['first'] = attrs['first_name']
        end
        if attrs['last_name']
          result['data']['attributes']['personal']['name']['last'] = attrs['last_name']
        end
        if attrs['email']
          result['data']['attributes']['contact']['email'] = attrs['email']
        end
        result
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
outreach-ruby-0.0.2 lib/outreach-ruby/service/prospect.rb