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

#ocsObject

Creates Ocs API instance

Returns:

  • (Object)

    OcsApi



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

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
# 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

#webdavObject

Create WebDav API instance

Returns:

  • (Object)

    WebdavApi



59
60
61
# File 'lib/nextcloud/api.rb', line 59

def webdav
  WebdavApi.new(url: @url, username: @username, password: @password)
end