Module: Rews::Util

Included in:
Client, Folder::BaseFolderId, Item::ItemId, Shape::Base, View::Base
Defined in:
lib/rews/util.rb

Class Method Summary collapse

Class Method Details

.apply_namespace(qname, apply_prefix, apply_uri) ⇒ Object

given an exploded qname, apply a given namespace and uri if the qname has no namespace already



72
73
74
75
76
77
78
79
80
81
# File 'lib/rews/util.rb', line 72

def apply_namespace(qname, apply_prefix, apply_uri)
  local_part, prefix, uri = qname
  
  if !prefix
    prefix = apply_prefix
    uri = apply_uri
  end

  [local_part, prefix, uri].compact
end

.camel_keys(h) ⇒ Object

camel-case the keys of a Hash



66
67
68
# File 'lib/rews/util.rb', line 66

def camel_keys(h)
  Hash[h.map{|k,v| [camelize(k.to_s), v]}]
end

.camelize(s) ⇒ Object

camel-case a String



57
58
59
60
61
62
63
# File 'lib/rews/util.rb', line 57

def camelize(s)
  if s.is_a?(Symbol)
    s.to_s.split('_').map(&:capitalize).join.to_sym
  else
    s.split('_').map(&:capitalize).join
  end
end

.camelize_qname(qname) ⇒ Object

given an exploded qname, camelize the local_part



84
85
86
87
# File 'lib/rews/util.rb', line 84

def camelize_qname(qname)
  local_part, prefix, uri = qname
  [camelize(local_part), prefix, uri].compact
end

.check_opts(constraints, opts = {}, prefix = nil) ⇒ Object

validates an options hash against a constraints hash in the constraints hash :

  • keys ending in ! indicate option is required

  • keys not ending in ! indicate option is not required

  • non-nil values provide defaults

  • hash values provide constraints for sub-hashes



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/rews/util.rb', line 23

def check_opts(constraints, opts={}, prefix=nil)
  required_keys = Hash[constraints.keys.select{|k| k.to_s[-1..-1] == '!'}.map{|k| [strip_bang(k),k]}]
  optional_keys = constraints.keys.select{|k| k.to_s[-1..-1] != '!'}

  # check each of the provided options
  opts.keys.each do |key|
    raise "unknown option: #{[prefix,key].compact.join(".")}" if !required_keys.include?(key) && !optional_keys.include?(key)
    
    ckey = required_keys[key] || key
    if constraints[ckey].is_a? Hash
      check_opts(constraints[ckey], opts[key] || {}, [prefix,key].compact.join("."))
    end

    required_keys.delete(key)
    optional_keys.delete(key)
  end

  raise "required options not given: #{required_keys.keys.map{|k| [prefix,k].compact.join('.')}.join(", ")}" if required_keys.size>0
  
  # defaults
  optional_keys.each{|k| opts[k] = constraints[k] if constraints[k] && !constraints[k].is_a?(Hash)}
  opts
end

.rsxml_to_xml(sexp) ⇒ Object

convert rsxml to xml, transforming local_parts of QNames to CamelCase and prefixing with the t: namespace prefix if no namespace is already applied



91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/rews/util.rb', line 91

def rsxml_to_xml(sexp)
  # visit the rsxml, prefix the element tags with "t" namespace prefix, and camelcase
  # all QName local_parts
  transform_visitor = Rsxml::Visitor::BuildRsxmlVisitor.new() do |context, element_name, attrs|
    t_element_name = camelize_qname(apply_namespace(element_name, "t", Rews::SCHEMA_TYPES))
    t_attrs = Hash[attrs.map{|attr_name,v| [camelize_qname(attr_name), v]}]
    [t_element_name, t_attrs]
  end

  xrsxml = Rsxml::Sexp.traverse(sexp, transform_visitor).sexp
  Rsxml.to_xml(xrsxml, :ns=>{"t"=>Rews::SCHEMA_TYPES, "wsdl"=>Rews::SCHEMA_MESSAGES})
end

.single_error_check(client, status) ⇒ Object

check the status of the response of a single part of a multi-part request



133
134
135
136
137
138
139
140
141
142
143
144
145
# File 'lib/rews/util.rb', line 133

def single_error_check(client, status)
  begin
    response_class = status[:response_class]
  rescue
    raise "no response_class found: #{status.inspect}" if !response_class
  end

  if status[:response_class] == "Error"
    return "#{status[:response_code]} - #{status[:message_text]}"
  elsif status[:response_class] == "Warning"
    Rews.log{|logger| logger.warn("#{status[:response_code]} - #{status[:message_text]}")}
  end
end

.strip_bang(k) ⇒ Object

strip a ! from the end of a String or Symbol



48
49
50
51
52
53
54
# File 'lib/rews/util.rb', line 48

def strip_bang(k)
  if k.is_a? Symbol
    k.to_s[0...-1].to_sym
  else
    k.to_s[0...-1]
  end
end

.tag_exception(e, tags) ⇒ Object



10
11
12
13
14
15
# File 'lib/rews/util.rb', line 10

def tag_exception(e, tags)
  tags.each do |k,v|
    mc = class << e ; self ; end
    mc.send(:define_method, k){v}
  end
end

.with_error_check(client, *response_msg_keys) ⇒ Object

check the response codes of an Exchange Web Services request. the supplied block makes a SOAP request, and the response is parsed out and checked

Raises:



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
# File 'lib/rews/util.rb', line 107

def with_error_check(client, *response_msg_keys)
  raise "no block" if !block_given?

  begin
    response = yield
    hash_response = response.to_hash
    statuses = hash_response.fetch_in(*response_msg_keys)
    
    if statuses.is_a?(Array)
      all_statuses = statuses
    else
      all_statuses = [statuses]
    end
    
    errors = all_statuses.map{|s| single_error_check(client, s)}.compact
  rescue Exception=>e
    Rews.log{|logger| logger.warn(e)}
    tag_exception(e, :savon_response=>response)
    raise e
  end

  raise Error.new(errors.join("\n")) if !errors.empty?
  statuses
end