Class: Nextcloud::Api

Inherits:
Object
  • Object
show all
Defined in:
lib/nextcloud/api.rb

Direct Known Subclasses

OcsApi, WebdavApi

Instance Method Summary collapse

Constructor Details

#initialize(args) ⇒ Api

Gathers credentials for communicating with Nextcloud instance

Parameters:

  • args (Hash)

    authentication credentials.

Options Hash (args):

  • :url (String)

    Nextcloud instance URL

  • :username (String)

    Nextcloud instance administrator username

  • :password (String)

    Nextcloud instance administrator password


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

#request(method, path, params = nil, body = nil, depth = nil, destination = nil, raw = false) ⇒ Object

Sends API request to Nextcloud

Parameters:

  • method (Symbol)

    Request type. Can be :get, :post, :put, etc.

  • path (String)

    Nextcloud OCS API request path

  • params (Hash, nil) (defaults to: nil)

    Parameters to send

Returns:

  • (Object)

    Nokogiri::XML::Document


26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# 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.to_s + path, 'Content-Type': "application/x-www-form-urlencoded"
    )
    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