class Contentful::Management::ResourceRequester

Generic Resource Request Class @private

Attributes

client[R]
resource_class[R]

Public Class Methods

new(client, resource_class) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 8
def initialize(client, resource_class)
  @client = client
  @resource_class = resource_class
end

Public Instance Methods

all(endpoint_options = {}, query = {}, headers = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 13
def all(endpoint_options = {}, query = {}, headers = {})
  query = resource_class.pre_process_params(query)
  get(endpoint_options, query, headers)
end
archive(object, endpoint_options = {}, headers = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 45
def archive(object, endpoint_options = {}, headers = {})
  update(object, endpoint_options, {}, headers)
end
Also aliased as: publish
create(endpoint_options = {}, attributes = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 22
def create(endpoint_options = {}, attributes = {})
  custom_id = attributes.is_a?(Hash) ? attributes[:id] : nil
  request = Request.new(
    client,
    resource_class.build_endpoint(endpoint_options),
    resource_class.create_attributes(client, attributes.clone),
    nil,
    resource_class.create_headers(client, attributes)
  )
  response = custom_id.nil? ? request.post : request.put
  resource = ResourceBuilder.new(response, client).run
  resource.after_create(attributes) if resource_class?(resource)
  resource
end
destroy(endpoint_options = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 41
def destroy(endpoint_options = {})
  delete(endpoint_options)
end
find(endpoint_options = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 18
def find(endpoint_options = {})
  get(endpoint_options)
end
publish(object, endpoint_options = {}, headers = {})
Alias for: archive
unarchive(object, endpoint_options = {}, headers = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 50
def unarchive(object, endpoint_options = {}, headers = {})
  object.refresh_data(delete(endpoint_options, {}, headers))
end
Also aliased as: unpublish
unpublish(object, endpoint_options = {}, headers = {})
Alias for: unarchive
update(object, endpoint_options = {}, attributes = {}, headers = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 37
def update(object, endpoint_options = {}, attributes = {}, headers = {})
  object.refresh_data(put(endpoint_options, attributes, headers, object))
end

Private Instance Methods

delete(endpoint_options = {}, attributes = {}, headers = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 86
def delete(endpoint_options = {}, attributes = {}, headers = {})
  request = Request.new(
    client,
    resource_class.build_endpoint(endpoint_options),
    attributes,
    nil,
    headers
  )
  response = request.delete
  return true if response.status == :no_content
  ResourceBuilder.new(response, client).run
end
get(endpoint_options = {}, query = {}, headers = {}) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 63
def get(endpoint_options = {}, query = {}, headers = {})
  request = Request.new(
    client,
    resource_class.build_endpoint(endpoint_options),
    query,
    nil,
    headers
  )
  ResourceBuilder.new(request.get, client).run
end
put(endpoint_options = {}, attributes = {}, headers = {}, object = nil) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 74
def put(endpoint_options = {}, attributes = {}, headers = {}, object = nil)
  is_update = !object.nil? && object.id
  request = Request.new(
    client,
    resource_class.build_endpoint(endpoint_options),
    attributes,
    nil,
    headers
  )
  ResourceBuilder.new(is_update ? request.put : request.post, client).run
end
resource_class?(object) click to toggle source
# File lib/contentful/management/resource_requester.rb, line 57
def resource_class?(object)
  object.resource?
rescue
  false
end