Module: Maestrano::API::Util

Defined in:
lib/maestrano/api/util.rb

Class Method Summary collapse

Class Method Details

.convert_to_maestrano_object(resp, api_token) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
# File 'lib/maestrano/api/util.rb', line 31

def self.convert_to_maestrano_object(resp, api_token)
  case resp
  when Array
    if resp.empty? || !resp.first[:object]
      resp
    else
      list = convert_to_maestrano_object({
        object: 'internal_list_object', 
        data:[],
        url: convert_to_maestrano_object(resp.first, api_token).class.url
      },api_token)
      
      resp.each do |i|
        list.data.push(convert_to_maestrano_object(i, api_token))
      end
      list
    end
  when Hash
    # Try converting to a known object class.  If none available, fall back to generic Maestrano::API::Object
    object_classes.fetch(resp[:object], Maestrano::API::Object).construct_from(resp, api_token)
  else
    # Automatically parse iso8601 dates
    if resp =~ /\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}/
      Time.iso8601(resp).utc
    else
      resp
    end
  end
end

.file_readable(file) ⇒ Object



61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/maestrano/api/util.rb', line 61

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

.flatten_params(params, parent_key = nil) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
# File 'lib/maestrano/api/util.rb', line 94

def self.flatten_params(params, parent_key=nil)
  result = []
  params.each do |key, value|
    calculated_key = parent_key ? "#{parent_key}[#{url_encode(key)}]" : url_encode(key)
    if value.is_a?(Hash)
      result += flatten_params(value, calculated_key)
    elsif value.is_a?(Array)
      result += flatten_params_array(value, calculated_key)
    else
      result << [calculated_key, value]
    end
  end
  result
end

.flatten_params_array(value, calculated_key) ⇒ Object



109
110
111
112
113
114
115
116
117
118
119
120
121
# File 'lib/maestrano/api/util.rb', line 109

def self.flatten_params_array(value, calculated_key)
  result = []
  value.each do |elem|
    if elem.is_a?(Hash)
      result += flatten_params(elem, calculated_key)
    elsif elem.is_a?(Array)
      result += flatten_params_array(elem, calculated_key)
    else
      result << ["#{calculated_key}[]", elem]
    end
  end
  result
end

.object_classesObject



21
22
23
24
25
26
27
28
29
# File 'lib/maestrano/api/util.rb', line 21

def self.object_classes
  @object_classes ||= {
    'account_user'           => Maestrano::Account::User,
    'account_group'          => Maestrano::Account::Group,
    'account_bill'           => Maestrano::Account::Bill,
    'account_recurring_bill' => Maestrano::Account::RecurringBill,
    'internal_list_object'   => Maestrano::API::ListObject
  }
end

.objects_to_ids(h) ⇒ Object



4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/maestrano/api/util.rb', line 4

def self.objects_to_ids(h)
  case h
  when Maestrano::API::Resource
    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) }
  when Time
    h.iso8601
  else
    h
  end
end

.symbolize_names(object) ⇒ Object



74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/maestrano/api/util.rb', line 74

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



90
91
92
# File 'lib/maestrano/api/util.rb', line 90

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