Sha256: a00310860af19be702b182449c1c61fa0a81cebe0ee5d196e953c54fe9dc4e8e

Contents?: true

Size: 1.46 KB

Versions: 5

Compression:

Stored size: 1.46 KB

Contents

require 'forwardable'

module ServiceContract
  module Avro
    class Endpoint < AbstractEndpoint
      extend Forwardable
      def_delegators :definition, :name, :response, :request

      def description
        [request_method, path].join(" ")
      end

      def doc
        definition.respond_to?(:doc) ? definition.doc : nil
      end

      def response_type
        Type.build(response)
      end

      def parameters
        request.fields.map{|field| Parameter.new(field) }
      end

      protected

      def request_method
        #Check for [<METHOD>] at the front of the doc string. this signals an action override
        if doc =~ /^\[([A-Z]+)\].+$/
          return $1
        end

        case name
        when "create"
          "POST"
        when "update"
          "PUT"
        when "destroy"
          "DELETE"
        else
          "GET"
        end
      end

      def path
        case name
        when "index", "create"
          protocol.path
        when "show", "destroy", "update"
          File.join(protocol.path, ":id")
        else
          if member?
            File.join(protocol.path, ":id", name)
          else
            File.join(protocol.path, name)
          end
        end
      end

      # seems kinda hacky
      def member?
        return false if parameters.empty?

        first_param_type = parameters.first.type
        first_param_type.is_a?(RecordType) && first_param_type.name == protocol.main_type
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
service_contract-0.6.0 lib/service_contract/avro/endpoint.rb
service_contract-0.5.1 lib/service_contract/avro/endpoint.rb
service_contract-0.5.0 lib/service_contract/avro/endpoint.rb
service_contract-0.4.1 lib/service_contract/avro/endpoint.rb
service_contract-0.4.0 lib/service_contract/avro/endpoint.rb