class RdHighriseApi::People

Reflects the People Api endpoint on Highrise

Public Class Methods

new(url, api_key) click to toggle source
# File lib/rd_highrise_api/people.rb, line 9
def initialize(url, api_key)
  @url = url
  @api_key = api_key
end

Public Instance Methods

all() click to toggle source
# File lib/rd_highrise_api/people.rb, line 14
def all
  response = faraday.get('/people.xml')
  fail ConnectionError if response.status != 200

  document = Nokogiri.XML(response.body)
  document.xpath('//person').map do |doc|
    as_hash(doc)
  end
end
create(params) click to toggle source
# File lib/rd_highrise_api/people.rb, line 24
def create(params)
  response = faraday.post('/people.xml') do |request|
    request.headers['Content-Type'] = 'application/xml'
    request.body = as_xml(params)
  end

  {
    status: 201,
    messages: error_messages_from(response.body)
  }
end

Private Instance Methods

as_hash(document) click to toggle source
# File lib/rd_highrise_api/people.rb, line 70
def as_hash(document)
  {
    first_name: document.at_xpath('first-name').content,
    last_name: document.at_xpath('last-name').content,
    title: document.at_xpath('title').content,
    background: document.at_xpath('background').content,
    linkedin_url: document.at_xpath('linkedin-url').content
  }
end
as_xml(params) click to toggle source
# File lib/rd_highrise_api/people.rb, line 38
def as_xml(params)
  builder = Nokogiri::XML::Builder.new(encoding: 'UTF-8') do |xml|
    xml.person do
      xml.contact_data do
        xml.email_addresses do
          xml.email_address do
            xml.address params.delete(:email_address)
            xml.location 'Work'
          end
        end
        xml.phone_numbers do
          xml.phone_number do
            xml.number params.delete(:phone_number)
            xml.location 'Work'
          end
        end
        xml.web_addresses do
          xml.web_address do
            xml.url params.delete(:web_address)
            xml.location 'Work'
          end
        end
      end

      params.each do |key, value|
        xml.send(key.to_s.gsub('_', '-'), value)
      end
    end
  end
  builder.to_xml
end