Class: Nextcloud::Api
- Inherits:
-
Object
- Object
- Nextcloud::Api
- Defined in:
- lib/nextcloud/api.rb
Instance Method Summary collapse
-
#initialize(args) ⇒ Api
constructor
Gathers credentials for communicating with Nextcloud instance.
-
#ocs ⇒ Object
Creates Ocs API instance.
-
#request(method, path, params = nil, body = nil, depth = nil, destination = nil, raw = false) ⇒ Object
Sends API request to Nextcloud.
-
#webdav ⇒ Object
Create WebDav API instance.
Constructor Details
#initialize(args) ⇒ Api
Gathers credentials for communicating with Nextcloud instance
14 15 16 17 18 |
# File 'lib/nextcloud/api.rb', line 14 def initialize(args) @url = URI(args[:url] + "/ocs/v2.php/cloud/") @username = args[:username] @password = args[:password] end |
Instance Method Details
#ocs ⇒ Object
Creates Ocs API instance
52 53 54 |
# File 'lib/nextcloud/api.rb', line 52 def ocs OcsApi.new(url: @url, username: @username, password: @password) end |
#request(method, path, params = nil, body = nil, depth = nil, destination = nil, raw = false) ⇒ Object
Sends API request to Nextcloud
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nextcloud/api.rb', line 26 def request(method, path, params = nil, body = nil, depth = nil, destination = nil, raw = false) response = Net::HTTP.start(@url.host, @url.port, use_ssl: @url.scheme == "https") do |http| req = Kernel.const_get("Net::HTTP::#{method.capitalize}").new(@url.request_uri + path) req["OCS-APIRequest"] = true req.basic_auth @username, @password req["Content-Type"] = "application/x-www-form-urlencoded" req["Depth"] = 0 if depth req["Destination"] = destination if destination req.set_form_data(params) if params req.body = body if body http.request(req) end # if ![201, 204, 207].include? response.code # raise Errors::Error.new("Nextcloud received invalid status code") # end raw ? response.body : Nokogiri::XML.parse(response.body) end |