lib/fake_braintree.rb in fake_braintree-0.0.6 vs lib/fake_braintree.rb in fake_braintree-0.1.0

- old
+ new

@@ -1,64 +1,53 @@ require 'fileutils' require 'braintree' -require 'capybara' -require 'capybara/server' -require 'rack/handler/mongrel' +require 'active_support/core_ext/module/attribute_accessors' require 'fake_braintree/helpers' require 'fake_braintree/customer' require 'fake_braintree/subscription' require 'fake_braintree/redirect' +require 'fake_braintree/registry' +require 'fake_braintree/server' require 'fake_braintree/sinatra_app' require 'fake_braintree/valid_credit_cards' require 'fake_braintree/version' module FakeBraintree - class << self - @customers = {} - @subscriptions = {} - @failures = {} - @transactions = {} - @redirects = {} + mattr_accessor :registry, :verify_all_cards, :decline_all_cards - @decline_all_cards = false - @verify_all_cards = false - attr_accessor :customers, :subscriptions, :failures, :transactions, :decline_all_cards, :verify_all_cards, :redirects - end - def self.activate! + self.registry = Registry.new + self.verify_all_cards = false + set_configuration clear! - boot_server + Server.new.boot end def self.log_file_path 'tmp/log' end def self.clear! - self.customers = {} - self.subscriptions = {} - self.failures = {} - self.transactions = {} - self.redirects = {} + self.registry.clear! self.decline_all_cards = false clear_log! end def self.clear_log! FileUtils.mkdir_p(File.dirname(log_file_path)) File.new(log_file_path, 'w').close end def self.failure?(card_number) - self.failures.include?(card_number) + registry.failure?(card_number) end def self.failure_response(card_number = nil) - failure = self.failures[card_number] || {} + failure = registry.failures[card_number] || {} failure["errors"] ||= { "errors" => [] } { "message" => failure["message"], "verification" => { "status" => failure["status"], "processor_response_text" => failure["message"], @@ -81,26 +70,17 @@ def self.decline_all_cards! self.decline_all_cards = true end def self.decline_all_cards? - self.decline_all_cards + decline_all_cards end def self.verify_all_cards! self.verify_all_cards = true end - def self.credit_card_from_token(token) - self.customers.values.detect do |customer| - next unless customer.key?("credit_cards") - - card = customer["credit_cards"].detect {|card| card["token"] == token } - return card if card - end - end - def self.generate_transaction(options = {}) history_item = { 'timestamp' => Time.now, 'amount' => options[:amount], 'status' => options[:status] } created_at = options[:created_at] || Time.now @@ -116,26 +96,9 @@ Braintree::Configuration.merchant_id = "xxx" Braintree::Configuration.public_key = "xxx" Braintree::Configuration.private_key = "xxx" end - def self.boot_server - with_mongrel_runner do - server = Capybara::Server.new(FakeBraintree::SinatraApp) - server.boot - ENV['GATEWAY_PORT'] = server.port.to_s - end - end - - def self.with_mongrel_runner - default_server_process = Capybara.server - Capybara.server do |app, port| - Rack::Handler::Mongrel.run(app, :Port => port) - end - yield - ensure - Capybara.server(&default_server_process) - end end FakeBraintree.activate! Braintree::Configuration.logger = Logger.new(FakeBraintree.log_file_path)