<commit_before># ------------------------------------------------
# [DONE] STAGE 1: FETCH JSON AND OUTPUT ON COMMAND LINE
# [DONE] STAGE 2: GENERATE PDF FILE
# STAGE 3: FORMAT OUTPUTTED PDF
# ------------------------------------------------
# Call: ruby ohana-pdf.rb ORGANIZATION_ID
# i.e : ruby ohana-pdf.rb 51a9fd0328217f89770001b2
# ------------------------------------------------
# TODOS
# Remove special character from PDF filename
# ------------------------------------------------

require 'httparty'
require 'active_support'
require 'prawn'

API_URL = "http://ohanapi.herokuapp.com/api/organizations/"

def pdf_body_print(obj)
  output = ""

  # FIELD VALUE CAN BE A STRING, A HASH, AN ARRAY 
  # OF STRING, HASH, OR OF ANOTHER ARRAY. 
  if obj.class == Array
    obj.each_with_index do |ele, index|
      output << pdf_body_print(ele)
      output << ", " if (index != obj.length-1)
    end
  elsif obj.class == Hash
    obj.each_with_index do |(key, value), index|
      if value.class == Array
        output << "#{ActiveSupport::Inflector.humanize(key)}: "
        output << pdf_body_print(value)
      else
        output << "#{ActiveSupport::Inflector.humanize(key)}: #{value}"
      end
      output << ", " if (index != obj.length-1)
    end
  else
    output << obj.to_s
  end

  return output
end

# ------------------------------------------------
# ---------------------- MAIN --------------------
# ------------------------------------------------

# FETCH JSON
@fetch = HTTParty.get("#{API_URL}#{ARGV[0]}")

# GENERATE PDF
Prawn::Document.generate("#{@fetch["response"]["name"] || ARGV[0]}.pdf") do |pdf|

  # PDF headings
  pdf.pad(20) { 
    pdf.text "Code for America: Human Services Finder", size: 20, align: :center, style: :bold
    pdf.text "#{@fetch["response"]["name"] || ARGV[0]}", size: 14, align: :center, style: :bold
  }

  # PDF body
  @fetch["response"].each do |key, value|
    if (!value.nil? && key != "_id")
      pdf.pad_bottom(5) { pdf.text "#{ActiveSupport::Inflector.humanize(key).upcase}:", 
        style: :bold, size: 9 }
      pdf.pad_bottom(20) { pdf.text pdf_body_print(value), size: 10, align: :justify }
    end
  end
end<commit_msg>Stage 2 -- todo: Strip out non-ascii characters and stops<commit_after># ------------------------------------------------
# [DONE] STAGE 1: FETCH JSON AND OUTPUT ON COMMAND LINE
# [DONE] STAGE 2: GENERATE PDF FILE
# STAGE 3: FORMAT OUTPUTTED PDF
# ------------------------------------------------
# Call: ruby ohana-pdf.rb ORGANIZATION_ID
# i.e : ruby ohana-pdf.rb 51a9fd0328217f89770001b2
# ------------------------------------------------
# TODOS
# Yippie none!
# ------------------------------------------------

require 'httparty'
require 'active_support'
require 'prawn'

API_URL = "http://ohanapi.herokuapp.com/api/organizations/"

def pdf_body_print(obj)
  output = ""

  # FIELD VALUE CAN BE A STRING, A HASH, AN ARRAY 
  # OF STRING, HASH, OR OF ANOTHER ARRAY. 
  if obj.class == Array
    obj.each_with_index do |ele, index|
      output << pdf_body_print(ele)
      output << ", " if (index != obj.length-1)
    end
  elsif obj.class == Hash
    obj.each_with_index do |(key, value), index|
      if value.class == Array
        output << "#{ActiveSupport::Inflector.humanize(key)}: "
        output << pdf_body_print(value)
      else
        output << "#{ActiveSupport::Inflector.humanize(key)}: #{value}"
      end
      output << ", " if (index != obj.length-1)
    end
  else
    output << obj.to_s
  end

  return output
end

# ------------------------------------------------
# ---------------------- MAIN --------------------
# ------------------------------------------------

# FETCH JSON
@fetch = HTTParty.get("#{API_URL}#{ARGV[0]}")

# GENERATE PDF
# Strip out non-ascii characters and stops
@safe_file_name = @fetch["response"]["name"].gsub(/[^0-9A-Za-z.\-]/, '_').gsub('.','') || ARGV[0]
Prawn::Document.generate("#{@safe_file_name}.pdf") do |pdf|

  # PDF headings
  pdf.pad(20) { 
    pdf.text "Code for America: Human Services Finder", size: 20, align: :center, style: :bold
    pdf.text "#{@fetch["response"]["name"] || ARGV[0]}", size: 14, align: :center, style: :bold
  }

  # PDF body
  @fetch["response"].each do |key, value|
    if (!value.nil? && key != "_id")
      pdf.pad_bottom(5) { pdf.text "#{ActiveSupport::Inflector.humanize(key).upcase}:", 
        style: :bold, size: 9 }
      pdf.pad_bottom(20) { pdf.text pdf_body_print(value), size: 10, align: :justify }
    end
  end
end
{'fraction_non_alphanumeric': 0.2816589044133954, 'fraction_numerical': 0.017964071856287425, 'mean_word_length': 1.9096774193548387, 'pattern_counts': {'":': 0, '<': 31, '<?xml version=': 0, '>': 3, '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': '4172049', 'n_tokens_mistral': 1506, 'n_tokens_neox': 1430, 'n_words': 360}