Sha256: b11af7856916b0e3eaeb29260ca296ccc11afcc737732f6b61a8ea8b6f6b25df

Contents?: true

Size: 1.02 KB

Versions: 7

Compression:

Stored size: 1.02 KB

Contents

require "addressable/uri"

module JsonApiClient
  module Query
    class Base
      class_attribute :request_method
      attr_reader :klass, :headers, :path, :params

      def initialize(klass, args)
        @klass = klass
        build_params(args)
        @headers = klass.default_headers.dup
        build_path(@params)
      end

      def build_path(parameters)
        @path = begin
          p = klass.path(parameters)
          if parameters.has_key?(klass.primary_key) && !parameters[klass.primary_key].is_a?(Array)
            resource_id = parameters.delete(klass.primary_key).to_s
            encoded_resource_id = Addressable::URI.encode_component(resource_id, Addressable::URI::CharacterClasses::UNRESERVED)
            p = File.join(p, encoded_resource_id)
          end
          p
        end
      end

      def build_params(args)
        @params = args.dup
      end

      def inspect
        "#{self.class.name}: method: #{request_method}; path: #{path}; params: #{params}, headers: #{headers}"
      end

    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
json_api_client-0.9.6 lib/json_api_client/query/base.rb
json_api_client-0.9.5 lib/json_api_client/query/base.rb
json_api_client-0.9.4 lib/json_api_client/query/base.rb
json_api_client-0.9.3 lib/json_api_client/query/base.rb
json_api_client-0.9.2 lib/json_api_client/query/base.rb
json_api_client-0.9.0 lib/json_api_client/query/base.rb
json_api_client-0.8.1 lib/json_api_client/query/base.rb