Sha256: 06b98152332c51365edeba95a2425e0f2e3cf468ffbf4a9fc2fa0d72820cfe9f

Contents?: true

Size: 1.5 KB

Versions: 5

Compression:

Stored size: 1.5 KB

Contents

require 'addressable/template'

require 'routemaster/api_client'
require 'routemaster/responses/hateoas_enumerable_response'
require 'routemaster/responses/hateoas_response'

module Routemaster
  module Resources
    class RestResource
      def initialize(url, client: nil)
        @url_template = Addressable::Template.new(url)
        @client = client || Routemaster::APIClient.new
      end

      def create(params)
        @client.with_response(Responses::HateoasResponse) do
          @client.post(expanded_url, body: params)
        end
      end

      def show(id=nil, enable_caching: true)
        @client.with_response(Responses::HateoasResponse) do
          @client.get(expanded_url(id: id), options: { enable_caching: enable_caching })
        end
      end

      def index(params: {}, filters: {}, enable_caching: false)
        @client.with_response(Responses::HateoasEnumerableResponse) do
          @client.get(expanded_url, params: params.merge(filters), options: { enable_caching: enable_caching })
        end
      end

      def update(id=nil, params)
        @client.with_response(Responses::HateoasResponse) do
          @client.patch(expanded_url(id: id), body: params)
        end
      end

      def destroy(id=nil)
        # no response wrapping as DELETE is supposed to 204.
        @client.delete(expanded_url(id: id))
      end

      def url
        @url_template.pattern
      end

      private

      def expanded_url(**params)
        @url_template.expand(params).to_s
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
routemaster-drain-2.5.4 lib/routemaster/resources/rest_resource.rb
routemaster-drain-2.5.3 lib/routemaster/resources/rest_resource.rb
routemaster-drain-2.5.2 lib/routemaster/resources/rest_resource.rb
routemaster-drain-2.5.1 lib/routemaster/resources/rest_resource.rb
routemaster-drain-2.5.0 lib/routemaster/resources/rest_resource.rb