<commit_before>require 'api/generator'

class PublicationsController < ApplicationController
  caches_page :index
  respond_to :json, :html
  
  def show
    section_name = Publication::SECTIONS.detect { |s| s.parameterize.to_s == params[:id] }
    audience_name = Publication::AUDIENCES.detect { |s| s.parameterize.to_s == params[:id] }
    
    if section_name
      data = show_collection('section', section_name) and return if section_name
    elsif audience_name
      data = show_collection('audience', audience_name) and return 
    else
      data = show_publication(params[:id], params[:edition], params[:snac])
    end
    
    if data
      respond_with data
    else
      head 404
    end
  end

  def show_collection(type, name)
    if type == 'section'
      publications = Publication.where(section: name).collect(&:published_edition).compact
    else
      publications = Publication.any_in(audiences: [params[:id]]).collect(&:published_edition).compact
    end
      
    publications = publications.to_a.collect do |g|
      {
        :title => g.title,
        :tags => g.container.tags,
        :slug => g.container.slug,
        :type => g.class.underscore
      }
    end
    
    return { :name => name, :type => type, :publications => publications }
  end

  def show_publication(slug, edition, snac)
    publication = Publication.where(slug: slug).first
    return nil if publication.nil?
    
    if edition
      # This is used for previewing yet-to-be-published editions. 
      # At some point this should require special authentication.
      edition = publication.editions.select { |e| e.version_number.to_i == edition.to_i }.first
    else
      edition = publication.published_edition
    end
    return nil if edition.nil?

    options = {}
    allowed_options = [:snac,:all]
    allowed_options.each do |a|
      options[a] = params[a] if params[a]
    end

    Api::Generator.edition_to_hash(edition, options)
  end

  def index

    published_editions = Publication.published.collect(&:published_edition).compact
    details = published_editions.collect do |g|
      {
        :title => g.title,
        :tags => g.container.tags,
        :url => publication_front_end_path(g.container)
      }
    end
    if params[:callback]  
      render :json=> details.to_json, :callback => params[:callback]
    else
      respond_with details 
    end
  end

  protected
  # TODO - find a better way to do this. cf. Admin::GuidesHelper
  def publication_front_end_path(publication)
    if publication.is_a?(Place)
      "/places/#{publication.slug}"
    else
      "/#{publication.slug}"
    end
  end

end
<commit_msg>Remove stray 'and return' statements that were breaking the API<commit_after>require 'api/generator'

class PublicationsController < ApplicationController
  caches_page :index
  respond_to :json, :html
  
  def show
    section_name = Publication::SECTIONS.detect { |s| s.parameterize.to_s == params[:id] }
    audience_name = Publication::AUDIENCES.detect { |s| s.parameterize.to_s == params[:id] }

    if section_name
      data = show_collection('section', section_name)
    elsif audience_name
      data = show_collection('audience', audience_name)
    else
      data = show_publication(params[:id], params[:edition], params[:snac])
    end
    
    if data
      respond_with(data)
    else
      head 404 and return
    end
  end

  def show_collection(type, name)
    if type == 'section'
      publications = Publication.where(section: name).collect(&:published_edition).compact
    else
      publications = Publication.any_in(audiences: [params[:id]]).collect(&:published_edition).compact
    end
      
    publications = publications.to_a.collect do |g|
      {
        :title => g.title,
        :tags => g.container.tags,
        :slug => g.container.slug,
        :type => g.class.underscore
      }
    end
    
    return { :name => name, :type => type, :publications => publications }
  end

  def show_publication(slug, edition, snac)
    publication = Publication.where(slug: slug).first
    return nil if publication.nil?
    
    if edition
      # This is used for previewing yet-to-be-published editions. 
      # At some point this should require special authentication.
      edition = publication.editions.select { |e| e.version_number.to_i == edition.to_i }.first
    else
      edition = publication.published_edition
    end
    return nil if edition.nil?

    options = {}
    allowed_options = [:snac,:all]
    allowed_options.each do |a|
      options[a] = params[a] if params[a]
    end

    Api::Generator.edition_to_hash(edition, options)
  end

  def index

    published_editions = Publication.published.collect(&:published_edition).compact
    details = published_editions.collect do |g|
      {
        :title => g.title,
        :tags => g.container.tags,
        :url => publication_front_end_path(g.container)
      }
    end
    if params[:callback]  
      render :json=> details.to_json, :callback => params[:callback]
    else
      respond_with details 
    end
  end

  protected
  # TODO - find a better way to do this. cf. Admin::GuidesHelper
  def publication_front_end_path(publication)
    if publication.is_a?(Place)
      "/places/#{publication.slug}"
    else
      "/#{publication.slug}"
    end
  end

end

{'fraction_non_alphanumeric': 0.10457024637953734, 'fraction_numerical': 0.0011284558961820576, 'mean_word_length': 2.876093294460641, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7138548', 'n_tokens_mistral': 1639, 'n_tokens_neox': 1611, 'n_words': 388}