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.



172
173
174
175
176
177
178
# File 'lib/action_web_service/api.rb', line 172

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.



169
170
171
# File 'lib/action_web_service/api.rb', line 169

def expects
  @expects
end

#nameObject (readonly)

Returns the value of attribute name.



167
168
169
# File 'lib/action_web_service/api.rb', line 167

def name
  @name
end

#public_nameObject (readonly)

Returns the value of attribute public_name.



168
169
170
# File 'lib/action_web_service/api.rb', line 168

def public_name
  @public_name
end

#returnsObject (readonly)

Returns the value of attribute returns.



170
171
172
# File 'lib/action_web_service/api.rb', line 170

def returns
  @returns
end

Instance Method Details

#[](sig_type) ⇒ Object

Backwards compatibility with previous API



216
217
218
219
220
221
222
223
# File 'lib/action_web_service/api.rb', line 216

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



187
188
189
# File 'lib/action_web_service/api.rb', line 187

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

#cast_returns(return_value) ⇒ Object

Cast a Ruby return value into the expected Ruby value



192
193
194
# File 'lib/action_web_service/api.rb', line 192

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



198
199
200
201
202
203
204
# File 'lib/action_web_service/api.rb', line 198

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



208
209
210
211
212
213
# File 'lib/action_web_service/api.rb', line 208

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



181
182
183
184
# File 'lib/action_web_service/api.rb', line 181

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

#to_sObject

String representation of this method



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

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