Class: Taskr::Actions::Rest

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

Overview

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

Instance Method Summary collapse

Methods inherited from Base

#initialize, #trigger

Constructor Details

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

Instance Method Details

#executeObject



202
203
204
205
206
207
208
209
210
211
212
# File 'lib/taskr/actions.rb', line 202

def execute
  auth = nil
  if parameters['username']
    auth = {}
    auth['username'] = parameters['username'] if parameters['username']
    auth['password'] = parameters['password'] if parameters['password']
  end
  
  Restr.logger = $LOG
  Restr.do(parameters['method'], parameters['url'], parameters['params'], auth)
end