class Jekyll::Strapi::StrapiCollection

Attributes

collection_name[RW]
config[RW]

Public Class Methods

new(site, collection_name, config) click to toggle source
# File lib/jekyll/strapi/collection.rb, line 10
def initialize(site, collection_name, config)
  @site = site
  @collection_name = collection_name
  @config = config
end

Public Instance Methods

each() { |x| ... } click to toggle source
# File lib/jekyll/strapi/collection.rb, line 20
def each
  # Initialize the HTTP query
  path = "/#{@config['type'] || @collection_name}"
  uri = URI("#{@site.endpoint}#{path}")
  if @config['query']
    uri += @config['query']
  end
  Jekyll.logger.info "Jekyll Strapi:", "Fetching entries from #{uri}"
  # 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?() click to toggle source
# File lib/jekyll/strapi/collection.rb, line 16
def generate?
  @config['output'] || false
end