Skip to content

Commit

Permalink
Add require option to require additional file
Browse files Browse the repository at this point in the history
When booting the stripe-mock-server it can be useful to have Ruby
`require` in additional files if you need to patch `StripeMock` in some
way. This is mostly useful for differences when you're running on an
older version of the Stripe API and need to tweak `StripeMock`'s
behavior until your Stripe API version is upgraded to be compatible.

When using `StripeMock` directly within a Rails app, for example, this
is unneeded as you can do the same thing via a Rails initializer file.
But if running the `StripeMock` server standalone - for use with front
end tests, for example, you might pass this new flag like this:

--require config/initializers/stripe_ruby_mock
  • Loading branch information
stevenharman committed Nov 14, 2024
1 parent 124835f commit 7909de6
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 4 deletions.
1 change: 1 addition & 0 deletions bin/stripe-mock-server
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ opts = Trollop::options do
opt :server, "Server to use", :type => :string, :default => 'thin'
opt :debug, "Request and response output", :default => true
opt :pid_path, "Location to put server pid file", :type => :string, :default => './stripe-mock-server.pid'
opt :require, "Extra path to require when loading the server; can be specified multiple times", :type => :string, :multi => true
end

require 'stripe_mock'
Expand Down
13 changes: 9 additions & 4 deletions lib/stripe_mock/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,16 @@
module StripeMock
class Server
def self.start_new(opts)
puts "Starting StripeMock server on port #{opts[:port] || 4999}"
host = opts.fetch(:host, "0.0.0.0")
port = opts.fetch(:port, 4999)
extra_requires = opts.fetch(:require, [])

host = opts.fetch :host,'0.0.0.0'
port = opts.fetch :port, 4999
extra_requires.each do |path|
puts "Requiring additional path: #{path}"
require(path)
end

puts "Starting StripeMock server on port #{port}"

DRb.start_service "druby://#{host}:#{port}", Server.new
DRb.thread.join
Expand Down Expand Up @@ -88,6 +94,5 @@ def ping
def upsert_stripe_object(object, attributes)
@instance.upsert_stripe_object(object, attributes)
end

end
end

0 comments on commit 7909de6

Please sign in to comment.