class Outreach::Service::Prospect

Public Class Methods

new(client) click to toggle source
# File lib/outreach/service/prospect.rb, line 4
def initialize(client)
  @request = client.request
end

Public Instance Methods

find(id) click to toggle source
# File lib/outreach/service/prospect.rb, line 8
def find(id)
  response = @request.get("#{api_url}/#{id}")
  collection_class.build_from_attributes_hash(response['data'])
end
find_all(attrs={}) click to toggle source
# File lib/outreach/service/prospect.rb, line 13
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
update(id, attrs) click to toggle source
# File lib/outreach/service/prospect.rb, line 20
def update(id, attrs)
  mapped_attrs = update_attribute_mapping(attrs)
  @request.patch(api_url + "/" + id.to_s, mapped_attrs)
end

Protected Instance Methods

api_url() click to toggle source
# File lib/outreach/service/prospect.rb, line 27
def api_url
  "https://api.outreach.io/1.0/prospects"
end
collection_class() click to toggle source
# File lib/outreach/service/prospect.rb, line 31
def collection_class
  Outreach::Prospect
end
filter_attribute_mapping(attrs) click to toggle source
# File lib/outreach/service/prospect.rb, line 35
def filter_attribute_mapping(attrs)
  if attrs[:first_name]
    attrs["filter[personal/name/first]"] = attrs.delete(:first_name)
  end
  if attrs[:last_name]
    attrs["filter[personal/name/last]"] = attrs.delete(:last_name)
  end
  attrs["filter[contact/email]"] = attrs.delete(:email) if attrs[:email]
  if attrs[:company_name]
    attrs["filter[company/name]"] = attrs.delete(company_name)
  end
  attrs
end
update_attribute_mapping(attrs) click to toggle source
# File lib/outreach/service/prospect.rb, line 49
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