Module: Nurego::Util
- Defined in:
- lib/nurego/util.rb
Class Method Summary collapse
- .convert_to_nurego_object(resp, api_key) ⇒ Object
- .file_readable(file) ⇒ Object
- .object_classes ⇒ Object
- .objects_to_ids(h) ⇒ Object
- .symbolize_names(object) ⇒ Object
- .url_encode(key) ⇒ Object
Class Method Details
.convert_to_nurego_object(resp, api_key) ⇒ Object
37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/nurego/util.rb', line 37 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
49 50 51 52 53 54 55 56 57 58 59 60 |
# File 'lib/nurego/util.rb', line 49 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_classes ⇒ Object
18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/nurego/util.rb', line 18 def self.object_classes @object_classes ||= { 'customer' => Customer, 'registration' => Registration, 'organization' => Organization, 'instance' => Instance, 'connector' => Connector, 'passwordreset' => PasswordReset, 'offering' => Offering, 'service' => Service, 'plan' => Plan, 'feature' => Feature, 'paymentmethod' => PaymentMethod, 'bill' => Bill, 'discount' => Discount, '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
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/nurego/util.rb', line 62 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
78 79 80 |
# File 'lib/nurego/util.rb', line 78 def self.url_encode(key) URI.escape(key.to_s, Regexp.new("[^#{URI::PATTERN::UNRESERVED}]")) end |