Sha256: 6ba73e85f299c8d950b5a6a960c8469c4c4609e7a429da37af21981ef25f4d51

Contents?: true

Size: 1.45 KB

Versions: 6

Compression:

Stored size: 1.45 KB

Contents

module DisqusApi
  class Request
    attr_reader :api, :path, :namespace, :action, :arguments, :type

    # @param [DisqusApi::Api] api
    # @param [String] namespace
    # @param [String] action
    # @param [Hash] arguments Request parameters
    def initialize(api, namespace, action, arguments = {})
      @api = api
      @namespace = namespace.to_s
      @action = action.to_s
      @path = "#@namespace/#@action.json"

      # Request type GET or POST
      namespace_specification = @api.specifications[@namespace] or raise(ArgumentError, "No such namespace: #@namespace")
      @type = namespace_specification[@action].try(:to_sym) or raise(ArgumentError, "No such API path: #@path")

      # Set request parameters
      @arguments = arguments
    end

    # Returns Disqus API response proxy
    # @param [Symbol] request_type
    # @param [Hash] arguments
    # @return [String]
    def response(arguments = {})
      Response.new(self, arguments)
    end

    # Returns plain JSON response received from Disqus
    # @param [Hash] arguments
    # @return [String]
    def perform(arguments = {})
      case type.to_sym
      when :post, :get
        api.public_send(type, path, @arguments.merge(arguments))
      else
        raise ArgumentError, "Unregistered request type #{request_type}"
      end
    end

    # @see #initialize
    # @param [String, Symbol] request_type
    # @return [Hash]
    def self.perform(*args)
      self.new(*args).perform
    end
  end
end

Version data entries

6 entries across 6 versions & 1 rubygems

Version Path
disqus_api-0.0.8 lib/disqus_api/request.rb
disqus_api-0.0.7 lib/disqus_api/request.rb
disqus_api-0.0.6 lib/disqus_api/request.rb
disqus_api-0.0.5 lib/disqus_api/request.rb
disqus_api-0.0.4 lib/disqus_api/request.rb
disqus_api-0.0.3 lib/disqus_api/request.rb