Class: Taskr::Actions::Rest

Inherits:
Base
  • Object
show all
Defined in:
lib/taskr/actions.rb

Overview

This is too complicated… we use Restr instead.

class ActiveResource < Base
  self.parameters = ['site', 'resource', 'action', 'parameters']
  self.description = "Perform a REST call on a remote service using ActiveResource."

  def execute
    $LOG.debug self
    ::ActiveResource::Base.logger = $LOG
    ::ActiveResource::Base.logger.progname = (task ? task.to_s : self)

    eval %{
      class Proxy < ::ActiveResource::Base
        self.site = "#{parameters['site']}"
        self.collection_name = "#{parameters['resource'].pluralize}"
      end
    }

    begin
      case parameters['action']
      when 'create'
        obj = Proxy.new(parameters['parameters'])
        obj.save
      when 'update', "'update' action is not implemented"
        raise NotImplementedError
      when 'delete'
        Proxy.delete(parameters['parameters'])
      when 'find'
        raise NotImplementedError, "'find' action is not implemented"
      else
        raise ArgumentError, "unknown action #{parameters['action'].inspect}"
      end
    rescue ::ActiveResource::ServerError => e
      $LOG.error #{self} ERROR: #{e.methods.inspect}"
      raise e
    end
  end
end

class Howlr < ActiveResource
  self.parameters = ['site', 'from', 'recipients', 'subject', 'body']
  self.description = "Send a message through a Howlr service."

  def execute
    parameters['action'] = 'create'
    parameters['resource'] = 'message'
    parameters['parameters'] = {
      'from' => parameters['from'],
      'recipients' => parameters['recipients'],
      'subject' => parameters['subject'],
      'body' => parameters['body']
    }

    super
  end
end

Direct Known Subclasses

Howlr

Instance Attribute Summary

Attributes inherited from Base

#parameters, #task, #task_action

Instance Method Summary collapse

Methods inherited from Base

#initialize, #to_s, #trigger

Constructor Details

This class inherits a constructor from Taskr::Actions::Base

Instance Method Details

#executeObject



254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
# File 'lib/taskr/actions.rb', line 254

def execute
  auth = nil
  if parameters['username']
    auth = {}
    auth['username'] = parameters['username'] if parameters['username']
    auth['password'] = parameters['password'] if parameters['password']
  end
  
  if parameters['params'].kind_of? String
    params2 = {}
    parameters['params'].split('&').collect do |p|
      key, value = p.split('=')
      params2[key] = value
    end
    parameters['params'] = params2
  end
  
  options = {:logger => LogEntry.logger_for_action(task_action)}
  options.merge!(auth) if auth
  Restr.do(parameters['method'], 
    parameters['url'], 
    (parameters['params'] unless parameters['params'].blank?), 
    options)
end