<commit_before>require 'cgi'
require 'readme-score'
require 'rest'

class StatsGenerator
  include HashInit
  attr_accessor :spec, :api_json_path, :cloc_results, :readme_location, :doc_percent, :download_location, :docset_location, :testing_estimate

  def upload
    vputs "Generating the CocoaDocs stats for CP Metrics"

    cloc_sum = @cloc_results.select do |cloc|
      cloc[:lang] == "SUM"
    end.first

    unless cloc_sum
      cloc_sum = { :lang => "SUM", :files => 0, :comment => 0, :code => 0 }
    end

    data = {
      :total_files => cloc_sum[:files],
      :total_comments => cloc_sum[:comment],
      :total_lines_of_code => cloc_sum[:code],
      :doc_percent => @doc_percent,
      :testing_estimate => testing_estimate,
      :readme_complexity => readme_metadata[:complexity],
      :rendered_readme_url => spec.or_cocoadocs_url + "/README.html",
      :initial_commit_date => get_first_commit_date,
      :download_size => generated_download_size,
      :license_short => spec.or_license_name_and_url[:license],
      :license_link => spec.or_license_name_and_url[:url]
    }

    # send it to the db
    REST.post("http://cocoadocs-api.cocoapods.org/pod/#{spec.name}", data.to_json)

  end

  def generated_download_size
    `du -sk #{ @download_location }`.split("\t")[0]
  end

  def get_first_commit_date
    Dir.chdir(File.join(@download_location)) do
      return `git log --pretty=format:%ad --date=iso| tail -1`
    end
  end

  def readme_metadata<commit_msg>use new heroku address for now<commit_after>require 'cgi'
require 'readme-score'
require 'rest'

class StatsGenerator
  include HashInit
  attr_accessor :spec, :api_json_path, :cloc_results, :readme_location, :doc_percent, :download_location, :docset_location, :testing_estimate

  def upload
    vputs "Generating the CocoaDocs stats for CP Metrics"

    cloc_sum = @cloc_results.select do |cloc|
      cloc[:lang] == "SUM"
    end.first

    unless cloc_sum
      cloc_sum = { :lang => "SUM", :files => 0, :comment => 0, :code => 0 }
    end

    data = {
      :total_files => cloc_sum[:files],
      :total_comments => cloc_sum[:comment],
      :total_lines_of_code => cloc_sum[:code],
      :doc_percent => @doc_percent,
      :testing_estimate => testing_estimate,
      :readme_complexity => readme_metadata[:complexity],
      :rendered_readme_url => spec.or_cocoadocs_url + "/README.html",
      :initial_commit_date => get_first_commit_date,
      :download_size => generated_download_size,
      :license_short => spec.or_license_name_and_url[:license],
      :license_link => spec.or_license_name_and_url[:url]
    }

    # send it to the db
#    REST.post("http://cocoadocs-api.cocoapods.org/pod/#{spec.name}", data.to_json)
    REST.post("https://cocoadocs-api-cocoapods-org.herokuapp.com/pod/#{spec.name}", data.to_json)

  end

  def generated_download_size
    `du -sk #{ @download_location }`.split("\t")[0]
  end

  def get_first_commit_date
    Dir.chdir(File.join(@download_location)) do
      return `git log --pretty=format:%ad --date=iso| tail -1`
    end
  end

  def readme_metadata
{'fraction_non_alphanumeric': 0.12774193548387097, 'fraction_numerical': 0.0032258064516129032, 'mean_word_length': 3.741590214067278, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 33, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7526621', 'n_tokens_mistral': 1124, 'n_tokens_neox': 1069, 'n_words': 210}