proof-sharepoint-ruby 1.0.0

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.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 02de9841d5e27fa6c194f2c556e2ada51f4fd3488174d81d3dea471f39aab4f5
4
+ data.tar.gz: b3e32950b55aa4408ec9fa2a9bec3d585aaf8f01ea9645f46f7b6085cc973f1a
5
+ SHA512:
6
+ metadata.gz: 142b225cdfbc1f844aff2670eb09f6b5ca740b0625571ca0c2461697d2f4ff86ceb142be4cf0f22ee13e1e089d4107509a7945c4a1c69fc8f4a8baf2d87ee56d
7
+ data.tar.gz: 46a37d9d7aadab3338668659e6f57f7c26655a7a281fab6210fc053d00fb1a4bbac5a5a53c582026fb63e8c53570f238706f330c9a892719884b3d7192761c31
@@ -0,0 +1,7 @@
1
+ ## [1.0.0]
2
+ * Initial Release
3
+
4
+ ### Changes
5
+ - Better error logging.
6
+ - Support for 2013 authentication.
7
+ - Extract make object to facilitate external testing.
data/Gemfile ADDED
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ source 'https://rubygems.org'
4
+
5
+ gemspec
@@ -0,0 +1,19 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ proof-sharepoint-ruby (1.0.0)
5
+ curb (~> 0.8, <= 0.9.10)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ curb (0.9.10)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ proof-sharepoint-ruby!
17
+
18
+ BUNDLED WITH
19
+ 2.1.4
data/LICENSE ADDED
@@ -0,0 +1,27 @@
1
+ Copyright (c) 2014, Plaristote
2
+ All rights reserved.
3
+
4
+ Redistribution and use in source and binary forms, with or without
5
+ modification, are permitted provided that the following conditions are met:
6
+
7
+ * Redistributions of source code must retain the above copyright notice, this
8
+ list of conditions and the following disclaimer.
9
+
10
+ * Redistributions in binary form must reproduce the above copyright notice,
11
+ this list of conditions and the following disclaimer in the documentation
12
+ and/or other materials provided with the distribution.
13
+
14
+ * Neither the name of the {organization} nor the names of its
15
+ contributors may be used to endorse or promote products derived from
16
+ this software without specific prior written permission.
17
+
18
+ THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19
+ AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20
+ IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
21
+ DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
22
+ FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
23
+ DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
24
+ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
25
+ CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
26
+ OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27
+ OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
@@ -0,0 +1,167 @@
1
+ sharepoint-ruby
2
+ ===============
3
+ A ruby gem that maps Sharepoint's REST API in a simple and accessible fashion.
4
+
5
+ How to use
6
+ ===============
7
+ First, you'll have to initialize a sharepoint site and open a session in order to start making requests to the REST API:
8
+
9
+ ```Ruby
10
+ require 'sharepoint-ruby'
11
+
12
+ site = Sharepoint::Site.new 'mysite.sharepoint.com', 'server-relative-site-url'
13
+ site.session.authenticate 'mylogin', 'mypassword'
14
+
15
+ blog = Sharepoint::Site.new 'mytenant.sharepoint.com', 'sites/blog'
16
+ blog.session.authenticate 'user', 'pwd'
17
+ lists = blog.lists
18
+ for l in lists
19
+ puts l.title
20
+ end
21
+ ```
22
+
23
+ Note that site.session.authenticate might throw an exception if the authentication doesn't go well (wrong urls, STS unavailable, or wrong login/password).
24
+ The exceptions might be of type ConnectionToStsFailed, AuthenticationFailed, ConnexionToSharepointFailed, UnknownAuthenticationError.
25
+
26
+ ### Connecting to your own STS
27
+ By default, sharepoint-ruby uses Microsoft's STS (https://login.microsoftonline.com/extSTS.srf), which works for Sharepoint Online. You may use your own STS by using the optional third parameter of Sharepoint::Site.new:
28
+
29
+ ```Ruby
30
+ site = Sharepoint::Site.new 'mysite.sharepoint.com', 'site-name'
31
+ site.session.authenticate 'username', 'password', 'https://sts_url.com/extSTS.srf'
32
+ ```
33
+
34
+ ### Connecting using NTLM
35
+ You may also connect using the NTLM method. For that purpose, you'll have to overwrite the default session handler with `Sharepoint::HttpAuth::Session`.
36
+
37
+ ```Ruby
38
+ require 'sharepoint-http-auth'
39
+
40
+ site = Sharepoint::Site.new 'mysite.sharepoint.com', 'site-name'
41
+ site.session = Sharepoint::HttpAuth::Session.new site
42
+ site.session.authenticate 'login', 'password'
43
+ site.protocole = 'http' # default protocole is https: don't forget to set this if you use http.
44
+ ```
45
+
46
+ ### Connecting using Kerberos
47
+ You may also connect using Kerberos if you're using *MIT Kerberos*.
48
+ For that purpose, you'll have to overwrite the default session handler with `Sharepoint::KerberosAuth::Session`.
49
+
50
+ ```Ruby
51
+ require 'sharepoint-kerberos-auth'
52
+
53
+ site = Sharepoint::Site.new 'mysite.sharepoint.com', 'site-name'
54
+ site.session = Sharepoint::KerberosAuth::Session.new site
55
+ site.session.authenticate 'login', 'password'
56
+ site.protocole = 'http' # default protocole is https: don't forget to set this if you use http.
57
+ ```
58
+
59
+ ### General features
60
+
61
+ Once you're logged in, you may access the site's ressource through the site object:
62
+ ```Ruby
63
+ fields = site.fields # Get all the site's fields
64
+ groups = site.groups # Get all the site's groups
65
+ users = site.users # Get all the site's users
66
+
67
+ lists = site.lists # Get all the site's list
68
+ list = site.list 'Documents' # Get a list by title
69
+ list = site.list '51925dd7-2108-481a-b1ef-4bfa4e69d48b' # Get a list by guid
70
+ views = list.views # Get all the lists views
71
+
72
+ folders = site.folders # Get all the site's folder
73
+ folder = site.folder '/SiteAssets/documents' # Get a folder by server relative path
74
+ files = folder.files # Get all the folder's files
75
+ ```
76
+
77
+ ### OData mapping
78
+ When Sharepoint answers with an OData object, the `site.query` method will automatically map it to the corresponding Sharepoint::Object class.
79
+ For instance, if Sharepoint answered with an OData object of type 'SP.List', `site.query` will return an instance of the Sharepoint::List class. These classes implement a getter and a setter for all the properties declared for the corresponding object in Sharepoint's 2013 Documentation.
80
+
81
+ N.B: Note that the setter only exists if the property is declared as write-accessible in the documentation.
82
+ N.B#2: Note that despite the camel casing used by Sharepoint, the getter and setter are snake cased (i.e: the CustomMasterUrl property becomes accessible through the custom_master_url getter and custom_mater_url= getter).
83
+
84
+ #### Sharepoint::Object specifics
85
+ Sharepoint::Object contains a few methods to help you handle your objects:
86
+
87
+ The `guid` method can be used to retrieve the guid of any object.
88
+
89
+ The `reload` method returns an instance of the same object from the remote sharepoint site. It may be useful if you want to be sure that your object contains the latest changes.
90
+
91
+ The `save` method will automatically compile your changes and perform the MERGE request with the Sharepoint site.
92
+
93
+ The `destroy` method will destroy the remote ressource on the Sharepoint site.
94
+
95
+ The `copy` method can duplicate an existing object. If you send it a Sharepoint::Object as a parameter, it will duplicate into the parameter. If you don't send any parameter, it will create a new object. Note that no changes happen on the sharepoint site until you've called the `save` method on the returned object.
96
+
97
+ ### Deferred objects
98
+ Some of the properties of the OData object are 'deferred', which means that the property only provides a link to a ressource that you would have to get for yourself.
99
+ Not with the sharepoint-ruby gem however: the first time you try to access a deferred property, the object will on it's own go look for the corresponding remote ressource: the result will be stored for later uses, and then be returned to you.
100
+
101
+ ### Modifying Sharepoint's ressources
102
+ The Sharepoint REST API provides us with methods to create, update or delete resources. In the Sharepoint::Object, these behaviours are implemented through the save and delete methods.
103
+
104
+ ##### Updating objects
105
+ This piece of code will change the custom master page used by the Sharepoint site to 'oslo.master':
106
+ ```Ruby
107
+ web = site.context_info # Sharepoint::Site.context_info returns the Web object for the current site (see: http://msdn.microsoft.com/en-us/library/office/dn499819(v=office.15).aspx )
108
+ web.custom_master_url = '/_catalogs/masterpage/oslo.master'
109
+ web.save
110
+ ```
111
+
112
+ ##### Creating objects
113
+ You may also create your own objects. This will be slightly different: we will create our own instance of a Sharepoint::List object.
114
+ Some Sharepoint objects have values that can only be set during their initialization: `sharepoint-ruby` doesn't allow you to set these values through a setter.
115
+ In the case of list, Sharepoint will require you to specify the value for the `BaseTemplate` property. This is how you would specify the default value for an attribute that isn't write-accessible:
116
+ ```Ruby
117
+ list = Sharepoint::List.new site, { 'BaseTemplate' => Sharepoint::LIST_TEMPLATE_TYPE[:GenericList] }
118
+ list.title = 'My new list'
119
+ list.description = 'A list created by sharepoint-ruby'
120
+ list = list.save # At creation, the remote object created will be returned by the save method.
121
+ ```
122
+ Note that the attribute's name in the constructor remains camel cased (`BaseTemplate`), though the getter for this attribute is still snake cased (`base_template`).
123
+
124
+ ##### Destroying objects
125
+ Now, say you want to destroy the list you just created, this will do nicely:
126
+ ```Ruby
127
+ list.destroy
128
+ ```
129
+
130
+ ### Parenting
131
+ In the previous paragraph, we saw how to create a Sharepoint::List object. Sharepoint lists aren't parented to any other objects: Sharepoint views however are parented to a list. If you wanted to create a view for the list we just created, you would have to specify a parent for the view:
132
+
133
+ ```Ruby
134
+ view = Sharepoint::View.new site
135
+ view.title = 'My new view'
136
+ view.personal_view = false
137
+ view.parent = list # Setting the view's parent to the Sharepoint::List
138
+ view.save
139
+ ```
140
+
141
+ ### Collections
142
+ In sharepoint-ruby, collections are merely arrays of Sharepoint::Objects. If you wish to add an object to a colleciton, set the parent to the object providing the collection.
143
+
144
+ ### List Items
145
+ Sharepoint doesn't allow the user to fetch more than 100 items per query. When your list contains more than a 100 items, you may fetch them using the `find_items` method:
146
+
147
+ ```Ruby
148
+ list = site.list 'My List'
149
+ # Use the skip option to paginate
150
+ list.find_items skip: 50 # returns items from 50-150
151
+ # You may use other operators in your query, such as orderby, select, filter and top:
152
+ list.find_items skip: 100, orderby: 'Title asc'
153
+ ```
154
+
155
+ ### Exceptions
156
+ Inevitably, some of your requests will fail. The object sharepoint returns when an error happens is also mapped by sharepoint-ruby in the Sharepoint::SPException class.
157
+ The SPException class contains methods to inspect the query that was made to the server:
158
+
159
+ ```Ruby
160
+ begin
161
+ list = site.list 'title that does not exists'
162
+ rescue Sharepoint::SPException => e
163
+ puts "Sharepoint complained about something: #{e.message}"
164
+ puts "The action that was being executed was: #{e.uri}"
165
+ puts "The request had a body: #{e.request_body}" unless e.request_body.nil?
166
+ end
167
+ ```
@@ -0,0 +1,3 @@
1
+ module Sharepoint
2
+ class SharepointError < StandardError; end
3
+ end
@@ -0,0 +1,126 @@
1
+ module Sharepoint
2
+ class Field < Sharepoint::Object
3
+ include Sharepoint::Type
4
+ sharepoint_resource
5
+ belongs_to :list
6
+
7
+ field 'DefaultValue'
8
+ field 'Description'
9
+ field 'Direction'
10
+ field 'EnforceUniqueValues'
11
+ field 'FieldTypeKind'
12
+ field 'Group'
13
+ field 'Hidden'
14
+ field 'Indexed'
15
+ field 'JSLink'
16
+ field 'ReadOnlyField'
17
+ field 'Required'
18
+ field 'SchemaXml'
19
+ field 'Scope'
20
+ field 'Sealed'
21
+ field 'Sortable'
22
+ field 'StaticName'
23
+ field 'Title'
24
+ field 'TypeAsString'
25
+ #field 'ValidationFormula'
26
+ #field 'ValidationMessage'
27
+ end
28
+
29
+ module Taxonomy
30
+ class TaxonomyField < Field
31
+ end
32
+ end
33
+
34
+ class FieldCalculated < Field
35
+ field 'DateFormat'
36
+ field 'Formula'
37
+ field 'OutputType'
38
+ end
39
+
40
+ class FieldComputed < Field
41
+ field 'EnableLookup'
42
+ end
43
+
44
+ class FieldDateTime < Field
45
+ field 'DateTimeCalendarType'
46
+ field 'DisplayFormat'
47
+ field 'FriendlyDisplayFormat'
48
+ end
49
+
50
+ class FieldGeolocation < Field
51
+ end
52
+
53
+ class FieldGuid < Field
54
+ end
55
+
56
+ class FieldLookup < Field
57
+ field 'AllowMultipleValues'
58
+ field 'IsRelationship'
59
+ field 'LookUpField'
60
+ field 'LookUpList'
61
+ field 'LookUpWebId'
62
+ field 'PrimaryFieldId'
63
+ field 'RelationshipDeleteBehavior'
64
+
65
+ def create_uri
66
+ unless parent.nil?
67
+ parent.__metadata['uri'] + '/fields/addfield'
68
+ else
69
+ 'fields/addfield'
70
+ end
71
+ end
72
+
73
+ def create
74
+ @site.query :post, create_uri, { parameters: @data }.to_json
75
+ end
76
+ end
77
+
78
+ class FieldUser < FieldLookup
79
+ field 'AllowDisplay'
80
+ field 'Presence'
81
+ field 'SelectionGroup'
82
+ field 'SelectionMode'
83
+ end
84
+
85
+ class FieldMultiChoice < Field
86
+ field 'Choices'
87
+ field 'FillInChoice'
88
+ end
89
+
90
+ class FieldChoice < Field
91
+ field 'EditFormat'
92
+ end
93
+
94
+ class FieldRatingScale < Field
95
+ field 'GridEndNumber'
96
+ field 'GridNAOptionText'
97
+ field 'GridStartNumber'
98
+ field 'GridTextRangeAverage'
99
+ field 'GridTextRangeHigh'
100
+ field 'GridTextRangeLow'
101
+ end
102
+
103
+ class FieldMultiLineText < Field
104
+ field 'AllowHyperlink'
105
+ field 'AppendOnly'
106
+ field 'NumberOfLines'
107
+ field 'RestrictedMode'
108
+ field 'RichText'
109
+ end
110
+
111
+ class FieldNumber < Field
112
+ field 'MaximumValue'
113
+ field 'MinimumValue'
114
+ end
115
+
116
+ class FieldCurrency < FieldNumber
117
+ field 'CurrencyLocaleId'
118
+ end
119
+
120
+ class FieldText < Field
121
+ field 'MaxLength'
122
+ end
123
+
124
+ class FieldUrl < Field
125
+ end
126
+ end
@@ -0,0 +1,80 @@
1
+ require 'open-uri'
2
+
3
+ module Sharepoint
4
+ class Folder < Sharepoint::Object
5
+ include Sharepoint::Type
6
+ sharepoint_resource get_from_name: 'GetFolderByServerRelativeUrl'
7
+
8
+ field 'WelcomePage'
9
+ field 'UniqueContentTypeOrder'
10
+
11
+ method :recycle
12
+
13
+ def file_from_name name
14
+ @site.query :get, "#{__metadata['uri']}/files/getbyurl('#{URI::encode(name.to_s)}')"
15
+ end
16
+
17
+ def add_file name, content
18
+ uri = "#{__metadata['uri']}/files/add(overwrite=true,url='#{URI::encode(name.to_s)}')"
19
+ @site.query :post, uri, content
20
+ end
21
+
22
+ def add_folder name
23
+ uri = "#{__metadata['uri']}/folders"
24
+ body = { '__metadata' => { 'type' => 'SP.Folder' }, 'ServerRelativeUrl' => name.to_s }
25
+ @site.query :post, uri, body.to_json
26
+ end
27
+ end
28
+
29
+ class File < Sharepoint::Object
30
+ include Sharepoint::Type
31
+ belongs_to :folder
32
+ sharepoint_resource getter: 'GetFileByServerRelativeUrl', no_root_collection: true
33
+
34
+ method :approve, default_params: ({ comment: '' })
35
+ method :deny, default_params: ({ comment: '' })
36
+ method :checkin, default_params: ({ comment: '', checkintype: 0 })
37
+ method :checkout
38
+ method :undo_checkout
39
+ method :copy_to, default_params: ({ overwrite: true })
40
+ method :move_to, default_params: ({ flags: 9 })
41
+ method :get_limited_webpart_manager, default_params: ({ scope: 0 }), http_method: :get
42
+ method :download, endpoint: '$value', http_method: :get, skip_json: true
43
+ method :upload, endpoint: '$value', http_method: :put
44
+ method :publish, default_params: ({ comment: '' })
45
+ method :unpublish, default_params: ({ comment: '' })
46
+ method :recycle
47
+
48
+ def upload_from_file filename
49
+ content = String.new
50
+ ::File.open filename, 'rb' do |file|
51
+ line = nil
52
+ content += line while line = file.gets
53
+ end
54
+ upload content
55
+ end
56
+
57
+ def download_to_file filename
58
+ content = download
59
+ ::File.open filename, "w:#{content.encoding.name}" do |file|
60
+ file.write content
61
+ end
62
+ end
63
+ end
64
+
65
+ class FileVersion < Sharepoint::Object
66
+ include Sharepoint::Type
67
+ belongs_to :file
68
+
69
+ ##
70
+ # created_by is taken an I can't seem to override it.
71
+ def creator
72
+ _, number, library_path = url.split('/', 3)
73
+ server_path = URI::encode("/#{site.name}/#{library_path}").gsub(/'/, '%27%27')
74
+ @site.query(
75
+ :get,
76
+ "GetFileByServerRelativeUrl('#{server_path}')/Versions(#{number})/CreatedBy"
77
+ )
78
+ end
79
+ end
80
+ end