Module: Nurego::Util

Defined in:
lib/nurego/util.rb

Class Method Summary collapse

Class Method Details

.convert_to_nurego_object(resp, api_key) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
# File 'lib/nurego/util.rb', line 42

def self.convert_to_nurego_object(resp, api_key)
  case resp
  when Array
    resp.map { |i| convert_to_nurego_object(i, api_key) }
  when Hash
    # Try converting to a known object class.  If none available, fall back to generic NuregoObject
    object_classes.fetch(resp[:object], NuregoObject).construct_from(resp, api_key)
  else
    resp
  end
end

.file_readable(file) ⇒ Object



54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/nurego/util.rb', line 54

def self.file_readable(file)
  # This is nominally equivalent to File.readable?, but that can
  # report incorrect results on some more oddball filesystems
  # (such as AFS)
  begin
    File.open(file) { |f| }
  rescue
    false
  else
    true
  end
end

.object_classesObject



18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
# File 'lib/nurego/util.rb', line 18

def self.object_classes
  @object_classes ||= {
    #TODO: {omry} - find out how to add category to the object classes.
    'catalog' => Catalog,
    'entitlement' => Entitlement,
    'customer' => Customer,
    'registration' => Registration,
    'organization' => Organization,
    'instance' => Instance,
    'connector' => Connector,
    'passwordreset' => PasswordReset,
    'offering' => Offering,
    'service' => Service,
    'subscription' => Subscription,
    'plan' => Plan,
    'feature' => Feature,
    'paymentmethod' => PaymentMethod,
    'bill' => Bill,
    'discount' => Discount,
    'user' => User,
    'list' => ListObject
  }
end

.objects_to_ids(h) ⇒ Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
# File 'lib/nurego/util.rb', line 3

def self.objects_to_ids(h)
  case h
  when APIResource
    h.id
  when Hash
    res = {}
    h.each { |k, v| res[k] = objects_to_ids(v) unless v.nil? }
    res
  when Array
    h.map { |v| objects_to_ids(v) }
  else
    h
  end
end

.symbolize_names(object) ⇒ Object



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

def self.symbolize_names(object)
  case object
  when Hash
    new = {}
    object.each do |key, value|
      key = (key.to_sym rescue key) || key
      new[key] = symbolize_names(value)
    end
    new
  when Array
    object.map { |value| symbolize_names(value) }
  else
    object
  end
end

.url_encode(key) ⇒ Object



83
84
85
# File 'lib/nurego/util.rb', line 83

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