Class: ActionWebService::API::Method

Inherits:
Object
  • Object
show all
Defined in:
lib/action_web_service/api.rb

Overview

Represents an API method and its associated metadata, and provides functionality to assist in commonly performed API method tasks.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, public_name, expects, returns) ⇒ Method

Returns a new instance of Method.



221
222
223
224
225
226
227
# File 'lib/action_web_service/api.rb', line 221

def initialize(name, public_name, expects, returns)
  @name = name
  @public_name = public_name
  @expects = expects
  @returns = returns
  @caster = ActionWebService::Casting::BaseCaster.new(self)
end

Instance Attribute Details

#expectsObject (readonly)

Returns the value of attribute expects.



218
219
220
# File 'lib/action_web_service/api.rb', line 218

def expects
  @expects
end

#nameObject (readonly)

Returns the value of attribute name.



216
217
218
# File 'lib/action_web_service/api.rb', line 216

def name
  @name
end

#public_nameObject (readonly)

Returns the value of attribute public_name.



217
218
219
# File 'lib/action_web_service/api.rb', line 217

def public_name
  @public_name
end

#returnsObject (readonly)

Returns the value of attribute returns.



219
220
221
# File 'lib/action_web_service/api.rb', line 219

def returns
  @returns
end

Instance Method Details

#[](sig_type) ⇒ Object

Backwards compatibility with previous API



265
266
267
268
269
270
271
272
# File 'lib/action_web_service/api.rb', line 265

def [](sig_type)
  case sig_type
  when :expects
    @expects.map{|x| compat_signature_entry(x)}
  when :returns
    @returns.map{|x| compat_signature_entry(x)}
  end
end

#cast_expects(params) ⇒ Object

Casts a set of Ruby values into the expected Ruby values



236
237
238
# File 'lib/action_web_service/api.rb', line 236

def cast_expects(params)
  @caster.cast_expects(params)
end

#cast_returns(return_value) ⇒ Object

Cast a Ruby return value into the expected Ruby value



241
242
243
# File 'lib/action_web_service/api.rb', line 241

def cast_returns(return_value)
  @caster.cast_returns(return_value)
end

#expects_index_of(param_name) ⇒ Object

Returns the index of the first expected parameter with the given name



247
248
249
250
251
252
253
# File 'lib/action_web_service/api.rb', line 247

def expects_index_of(param_name)
  return -1 if @expects.nil?
  (0..(@expects.length-1)).each do |i|
    return i if @expects[i].name.to_s == param_name.to_s
  end
  -1
end

#expects_to_hash(params) ⇒ Object

Returns a hash keyed by parameter name for the given parameter list



257
258
259
260
261
262
# File 'lib/action_web_service/api.rb', line 257

def expects_to_hash(params)
  return {} if @expects.nil?
  h = {}
  @expects.zip(params){ |type, param| h[type.name] = param }
  h
end

#param_namesObject

The list of parameter names for this method



230
231
232
233
# File 'lib/action_web_service/api.rb', line 230

def param_names
  return [] unless @expects
  @expects.map{ |type| type.name }
end

#to_sObject

String representation of this method



275
276
277
278
279
280
281
282
# File 'lib/action_web_service/api.rb', line 275

def to_s
  fqn = ""
  fqn << (@returns ? (@returns[0].human_name(false) + " ") : "void ")
  fqn << "#{@public_name}("
  fqn << @expects.map{ |p| p.human_name }.join(", ") if @expects
  fqn << ")"
  fqn
end