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, header = {}) ⇒ 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
# File 'lib/contentful/management/request.rb', line 9

def initialize(client, endpoint, query = {}, id = nil, header = {})
  @header = header
  @initial_id = id
  @client = client
  @client.version = header[:version]
  @client.organization_id = header[:organization_id]
  @client.content_type_id = header[:content_type_id]
  @client.zero_length = query.empty?
  @endpoint = endpoint

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

  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

#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)


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

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

#copyObject

Returns a new Request object with the same data



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

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

#deleteObject

Delegates the actual HTTP DELETE request to the client



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

def delete
  client.delete(self)
end

#getObject

Delegates the actual HTTP work to the client



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

def get
  client.get(self)
end

#postObject

Delegates the actual HTTP POST request to the client



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

def post
  client.post(self)
end

#putObject

Delegates the actual HTTP PUT request to the client



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

def put
  client.put(self)
end

#urlObject

Returns the final URL, relative to a contentful space



31
32
33
# File 'lib/contentful/management/request.rb', line 31

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