Module: RightScale::Api::Gateway

Instance Attribute Summary

Attributes included from Base

#params

Instance Method Summary collapse

Methods included from GatewayConnection

#connection

Methods included from Base

#destroy, #reload, #resource_plural_name, #resource_singular_name, #rs_id

Methods included from BaseConnection

#connection

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args) ⇒ Object



132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 132

def method_missing(method_name, *args)
  puts "DEBUG: method_missing in #{self.class.to_s}: #{method_name.to_s}" if ENV['REST_CONNECT_DEBUG']
  mn = method_name.to_s
  assignment = mn.gsub!(/=/,"")
  mn_dash = mn.gsub(/_/,"-")
  if self[mn]
    if assignment
      self[mn] = args[0]
      self[mn_dash] = args[0]
    end
    return self[mn]
  elsif self[mn_dash]
    if assignment
      self[mn_dash] = args[0]
      self[mn] = args[0]
    end
    return self[mn_dash]
  elsif self[mn.to_sym]
    return self[mn.to_sym]
  elsif assignment
    self[mn] = args[0]
    self[mn_dash] = args[0]
    return self[mn]
  else
    return nil
    warn "!!!! WARNING - called unknown method #{method_name.to_s}# with #{args.inspect}"
    #raise "called unknown method #{method_name.to_s}# with #{args.inspect}"
  end
end

Instance Method Details

#[](name) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 162

def [](name)
  try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
  if try_these.include?(:nickname)
    try_these += ["name", :name]
  end
  try_these.each do |t|
    if @params[t]
      return @params[t]
    elsif hash_of_links[t]
      return hash_of_links[t]
    end
  end
  return nil
end

#[]=(name, val) ⇒ Object



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 177

def []=(name,val)
  try_these = [name.to_s, name.to_s.gsub(/_/,'-'), name.to_sym]
  if try_these.include?(:nickname)
    try_these += ["name", :name]
  end
  try_these.each do |t|
    if @params[t]
      @params[t] = val
    elsif hash_of_links[t]
      @params['links'].each { |link|
        link['href'] = val if link['rel'] == t
      }
    end
  end
  val
end

#actionsObject



121
122
123
124
125
126
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 121

def actions
  ret = []
  self.rediscover unless @params['actions']
  @params['actions'].each { |action| ret << action['rel'] }
  ret
end


106
107
108
109
110
111
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 106

def hash_of_links
  ret = {}
  self.rediscover unless @params['links']
  @params['links'].each { |link| ret[link['rel']] = link['href'] } if @params['links']
  ret
end

#hrefObject



113
114
115
116
117
118
119
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 113

def href
  return @params['href'] if @params['href']
  ret = nil
  self.rediscover unless @params['links']
  @params['links'].each { |link| ret = link['href'] if link['rel'] == 'self' }
  ret
end

#initialize(params = {}) ⇒ Object



83
84
85
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 83

def initialize(params = {})
  @params = parse_params(params)
end

#load(resource) ⇒ Object



194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 194

def load(resource)
  mod = RightScale::Api::GatewayExtend
  @@gateway_resources ||= Object.constants.map do |const|
    klass = Object.const_get(const)
    (mod === klass ? klass : nil)
  end.compact
  pp @@gateway_resources
  if mod === resource
    klass = resource
  elsif resource.is_a?(String) or resource.is_a?(Symbol)
    klass = @@gateway_resources.detect do |const|
      [const.resource_singular_name, const.resource_plural_name].include?(resource.to_s)
    end
  elsif Class === resource
    raise TypeError.new("#{resource} doesn't extend #{mod}")
  else
    raise TypeError.new("can't convert #{resource.class} into supported Class")
  end

  if self[klass.resource_singular_name]
    return klass.load(self[klass.resource_singular_name])
  elsif self[klass.resource_plural_name]
    return klass.load_all(self[klass.resource_plural_name])
  else
    raise NameError.new("no resource_hrefs found for #{klass}")
  end
end

#nicknameObject

Raises:

  • (TypeError)


91
92
93
94
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 91

def nickname
  raise TypeError.new("@params isn't a Hash! @params.to_s=#{@params.to_s}") unless @params.is_a?(Hash)
  @params["nickname"] || @params["name"]
end

#parse_params(params = {}) ⇒ Object



87
88
89
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 87

def parse_params(params = {})
  params
end

#rediscoverObject



96
97
98
99
100
101
102
103
104
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 96

def rediscover
  self.reload if @params['href']
  raise "Cannot find attribute 'nickname' or 'name' in #{self.inspect}. Aborting." unless self.nickname
  if self.class.filters.include?(:name)
    @params = self.class.find_with_filter(:name => self.nickname).first.params
  else
    @params = self.class.find_by(:name) { |n| n == self.nickname }.first.params
  end
end

#saveObject



128
129
130
# File 'lib/rest_connection/rightscale/rightscale_api_gateway.rb', line 128

def save
  update
end