<commit_before># encoding: UTF-8
require 'rubygems'
require 'bundler/setup'
$LOAD_PATH << File.dirname(__FILE__) + '/lib'
require 'sinatra_env'

set :public_folder, File.dirname(__FILE__) + '/public'
set :root, File.dirname(__FILE__)
enable :logging

use Rack::Session::Cookie, :key => 'kindleizer.rack.session',
:path => '/',
:expire_after => 6592000, # In seconds
:secret => "update_secret_#{ENV['MAILGUN_API_KEY']}"

configure :development do
  require "better_errors"
  use BetterErrors::Middleware
  BetterErrors.application_root = File.dirname(__FILE__)
end

configure :production do
  require 'newrelic_rpm'
  Airbrake.configure do |config|
    config.api_key = ENV['B2B_ERRBIT_API_KEY']
    config.host    = ENV['ERRBIT_HOST']
    config.port    = 80
    config.secure  = config.port == 443
  end
  use Rack::Catcher
  use Airbrake::Rack
  set :raise_errors, true
end

helpers do

  def possilbe_url_value
    if url = params['url']
      "value = '#{url}'"
    else
      ''
    end
  end

  def possilbe_content_value
    if @content
      @content
    elsif content = params['content']
      content
    else
      ''
    end
  end

end

before /.*/ do
  if request.host.match(/herokuapp.com/)
    redirect request.url.gsub("herokuapp.com",'picoappz.com'), 301
  end
end

def self.get_or_post(url,&block)
  get(url,&block)
  post(url,&block)
end

#this allows preview of git books to work
get "/tmp_images/:file" do |file|
  send_file File.join("./tmp/git_book/images/", file)
end

get_or_post '/' do
  @usage = UsageCount.usage_remaining
  erb :index
end

def fix_title(title)
  title    = content.split("\n").first
  title    = title.gsub(/\(.*/,'').gsub(/http.*/,'')
  title.match(/[\w\s-]*/)[0]
end
  
get_or_post '/kindleizecontent' do
  verify_content_and_email
  verify_usage
  content  = params['content']
  title    = fix_title(title)
  to_email = user_email

  if pubish_request?
    book = BookFormatter.new(title, content)
    BookDelivery.email_book_to_kindle(book, to_email)
    success_response('Your content is being emailed to your kindle shortly.')
  else
    @content = content
    render_preview(title,content)
  end
end

get_or_post '/kindleize' do
  begin
    if params['url'].nil?<commit_msg>blah in a rush via ssh term fix<commit_after># encoding: UTF-8
require 'rubygems'
require 'bundler/setup'
$LOAD_PATH << File.dirname(__FILE__) + '/lib'
require 'sinatra_env'

set :public_folder, File.dirname(__FILE__) + '/public'
set :root, File.dirname(__FILE__)
enable :logging

use Rack::Session::Cookie, :key => 'kindleizer.rack.session',
:path => '/',
:expire_after => 6592000, # In seconds
:secret => "update_secret_#{ENV['MAILGUN_API_KEY']}"

configure :development do
  require "better_errors"
  use BetterErrors::Middleware
  BetterErrors.application_root = File.dirname(__FILE__)
end

configure :production do
  require 'newrelic_rpm'
  Airbrake.configure do |config|
    config.api_key = ENV['B2B_ERRBIT_API_KEY']
    config.host    = ENV['ERRBIT_HOST']
    config.port    = 80
    config.secure  = config.port == 443
  end
  use Rack::Catcher
  use Airbrake::Rack
  set :raise_errors, true
end

helpers do

  def possilbe_url_value
    if url = params['url']
      "value = '#{url}'"
    else
      ''
    end
  end

  def possilbe_content_value
    if @content
      @content
    elsif content = params['content']
      content
    else
      ''
    end
  end

end

before /.*/ do
  if request.host.match(/herokuapp.com/)
    redirect request.url.gsub("herokuapp.com",'picoappz.com'), 301
  end
end

def self.get_or_post(url,&block)
  get(url,&block)
  post(url,&block)
end

#this allows preview of git books to work
get "/tmp_images/:file" do |file|
  send_file File.join("./tmp/git_book/images/", file)
end

get_or_post '/' do
  @usage = UsageCount.usage_remaining
  erb :index
end

def create_title(content)
  title    = content.split("\n").first
  title    = title.gsub(/\(.*/,'').gsub(/http.*/,'')
  title.match(/[\w\s-]*/)[0]
end
  
get_or_post '/kindleizecontent' do
  verify_content_and_email
  verify_usage
  content  = params['content']
  title    = create_title(content)
  to_email = user_email

  if pubish_request?
    book = BookFormatter.new(title, content)
    BookDelivery.email_book_to_kindle(book, to_email)
    success_response('Your content is being emailed to your kindle shortly.')
  else
    @content = content
    render_preview(title,content)
  end
end

get_or_post '/kindleize' do
  begin
    if params['url'].nil?
{'fraction_non_alphanumeric': 0.12901785714285716, 'fraction_numerical': 0.008035714285714285, 'mean_word_length': 3.706932773109244, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '383711', 'n_tokens_mistral': 1630, 'n_tokens_neox': 1590, 'n_words': 360}