require "payback/version"
require 'payback/error'
require 'payback/conversion'

require 'open-uri'
require 'base64'
require 'excon'
require 'json'
require 'csv'
require 'savon'
require 'tzinfo'

module Payback

  def self.networks
    @@networks ||= {}
  end

  def self.parse_json(body, opts = {})
    JSON.parse(body, opts)
  rescue JSON::ParserError
    raise Payback::JsonParserError, 'Failed to parse JSON'
  end

  def self.safely_parse_json(body, opts = {})
    parse_json(body, opts)
  rescue Payback::JsonParserError
    nil
  end

  def self.register(network_klass)
    networks[network_klass.identifier] = network_klass
  end

end

require 'payback/networks'

def Payback(network_name, options = {})
  klass = Payback.networks.fetch(network_name.to_s) do |missing_name|
    raise "Network named #{missing_name} does not exist!"
  end
  klass.new(options)
end
