class Contentful::Request

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.

Attributes

client[R]
endpoint[R]
id[R]
query[R]
type[R]

Public Class Methods

new(client, endpoint, query = {}, id = nil) click to toggle source
# File lib/contentful/request.rb, line 8
def initialize(client, endpoint, query = {}, id = nil)
  @client = client
  @endpoint = endpoint

  @query = (normalize_query(query) if query && !query.empty?)

  if id
    @type = :single
    # Given the deprecation of `URI::escape` and `URI::encode`
    # it is needed to replace it with `URI::encode_www_form_component`.
    # This method, does replace spaces with `+` instead of `%20`.
    # Therefore, to keep backwards compatibility, we're replacing the resulting `+`
    # back with `%20`.
    @id = URI.encode_www_form_component(id).gsub('+', '%20')
  else
    @type = :multi
    @id = nil
  end
end

Public Instance Methods

absolute?() click to toggle source

Returns true if endpoint is an absolute url

# File lib/contentful/request.rb, line 39
def absolute?
  @endpoint.start_with?('http')
end
copy() click to toggle source

Returns a new Request object with the same data

# File lib/contentful/request.rb, line 44
def copy
  Marshal.load(Marshal.dump(self))
end
get() click to toggle source

Delegates the actual HTTP work to the client

# File lib/contentful/request.rb, line 34
def get
  client.get(self)
end
url() click to toggle source

Returns the final URL, relative to a contentful space

# File lib/contentful/request.rb, line 29
def url
  "#{@endpoint}#{@type == :single ? "/#{id}" : ''}"
end

Private Instance Methods

normalize_query(query) click to toggle source
# File lib/contentful/request.rb, line 50
def normalize_query(query)
  Hash[
    query.map do |key, value|
      [
        key.to_sym,
        value.is_a?(::Array) ? value.join(',') : value
      ]
    end
  ]
end