Sha256: e9dc144950e8c0ed38b765dcfc832edd93fa7084ac46f857860ca5332666f386

Contents?: true

Size: 1.98 KB

Versions: 4

Compression:

Stored size: 1.98 KB

Contents

module Acfs

  # @api private
  #
  # Describes a CRUD operation. Handle request creation and response
  # processing as well as error handling and stubbing.
  #
  class Operation
    attr_reader :action, :params, :resource, :data, :callback, :location, :url
    delegate :service, to: :resource
    delegate :call, to: :callback

    def initialize(resource, action, opts = {}, &block)
      @resource = resource
      @action   = action.to_sym

      # Operations can be delayed so dup params and data to avoid
      # later in-place changes by modifying passed hash
      @params   = (opts[:params] || {}).dup
      @data     = (opts[:data]   || {}).dup

      if opts[:url]
        @url      = opts[:url]
      else
        @location = resource.location(action: @action).extract_from(@params, @data)
        @url      = location.str
      end

      @callback = block
    end

    def single?
      [:read, :update, :delete].include? action
    end

    def synchronous?
      [:update, :delete, :create].include? action
    end

    def id
      # TODO
      @id ||= params.delete(:id) || data[:id]
    end

    def full_params
      (id ? params.merge(id: id) : params).merge location_args
    end

    def location_args
      location ? location.args : {}
    end

    def method
      { read: :get, list: :get, update: :put, create: :post, delete: :delete }[action]
    end

    def request
      request = ::Acfs::Request.new url, method: method, params: params, data: data
      request.on_complete do |response|
        handle_failure response unless response.success?
        callback.call response.data, response
      end
      request
    end

    def handle_failure(response)
      case response.code
        when 404
          raise ::Acfs::ResourceNotFound.new response: response
        when 422
          raise ::Acfs::InvalidResource.new response: response, errors: response.data.try(:[], 'errors')
        else
          raise ::Acfs::ErroneousResponse.new response: response
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
acfs-0.34.1.1.b289 lib/acfs/operation.rb
acfs-0.34.1 lib/acfs/operation.rb
acfs-0.34.0.1.b288 lib/acfs/operation.rb
acfs-0.34.0.1.b287 lib/acfs/operation.rb