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.



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

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.



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

def expects
  @expects
end

#nameObject (readonly)

Returns the value of attribute name.



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

def name
  @name
end

#public_nameObject (readonly)

Returns the value of attribute public_name.



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

def public_name
  @public_name
end

#returnsObject (readonly)

Returns the value of attribute returns.



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

def returns
  @returns
end

Instance Method Details

#[](sig_type) ⇒ Object

Backwards compatibility with previous API



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

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



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

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

#cast_returns(return_value) ⇒ Object

Cast a Ruby return value into the expected Ruby value



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

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



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

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



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

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



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

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

#to_sObject

String representation of this method



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

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