<commit_before>module API
  module V1
    class Articles < Grape::API
      before { detect_stuff_by_domain }
      before { authenticate! }

      resource :articles do

        # Collect comments from articles
        #
        # Parameters:
        #   from             - date where the search will begin. If nothing is passed the default date will be the date of the first article created
        #   oldest           - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
        #   limit            - amount of comments returned. The default value is 20
        #
        # Example Request:
        #  GET /articles?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10&content_type=Hub
#    desc 'Articles.', {
#      :params => API::Entities::Article.documentation
#    }
        get do
          articles = select_filtered_collection_of(environment, 'articles', params)
          present articles, :with => Entities::Article 
        end

        desc "Return the article id"
        get ':id' do
          present environment.articles.find(params[:id]), :with => Entities::Article
        end

        get ':id/children' do

          conditions = make_conditions_with_parameter(params)
          if params[:reference_id]
            articles = environment.articles.find(params[:id]).children.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
          else
            articles = environment.articles.find(params[:id]).children.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
          end
          present articles, :with => Entities::Article
        end

        get ':id/children/:child_id' do
          present environment.articles.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article
        end


      end

      resource :communities do 
        segment '/:community_id' do 
          resource :articles do
            get do
              community = environment.communities.find(params[:community_id])
              articles = select_filtered_collection_of(community, 'articles', params)
              present articles, :with => Entities::Article 
            end

            get '/:id' do
              community = environment.communities.find(params[:community_id])
              present community.articles.find(params[:id]), :with => Entities::Article
            end

            # Example Request:
            #  POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body
            post do
              community = environment.communities.find(params[:community_id])
              article = community.articles.build(params[:article].merge(:last_changed_by => current_person))
              article.type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]
              if !article.save
                render_api_errors!(article.errors.full_messages)
              end
              present article, :with => Entities::Article
            end

          end
        end

      end
   
    end
  end
end<commit_msg>refactoring article creation<commit_after>module API
  module V1
    class Articles < Grape::API
      before { detect_stuff_by_domain }
      before { authenticate! }

      resource :articles do

        # Collect comments from articles
        #
        # Parameters:
        #   from             - date where the search will begin. If nothing is passed the default date will be the date of the first article created
        #   oldest           - Collect the oldest comments from reference_id comment. If nothing is passed the newest comments are collected
        #   limit            - amount of comments returned. The default value is 20
        #
        # Example Request:
        #  GET /articles?from=2013-04-04-14:41:43&until=2014-04-04-14:41:43&limit=10&content_type=Hub
#    desc 'Articles.', {
#      :params => API::Entities::Article.documentation
#    }
        get do
          articles = select_filtered_collection_of(environment, 'articles', params)
          present articles, :with => Entities::Article 
        end

        desc "Return the article id"
        get ':id' do
          present environment.articles.find(params[:id]), :with => Entities::Article
        end

        get ':id/children' do

          conditions = make_conditions_with_parameter(params)
          if params[:reference_id]
            articles = environment.articles.find(params[:id]).children.send("#{params.key?(:oldest) ? 'older_than' : 'newer_than'}", params[:reference_id]).find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
          else
            articles = environment.articles.find(params[:id]).children.find(:all, :conditions => conditions, :limit => limit, :order => "created_at DESC")
          end
          present articles, :with => Entities::Article
        end

        get ':id/children/:child_id' do
          present environment.articles.find(params[:id]).children.find(params[:child_id]), :with => Entities::Article
        end


      end

      resource :communities do 
        segment '/:community_id' do 
          resource :articles do
            get do
              community = environment.communities.find(params[:community_id])
              articles = select_filtered_collection_of(community, 'articles', params)
              present articles, :with => Entities::Article 
            end

            get '/:id' do
              community = environment.communities.find(params[:community_id])
              present community.articles.find(params[:id]), :with => Entities::Article
            end

            # Example Request:
            #  POST api/v1/communites/:community_id/articles?private_toke=234298743290432&article[name]=title&article[body]=body
            post do
              community = environment.communities.find(params[:community_id])
              klass_type= params[:content_type].nil? ? 'TinyMceArticle' : params[:content_type]
              article = klass_type.constantize.new(params[:article])
              article.last_changed_by = current_person
              article.created_by= current_person
              article.profile = community

              if !article.save
                render_api_errors!(article.errors.full_messages)
              end
              present article, :with => Entities::Article
            end

          end
        end

      end
   
    end
  end
end
{'fraction_non_alphanumeric': 0.1003309265944645, 'fraction_numerical': 0.014741275571600482, 'mean_word_length': 2.2545276554087126, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 32, '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': '5734405', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1672, 'n_words': 420}