Sha256: c58f69c8e5e1aa88e591fecc8906f80088714a593b13fde63fb8364fb91bb59a

Contents?: true

Size: 1.78 KB

Versions: 3

Compression:

Stored size: 1.78 KB

Contents

module HTTP
  module RestClient
    # Create/Read/Update/Delete helpers.
    module CRUD
      # Resource collection finder, uses the default limit
      #
      # @param params [Hash] URI parameters to pass to the endpoint.
      # @return [Array] of [Object] instances
      def all(params = {})
        objectify(request(:get, uri, params: params))
      end

      # Resource finder
      #
      # @param id [String] resource indentifier
      # @param params [Hash] URI parameters to pass to the endpoint.
      # @return [Object] instance
      def find(id, params = {})
        objectify(request(:get, uri(id), params: params))
      end

      # Resource deletion handler
      #
      # @param id [String] resource indentifier
      # @return [Object] instance
      def delete(id)
        objectify(request(:delete, uri(id)))
      end

      # Resource creation helper
      #
      # @param params [Hash] request parameters to pass to the endpoint as JSON.
      # @return [Object] instance
      def create(params = {})
        objectify(request(:post, uri, json: params))
      end

      # Resource update helper
      #
      # @param id [String] resource indentifier
      # @param params [Hash] request parameters to pass to the endpoint as JSON.
      # @return [Object] instance
      def update(id, params = {})
        objectify(request(:patch, uri(id), json: params))
      end

      private

      # Resource constructor wrapper
      #
      # @param payload [Hash] response payload to build a resource.
      # @return [Object] instance or a list of instances.
      def objectify(payload)
        if payload.is_a?(Array)
          payload.map { |data| new(data) }
        elsif payload.is_a?(Hash)
          new(payload)
        else
          payload
        end
      end
    end
  end
end

Version data entries

3 entries across 3 versions & 1 rubygems

Version Path
http-rest_client-0.2.0 lib/http/rest_client/crud.rb
http-rest_client-0.1.1 lib/http/rest_client/crud.rb
http-rest_client-0.1.0 lib/http/rest_client/crud.rb