Sha256: c74ce619f524d86b1d0e8149eb4b114c7a5a7933af6a3b3727e588b2e259d5c3

Contents?: true

Size: 1.18 KB

Versions: 1

Compression:

Stored size: 1.18 KB

Contents

module Contentful
  # This object represents a request that is to be made. It gets initialized by the client
  # with domain specific logic. The client later uses the Request's #url and #query methods
  # to execute the HTTP request.
  class Request
    attr_reader :client, :type, :query, :id

    def initialize(client, endpoint, query = {}, id = nil)
      @client = client
      @endpoint = endpoint
      @query = if query && !query.empty?
        normalize_query(query)
      end

      if id
        @type = :single
        @id = URI.escape(id)
      else
        @type = :multi
        @id = nil
      end
    end

    # Returns the final URL, relative to a contentful space
    def url
      "#{@endpoint}#{ @type == :single ? "/#{id}" : '' }"
    end

    # Delegates the actual HTTP work to the client
    def get
      client.get(self)
    end

    # Returns a new Request object with the same data
    def copy
      Marshal.load(Marshal.dump(self))
    end


    private

    def normalize_query(query)
      Hash[
        query.map do |key, value|
          [
            key.to_sym,
            value.is_a?(::Array) ? value.join(',') : value
          ]
        end
      ]
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
contentful-0.2.0 lib/contentful/request.rb