Sha256: 3d3179a14a62474095faaba3ce02b5a46ab00ea118616e2ff06789016e9dd567

Contents?: true

Size: 1.2 KB

Versions: 2

Compression:

Stored size: 1.2 KB

Contents

module Stir
  module Endpoints

    def self.included(base)
      base.extend(ClassMethods)
    end

    def routes(name, *args)
      endpoints = self.class.send(:endpoints)
      endpoint = nil
      endpoints.each {|x| endpoint = x[name.to_sym] if x[name.to_sym]}
      return nil if endpoint.nil?
      base_uri + endpoint.interpolate(args.first)
    end

    private
    module ClassMethods
      def get(name, &block)
        endpoint(name, :get, &block)
      end

      def put(name, &block)
        endpoint(name, :put, &block)
      end

      def post(name, &block)
        endpoint(name, :post, &block)
      end

      def head(name, &block)
        endpoint(name, :head, &block)
      end

      def delete(name, &block)
        endpoint(name, :delete, &block)
      end
      
      def patch(name, &block)
        endpoint(name, :patch, &block)
      end

      private
      def endpoint(name, method, &block)
        send(:define_method, name) do |*args|
          @response = HTTParty.send(method, yield.interpolate(args.first), merge_configs(args.flatten.first))
        end
        endpoints.push({name => yield.to_s})
      end

      def endpoints
        @endpoints ||= []
      end
    end

  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
stir-2.2.3 lib/stir/rest/endpoints.rb
stir-2.2.2 lib/stir/rest/endpoints.rb