Class: GatherContent::Api::Items

Inherits:
Base
  • Object
show all
Includes:
Enumerable
Defined in:
lib/gather_content/api/items.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods inherited from Base

#fetch, #get, #post, #post_json, #reset

Constructor Details

#initialize(project_id) ⇒ Items

Returns a new instance of Items.

Raises:

  • (ArgumentError)


9
10
11
12
# File 'lib/gather_content/api/items.rb', line 9

def initialize(project_id)
  raise ArgumentError, "Project_id is required!" if project_id.nil?
  @project_id = project_id
end

Instance Attribute Details

#project_idObject

Returns the value of attribute project_id.



7
8
9
# File 'lib/gather_content/api/items.rb', line 7

def project_id
  @project_id
end

Instance Method Details

#create(data) ⇒ Object

Raises:

  • (ArgumentError)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/gather_content/api/items.rb', line 20

def create(data)
  data.delete("parent_id") if data["parent_id"].nil? || data["parent_id"].empty?
  data.delete("template_id") if data["template_id"].nil? || data["template_id"].empty?

  config = data.delete("config")
  data["config"] = Base64.strict_encode64(config.to_json) unless config.nil? || config.empty?

  raise ArgumentError, "name is required!" if data["name"].nil? || data["name"].empty?

  result = post_json(data.merge({ 'project_id' => @project_id }))

  if result.status == 202
    item_id = result.headers['location'].split('/').last
    GatherContent::Api::Item.new(item_id)
  else
    raise GatherContent::Error::RequestError.new(result)
  end
end

#each(&block) ⇒ Object



14
15
16
17
18
# File 'lib/gather_content/api/items.rb', line 14

def each(&block)
  fetch.each do |item|
    yield GatherContent::Api::Item.new(item['id'], item)
  end
end