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(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
29
30
31
# File 'lib/contentful/management/request.rb', line 9

def initialize(endpoint, query = {}, id = nil, header = {})
  @header = header
  @initial_id = id
  @client = Contentful::Management::Client.shared_instance
  @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
  @absolute = true if @endpoint.start_with?('http')

  @query = if query && !query.empty?
             normalize_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

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


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

def absolute?
  !!@absolute
end

#copyObject

Returns a new Request object with the same data



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

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

#deleteObject



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

def delete
  client.delete(self)
end

#getObject

Delegates the actual HTTP work to the client



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

def get
  client.get(self)
end

#postObject

Delegates the actual HTTP POST request to the client



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

def post
  client.post(self)
end

#putObject

Delegates the actual HTTP PUT request to the client



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

def put
  client.put(self)
end

#urlObject

Returns the final URL, relative to a contentful space



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

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