Module: ActiveCMIS::Internal::Utils

Defined in:
lib/active_cmis/internal/utils.rb

Class Method Summary collapse

Class Method Details

.append_parameters(uri, parameters) ⇒ Object

Given an url (string or URI) returns that url with the given parameters appended

This method does not perform any encoding on the paramter or key values. This method does not check the existing parameters for duplication in keys



20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/active_cmis/internal/utils.rb', line 20

def self.append_parameters(uri, parameters)
  uri       = case uri
              when String; string = true; URI.parse(uri)
              when URI;    uri.dup
              end
  uri.query = [uri.query, *parameters.map {|key, value| "#{key}=#{value}"} ].compact.join "&"
  if string
    uri.to_s
  else
    uri
  end
end

.escape_url_parameter(parameter) ⇒ Object



6
7
8
9
10
11
12
13
# File 'lib/active_cmis/internal/utils.rb', line 6

def self.escape_url_parameter(parameter)
  control = "\x00-\x1F\x7F"
  space   = " "
  delims  = "<>#%\""
  unwise  = '{}|\\\\^\[\]`'
  query   = ";/?:@&=+,$"
  URI.escape(parameter, /[#{control+space+delims+unwise+query}]/o)
end


49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/active_cmis/internal/utils.rb', line 49

def self.extract_links(xml, rel, type_main = nil, type_params = {})
  links = xml.xpath("at:link[@rel = '#{rel}']", NS::COMBINED)

  if type_main
    type_main = Regexp.escape(type_main)
    if type_params.empty?
      regex = /#{type_main}/
    else
      parameters = type_params.map {|k,v| "#{Regexp.escape(k)}=#{Regexp.escape(v)}" }.join(";\s*")
      regex = /#{type_main};\s*#{parameters}/
    end
    links = links.select do |node|
       regex === node.attribute("type").to_s
    end
  end

  links.map {|l| l.attribute("href").to_s}
end

.percent_encode(string) ⇒ Object

FIXME?? percent_encode and escape_url_parameter serve nearly the same purpose, replace one?



35
36
37
# File 'lib/active_cmis/internal/utils.rb', line 35

def self.percent_encode(string)
  URI.escape(string, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]"))
end

.string_or_id_to_object(id) ⇒ Object

Returns id if id is already an object, object_by_id if id is a string, nil otherwise



41
42
43
44
45
46
# File 'lib/active_cmis/internal/utils.rb', line 41

def self.string_or_id_to_object(id)
  case id
  when String; repository.object_by_id(id)
  when ::ActiveCMIS::Object; id
  end
end