Sha256: 886385bb5bf8c232d31df45464022dabaa0a122e17622e4ffa15477c58505ea7

Contents?: true

Size: 1.49 KB

Versions: 6

Compression:

Stored size: 1.49 KB

Contents

# frozen_string_literal: true

module RatingChgkV2
  module Endpoints
    class BaseEndpoint
      include RatingChgkV2::Request

      attr_reader :params

      def initialize(client, query_params = [], params = {})
        @instance_query = base_query.push(*query_params)
        setup client, @instance_query, params
      end

      def reinitialize(new_params: {}, add_query: [])
        add_query = [add_query] unless add_query.is_a?(Array)
        new_query = add_query&.any? ? @instance_query + add_query : @query_params
        setup @client, new_query, new_params
        self
      end

      private

      HTTP_METHODS_REGEXP = /\Ado_(get|post|put|delete)\z/.freeze

      def respond_to_missing?(method, _include_all)
        return true if HTTP_METHODS_REGEXP.match?(method.to_s)

        super
      end

      def method_missing(method, *_args)
        if method.to_s =~ HTTP_METHODS_REGEXP
          send Regexp.last_match(1), @uri, @client, @params
        else
          super
        end
      end

      def setup(client, query_params = [], params = {})
        @query_params = query_params
        @uri = partial_uri(@query_params)
        @client = client
        @params = params
      end

      def partial_uri(raw_mapping)
        template = Addressable::Template.new '{/segments*}'
        template.expand(
          segments: raw_mapping.to_a.flatten
        ).to_s
      end

      def base_query
        []
      end
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
rating-chgk-v2-1.5.0 lib/rating_chgk_v2/endpoints/base_endpoint.rb
rating-chgk-v2-1.4.0 lib/rating_chgk_v2/endpoints/base_endpoint.rb
rating-chgk-v2-1.3.0 lib/rating_chgk_v2/endpoints/base_endpoint.rb
rating-chgk-v2-1.2.0 lib/rating_chgk_v2/endpoints/base_endpoint.rb
rating-chgk-v2-1.1.0 lib/rating_chgk_v2/endpoints/base_endpoint.rb
rating-chgk-v2-1.0.1 lib/rating_chgk_v2/endpoints/base_endpoint.rb