viewpoint_reloaded 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (89) hide show
  1. checksums.yaml +7 -0
  2. data/Changelog.txt +1004 -0
  3. data/README.md +201 -0
  4. data/TODO +17 -0
  5. data/lib/ews/calendar_accessors.rb +34 -0
  6. data/lib/ews/connection.rb +130 -0
  7. data/lib/ews/connection_helper.rb +35 -0
  8. data/lib/ews/convert_accessors.rb +56 -0
  9. data/lib/ews/errors.rb +56 -0
  10. data/lib/ews/ews_client.rb +103 -0
  11. data/lib/ews/exceptions/exceptions.rb +61 -0
  12. data/lib/ews/folder_accessors.rb +264 -0
  13. data/lib/ews/impersonation.rb +30 -0
  14. data/lib/ews/item_accessors.rb +231 -0
  15. data/lib/ews/mailbox_accessors.rb +92 -0
  16. data/lib/ews/message_accessors.rb +93 -0
  17. data/lib/ews/push_subscription_accessors.rb +33 -0
  18. data/lib/ews/room_accessors.rb +48 -0
  19. data/lib/ews/roomlist_accessors.rb +47 -0
  20. data/lib/ews/soap.rb +64 -0
  21. data/lib/ews/soap/builders/ews_builder.rb +1266 -0
  22. data/lib/ews/soap/ews_response.rb +84 -0
  23. data/lib/ews/soap/ews_soap_availability_response.rb +58 -0
  24. data/lib/ews/soap/ews_soap_free_busy_response.rb +119 -0
  25. data/lib/ews/soap/ews_soap_response.rb +103 -0
  26. data/lib/ews/soap/ews_soap_room_response.rb +53 -0
  27. data/lib/ews/soap/ews_soap_roomlist_response.rb +54 -0
  28. data/lib/ews/soap/exchange_availability.rb +61 -0
  29. data/lib/ews/soap/exchange_data_services.rb +780 -0
  30. data/lib/ews/soap/exchange_notification.rb +146 -0
  31. data/lib/ews/soap/exchange_synchronization.rb +93 -0
  32. data/lib/ews/soap/exchange_time_zones.rb +56 -0
  33. data/lib/ews/soap/exchange_user_configuration.rb +33 -0
  34. data/lib/ews/soap/exchange_web_service.rb +264 -0
  35. data/lib/ews/soap/parsers/ews_parser.rb +43 -0
  36. data/lib/ews/soap/parsers/ews_sax_document.rb +70 -0
  37. data/lib/ews/soap/response_message.rb +80 -0
  38. data/lib/ews/soap/responses/create_attachment_response_message.rb +47 -0
  39. data/lib/ews/soap/responses/create_item_response_message.rb +25 -0
  40. data/lib/ews/soap/responses/find_item_response_message.rb +80 -0
  41. data/lib/ews/soap/responses/get_events_response_message.rb +53 -0
  42. data/lib/ews/soap/responses/send_notification_response_message.rb +59 -0
  43. data/lib/ews/soap/responses/subscribe_response_message.rb +35 -0
  44. data/lib/ews/soap/responses/sync_folder_hierarchy_response_message.rb +36 -0
  45. data/lib/ews/soap/responses/sync_folder_items_response_message.rb +36 -0
  46. data/lib/ews/templates/calendar_item.rb +79 -0
  47. data/lib/ews/templates/forward_item.rb +24 -0
  48. data/lib/ews/templates/message.rb +73 -0
  49. data/lib/ews/templates/reply_to_item.rb +25 -0
  50. data/lib/ews/templates/task.rb +74 -0
  51. data/lib/ews/types.rb +194 -0
  52. data/lib/ews/types/attachment.rb +77 -0
  53. data/lib/ews/types/attendee.rb +27 -0
  54. data/lib/ews/types/calendar_folder.rb +50 -0
  55. data/lib/ews/types/calendar_item.rb +130 -0
  56. data/lib/ews/types/contact.rb +7 -0
  57. data/lib/ews/types/contacts_folder.rb +8 -0
  58. data/lib/ews/types/copied_event.rb +51 -0
  59. data/lib/ews/types/created_event.rb +24 -0
  60. data/lib/ews/types/deleted_event.rb +24 -0
  61. data/lib/ews/types/distribution_list.rb +7 -0
  62. data/lib/ews/types/event.rb +62 -0
  63. data/lib/ews/types/export_items_response_message.rb +52 -0
  64. data/lib/ews/types/file_attachment.rb +65 -0
  65. data/lib/ews/types/folder.rb +60 -0
  66. data/lib/ews/types/free_busy_changed_event.rb +24 -0
  67. data/lib/ews/types/generic_folder.rb +418 -0
  68. data/lib/ews/types/item.rb +441 -0
  69. data/lib/ews/types/item_attachment.rb +84 -0
  70. data/lib/ews/types/item_field_uri_map.rb +208 -0
  71. data/lib/ews/types/mailbox_user.rb +156 -0
  72. data/lib/ews/types/meeting_cancellation.rb +7 -0
  73. data/lib/ews/types/meeting_message.rb +7 -0
  74. data/lib/ews/types/meeting_request.rb +7 -0
  75. data/lib/ews/types/meeting_response.rb +7 -0
  76. data/lib/ews/types/message.rb +7 -0
  77. data/lib/ews/types/modified_event.rb +48 -0
  78. data/lib/ews/types/moved_event.rb +51 -0
  79. data/lib/ews/types/new_mail_event.rb +24 -0
  80. data/lib/ews/types/out_of_office.rb +147 -0
  81. data/lib/ews/types/search_folder.rb +8 -0
  82. data/lib/ews/types/status_event.rb +39 -0
  83. data/lib/ews/types/task.rb +41 -0
  84. data/lib/ews/types/tasks_folder.rb +27 -0
  85. data/lib/viewpoint.rb +109 -0
  86. data/lib/viewpoint/logging.rb +27 -0
  87. data/lib/viewpoint/logging/config.rb +24 -0
  88. data/lib/viewpoint/string_utils.rb +76 -0
  89. metadata +192 -0
