Sha256: 4e3f84b59a5f902055049ee21048682547bf263cce4ebf5ae38c97e18b4d2387

Contents?: true

Size: 1.3 KB

Versions: 21

Compression:

Stored size: 1.3 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.post(expanded_url, body: params)
      end

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

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

      def update(id=nil, params)
        @client.patch(expanded_url(id: id), body: params)
      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

21 entries across 21 versions & 1 rubygems

Version Path
routemaster-drain-3.7.1 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.7.0 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.8 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.7 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.6 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.5 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.4 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.3 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.2 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.1 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.6.0 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.5.1 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.5.0 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.4.0 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.3.0 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.2.0 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.1.0 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.0.3 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.0.2 lib/routemaster/resources/rest_resource.rb
routemaster-drain-3.0.1 lib/routemaster/resources/rest_resource.rb