stxt-viewpoint 1.0.0.beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
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 +46 -0
  6. data/lib/ews/connection.rb +120 -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 +101 -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/builders/ews_builder.rb +1257 -0
  21. data/lib/ews/soap/ews_response.rb +84 -0
  22. data/lib/ews/soap/ews_soap_availability_response.rb +58 -0
  23. data/lib/ews/soap/ews_soap_free_busy_response.rb +119 -0
  24. data/lib/ews/soap/ews_soap_response.rb +103 -0
  25. data/lib/ews/soap/ews_soap_room_response.rb +53 -0
  26. data/lib/ews/soap/ews_soap_roomlist_response.rb +54 -0
  27. data/lib/ews/soap/exchange_availability.rb +61 -0
  28. data/lib/ews/soap/exchange_data_services.rb +780 -0
  29. data/lib/ews/soap/exchange_notification.rb +146 -0
  30. data/lib/ews/soap/exchange_synchronization.rb +93 -0
  31. data/lib/ews/soap/exchange_time_zones.rb +56 -0
  32. data/lib/ews/soap/exchange_user_configuration.rb +33 -0
  33. data/lib/ews/soap/exchange_web_service.rb +264 -0
  34. data/lib/ews/soap/parsers/ews_parser.rb +43 -0
  35. data/lib/ews/soap/parsers/ews_sax_document.rb +70 -0
  36. data/lib/ews/soap/response_message.rb +80 -0
  37. data/lib/ews/soap/responses/create_attachment_response_message.rb +47 -0
  38. data/lib/ews/soap/responses/create_item_response_message.rb +25 -0
  39. data/lib/ews/soap/responses/find_item_response_message.rb +80 -0
  40. data/lib/ews/soap/responses/get_events_response_message.rb +53 -0
  41. data/lib/ews/soap/responses/send_notification_response_message.rb +59 -0
  42. data/lib/ews/soap/responses/subscribe_response_message.rb +35 -0
  43. data/lib/ews/soap/responses/sync_folder_hierarchy_response_message.rb +36 -0
  44. data/lib/ews/soap/responses/sync_folder_items_response_message.rb +36 -0
  45. data/lib/ews/soap.rb +63 -0
  46. data/lib/ews/templates/calendar_item.rb +78 -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/attachment.rb +77 -0
  52. data/lib/ews/types/attendee.rb +27 -0
  53. data/lib/ews/types/calendar_folder.rb +50 -0
  54. data/lib/ews/types/calendar_item.rb +122 -0
  55. data/lib/ews/types/contact.rb +7 -0
  56. data/lib/ews/types/contacts_folder.rb +8 -0
  57. data/lib/ews/types/copied_event.rb +51 -0
  58. data/lib/ews/types/created_event.rb +24 -0
  59. data/lib/ews/types/deleted_event.rb +24 -0
  60. data/lib/ews/types/distribution_list.rb +7 -0
  61. data/lib/ews/types/event.rb +62 -0
  62. data/lib/ews/types/export_items_response_message.rb +52 -0
  63. data/lib/ews/types/file_attachment.rb +65 -0
  64. data/lib/ews/types/folder.rb +60 -0
  65. data/lib/ews/types/free_busy_changed_event.rb +24 -0
  66. data/lib/ews/types/generic_folder.rb +420 -0
  67. data/lib/ews/types/item.rb +438 -0
  68. data/lib/ews/types/item_attachment.rb +84 -0
  69. data/lib/ews/types/item_field_uri_map.rb +224 -0
  70. data/lib/ews/types/mailbox_user.rb +156 -0
  71. data/lib/ews/types/meeting_cancellation.rb +7 -0
  72. data/lib/ews/types/meeting_message.rb +7 -0
  73. data/lib/ews/types/meeting_request.rb +7 -0
  74. data/lib/ews/types/meeting_response.rb +7 -0
  75. data/lib/ews/types/message.rb +7 -0
  76. data/lib/ews/types/modified_event.rb +48 -0
  77. data/lib/ews/types/moved_event.rb +51 -0
  78. data/lib/ews/types/new_mail_event.rb +24 -0
  79. data/lib/ews/types/out_of_office.rb +147 -0
  80. data/lib/ews/types/search_folder.rb +8 -0
  81. data/lib/ews/types/status_event.rb +39 -0
  82. data/lib/ews/types/task.rb +37 -0
  83. data/lib/ews/types/tasks_folder.rb +27 -0
  84. data/lib/ews/types.rb +191 -0
  85. data/lib/viewpoint/logging/config.rb +24 -0
  86. data/lib/viewpoint/logging.rb +27 -0
  87. data/lib/viewpoint/string_utils.rb +44 -0
  88. data/lib/viewpoint.rb +109 -0
  89. metadata +191 -0
