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(string, pattern) ⇒ Object


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

def self.escape(string, pattern)
  if defined?(URI::Parser)
    parser = URI::Parser.new
    parser.escape(string, pattern)
  else
    URI.escape(string, pattern)
  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   = ";/?:@&=+,$"
  escape(parameter, /[#{control+space+delims+unwise+query}]/o)
end

62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/active_cmis/internal/utils.rb', line 62

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)
  escape(string, /[^#{URI::PATTERN::UNRESERVED}]/o)
end

.string_or_id_to_object(repository, id) ⇒ Object

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


50
51
52
53
54
55
56
57
58
59
# File 'lib/active_cmis/internal/utils.rb', line 50

def self.string_or_id_to_object(repository, id)
  # FIXME: only used in lib/activecmis/relationship.rb, the repository parameter
  # would be unnecessary if included.
  # Should this be a generic method, or should this be moved to the Relationship class?
  # Or should I start including this module in every place that needs it?
  case id
  when String; repository.object_by_id(id)
  when ::ActiveCMIS::Object; id
  end
end