Class: Jekyll::Strapi::StrapiCollection

Inherits:
Object
  • Object
show all
Defined in:
lib/jekyll/strapi/collection.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, collection_name, config) ⇒ StrapiCollection

Returns a new instance of StrapiCollection.



10
11
12
13
14
# File 'lib/jekyll/strapi/collection.rb', line 10

def initialize(site, collection_name, config)
  @site = site
  @collection_name = collection_name
  @config = config
end

Instance Attribute Details

#collection_nameObject

Returns the value of attribute collection_name.



8
9
10
# File 'lib/jekyll/strapi/collection.rb', line 8

def collection_name
  @collection_name
end

#configObject

Returns the value of attribute config.



8
9
10
# File 'lib/jekyll/strapi/collection.rb', line 8

def config
  @config
end

Instance Method Details

#eachObject



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/jekyll/strapi/collection.rb', line 20

def each
  # Initialize the HTTP query
  endpoint = URI.parse(@site.endpoint)
  uri = URI::HTTP.build({:host => endpoint.host,
                         :port => endpoint.port,
                         :path => "/#{@config['type'] || @collection_name}",
                         :query => "_limit=10000"})

  # Get entries
  response = Net::HTTP.get_response(uri)

  # Check response code
  if response.code == "200"
    result = JSON.parse(response.body, object_class: OpenStruct)
  elsif response.code == "401"
    raise "The Strapi server sent a error with the following status: #{response.code}. Please make sure you authorized the API access in the Users & Permissions section of the Strapi admin panel."
  else
    raise "The Strapi server sent a error with the following status: #{response.code}. Please make sure it is correctly running."
  end

  # Add necessary properties
  result.each do |document|
    document.type = collection_name
    document.collection = collection_name
    document.id ||= document._id
    document.url = @site.strapi_link_resolver(collection_name, document)
  end

  result.each {|x| yield(x)}
end

#generate?Boolean

Returns:

  • (Boolean)


16
17
18
# File 'lib/jekyll/strapi/collection.rb', line 16

def generate?
  @config['output'] || false
end