Class: RTM::RTMMethodSpace

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

Overview

Generic means of calling an RTM method. This is returned by RTM.method_missing and, in most cases, is the end point that calls an RTM method.

Instance Method Summary collapse

Constructor Details

#initialize(name, endpoint) ⇒ RTMMethodSpace

Create an RTMMethodSpace

name

the name of this method space, e.g. ‘tasks’

endpoint

an endpoing to RTM



97
98
99
100
101
# File 'lib/moocow/moocow.rb', line 97

def initialize(name,endpoint)
  @name = name
  @endpoint = endpoint
  raise "No endpoint" if @endpoint.nil?
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(symbol, *args) ⇒ Object

Calls the method on RTM in most cases. The only exception is if this RTMMethodSpace is ‘tasks’ and you call the ‘notes’ method on it: a new RTMMethodSpace is returned for the ‘rtm.tasks.notes’ method-space.

This returns a response object as from HTTParty, dereferenced into <rsp>. So, for example, if you called the ‘tasks.getList’ method, you would get a hash that could be accessed via response. This object is a Hash and Array structure that mimcs the XML returned by RTM. One quirk is that for methods that could return 1 or more of the same item (tasks.getList is a good example; it will return multilple <list> elements unless you restrict by list, in which case it returns only one <list> element). Because HTTParty doesn’t understand this, you may find it convienient to convert such results to arrays. the to_array extension on Hash and Array accomplish this:

response = rtm.tasks.getList(:filter => 'list:Work')
response['tasks']['list'].as_array.each do |list|
  list['taskseries'].as_array.each do |task|
    puts task['name']
  end
end

So, call to_array on anything you expect to be a list.

This method raises either a BadResponseException if you got a bad or garbled response from RTM or a VerificationException if you got a non-OK response.



124
125
126
127
128
129
130
131
# File 'lib/moocow/moocow.rb', line 124

def method_missing(symbol,*args)
  if (@name == 'tasks' && symbol.to_s == 'notes')
    return RTMMethodSpace.new("tasks.notes",@endpoint)
  else
    rtm_method = "rtm.#{@name}.#{symbol.to_s.rtmize}"
    @endpoint.call_method(rtm_method,*args)
  end
end