<commit_before>_                   = require 'underscore'
Case                = require 'case'
Koa                 = require 'koa'
KoaRouter           = require 'koa-router'
bodyParser          = require 'koa-bodyparser'
request             = require 'request-promise'
url                 = require 'url'

# Base Nodeswork class.
#
# About the configuration
#
# Nodeswork first determine the running env based on the order of
#
#   1. process.env
#   2. class configurations
#
# If the box env is 'DEV', then class configuration has higher priority,
# otherwise, process.env has higher priority.
#
class Nodeswork

  constructor: (@_opts={}) ->
    @opts            = {}
    @config @_opts
    @app             = new Koa
    @router          = new KoaRouter
    @processes       = []
    @accountClazz    = {}
    @componentClazz  = {}

    @router
      .post PROCESSING_URL_PATH, _.bind fetchRequiredInformation, null, @


  # Either to config current nodeswork instance, or retrieve a config value.
  #
  # @param opts [Object] config current nodeswork instance.
  # @param opts [String] retrieve the config value for this key.
  config: (opts) ->
    if _.isString(opts) then return @opts[opts]

    _.extend @_opts, opts
    keys    = _.filter _.keys(process.env), (x) -> x.startsWith 'NW_'
    envOpts = _.object _.map keys, (key) -> [key.substring(3), process.env[key]]

    env = envOpts.env ? opts.env ? 'DEV'

    switch env
      when 'DEV' then _.extend @opts, envOpts, opts
      when 'PROD' then _.extend @opts, opts, envOpts

    @opts.env = env
    @jar      = @opts.jar ? request.jar()
    @

  withAccount: (accountClazz...) ->
    _.each accountClazz, (cls) =>
      @accountClazz[cls.name]  = cls
      cls::nodeswork           = @
    @

  withComponent: (components...) ->
    _.each components, (cls) =>
      @componentClazz[cls.name]   = cls
      cls::nodeswork              = @
    @

  process: (middlewares...) ->
    Array::push.apply @processes, middlewares
    @router.post.apply @router, [PROCESSING_URL_PATH].concat middlewares
    @

  view: (path, middlewares...) ->

  request: (opts) ->
    @requestClient _.extend {}, opts, {
      url: url.resolve @opts.server, opts.url
    }

  requestDefaults: (opts) ->
    @requestClient = @requestClient.defaults opts

  start: (cb) ->
    @config @_opts

    unless @opts.server?
      throw new Error "Required configuration 'server' is missing."

    unless @opts.port?
      throw new Error "Required configuration 'port' is missing."

    @requestClient = request.defaults {
      jar:                 @jar
      followAllRedirects:  true
      json:                true
    }

    @app
      .use bodyParser()
      .use @router.routes()<commit_msg>Create requestClient immediately after config changes.<commit_after>_                   = require 'underscore'
Case                = require 'case'
Koa                 = require 'koa'
KoaRouter           = require 'koa-router'
bodyParser          = require 'koa-bodyparser'
request             = require 'request-promise'
url                 = require 'url'

# Base Nodeswork class.
#
# About the configuration
#
# Nodeswork first determine the running env based on the order of
#
#   1. process.env
#   2. class configurations
#
# If the box env is 'DEV', then class configuration has higher priority,
# otherwise, process.env has higher priority.
#
class Nodeswork

  constructor: (@_opts={}) ->
    @opts            = {}
    @config @_opts
    @app             = new Koa
    @router          = new KoaRouter
    @processes       = []
    @accountClazz    = {}
    @componentClazz  = {}

    @router
      .post PROCESSING_URL_PATH, _.bind fetchRequiredInformation, null, @


  # Either to config current nodeswork instance, or retrieve a config value.
  #
  # @param opts [Object] config current nodeswork instance.
  # @param opts [String] retrieve the config value for this key.
  config: (opts) ->
    if _.isString(opts) then return @opts[opts]

    _.extend @_opts, opts
    keys    = _.filter _.keys(process.env), (x) -> x.startsWith 'NW_'
    envOpts = _.object _.map keys, (key) -> [key.substring(3), process.env[key]]

    env = envOpts.env ? opts.env ? 'DEV'

    switch env
      when 'DEV' then _.extend @opts, envOpts, opts
      when 'PROD' then _.extend @opts, opts, envOpts

    @opts.env       = env
    @jar            = @opts.jar ? request.jar()
    @requestClient  = request.defaults {
      jar:                 @jar
      followAllRedirects:  true
      json:                true
    }
    @

  withAccount: (accountClazz...) ->
    _.each accountClazz, (cls) =>
      @accountClazz[cls.name]  = cls
      cls::nodeswork           = @
    @

  withComponent: (components...) ->
    _.each components, (cls) =>
      @componentClazz[cls.name]   = cls
      cls::nodeswork              = @
    @

  process: (middlewares...) ->
    Array::push.apply @processes, middlewares
    @router.post.apply @router, [PROCESSING_URL_PATH].concat middlewares
    @

  view: (path, middlewares...) ->

  request: (opts) ->
    @requestClient _.extend {}, opts, {
      url: url.resolve @opts.server, opts.url
    }

  requestDefaults: (opts) ->
    @requestClient = @requestClient.defaults opts

  start: (cb) ->
    @config @_opts

    unless @opts.server?
      throw new Error "Required configuration 'server' is missing."

    unless @opts.port?
      throw new Error "Required configuration 'port' is missing."

    @app
      .use bodyParser()
      .use @router.routes()
{'fraction_non_alphanumeric': 0.11827567469661293, 'fraction_numerical': 0.0010867596449918493, 'mean_word_length': 2.1790443293033968, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 29, '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': '1264955', 'n_tokens_mistral': 1672, 'n_tokens_neox': 1638, 'n_words': 478}