Module: OfficeAutopilot::Client::Contacts

Included in:
OfficeAutopilot::Client
Defined in:
lib/office_autopilot/client/contacts.rb

Constant Summary collapse

CONTACTS_ENDPOINT =
'/cdata.php'

Instance Method Summary collapse

Instance Method Details

#contacts_add(options) ⇒ Object



13
14
15
16
17
# File 'lib/office_autopilot/client/contacts.rb', line 13

def contacts_add(options)
  xml = xml_for_contact(options)
  response = request(:post, CONTACTS_ENDPOINT, :body => {'reqType' => 'add', 'return_id' => '1', 'data' => xml}.merge(auth))
  parse_contacts_xml(response)[0]
end

#contacts_search(options) ⇒ Object



7
8
9
10
11
# File 'lib/office_autopilot/client/contacts.rb', line 7

def contacts_search(options)
  xml = xml_for_search(options)
  response = request(:post, CONTACTS_ENDPOINT, :body => {'reqType' => 'search', 'data' => xml}.merge(auth))
  parse_contacts_xml(response)
end

#parse_contacts_xml(response) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# File 'lib/office_autopilot/client/contacts.rb', line 54

def parse_contacts_xml(response)
  contacts = []
  xml = Nokogiri::XML(response)
  xml.css('result contact').each do |node|
    contact = {}
    contact['id'] = node['id']

    node.css('Group_Tag').each do |group_tag|
      group_tag_name = group_tag['name']
      contact[group_tag_name] = {}

      group_tag.css('field').each do |field|
        field_name = field['name']
        contact[group_tag_name][field_name] = field.content
      end
    end
    contacts << contact
  end
  contacts
end

#xml_for_contact(options) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/office_autopilot/client/contacts.rb', line 36

def xml_for_contact(options)
  attrs = {}

  id = options.delete('id')
  attrs[:id] = id if id

  xml = Builder::XmlMarkup.new
  xml.contact(attrs) do
    options.each_key do |group_tag|
      xml.Group_Tag(:name => group_tag) do
        options[group_tag].each do |field, value|
          xml.field(value, :name => field)
        end
      end
    end
  end
end

#xml_for_search(options) ⇒ Object



19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/office_autopilot/client/contacts.rb', line 19

def xml_for_search(options)
  if options.is_a?(Hash)
    options = [options]
  end

  xml = Builder::XmlMarkup.new
  xml.search do
    options.each do |option|
      xml.equation do
        xml.field option[:field]
        xml.op option[:op]
        xml.value option[:value]
      end
    end
  end
end