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.



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/contentful/management/request.rb', line 11

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.encode_www_form_component(id)
  else
    @type = :multi
    @id = nil
  end
end

Instance Attribute Details

#clientObject (readonly)

Returns the value of attribute client.



9
10
11
# File 'lib/contentful/management/request.rb', line 9

def client
  @client
end

#endpointObject (readonly)

Returns the value of attribute endpoint.



9
10
11
# File 'lib/contentful/management/request.rb', line 9

def endpoint
  @endpoint
end

#headersObject (readonly)

Returns the value of attribute headers.



9
10
11
# File 'lib/contentful/management/request.rb', line 9

def headers
  @headers
end

#idObject (readonly)

Returns the value of attribute id.



9
10
11
# File 'lib/contentful/management/request.rb', line 9

def id
  @id
end

#queryObject (readonly)

Returns the value of attribute query.



9
10
11
# File 'lib/contentful/management/request.rb', line 9

def query
  @query
end

#typeObject (readonly)

Returns the value of attribute type.



9
10
11
# File 'lib/contentful/management/request.rb', line 9

def type
  @type
end

Instance Method Details

#absolute?Boolean

Returns true if endpoint is an absolute url

Returns:

  • (Boolean)


63
64
65
# File 'lib/contentful/management/request.rb', line 63

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

#copyObject

Returns a new Request object with the same data



68
69
70
# File 'lib/contentful/management/request.rb', line 68

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

#deleteObject

Delegates the actual HTTP DELETE request to the client



57
58
59
# File 'lib/contentful/management/request.rb', line 57

def delete
  client.delete(self)
end

#getObject

Delegates the actual HTTP work to the client



42
43
44
# File 'lib/contentful/management/request.rb', line 42

def get
  client.get(self)
end

#postObject

Delegates the actual HTTP POST request to the client



47
48
49
# File 'lib/contentful/management/request.rb', line 47

def post
  client.post(self)
end

#putObject

Delegates the actual HTTP PUT request to the client



52
53
54
# File 'lib/contentful/management/request.rb', line 52

def put
  client.put(self)
end

#urlObject

Returns the final URL, relative to a contentful space



37
38
39
# File 'lib/contentful/management/request.rb', line 37

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