@@ -0,0 +1,201 @@
1
+ [![Build Status](https://travis-ci.org/WinRb/Viewpoint.svg?branch=master)](https://travis-ci.org/WinRb/Viewpoint)
2
+
3
+ # Viewpoint for Exchange Web Services 1.0
4
+ http://github.com/zenchild/Viewpoint/wiki
5
+
6
+ Viewpoint for EWS provides a thin Ruby layer on top of Microsoft Exchange
7
+ Web Services(EWS). It also includes a bunch of model classes that add an
8
+ additional layer of abstraction on top of EWS for use in implementing
9
+ programs with Viewpoint.
10
+
11
+ BLOG: http://distributed-frostbite.blogspot.com/
12
+
13
+ Add me in LinkedIn: http://www.linkedin.com/in/danwanek
14
+
15
+ Find me on irc.freenode.net in #ruby-lang (zenChild)
16
+
17
+ # Features
18
+
19
+ ## New in 1.0
20
+
21
+ * SOAP backend is now only dependant on Nokogiri. Before version 1.0 Viewpoint
22
+ went through a number of iterations in backends including SOAP4r and Handsoap.
23
+ Each of these approaches had major issues so in the end I decided it was
24
+ easiest to just build the SOAP messages with Nokogiri since I was using it as
25
+ the parser for response messages already.
26
+
27
+ * Viewpoint is no longer built on a Singleton pattern. The reason it was
28
+ previously is because of the Handsoap backend. Handsoap itself uses a
29
+ Singleton pattern for connection to the SOAP endpoint so with authentication
30
+ I was forced to implement Viewpoint as a Singleton as well. Now with Handsoap
31
+ out of the picture this is no longer required. Go crazy ;)
32
+
33
+ ## Enhanced in 1.0
34
+
35
+ * *Delegate access is supported*
36
+ One thing that was often asked for, but missing from the previous version was delegate access to mailboxes and calendars. This is now supported via the 'act_as' parameter to the GenericFolder::get_folder method.
37
+
38
+ >> Inbox example:
39
+ ```ofolder = Folder.get_folder(:inbox, opts = {act_as: "user@host.com"})```
40
+
41
+ >> If your user has delegate access to the Inbox for user@host.com this operation will retrieve their inbox and allow you to manipulate it as you would with your own Inbox.
42
+
43
+ >> Calendar example:
44
+ ```oCalendar = cli.get_folder(:calendar, opts = {act_as: "user@host.com"})```
45
+
46
+ >> If your user has delegate access to the Calendar for user@host.com this operation will retrieve their calendar and allow you to manipulate it as you would with your own Calendar, depending on the permissions the other user has granted you.
47
+
48
+
49
+ * There is also some support for manipulation of delegate access itself via
50
+ the methods MailboxUser#add_delegate!, MailboxUser#update_delegate!, and
51
+ MailboxUser#get_delegate_info.
52
+
53
+
54
+ # Using Viewpoint
55
+
56
+ The version 1.0 API is quite a departure from the 0.1.x code base. If you have a lot of legacy code and aren't suffering from performance issues, think twice about upgrading. That said, I hope you'll find the new API much clean and more intuitive than previous versions.
57
+
58
+ I also try and document the code to the base of my ability. Included in that code are links to the official Microsoft EWS documentation that might be helpful when troubleshooting "interesting" issues. You can either generate the documentation yourself with yard or check it out on [rdoc.info](http://rdoc.info/github/zenchild/Viewpoint/frames).
59
+
60
+ Note the `cli` variable in the setup code directly below. I will use that variable throughout without the setup code.
61
+
62
+ ## The Setup
63
+ ```ruby
64
+ require 'viewpoint'
65
+ include Viewpoint::EWS
66
+
67
+ endpoint = 'https://example.com/ews/Exchange.asmx'
68
+ user = 'username'
69
+ pass = 'password'
70
+
71
+ cli = Viewpoint::EWSClient.new endpoint, user, pass
72
+ ```
73
+
74
+ There are also various options you can pass to EWSClient.
75
+
76
+ If you are testing in an environment using a self-signed certificate you can pass a connection parameter to ignore SSL verification by passing `http_opts: {ssl_verify_mode: 0}`.
77
+
78
+ If you want to target a specific version of Exchange you can pass `server_version: SOAP::ExchangeWebService::VERSION_2010_SP1`. You really shouldn't have to use this unless you know why. If you are interacting with servers which you do not know the version(s) of, an incorrect version may manifest as a `SoapResponseError` or a HTTP 400 `ResponseError`. Note that different versions are particular; for example, `VERSION_2007` may not work against a `VERSION_2007_SP1` system.
79
+
80
+ ## Accessors
81
+
82
+ There are some basic accessors available on the Viewpoint::EWSClient instance. In prior versions these were typically class methods off of the models themselves (ex: Folder.get_folder(id)). Now all accessors are available through EWSClient.
83
+
84
+ ### Folder Accessors
85
+
86
+ #### Listing Folders
87
+ ```ruby
88
+ # Find all folders under :msgfolderroot
89
+ folders = cli.folders
90
+
91
+ # Find all folders under Inbox
92
+ inbox_folders = cli.folders root: :inbox
93
+
94
+ # Find all folders under :root and do a Deep search
95
+ all_folders = cli.folders root: :root, traversal: :deep
96
+ ```
97
+
98
+ #### Finding single folders
99
+ ```ruby
100
+ # If you know the EWS id
101
+ cli.get_folder <folder_id>
102
+ # ... or if it's a standard folder pass its symbol
103
+ cli.get_folder :index
104
+ # or by name
105
+ cli.get_folder_by_name 'test'
106
+ # by name as a subfolder under "Inbox"
107
+ cli.get_folder_by_name 'test', parent: :inbox
108
+ ```
109
+
110
+ #### Creating/Deleting a folder
111
+ ```ruby
112
+ # make a folder under :msgfolderroot
113
+ cli.make_folder 'myfolder'
114
+
115
+ # make a folder under Inbox
116
+ my_folder = cli.make_folder 'My Stuff', parent: :inbox
117
+
118
+ # make a new Tasks folder
119
+ tasks = cli.make_folder 'New Tasks', type: :tasks
120
+
121
+ # delete a folder
122
+ my_folder.delete!
123
+ ```
124
+
125
+ ### Item Accessors
126
+
127
+ #### Finding items
128
+ ```ruby
129
+ items = inbox.items
130
+
131
+ # for today
132
+ inbox.todays_items
133
+
134
+ # since a specific date
135
+ sd = Date.iso8601 '2013-01-01'
136
+ inbox.items_since sd
137
+
138
+ # between 2 dates
139
+ sd = Date.iso8601 '2013-01-01'
140
+ ed = Date.iso8601 '2013-02-01'
141
+ inbox.items_between sd, ed
142
+ ```
143
+ ### Free/Busy Calendar Accessors
144
+
145
+ ```ruby
146
+ # Find when a user is busy
147
+ require 'time'
148
+ start_time = DateTime.parse("2013-02-19").iso8601
149
+ end_time = DateTime.parse("2013-02-20").iso8601
150
+ user_free_busy = cli.get_user_availability(['joe.user@exchange.site.com'],
151
+ start_time: start_time,
152
+ end_time: end_time,
153
+ requested_view: :free_busy)
154
+ busy_times = user_free_busy.calendar_event_array
155
+
156
+ # Parse events from the calendar event array for start/end times and type
157
+ busy_times.each { | event |
158
+ cli.event_busy_type(event)
159
+ cli.event_start_time(event)
160
+ cli.event_end_time(event)
161
+ }
162
+
163
+ # Find the user's working hours
164
+ user_free_busy.working_hours
165
+ ```
166
+
167
+
168
+ ### Mailbox Accessors
169
+ ### Message Accessors
170
+ ```ruby
171
+ cli.send_message subject: "Test", body: "Test", to_recipients: ['test@example.com']
172
+
173
+ # or
174
+ cli.send_message do |m|
175
+ m.subject = "Test"
176
+ m.body = "Test"
177
+ m.to_recipients << 'test@example.com'
178
+ m.to_recipients << 'test2@example.com'
179
+ end
180
+
181
+ # set :draft => true or use cli.draft_message to only create a draft and not send.
182
+ ```
183
+
184
+ ## Thanks go out to...
185
+
186
+ * Handl.it for sponsoring a good portion of the development effort.
187
+ ** https://www.handle.com/
188
+ * The National Association of REALTORS® for providing a testing account
189
+ and much appreciated input and support.
190
+ * The Holmes Group Limited for providing Exchange 2013 test accounts.
191
+
192
+ ---
193
+ DISCLAIMER: If you see something that could be done better or would like
194
+ to help out in the development of this code please feel free to clone the
195
+ 'git' repository and send me patches:
196
+ git clone git://github.com/zenchild/Viewpoint.git
197
+ or add an issue on GitHub:
198
+ http://github.com/zenchild/Viewpoint/issues
199
+
200
+ Cheers!
201
+ ---
data/TODO ADDED
@@ -0,0 +1,17 @@
1
+ h2. TODOS
2
+
3
+ * Automate "deepening" of Model objects by over-riding method_missing
4
+
5
+ <pre>
6
+ if object.is_shallow? && all_methods.index(method)
7
+ deepen!
8
+ else
9
+ raise the NoMethod error
10
+ </pre>
11
+
12
+ * Clean-up exceptions. There is exception raising in the Model that will never ocurr because it is already being checked for in the Parser
13
+
14
+ * Refactor #find_folders methods. There is a lot of duplicate code right now and it could be simplified quite a bit.
15
+
16
+ * Test TODO
17
+ ** Test for undefined methods and make sure the method_missing method isn't causing problems
@@ -0,0 +1,34 @@
1
+ =begin
2
+ This file is a cotribution to Viewpoint; the Ruby library for Microsoft Exchange Web Services.
3
+
4
+ Copyright © 2013 Mark McCahill <mark.mccahill@duke.edu>
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ =end
18
+
19
+ module Viewpoint::EWS::CalendarAccessors
20
+ include Viewpoint::EWS
21
+
22
+ def event_busy_type( the_event )
23
+ the_event[:calendar_event][:elems][2][:busy_type][:text]
24
+ end
25
+
26
+ def event_start_time( the_event )
27
+ the_event[:calendar_event][:elems][0][:start_time][:text]
28
+ end
29
+
30
+ def event_end_time( the_event )
31
+ the_event[:calendar_event][:elems][1][:end_time][:text]
32
+ end
33
+
34
+ end # Viewpoint::EWS::CalendarAccessors
@@ -0,0 +1,130 @@
1
+ =begin
2
+ This file is part of Viewpoint; the Ruby library for Microsoft Exchange Web Services.
3
+
4
+ Copyright © 2011 Dan Wanek <dan.wanek@gmail.com>
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ =end
18
+ require 'httpclient'
19
+
20
+ class Viewpoint::EWS::Connection
21
+ include Viewpoint::EWS::ConnectionHelper
22
+ include Viewpoint::EWS
23
+
24
+ attr_reader :endpoint
25
+ # @param [String] endpoint the URL of the web service.
26
+ # @example https://<site>/ews/Exchange.asmx
27
+ # @param [Hash] opts Misc config options (mostly for developement)
28
+ # @option opts [Fixnum] :ssl_verify_mode
29
+ # @option opts [Fixnum] :receive_timeout override the default receive timeout
30
+ # seconds
31
+ # @option opts [Array] :trust_ca an array of hashed dir paths or a file
32
+ def initialize(endpoint, opts = {})
33
+ @log = Logging.logger[self.class.name.to_s.to_sym]
34
+ @httpcli = HTTPClient.new
35
+ if opts[:trust_ca]
36
+ @httpcli.ssl_config.clear_cert_store
37
+ opts[:trust_ca].each do |ca|
38
+ @httpcli.ssl_config.add_trust_ca ca
39
+ end
40
+ end
41
+ @httpcli.ssl_config.verify_mode = opts[:ssl_verify_mode] if opts[:ssl_verify_mode]
42
+ @httpcli.ssl_config.ssl_version = opts[:ssl_version] if opts[:ssl_version]
43
+ # Up the keep-alive so we don't have to do the NTLM dance as often.
44
+ @httpcli.keep_alive_timeout = 60
45
+ @httpcli.receive_timeout = opts[:receive_timeout] if opts[:receive_timeout]
46
+ @endpoint = endpoint
47
+ end
48
+
49
+ def set_auth(user,pass)
50
+ @httpcli.set_auth(@endpoint.to_s, user, pass)
51
+ end
52
+
53
+ # Authenticate to the web service. You don't have to do this because
54
+ # authentication will happen on the first request if you don't do it here.
55
+ # @return [Boolean] true if authentication is successful, false otherwise
56
+ def authenticate
57
+ self.get && true
58
+ end
59
+
60
+ # Every Connection class must have the dispatch method. It is what sends the
61
+ # SOAP request to the server and calls the parser method on the EWS instance.
62
+ #
63
+ # This was originally in the ExchangeWebService class but it was added here
64
+ # to make the processing chain easier to modify. For example, it allows the
65
+ # reactor pattern to handle the request with a callback.
66
+ # @param ews [Viewpoint::EWS::SOAP::ExchangeWebService] used to call
67
+ # #parse_soap_response
68
+ # @param soapmsg [String]
69
+ # @param opts [Hash] misc opts for handling the Response
70
+ def dispatch(ews, soapmsg, opts)
71
+ respmsg = post(soapmsg)
72
+ @log.debug <<-EOF.gsub(/^ {6}/, '')
73
+ Received SOAP Response:
74
+ ----------------
75
+ #{Nokogiri::XML(respmsg).to_xml}
76
+ ----------------
77
+ EOF
78
+ opts[:raw_response] ? respmsg : ews.parse_soap_response(respmsg, opts)
79
+ end
80
+
81
+ # Send a GET to the web service
82
+ # @return [String] If the request is successful (200) it returns the body of
83
+ # the response.
84
+ def get
85
+ check_response( @httpcli.get(@endpoint) )
86
+ end
87
+
88
+ # Send a POST to the web service
89
+ # @return [String] If the request is successful (200) it returns the body of
90
+ # the response.
91
+ def post(xmldoc)
92
+ headers = {'Content-Type' => 'text/xml'}
93
+ check_response( @httpcli.post(@endpoint, xmldoc, headers) )
94
+ end
95
+
96
+
97
+ private
98
+
99
+ def check_response(resp)
100
+ case resp.status
101
+ when 200
102
+ resp.body
103
+ when 302
104
+ # @todo redirect
105
+ raise Errors::UnhandledResponseError.new("Unhandled HTTP Redirect", resp)
106
+ when 401
107
+ raise Errors::UnauthorizedResponseError.new("Unauthorized request", resp)
108
+ when 500
109
+ if resp.headers['Content-Type'] =~ /xml/
110
+ err_string, err_code = parse_soap_error(resp.body)
111
+ raise Errors::SoapResponseError.new("SOAP Error: Message: #{err_string} Code: #{err_code}", resp, err_code, err_string)
112
+ else
113
+ raise Errors::ServerError.new("Internal Server Error. Message: #{resp.body}", resp)
114
+ end
115
+ else
116
+ raise Errors::ResponseError.new("HTTP Error Code: #{resp.status}, Msg: #{resp.body}", resp)
117
+ end
118
+ end
119
+
120
+ # @param [String] xml to parse the errors from.
121
+ def parse_soap_error(xml)
122
+ ndoc = Nokogiri::XML(xml)
123
+ ns = ndoc.collect_namespaces
124
+ err_string = ndoc.xpath("//faultstring",ns).text
125
+ err_code = ndoc.xpath("//faultcode",ns).text
126
+ @log.debug "Internal SOAP error. Message: #{err_string}, Code: #{err_code}"
127
+ [err_string, err_code]
128
+ end
129
+
130
+ end
@@ -0,0 +1,35 @@
1
+ =begin
2
+ This file is part of Viewpoint; the Ruby library for Microsoft Exchange Web Services.
3
+
4
+ Copyright © 2011 Dan Wanek <dan.wanek@gmail.com>
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ =end
18
+
19
+ module Viewpoint::EWS::ConnectionHelper
20
+
21
+ def init_logging!
22
+ @log = Logging.logger[self.class.name.to_s.to_sym]
23
+ end
24
+
25
+ # @param [String] xml to parse the errors from.
26
+ def parse_soap_error(xml)
27
+ ndoc = Nokogiri::XML(xml)
28
+ ns = ndoc.collect_namespaces
29
+ err_string = ndoc.xpath("//faultstring",ns).text
30
+ err_code = ndoc.xpath("//faultcode",ns).text
31
+ @log.debug "Internal SOAP error. Message: #{err_string}, Code: #{err_code}"
32
+ [err_string, err_code]
33
+ end
34
+
35
+ end
@@ -0,0 +1,56 @@
1
+ =begin
2
+ This file is part of Viewpoint; the Ruby library for Microsoft Exchange Web Services.
3
+
4
+ Copyright © 2013 Dan Wanek <dan.wanek@gmail.com>
5
+
6
+ Licensed under the Apache License, Version 2.0 (the "License");
7
+ you may not use this file except in compliance with the License.
8
+ You may obtain a copy of the License at
9
+
10
+ http://www.apache.org/licenses/LICENSE-2.0
11
+
12
+ Unless required by applicable law or agreed to in writing, software
13
+ distributed under the License is distributed on an "AS IS" BASIS,
14
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
+ See the License for the specific language governing permissions and
16
+ limitations under the License.
17
+ =end
18
+ module Viewpoint::EWS::ConvertAccessors
19
+ include Viewpoint::EWS
20
+
21
+ # This is a class method that converts identifiers between formats.
22
+ # @param [String] id The id to be converted
23
+ # @param [Hash] opts Misc options to control request
24
+ # @option opts [Symbol] :format :ews_legacy_id/:ews_id/:entry_id/:hex_entry_id/:store_id/:owa_id
25
+ # @option opts [Symbol] :destination_format :ews_legacy_id/:ews_id/:entry_id/:hex_entry_id/:store_id/:owa_id
26
+ # @option opts [String] :mailbox Mailbox, if required
27
+ # @return [EwsResponse] Returns an EwsResponse containing the convert response message
28
+
29
+ def convert_id(id, opts = {})
30
+ args = convert_id_args(id, opts.clone)
31
+ obj = OpenStruct.new(opts: args)
32
+ yield obj if block_given?
33
+ resp = ews.convert_id(args)
34
+ convert_id_parser(resp)
35
+ end
36
+
37
+ private
38
+
39
+ def convert_id_args(id, opts)
40
+ { id: id }.merge opts
41
+ end
42
+
43
+ def convert_id_parser(resp)
44
+ rm = resp.response_messages[0]
45
+
46
+ if(rm && rm.status == 'Success')
47
+ # @todo create custom response class
48
+ rm
49
+ else
50
+ code = rm.respond_to?(:code) ? rm.code : "Unknown"
51
+ text = rm.respond_to?(:message_text) ? rm.message_text : "Unknown"
52
+ raise EwsError, "Could not convert id. #{rm.code}: #{rm.message_text}"
53
+ end
54
+ end
55
+
56
+ end # Viewpoint::EWS::ItemAccessors