Class: Contentful::Management::Request

Inherits:
Object
  • Object
show all
Defined in:
lib/contentful/management/request.rb

Overview

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.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client, endpoint, query = {}, id = nil, headers = {}) ⇒ Request

Returns a new instance of Request.



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# 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

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



7
8
9
# File 'lib/contentful/management/request.rb', line 7

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



7
8
9
# File 'lib/contentful/management/request.rb', line 7

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



7
8
9
# File 'lib/contentful/management/request.rb', line 7

def headers
  @headers
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/contentful/management/request.rb', line 7

def id
  @id
end

#queryObject (readonly)

Returns the value of attribute query.



7
8
9
# File 'lib/contentful/management/request.rb', line 7

def query
  @query
end

#typeObject (readonly)

Returns the value of attribute type.



7
8
9
# File 'lib/contentful/management/request.rb', line 7

def type
  @type
end

Instance Method Details

#absolute?Boolean

Returns true if endpoint is an absolute url

Returns:

  • (Boolean)


61
62
63
# File 'lib/contentful/management/request.rb', line 61

def absolute?
  @endpoint.start_with?('http')
end

#copyObject

Returns a new Request object with the same data



66
67
68
# File 'lib/contentful/management/request.rb', line 66

def copy
  self.class.new(@client, @endpoint, @query, @initial_id, @headers)
end

#deleteObject

Delegates the actual HTTP DELETE request to the client



55
56
57
# File 'lib/contentful/management/request.rb', line 55

def delete
  client.delete(self)
end

#getObject

Delegates the actual HTTP work to the client



40
41
42
# File 'lib/contentful/management/request.rb', line 40

def get
  client.get(self)
end

#postObject

Delegates the actual HTTP POST request to the client



45
46
47
# File 'lib/contentful/management/request.rb', line 45

def post
  client.post(self)
end

#putObject

Delegates the actual HTTP PUT request to the client



50
51
52
# File 'lib/contentful/management/request.rb', line 50

def put
  client.put(self)
end

#urlObject

Returns the final URL, relative to a contentful space



35
36
37
# File 'lib/contentful/management/request.rb', line 35

def url
  "#{@endpoint}#{@type == :single ? "/#{id}" : ''}"
end