Support "app info" for plugins in Ruby

Adds support for "app info" (a mechanism that allows a plugin's author
to identify that plugin) in Ruby. This is already supported in PHP and
we're adding it elsewhere.
This commit is contained in:
Brandur 2017-04-14 14:37:01 -07:00
parent 74b5f08930
commit 6acd21ac48
5 changed files with 83 additions and 1 deletions

View File

@ -122,6 +122,16 @@ an intermittent network problem:
[Idempotency keys][idempotency-keys] are added to requests to guarantee that
retries are safe.
### Writing a Plugin
If you're writing a plugin that uses the library, we'd appreciate it if you
identified using `#set_app_info`:
Stripe.set_app_info("MyAwesomePlugin", version: "1.2.34", url: "https://myawesomeplugin.info");
This information is passed along when the library makes calls to the Stripe
API.
## Development
Run all tests:

View File

@ -92,6 +92,16 @@ module Stripe
attr_reader :max_network_retry_delay, :initial_network_retry_delay
end
# Gets the application for a plugin that's identified some. See
# #set_app_info.
def self.app_info
@app_info
end
def self.app_info=(info)
@app_info = info
end
# The location of a file containing a bundle of CA certificates. By default
# the library will use an included bundle that can successfully validate
# Stripe certificates.
@ -131,6 +141,19 @@ module Stripe
@max_network_retries = val.to_i
end
# Sets some basic information about the running application that's sent along
# with API requests. Useful for plugin authors to identify their plugin when
# communicating with Stripe.
#
# Takes a name and optional version and plugin URL.
def self.set_app_info(name, version: nil, url: nil)
@app_info = {
name: name,
url: url,
version: version,
}
end
private
# DEPRECATED. Use `Util#encode_parameters` instead.

View File

@ -382,6 +382,7 @@ module Stripe
lang_version = "#{RUBY_VERSION} p#{RUBY_PATCHLEVEL} (#{RUBY_RELEASE_DATE})"
{
:application => Stripe.app_info,
:bindings_version => Stripe::VERSION,
:lang => 'ruby',
:lang_version => lang_version,
@ -390,7 +391,7 @@ module Stripe
:publisher => 'stripe',
:uname => @uname,
:hostname => Socket.gethostname,
}
}.delete_if { |k, v| v.nil? }
end
end
end

View File

@ -176,6 +176,36 @@ module Stripe
end
end
context "app_info" do
should "send app_info if set" do
begin
old = Stripe.app_info
Stripe.set_app_info(
"MyAwesomePlugin",
url: "https://myawesomeplugin.info",
version: "1.2.34"
)
stub_request(:post, "#{Stripe.api_base}/v1/account").
with { |req|
data = JSON.parse(req.headers["X-Stripe-Client-User-Agent"],
symbolize_names: true)
data["application"] == {
name: "MyAwesomePlugin",
url: "https://myawesomeplugin.info",
version: "1.2.34"
}
}.to_return(body: JSON.generate(API_FIXTURES.fetch(:account)))
client = StripeClient.new
client.execute_request(:post, '/v1/account')
ensure
Stripe.app_info = old
end
end
end
context "error handling" do
should "handle error response with empty body" do
stub_request(:post, "#{Stripe.api_base}/v1/charges").

View File

@ -14,6 +14,24 @@ class StripeTest < Test::Unit::TestCase
end
end
should "allow app_info to be configured" do
begin
old = Stripe.app_info
Stripe.set_app_info(
"MyAwesomePlugin",
url: "https://myawesomeplugin.info",
version: "1.2.34"
)
assert_equal({
name: "MyAwesomePlugin",
url: "https://myawesomeplugin.info",
version: "1.2.34"
}, Stripe.app_info)
ensure
Stripe.app_info = old
end
end
should "allow ca_bundle_path to be configured" do
begin
old = Stripe.ca_bundle_path