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) ⇒ 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 13 14 15 16 |
# 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 @httpclient = HTTPClient.new @httpclient.set_auth(nil, @username, @password) if @username @httpclient.ssl_config.set_default_paths end |
Instance Attribute Details
#site_url ⇒ Object (readonly)
Returns the value of attribute site_url.
18 19 20 |
# File 'lib/restful-sharepoint/connection.rb', line 18 def site_url @site_url end |
Instance Method Details
#get(path, options: {}) ⇒ Object
24 25 26 |
# File 'lib/restful-sharepoint/connection.rb', line 24 def get(path, options: {}) request path, :get, options: end |
#get_as_object(path, options: {}) ⇒ Object
20 21 22 |
# File 'lib/restful-sharepoint/connection.rb', line 20 def get_as_object(path, options: {}) objectify(get(path, options: )) end |
#objectified?(v) ⇒ Boolean
76 77 78 |
# File 'lib/restful-sharepoint/connection.rb', line 76 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.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/restful-sharepoint/connection.rb', line 60 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) ⇒ 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)
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 |
# File 'lib/restful-sharepoint/connection.rb', line 30 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 += "?#{}" headers = {'accept' => 'application/json; odata=verbose'} if body body = JSON.dump(body).gsub('/', '\\/') # SharePoint requires forward slashes be escaped in JSON (WTF!!!) headers['Content-Type'] = 'application/json' headers['X-HTTP-Method'] = 'MERGE' # TODO: Extend logic to support all operations headers['If-Match'] = '*' end LOG.info "Making HTTP request to: #{url}" response = @httpclient.request(method, url, nil, body, headers) 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 |