Module: Nextcloud::Helpers

Included in:
Ocs::App, Ocs::FileSharingApi, Ocs::FileSharingApi::FederatedCloudShares, Ocs::Group, Ocs::User, Ocs::User::Group, Webdav::Directory
Defined in:
lib/nextcloud/helpers/nextcloud.rb,
lib/nextcloud/helpers/properties.rb

Overview

Helper methods that are used through lib

Defined Under Namespace

Modules: Properties

Instance Method Summary collapse

Instance Method Details

#add_meta(doc, obj) ⇒ #define_singleton_method

Adds meta method to an object

Parameters:

  • doc (Object)

    Nokogiri::XML::Document to take meta information from

  • obj (#define_singleton_method)

    Object to add meta method to

Returns:

  • (#define_singleton_method)

    Object with meta method defined


49
50
51
52
# File 'lib/nextcloud/helpers/nextcloud.rb', line 49

def add_meta(doc, obj)
  meta = get_meta(doc)
  obj.define_singleton_method(:meta) { meta } && obj
end

#doc_to_hash(doc, xpath = "/") ⇒ Hash

Converts document to hash

Parameters:

  • doc (Object)

    Nokogiri::XML::Document

  • xpath (String) (defaults to: "/")

    Document path to convert to hash

Returns:

  • (Hash)

    Hash that was produced from XML document


39
40
41
42
# File 'lib/nextcloud/helpers/nextcloud.rb', line 39

def doc_to_hash(doc, xpath = "/")
  h = Hash.from_xml(doc.xpath(xpath).to_xml)
  h
end

#get_meta(doc) ⇒ Hash

Parses meta information returned by API, may include a status, status code and a message

Parameters:

  • doc (Object)

    Nokogiri::XML::Document

Returns:

  • (Hash)

    Parsed hash


28
29
30
31
32
# File 'lib/nextcloud/helpers/nextcloud.rb', line 28

def get_meta(doc)
  meta = doc.xpath("//meta/*").each_with_object({}) do |node, meta|
    meta[node.name] = node.text
  end
end

#has_dav_errors(doc) ⇒ Hash, Boolean

Shows error or returns false

Parameters:

  • doc (Object)

    Nokogiri::XML::Document

Returns:

  • (Hash, Boolean)

    Returns error message if found, false otherwise


89
90
91
92
93
94
95
96
97
98
99
# File 'lib/nextcloud/helpers/nextcloud.rb', line 89

def has_dav_errors(doc)
  doc.remove_namespaces!
  if doc.at_xpath("//error")
    {
        exception: doc.xpath("//exception").text,
        message: doc.xpath("//message").text
    }
  else
    false
  end
end

#parse_dav_response(doc) ⇒ Hash

Shows errors, or success message

Parameters:

  • doc (Object)

    Nokogiri::XML::Document

Returns:

  • (Hash)

    State response


67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
# File 'lib/nextcloud/helpers/nextcloud.rb', line 67

def parse_dav_response(doc)
  doc.remove_namespaces!
  if doc.at_xpath("//error")
    {
        exception: doc.xpath("//exception").text,
        message: doc.xpath("//message").text
    }
  elsif doc.at_xpath("//status")
    {
        status: doc.xpath("//status").text
    }
  else
    {
        status: "ok"
    }
  end
end

#parse_with_meta(doc, xpath) ⇒ Array

Makes an array out of repeated elements

Parameters:

  • doc (Object)

    Nokogiri::XML::Document

  • xpath (String)

    Path to element that is being repeated

Returns:

  • (Array)

    Parsed array


12
13
14
15
16
17
18
19
20
21
22
# File 'lib/nextcloud/helpers/nextcloud.rb', line 12

def parse_with_meta(doc, xpath)
  groups = []
  doc.xpath(xpath).each do |prop|
    groups << prop.text
  end
  meta = get_meta(doc)
  groups.send(:define_singleton_method, :meta) do
    meta
  end
  groups
end

#path_from_href(href, url) ⇒ String

Extracts remaining part of url

Parameters:

  • href (String)

    Full url

  • url (String)

    Part to give away

Returns:

  • (String)

    “Right” part of string


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

def path_from_href(href, url)
  href.match(/#{url}(.*)/)[1]
end