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.



225
226
227
228
229
230
231
# File 'lib/action_web_service/api.rb', line 225

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.



222
223
224
# File 'lib/action_web_service/api.rb', line 222

def expects
  @expects
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#public_nameObject (readonly)

Returns the value of attribute public_name.



221
222
223
# File 'lib/action_web_service/api.rb', line 221

def public_name
  @public_name
end

#returnsObject (readonly)

Returns the value of attribute returns.



223
224
225
# File 'lib/action_web_service/api.rb', line 223

def returns
  @returns
end

Instance Method Details

#[](sig_type) ⇒ Object

Backwards compatibility with previous API



269
270
271
272
273
274
275
276
# File 'lib/action_web_service/api.rb', line 269

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



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

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

#cast_returns(return_value) ⇒ Object

Cast a Ruby return value into the expected Ruby value



245
246
247
# File 'lib/action_web_service/api.rb', line 245

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



251
252
253
254
255
256
257
# File 'lib/action_web_service/api.rb', line 251

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



261
262
263
264
265
266
# File 'lib/action_web_service/api.rb', line 261

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



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

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

#to_sObject

String representation of this method



279
280
281
282
283
284
285
286
# File 'lib/action_web_service/api.rb', line 279

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