data/README.md ADDED
@@ -0,0 +1,201 @@
1
+ **ATTENTION:** If you want to fix a bug in the currently released gem please use the 0.1.x-support branch. The master is the current development branch and is not ready for prime-time just yet.
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.
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,46 @@
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
+ def event_name( the_event )
35
+ the_event[:calendar_event][:elems][3][:calendar_event_details][:elems][1][:subject][:text]
36
+ end
37
+
38
+ def event_location( the_event )
39
+ the_event[:calendar_event][:elems][3][:calendar_event_details][:elems][2][:location][:text]
40
+ end
41
+
42
+ def event_is_recurring( the_event )
43
+ the_event[:calendar_event][:elems][3][:calendar_event_details][:elems][4][:is_recurring][:text]
44
+ end
45
+
46
+ end # Viewpoint::EWS::CalendarAccessors
@@ -0,0 +1,120 @@
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
+ def initialize(endpoint, opts = {})
30
+ @log = Logging.logger[self.class.name.to_s.to_sym]
31
+ @httpcli = HTTPClient.new
32
+ @httpcli.ssl_config.verify_mode = opts[:ssl_verify_mode] if opts[:ssl_verify_mode]
33
+ @httpcli.ssl_config.ssl_version = opts[:ssl_version] if opts[:ssl_version]
34
+ # Up the keep-alive so we don't have to do the NTLM dance as often.
35
+ @httpcli.keep_alive_timeout = 60
36
+ @endpoint = endpoint
37
+ end
38
+
39
+ def set_auth(user,pass)
40
+ @httpcli.set_auth(@endpoint.to_s, user, pass)
41
+ end
42
+
43
+ # Authenticate to the web service. You don't have to do this because
44
+ # authentication will happen on the first request if you don't do it here.
45
+ # @return [Boolean] true if authentication is successful, false otherwise
46
+ def authenticate
47
+ self.get && true
48
+ end
49
+
50
+ # Every Connection class must have the dispatch method. It is what sends the
51
+ # SOAP request to the server and calls the parser method on the EWS instance.
52
+ #
53
+ # This was originally in the ExchangeWebService class but it was added here
54
+ # to make the processing chain easier to modify. For example, it allows the
55
+ # reactor pattern to handle the request with a callback.
56
+ # @param ews [Viewpoint::EWS::SOAP::ExchangeWebService] used to call
57
+ # #parse_soap_response
58
+ # @param soapmsg [String]
59
+ # @param opts [Hash] misc opts for handling the Response
60
+ def dispatch(ews, soapmsg, opts)
61
+ respmsg = post(soapmsg)
62
+ @log.debug <<-EOF.gsub(/^ {6}/, '')
63
+ Received SOAP Response:
64
+ ----------------
65
+ #{Nokogiri::XML(respmsg).to_xml}
66
+ ----------------
67
+ EOF
68
+ opts[:raw_response] ? respmsg : ews.parse_soap_response(respmsg, opts)
69
+ end
70
+
71
+ # Send a GET to the web service
72
+ # @return [String] If the request is successful (200) it returns the body of
73
+ # the response.
74
+ def get
75
+ check_response( @httpcli.get(@endpoint) )
76
+ end
77
+
78
+ # Send a POST to the web service
79
+ # @return [String] If the request is successful (200) it returns the body of
80
+ # the response.
81
+ def post(xmldoc)
82
+ headers = {'Content-Type' => 'text/xml'}
83
+ check_response( @httpcli.post(@endpoint, xmldoc, headers) )
84
+ end
85
+
86
+
87
+ private
88
+
89
+ def check_response(resp)
90
+ case resp.status
91
+ when 200
92
+ resp.body
93
+ when 302
94
+ # @todo redirect
95
+ raise Errors::UnhandledResponseError.new("Unhandled HTTP Redirect", resp)
96
+ when 401
97
+ raise Errors::UnauthorizedResponseError.new("Unauthorized request", resp)
98
+ when 500
99
+ if resp.headers['Content-Type'].include?('xml')
100
+ err_string, err_code = parse_soap_error(resp.body)
101
+ raise Errors::SoapResponseError.new("SOAP Error: Message: #{err_string} Code: #{err_code}", resp, err_code, err_string)
102
+ else
103
+ raise Errors::ServerError.new("Internal Server Error. Message: #{resp.body}", resp)
104
+ end
105
+ else
106
+ raise Errors::ResponseError.new("HTTP Error Code: #{resp.status}, Msg: #{resp.body}", resp)
107
+ end
108
+ end
109
+
110
+ # @param [String] xml to parse the errors from.
111
+ def parse_soap_error(xml)
112
+ ndoc = Nokogiri::XML(xml)
113
+ ns = ndoc.collect_namespaces
114
+ err_string = ndoc.xpath("//faultstring",ns).text
115
+ err_code = ndoc.xpath("//faultcode",ns).text
116
+ @log.debug "Internal SOAP error. Message: #{err_string}, Code: #{err_code}"
117
+ [err_string, err_code]
118
+ end
119
+
120
+ 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
data/lib/ews/errors.rb ADDED
@@ -0,0 +1,56 @@
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::Errors
20
+ class ResponseError < RuntimeError
21
+ attr_reader :response
22
+
23
+ def initialize(message, response)
24
+ super(message)
25
+ @response = response
26
+ end
27
+
28
+ def status
29
+ response.status
30
+ end
31
+
32
+ def body
33
+ response.body
34
+ end
35
+ end
36
+
37
+ class UnhandledResponseError < ResponseError
38
+ end
39
+
40
+ class ServerError < ResponseError
41
+ end
42
+
43
+ class UnauthorizedResponseError < ResponseError
44
+ end
45
+
46
+ class SoapResponseError < ResponseError
47
+ attr_reader :faultcode,
48
+ :faultstring
49
+
50
+ def initialize(message, response, faultcode, faultstring)
51
+ super(message, response)
52
+ @faultcode = faultcode
53
+ @faultstring = faultstring
54
+ end
55
+ end
56
+ end
@@ -0,0 +1,101 @@
1
+ require 'ews/folder_accessors'
2
+ require 'ews/item_accessors'
3
+ require 'ews/message_accessors'
4
+ require 'ews/mailbox_accessors'
5
+ require 'ews/push_subscription_accessors'
6
+ require 'ews/calendar_accessors'
7
+ require 'ews/room_accessors'
8
+ require 'ews/roomlist_accessors'
9
+ require 'ews/convert_accessors'
10
+
11
+ # This class is the glue between the Models and the Web Service.
12
+ class Viewpoint::EWSClient
13
+ include Viewpoint::EWS
14
+ include Viewpoint::EWS::FolderAccessors
15
+ include Viewpoint::EWS::ItemAccessors
16
+ include Viewpoint::EWS::MessageAccessors
17
+ include Viewpoint::EWS::MailboxAccessors
18
+ include Viewpoint::EWS::PushSubscriptionAccessors
19
+ include Viewpoint::EWS::CalendarAccessors
20
+ include Viewpoint::EWS::RoomAccessors
21
+ include Viewpoint::EWS::RoomlistAccessors
22
+ include Viewpoint::EWS::ConvertAccessors
23
+ include Viewpoint::StringUtils
24
+
25
+ # The instance of Viewpoint::EWS::SOAP::ExchangeWebService
26
+ attr_reader :ews
27
+
28
+ # Initialize the EWSClient instance.
29
+ # @param [String] endpoint The EWS endpoint we will be connecting to
30
+ # @param [String] user The user to authenticate as. If you are using
31
+ # NTLM or Negotiate authentication you do not need to pass this parameter.
32
+ # @param [String] pass The user password. If you are using NTLM or
33
+ # Negotiate authentication you do not need to pass this parameter.
34
+ # @param [Hash] opts Various options to pass to the backends
35
+ # @option opts [String] :server_version The Exchange server version to
36
+ # target. See the VERSION_* constants in
37
+ # Viewpoint::EWS::SOAP::ExchangeWebService.
38
+ # @option opts [Object] :http_class specify an alternate HTTP connection class.
39
+ # @option opts [Hash] :http_opts options to pass to the connection
40
+ def initialize(endpoint, user = nil, pass = nil, opts = {})
41
+ # dup all. @see ticket https://github.com/zenchild/Viewpoint/issues/68
42
+ endpoint, user, pass = endpoint.dup, user.dup, pass.dup
43
+ opts = opts.dup
44
+ http_klass = opts[:http_class] || Viewpoint::EWS::Connection
45
+ con = http_klass.new(endpoint, opts[:http_opts] || {})
46
+ con.set_auth(user,pass) if(user && pass)
47
+ @ews = SOAP::ExchangeWebService.new(con, opts)
48
+ end
49
+
50
+ # @param deepen [Boolean] true to autodeepen, false otherwise
51
+ # @param behavior [Symbol] :raise, :nil When setting autodeepen to false you
52
+ # can choose what the behavior is when an attribute does not exist. The
53
+ # default is to raise a EwsMinimalObjectError.
54
+ def set_auto_deepen(deepen, behavior = :raise)
55
+ if deepen
56
+ @ews.auto_deepen = true
57
+ else
58
+ behavior = [:raise, :nil].include?(behavior) ? behavior : :raise
59
+ @ews.no_auto_deepen_behavior = behavior
60
+ @ews.auto_deepen = false
61
+ end
62
+ end
63
+
64
+ def auto_deepen=(deepen)
65
+ set_auto_deepen deepen
66
+ end
67
+
68
+ # Specify a default time zone context for all time attributes
69
+ # @param id [String] Identifier of a Microsoft well known time zone (e.g: 'UTC', 'W. Europe Standard Time')
70
+ # @note A list of time zones known by the server can be requested via {EWS::SOAP::ExchangeTimeZones#get_time_zones}
71
+ def set_time_zone(microsoft_time_zone_id)
72
+ @ews.set_time_zone_context microsoft_time_zone_id
73
+ end
74
+
75
+ private
76
+
77
+
78
+ # This method also exists in EWS::Types, but there is a lot of other stuff
79
+ # in there that I didn't want to include directly in this class.
80
+ def class_by_name(cname)
81
+ if(cname.instance_of? Symbol)
82
+ cname = camel_case(cname)
83
+ end
84
+ Viewpoint::EWS::Types.const_get(cname)
85
+ end
86
+
87
+ # Used for multiple accessors
88
+ def merge_restrictions!(obj, merge_type = :and)
89
+ if obj.opts[:restriction] && !obj.opts[:restriction].empty? && !obj.restriction.empty?
90
+ obj.opts[:restriction] = {
91
+ merge_type => [
92
+ obj.opts.delete(:restriction),
93
+ obj.restriction
94
+ ]
95
+ }
96
+ elsif !obj.restriction.empty?
97
+ obj.opts[:restriction] = obj.restriction
98
+ end
99
+ end
100
+
101
+ end