Class: RTM::Endpoint

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

Overview

Acts as the endopint to RTM actions, providing a means to separate the API’s behavior with interaction with RTM.

Constant Summary collapse

NO_TIMELINE =
{
  "rtm.contacts.getList" => true,
  "rtm.groups.getList" => true,
  "rtm.lists.getList" => true,
  "rtm.reflection.getMethodInfo" => true,
  "rtm.reflection.getMethods" => true,
  "rtm.settings.getList" => true,
  "rtm.tasks.getList" => true,
  "rtm.test.echo" => true,
  "rtm.test.login" => true,
  "rtm.time.convert" => true,
  "rtm.time.parse" => true,
  "rtm.timelines.create" => true,
  "rtm.timezones.getList" => true,
  "rtm.transactions.undo" => true,
}
BASE_URL =
'https://api.rememberthemilk.com/services/'

Instance Method Summary collapse

Constructor Details

#initialize(api_key, secret, http = HTTParty) ⇒ Endpoint

Create an endpoint to RTM, upon which methods may be called.

api_key

your api key

secret

your secret

http

a class that acts like HTTParty (omit; this is used for testing mostly)



29
30
31
32
33
34
35
# File 'lib/moocow/endpoint.rb', line 29

def initialize(api_key,secret,http=HTTParty)
  @api_key = api_key
  @secret = secret
  @http=http
  raise "Cannot work with a secret key" if @secret.nil?
  raise "Cannot work with a api_key key" if @api_key.nil?
end

Instance Method Details

#auto_timeline=(auto) ⇒ Object



37
38
39
# File 'lib/moocow/endpoint.rb', line 37

def auto_timeline=(auto)
  @auto_timeline = auto
end

#call_method(method, params = {}, token_required = true) ⇒ Object

Calls the RTM method with the given parameters

method

the full RTM method, e.g. rtm.tasks.getList

params

the parameters to pass, can be symbols. api_key, token, and signature not required

token_required

if false, this method will not require a token

Raises:



54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
# File 'lib/moocow/endpoint.rb', line 54

def call_method(method,params={},token_required=true)
  @last_timeline = nil
  raise NoTokenException if token_required && (!@token || @token.nil?)
  params = {} if params.nil?
  params[:auth_token] = @token if !@token.nil?

  if (@auto_timeline && !NO_TIMELINE[method])
    response = @http.get(url_for('rtm.timelines.create',{'auth_token' => @token}));
    if response['timeline']
      @last_timeline = response['timeline']
      params[:timeline] = @last_timeline
    else
      raise BadResponseException, "Expected a <timeline></timeline> type response, but got: #{response.body}"
    end
  end

  params_no_symbols = Hash.new
  params.each do |k,v|
    params_no_symbols[k.to_s] = v
  end

  response = @http.get(url_for(method,params_no_symbols))
  verify(response)
  return response['rsp']
end

#last_timelineObject



46
47
48
# File 'lib/moocow/endpoint.rb', line 46

def last_timeline
  @last_timeline
end

#token=(token) ⇒ Object

Update the token used to access this endpoint



42
43
44
# File 'lib/moocow/endpoint.rb', line 42

def token=(token)
  @token = token
end

#url_for(method, params = {}, endpoint = 'rest') ⇒ Object

Get the url for a particular call, doing the signing and all that other stuff.

method

the RTM method to call

params

hash of parameters. The method, api_key, and api_sig parameters should not be included.

endpoint

the endpoint relate to BASE_URL at which this request should be made.



85
86
87
88
89
90
91
# File 'lib/moocow/endpoint.rb', line 85

def url_for(method,params={},endpoint='rest')
  params['api_key'] = @api_key
  params['method'] = method if method
  signature = sign(params)
  url = BASE_URL + endpoint + '/' + params_to_url(params.merge({'api_sig' => signature}))
  url
end