diff --git a/lib/ews/ews_client.rb b/lib/ews/ews_client.rb index 2d0a43d3..cd60fb74 100644 --- a/lib/ews/ews_client.rb +++ b/lib/ews/ews_client.rb @@ -6,6 +6,7 @@ require 'ews/calendar_accessors' require 'ews/room_accessors' require 'ews/roomlist_accessors' +require 'ews/resolve_names_accessors' require 'ews/convert_accessors' require 'ews/meeting_accessors' @@ -20,6 +21,7 @@ class Viewpoint::EWSClient include Viewpoint::EWS::CalendarAccessors include Viewpoint::EWS::RoomAccessors include Viewpoint::EWS::RoomlistAccessors + include Viewpoint::EWS::ResolveNamesAccessors include Viewpoint::EWS::ConvertAccessors include Viewpoint::EWS::MeetingAccessors include Viewpoint::StringUtils diff --git a/lib/ews/resolve_names_accessors.rb b/lib/ews/resolve_names_accessors.rb new file mode 100644 index 00000000..ac44f442 --- /dev/null +++ b/lib/ews/resolve_names_accessors.rb @@ -0,0 +1,57 @@ +=begin + This file is part of Viewpoint; the Ruby library for Microsoft Exchange Web Services. + + Copyright © 2013 Camille Baldock + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +=end + +module Viewpoint::EWS::ResolveNamesAccessors + include Viewpoint::EWS + + # Resolves a contact's name given an email address. + # @see https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/resolvenames-operation + def resolve_names(name, full_contact_data = true) + resp = ews.resolve_names(name, full_contact_data) + resp + end + + def mailbox(resolved) + resolved.response_message[:elems][:resolution_set][:elems].first[:resolution][:elems][0][:mailbox][:elems] + end + + def contact(resolved) + resolved.response_message[:elems][:resolution_set][:elems].first[:resolution][:elems][1][:contact][:elems] + end + + [:display_name, :given_name, :initials, :office_location, :department, :surname, :job_title].each do |item_sym| + define_method(item_sym) do |resolved| + contact_item(contact(resolved), item_sym) + end + end + + private + + def contact_item(contact, sym) + contact.find{|h| h.first.first == sym}[sym][:text] + end + + def get_resolve_names_parser(resp) + if resp.success? + resp + else + raise EwsError, "ResolveNames produced an error: #{resp.code}: #{resp.message}" + end + end + +end # Viewpoint::EWS::ResolveNamesAccessors \ No newline at end of file diff --git a/lib/ews/soap/builders/ews_builder.rb b/lib/ews/soap/builders/ews_builder.rb index f401c9f9..2500b9af 100644 --- a/lib/ews/soap/builders/ews_builder.rb +++ b/lib/ews/soap/builders/ews_builder.rb @@ -1262,6 +1262,12 @@ def room_lists! @nbuild[NS_EWS_MESSAGES].GetRoomLists end + def resolve_names!(name, full_contact_data) + @nbuild[NS_EWS_MESSAGES].ResolveNames('ReturnFullContactData' => full_contact_data) { + @nbuild[NS_EWS_MESSAGES].UnresolvedEntry(name) + } + end + def accept_item!(opts) @nbuild[NS_EWS_TYPES].AcceptItem { sensitivity!(opts) diff --git a/lib/ews/soap/ews_soap_resolve_names_response.rb b/lib/ews/soap/ews_soap_resolve_names_response.rb new file mode 100644 index 00000000..4227714a --- /dev/null +++ b/lib/ews/soap/ews_soap_resolve_names_response.rb @@ -0,0 +1,38 @@ +=begin + This file is a contribution to Viewpoint; the Ruby library for Microsoft Exchange Web Services. + + Copyright © 2013 Camille Baldock + + Licensed under the Apache License, Version 2.0 (the "License"); + you may not use this file except in compliance with the License. + You may obtain a copy of the License at + + http://www.apache.org/licenses/LICENSE-2.0 + + Unless required by applicable law or agreed to in writing, software + distributed under the License is distributed on an "AS IS" BASIS, + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + See the License for the specific language governing permissions and + limitations under the License. +=end + +module Viewpoint::EWS::SOAP + + # A class for resolve names SOAP returns. + # @attr_reader [String] :message The text from the EWS element + class EwsSoapResolveNamesResponse < EwsSoapResponse + + def response_messages + key = response.keys.first + subresponse = response[key][:elems][0] + response_class = subresponse.keys.first # :response_messages + subresponse[response_class][:elems] + end + + def success? + response_messages.first[:resolve_names_response_message][:attribs][:response_class] == "Success" + end + + end # EwsSoapResolveNamesResponse + +end # Viewpoint::EWS::SOAP diff --git a/lib/ews/soap/exchange_web_service.rb b/lib/ews/soap/exchange_web_service.rb index 02b6be75..de25451d 100644 --- a/lib/ews/soap/exchange_web_service.rb +++ b/lib/ews/soap/exchange_web_service.rb @@ -196,6 +196,18 @@ def get_room_lists do_soap_request(req, response_class: EwsSoapRoomlistResponse) end + # Resolves a contact's name given an email address. + # @see https://docs.microsoft.com/en-us/exchange/client-developer/web-service-reference/resolvenames-operation + def resolve_names(name, full_contact_data) + req = build_soap! do |type, builder| + if(type == :header) + else + builder.resolve_names!(name, full_contact_data) + end + end + do_soap_request(req, response_class: EwsSoapResolveNamesResponse) + end + # Send the SOAP request to the endpoint and parse it. # @param [String] soapmsg an XML formatted string # @todo make this work for Viewpoint (imported from SPWS) diff --git a/lib/viewpoint.rb b/lib/viewpoint.rb index dfce8991..6c099c0d 100644 --- a/lib/viewpoint.rb +++ b/lib/viewpoint.rb @@ -41,6 +41,7 @@ require 'ews/soap/ews_soap_free_busy_response' require 'ews/soap/ews_soap_room_response' require 'ews/soap/ews_soap_roomlist_response' +require 'ews/soap/ews_soap_resolve_names_response' require 'ews/soap/builders/ews_builder' require 'ews/soap/parsers/ews_parser' require 'ews/soap/parsers/ews_sax_document' diff --git a/spec/soap_data/resolve_names_request.xml b/spec/soap_data/resolve_names_request.xml new file mode 100644 index 00000000..e9e291ab --- /dev/null +++ b/spec/soap_data/resolve_names_request.xml @@ -0,0 +1,11 @@ + + + + + + + + TestName@test.microsoft.com + + + diff --git a/spec/unit/ews_resolve_names_operation_spec.rb b/spec/unit/ews_resolve_names_operation_spec.rb new file mode 100644 index 00000000..7ad7b8c1 --- /dev/null +++ b/spec/unit/ews_resolve_names_operation_spec.rb @@ -0,0 +1,21 @@ +require_relative '../spec_helper' + +describe "Resolve Names operation on Exchange Data Services" do + + #let(:ecli) { Viewpoint::EWSClient.new('dontcare', 'dontcare', 'dontcare') } + before do + con = double('Connection') + @ews = Viewpoint::EWS::SOAP::ExchangeWebService.new con, + {:server_version => Viewpoint::EWS::SOAP::VERSION_2010_SP2} + @ews.stub(:do_soap_request) + end + + it "generates ResolveNames XML" do + @ews.should_receive(:do_soap_request) do |request_document| + doc = request_document.to_s.gsub(%r{>\s+}, '>') + doc.should eq load_soap("resolve_names", :request) + end.and_return(double(:resp)) + @ews.resolve_names('TestName@test.microsoft.com', true) + end + +end