class Contentful::Management::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]
headers[R]
id[R]
query[R]
type[R]

Public Class Methods

new(client, endpoint, query = {}, id = nil, headers = {}) click to toggle source
# File lib/contentful/management/request.rb, line 9
def initialize(client, endpoint, query = {}, id = nil, headers = {})
  @headers = headers
  @initial_id = id
  @client = client
  @client.version = headers[:version]
  @client.organization_id = headers[:organization_id]
  @client.content_type_id = headers[:content_type_id]
  @endpoint = endpoint

  case query
  when Hash
    @query = normalize_query(query) if query && !query.empty?
  else
    @query = query
  end

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

Public Instance Methods

absolute?() click to toggle source

Returns true if endpoint is an absolute url @return [Boolean]

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

Returns a new Request object with the same data

# File lib/contentful/management/request.rb, line 66
def copy
  self.class.new(@client, @endpoint, @query, @initial_id, @headers)
end
delete() click to toggle source

Delegates the actual HTTP DELETE request to the client

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

Delegates the actual HTTP work to the client

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

Delegates the actual HTTP POST request to the client

# File lib/contentful/management/request.rb, line 45
def post
  client.post(self)
end
put() click to toggle source

Delegates the actual HTTP PUT request to the client

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

Returns the final URL, relative to a contentful space

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

Private Instance Methods

normalize_query(query) click to toggle source
# File lib/contentful/management/request.rb, line 72
def normalize_query(query)
  Hash[
    query.map do |key, value|
      [
        key.to_sym,
        value
      ]
    end
  ]
end