restful-sharepoint 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 1324f5b122d4dd534dcf277599dd89358cbe7bc8
4
+ data.tar.gz: 2cf9047b24b6a1e6f0e133b71f4e6d50902c29f7
5
+ SHA512:
6
+ metadata.gz: d7773e2dd73c2ab3fd23d31e13949959c6b24521b44492a9f2350ada71a9181087fc37cb3ffd67f624970a1981be0ca2acd028aab02494bb55b8b6f55f92b66b
7
+ data.tar.gz: 2e222664a50607fe17d2e7cb0add5ff1bf2ec4f05bc4bc60a008c1ffff5090792c974b367b974863c33b990922b1f5fdefc369cdb684950bcca0f26f7fe52baf
data/Gemfile ADDED
@@ -0,0 +1 @@
1
+ gemspec
data/LICENSE ADDED
@@ -0,0 +1,7 @@
1
+ Copyright (c) 2017 Mareeba Shire Council
2
+
3
+ Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
4
+
5
+ The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
6
+
7
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,15 @@
1
+ Provides a convenient object model to the OData REST API of SharePoint 2013 and newer.
2
+
3
+ No unit tests as of yet.
4
+
5
+ Examples
6
+ --------
7
+ ``` ruby
8
+ require 'restful-sharepoint'
9
+ connection = RestfulSharePoint::Connection.new('http://sharepoint/mysite/', 'username', 'password')
10
+ list = RestfulSharePoint::List.from_title('My List', connection)
11
+ list_item = list.Items[0] # Dynamically invoke the deferred "Items" element
12
+ first_attachment = list_item.AttachmentFiles[0].content
13
+
14
+ list_item.values # Return the raw tree structure
15
+ ```
@@ -0,0 +1,32 @@
1
+ require 'json'
2
+ require 'httpi'
3
+ require 'curb'
4
+ require 'cgi'
5
+ require 'require_pattern'
6
+
7
+ HTTPI.adapter = :curb
8
+
9
+ module RestfulSharePoint
10
+ OBJECT_MAP = {
11
+ "SP.Web" => :Web,
12
+ "SP.List" => :List,
13
+ /SP\.Data\..*Item/ => :ListItem,
14
+ "SP.File" => :File,
15
+ "SP.Attachment" => :Attachment
16
+ }
17
+
18
+ COLLECTION_MAP = {
19
+ "SP.Web" => :Webs,
20
+ "SP.List" => :Lists,
21
+ /SP\.Data\..*Item/ => :ListItems,
22
+ "SP.Attachment" => :Attachments
23
+ }
24
+ end
25
+
26
+ require_relative './restful-sharepoint/version.rb'
27
+ require_relative './restful-sharepoint/error.rb'
28
+ require_relative './restful-sharepoint/connection.rb'
29
+ require_relative './restful-sharepoint/common_base.rb'
30
+ require_relative './restful-sharepoint/object.rb'
31
+ require_relative './restful-sharepoint/collection.rb'
32
+ require_relative_pattern './restful-sharepoint/*/*.rb'
@@ -0,0 +1,60 @@
1
+ module RestfulSharePoint
2
+ class Collection < CommonBase
3
+ DEFAULT_OPTIONS = {}
4
+
5
+ def self.object_class
6
+ Object
7
+ end
8
+
9
+ def initialize(parent: nil, connection: nil, collection: nil, options: {})
10
+ @parent = parent
11
+ @connection = connection || @parent.connection # Iterate collection and coerce each into into a ListItem
12
+ self.collection = collection
13
+ self.options = options
14
+ end
15
+
16
+ attr_accessor :connection
17
+
18
+ attr_reader :options
19
+ def options=(options)
20
+ @options = self.class::DEFAULT_OPTIONS.merge(options)
21
+ end
22
+
23
+ attr_writer :endpoint
24
+ def endpoint
25
+ @endpoint || (raise NotImplementedError, "Endpoint needs to be set")
26
+ end
27
+
28
+ def collection=(collection)
29
+ @collection = collection
30
+ @collection&.each_with_index do |v,i|
31
+ @collection[i] = objectify(v)
32
+ end
33
+ @properties
34
+ end
35
+
36
+ def collection
37
+ @collection || self.collection = connection.get(endpoint, options: @options)
38
+ end
39
+
40
+ def values
41
+ collection.dup.each { |k,v| properties[k] = v.values if v.is_a?(Object) || v.is_a?(Collection) }
42
+ end
43
+
44
+ def next
45
+ self.new(@connection, @connection.get(collection['__next']))
46
+ end
47
+
48
+ def to_json(*args, &block)
49
+ collection.to_json(*args, &block)
50
+ end
51
+
52
+ def method_missing(method, *args, &block)
53
+ collection.respond_to?(method) ? collection.send(method, *args, &block) : super
54
+ end
55
+
56
+ def respond_to_missing?(method, include_all = false)
57
+ collection.respond_to?(method, include_all)
58
+ end
59
+ end
60
+ end
@@ -0,0 +1,13 @@
1
+ module RestfulSharePoint
2
+ class Attachments < Collection
3
+
4
+ def self.object_class
5
+ Attachment
6
+ end
7
+
8
+ def endpoint
9
+ "#{@parent.endpoint}/AttachmentFiles"
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module RestfulSharePoint
2
+ class ListItems < Collection
3
+
4
+ def self.object_class
5
+ ListItem
6
+ end
7
+
8
+ def endpoint
9
+ "#{@parent.endpoint}/Items"
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module RestfulSharePoint
2
+ class Lists < Collection
3
+
4
+ def self.object_class
5
+ List
6
+ end
7
+
8
+ def endpoint
9
+ "#{@parent.endpoint}/Lists"
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,13 @@
1
+ module RestfulSharePoint
2
+ class Webs < Collection
3
+
4
+ def self.object_class
5
+ Web
6
+ end
7
+
8
+ def endpoint
9
+ "#{@parent}/Webs"
10
+ end
11
+
12
+ end
13
+ end
@@ -0,0 +1,16 @@
1
+ module RestfulSharePoint
2
+ class CommonBase
3
+ # Converts the given enumerable tree to a collection or object.
4
+ def objectify(tree)
5
+ if tree['results'] && !tree['results'].empty?
6
+ type = tree.dig('__metadata', 'type') || tree.dig('results', 0, '__metadata', 'type')
7
+ pattern, klass = COLLECTION_MAP.find { |pattern,| pattern.match(type) }
8
+ klass ? RestfulSharePoint.const_get(klass).new(parent: self, collection: tree['results']) : tree['results']
9
+ elsif tree['__metadata']
10
+ type = tree['__metadata']['type']
11
+ pattern, klass = OBJECT_MAP.find { |pattern,| pattern.match(type) }
12
+ klass ? RestfulSharePoint.const_get(klass).new(parent: self, properties: tree) : tree
13
+ end
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,72 @@
1
+ require 'date'
2
+ require 'uri'
3
+
4
+ module RestfulSharePoint
5
+ class Connection
6
+ def initialize(site_url, username = nil, password = nil)
7
+ @site_url = site_url
8
+ @username = username
9
+ @password = password
10
+ end
11
+
12
+ attr_reader :site_url
13
+
14
+ def get(path, options: {})
15
+ request path, :get, options: options
16
+ end
17
+
18
+ # Path can be either relative to the site URL, or a complete URL itself.
19
+ # Takes an optional `options` hash which are any number of valid OData query options (dollar sign prefix is added automatically)
20
+ # Also takes an optional block that is provided the HTTPI::Request instance, allowing customisation of the request.
21
+ def request(path, method, options: {}, body: nil)
22
+ url = URI.parse(path).is_a?(URI::HTTP) ? path : "#{@site_url}#{path}"
23
+ options_str = options.map { |k,v| "$#{k}=#{CGI.escape v.to_s}" }.join('&')
24
+ url += "?#{options_str}"
25
+ req = HTTPI::Request.new(url: url, headers: {'accept' => 'application/json; odata=verbose'})
26
+ req.auth.ntlm(@username, @password) if @username
27
+ if body
28
+ req.body = body.to_json.gsub('/', '\\/') # SharePoint requires forward slashes be escaped in JSON (WTF!!!)
29
+ req.headers['Content-Type'] = 'application/json'
30
+ req.headers['X-HTTP-Method'] = 'MERGE' # TODO: Extend logic to support all operations
31
+ req.headers['If-Match'] = '*'
32
+ end
33
+ yield(request) if block_given?
34
+ response = HTTPI.request(method, req)
35
+ if response.body.empty?
36
+ if response.code >= 300
37
+ raise RestError, "Server returned HTTP status #{response.code} with no message body."
38
+ end
39
+ else
40
+ if response.headers['Content-Type'].start_with? "application/json"
41
+ data_tree = parse(response.body)
42
+ else
43
+ response.body
44
+ end
45
+ end
46
+ end
47
+
48
+ protected
49
+
50
+ def parse(str)
51
+ data = JSON.parse(str)
52
+ raise RestError, "(#{data['error']['code']}): #{data['error']['message']['value']}" if data['error']
53
+ parse_tree(data['d'])
54
+ end
55
+
56
+ def parse_tree(tree)
57
+ indices = tree.respond_to?(:keys) ? tree.keys : 0...tree.length
58
+ indices.each do |i|
59
+ if tree[i].respond_to?(:=~) && tree[i] =~ /^[0-9]{4}-[0-9]{2}-[0-9]{2}T[0-9]{2}:[0-9]{2}:[0-9]{2}Z$/
60
+ tree[i] = DateTime.parse(tree[i]).new_offset(DateTime.now.offset)
61
+ elsif tree[i].respond_to?(:gsub!)
62
+ # Convert relative paths to absolute URL's.
63
+ tree[i].gsub!(/((?<=href=)|(?<=src=))['"](\/.+?)['"]/, %Q("#{@site_url}\\2\"))
64
+ elsif tree[i].is_a? Enumerable
65
+ parse_tree(tree[i])
66
+ end
67
+ end
68
+ tree
69
+ end
70
+
71
+ end
72
+ end
@@ -0,0 +1,5 @@
1
+ module RestfulSharePoint
2
+ class Error < RuntimeError; end
3
+ class RestError < Error; end
4
+ class FileNotFound < Error; end
5
+ end
@@ -0,0 +1,76 @@
1
+ module RestfulSharePoint
2
+ class Object < CommonBase
3
+ DEFAULT_OPTIONS = {}
4
+
5
+ def initialize(parent: nil, connection: nil, properties: nil, id: nil, options: {})
6
+ raise Error, "Either a parent or connection must be provided." unless parent || connection
7
+ @parent = parent
8
+ @connection = connection || @parent.connection
9
+ self.properties = properties
10
+ @id = id
11
+ self.options = options
12
+ end
13
+
14
+ attr_accessor :connection
15
+ attr_reader :options
16
+ def options=(options)
17
+ @options = self.class::DEFAULT_OPTIONS.merge(options)
18
+ end
19
+
20
+ attr_writer :endpoint
21
+ def endpoint
22
+
23
+ end
24
+
25
+ def properties=(properties)
26
+ @properties = properties
27
+ @properties&.each do |k,v|
28
+ if v.respond_to?(:keys) && v['__deferred']
29
+ define_singleton_method(k) do |options = {}|
30
+ if Hash === properties[k] && properties[k]['__deferred']
31
+ fetch_deferred(k, options)
32
+ else
33
+ warn("`options` have been ignored as `#{k}` has already been loaded") unless options.empty?
34
+ properties[k]
35
+ end
36
+ end
37
+ elsif v.respond_to?(:keys) && (v['__metadata'] || v['results'])
38
+ @properties[k] = objectify(v)
39
+ end
40
+ end
41
+ @properties
42
+ end
43
+
44
+ def properties
45
+ @properties || self.properties = connection.get(endpoint, options: @options)
46
+ end
47
+
48
+ def values
49
+ properties.dup.each { |k,v| properties[k] = v.values if v.is_a?(Object) || v.is_a?(Collection) }
50
+ end
51
+
52
+ def fetch_deferred(property, options = {})
53
+ data = connection.get(@properties[property]['__deferred']['uri'], options: options)
54
+ @properties[property] = objectify(data)
55
+ end
56
+
57
+ def to_json(*args, &block)
58
+ properties.to_json(*args, &block)
59
+ end
60
+
61
+ def method_missing(method, *args, &block)
62
+ if properties.respond_to?(method)
63
+ properties.send(method, *args, &block)
64
+ elsif self.methods(false).include?(method) # Works around lazily loaded `properties`
65
+ self.send(method, *args, &block)
66
+ else
67
+ super
68
+ end
69
+ end
70
+
71
+ def respond_to_missing?(method, include_all = false)
72
+ properties.respond_to?(method, include_all)
73
+ end
74
+
75
+ end
76
+ end
@@ -0,0 +1,10 @@
1
+ module RestfulSharePoint
2
+ class Attachment < File
3
+
4
+ def endpoint
5
+ url = URI.parse(connection.site_url)
6
+ url.path = URI.encode(@properties['ServerRelativeUrl'])
7
+ url.to_s
8
+ end
9
+ end
10
+ end
@@ -0,0 +1,35 @@
1
+ module RestfulSharePoint
2
+ class File < Object
3
+
4
+ def endpoint
5
+ "#{@parent.endpoint}/File"
6
+ end
7
+
8
+ def content
9
+ @content ||= connection.get(url)
10
+ end
11
+
12
+ def url
13
+ url = URI.parse(connection.site_url)
14
+ url.path = URI.encode(self['ServerRelativeUrl'])
15
+ url.to_s
16
+ end
17
+
18
+ # In bytes
19
+ def size
20
+ self['Length'] || content.length
21
+ end
22
+
23
+ def name
24
+ filename.rpartition('.').first
25
+ end
26
+
27
+ def filename
28
+ self['ServerRelativeUrl'].rpartition('/').last
29
+ end
30
+
31
+ def extension
32
+ self['ServerRelativeUrl'].rpartition('.').last
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,19 @@
1
+ module RestfulSharePoint
2
+ class File < Object
3
+ # Overwrite can be true, false, or a date.
4
+ def save_thumbnail(dest, size: '128x128', format: 'jpg', overwrite: true)
5
+ if !::File.exist?(dest) || overwrite == true || (Time === overwrite && ::File.mtime(dest) < overwrite)
6
+ image = MiniMagick::Image.read(content).collapse!
7
+ image.combine_options do |img|
8
+ img.thumbnail "#{size}^"
9
+ img.gravity "center"
10
+ img.colorspace "sRGB"
11
+ img.background "white"
12
+ img.flatten # Merges images with white background
13
+ end
14
+ image.format format
15
+ image.write dest
16
+ end
17
+ end
18
+ end
19
+ end
@@ -0,0 +1,9 @@
1
+ module RestfulSharePoint
2
+ class ListItem < Object
3
+
4
+ def endpoint
5
+ "#{@parent.endpoint}/Items(#{@id})"
6
+ end
7
+
8
+ end
9
+ end
@@ -0,0 +1,14 @@
1
+ module RestfulSharePoint
2
+ class List < Object
3
+
4
+ def self.from_title(title, connection)
5
+ new(connection: connection).tap do |list|
6
+ list.define_singleton_method(:endpoint) { "/_api/web/lists/getbytitle('#{URI.encode title}')" }
7
+ end
8
+ end
9
+
10
+ def endpoint
11
+ "/_api/web/lists(guid'#{@id}')"
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,8 @@
1
+ module RestfulSharePoint
2
+ class Web < Object
3
+
4
+ def endpoint
5
+ "/_api/web/"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,3 @@
1
+ module RestfulSharePoint
2
+ VERSION = '0.1.1'
3
+ end
@@ -0,0 +1,19 @@
1
+ $LOAD_PATH.unshift File.expand_path('../lib', __FILE__)
2
+ require 'restful-sharepoint/version'
3
+
4
+ Gem::Specification.new 'restful-sharepoint', RestfulSharePoint::VERSION do |s|
5
+ s.summary = 'Provides a convenient object model to the OData REST API of SharePoint 2013 and newer.'
6
+ s.description = 'Provides a convenient object model to the OData REST API of SharePoint 2013 and newer.'
7
+ s.authors = ['Tom Wardrop']
8
+ s.email = 'tomw@msc.qld.gov.au'
9
+ s.homepage = 'https://github.com/Wardrop/restful-sharepoint'
10
+ s.license = 'MIT'
11
+ s.files = Dir.glob(`git ls-files`.split("\n") - %w[.gitignore])
12
+ s.has_rdoc = 'yard'
13
+
14
+ s.required_ruby_version = '>= 2.0.0'
15
+
16
+ s.add_dependency 'httpi', '~> 2.4'
17
+ s.add_dependency 'curb', '~> 0.9'
18
+ s.add_dependency 'require_pattern'
19
+ end
metadata ADDED
@@ -0,0 +1,108 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: restful-sharepoint
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.1
5
+ platform: ruby
6
+ authors:
7
+ - Tom Wardrop
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-11-02 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: httpi
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '2.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: curb
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '0.9'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '0.9'
41
+ - !ruby/object:Gem::Dependency
42
+ name: require_pattern
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ description: Provides a convenient object model to the OData REST API of SharePoint
56
+ 2013 and newer.
57
+ email: tomw@msc.qld.gov.au
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - Gemfile
63
+ - LICENSE
64
+ - README.md
65
+ - lib/restful-sharepoint.rb
66
+ - lib/restful-sharepoint/collection.rb
67
+ - lib/restful-sharepoint/collections/attachments.rb
68
+ - lib/restful-sharepoint/collections/list-items.rb
69
+ - lib/restful-sharepoint/collections/lists.rb
70
+ - lib/restful-sharepoint/collections/webs.rb
71
+ - lib/restful-sharepoint/common-base.rb
72
+ - lib/restful-sharepoint/connection.rb
73
+ - lib/restful-sharepoint/error.rb
74
+ - lib/restful-sharepoint/object.rb
75
+ - lib/restful-sharepoint/objects/attachment.rb
76
+ - lib/restful-sharepoint/objects/file.rb
77
+ - lib/restful-sharepoint/objects/image.rb
78
+ - lib/restful-sharepoint/objects/list-item.rb
79
+ - lib/restful-sharepoint/objects/list.rb
80
+ - lib/restful-sharepoint/objects/web.rb
81
+ - lib/restful-sharepoint/version.rb
82
+ - restful-sharepoint.gemspec
83
+ homepage: https://github.com/Wardrop/restful-sharepoint
84
+ licenses:
85
+ - MIT
86
+ metadata: {}
87
+ post_install_message:
88
+ rdoc_options: []
89
+ require_paths:
90
+ - lib
91
+ required_ruby_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: 2.0.0
96
+ required_rubygems_version: !ruby/object:Gem::Requirement
97
+ requirements:
98
+ - - ">="
99
+ - !ruby/object:Gem::Version
100
+ version: '0'
101
+ requirements: []
102
+ rubyforge_project:
103
+ rubygems_version: 2.6.12
104
+ signing_key:
105
+ specification_version: 4
106
+ summary: Provides a convenient object model to the OData REST API of SharePoint 2013
107
+ and newer.
108
+ test_files: []