Class: RestfulSharePoint::Connection
- Inherits:
-
Object
- Object
- RestfulSharePoint::Connection
- Defined in:
- lib/restful-sharepoint/connection.rb
Instance Attribute Summary collapse
-
#site_url ⇒ Object
readonly
Returns the value of attribute site_url.
Instance Method Summary collapse
- #get(path, options: {}) ⇒ Object
- #get_as_object(path, options: {}) ⇒ Object
-
#initialize(site_url, username = nil, password = nil, debug: false) ⇒ Connection
constructor
A new instance of Connection.
- #objectified?(v) ⇒ Boolean
-
#objectify(tree, parent: nil) ⇒ Object
Converts the given enumerable tree to a collection or object.
-
#request(path, method, options: {}, body: nil) {|req| ... } ⇒ Object
Path can be either relative to the site URL, or a complete URL itself.
Constructor Details
#initialize(site_url, username = nil, password = nil, debug: false) ⇒ Connection
Returns a new instance of Connection.
7 8 9 10 11 12 |
# File 'lib/restful-sharepoint/connection.rb', line 7 def initialize(site_url, username = nil, password = nil, debug: false) @site_url = site_url @username = username @password = password @debug = debug end |
Instance Attribute Details
#site_url ⇒ Object (readonly)
Returns the value of attribute site_url.
14 15 16 |
# File 'lib/restful-sharepoint/connection.rb', line 14 def site_url @site_url end |
Instance Method Details
#get(path, options: {}) ⇒ Object
20 21 22 |
# File 'lib/restful-sharepoint/connection.rb', line 20 def get(path, options: {}) request path, :get, options: end |
#get_as_object(path, options: {}) ⇒ Object
16 17 18 |
# File 'lib/restful-sharepoint/connection.rb', line 16 def get_as_object(path, options: {}) objectify(get(path, options: )) end |
#objectified?(v) ⇒ Boolean
72 73 74 |
# File 'lib/restful-sharepoint/connection.rb', line 72 def objectified?(v) CommonBase === v || !v.is_a?(Hash) end |
#objectify(tree, parent: nil) ⇒ Object
Converts the given enumerable tree to a collection or object.
56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
# File 'lib/restful-sharepoint/connection.rb', line 56 def objectify(tree, parent: nil) if tree['results'] type = tree['__metadata']&.[]('type') || tree['results'][0]&.[]('__metadata')&.[]('type') || '' pattern, klass = COLLECTION_MAP.any? { |pattern,| pattern.match(type) } klass ||= :Collection RestfulSharePoint.const_get(klass).new(connection: self, parent: parent, collection: tree['results']) elsif tree['__metadata'] type = tree['__metadata']['type'] raise "Object type not specified. #{tree.inspect}" unless type pattern, klass = OBJECT_MAP.find { |pattern,| pattern.match(type) } klass ? RestfulSharePoint.const_get(klass).new(connection: self, parent: parent, properties: tree) : tree else tree end end |
#request(path, method, options: {}, body: nil) {|req| ... } ⇒ Object
Path can be either relative to the site URL, or a complete URL itself. Takes an optional ‘options` hash which are any number of valid OData query options (dollar sign prefix is added automatically) Also takes an optional block that is provided the HTTPI::Request instance, allowing customisation of the request.
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 |
# File 'lib/restful-sharepoint/connection.rb', line 27 def request(path, method, options: {}, body: nil) url = URI.parse(path).is_a?(URI::HTTP) ? path : "#{@site_url}#{path}" = .map { |k,v| "$#{k}=#{CGI.escape v.to_s}" }.join('&') url += "?#{}" req = HTTPI::Request.new(url: url, headers: {'accept' => 'application/json; odata=verbose'}) req.auth.ntlm(@username, @password) if @username if body req.body = JSON.dump(body).gsub('/', '\\/') # SharePoint requires forward slashes be escaped in JSON (WTF!!!) req.headers['Content-Type'] = 'application/json' req.headers['X-HTTP-Method'] = 'MERGE' # TODO: Extend logic to support all operations req.headers['If-Match'] = '*' end yield(req) if block_given? LOG.info "Making HTTP request to: #{req.url.to_s}" response = HTTPI.request(method, req) { |x| x.ssl_config.set_default_paths } if response.body.empty? if response.code >= 300 raise RestError, "Server returned HTTP status #{response.code} with no message body." end else if response.headers['Content-Type'].start_with? "application/json" parse response.body else response.body end end end |