diff --git a/lib/stripe.rb b/lib/stripe.rb index e81e9bd7..2041ea09 100644 --- a/lib/stripe.rb +++ b/lib/stripe.rb @@ -70,6 +70,8 @@ require 'stripe/transfer' module Stripe DEFAULT_CA_BUNDLE_PATH = File.dirname(__FILE__) + '/data/ca-certificates.crt' + @app_info = nil + @api_base = 'https://api.stripe.com' @connect_base = 'https://connect.stripe.com' @uploads_base = 'https://uploads.stripe.com' diff --git a/lib/stripe/stripe_client.rb b/lib/stripe/stripe_client.rb index bd12ad6f..38dce192 100644 --- a/lib/stripe/stripe_client.rb +++ b/lib/stripe/stripe_client.rb @@ -218,6 +218,16 @@ module Stripe http_status: status, http_body: body) end + # Formats a plugin "app info" hash into a string that we can tack onto the + # end of a User-Agent string where it'll be fairly prominant in places like + # the Dashboard. Note that this formatting has been implemented to match + # other libraries, and shouldn't be changed without universal consensus. + private def format_app_info(info) + str = info[:name] + str = "#{str}/#{info[:version]}" unless info[:version].nil? + str = "#{str} (#{info[:url]})" unless info[:url].nil? + str + end def handle_api_error(http_resp) begin @@ -309,8 +319,13 @@ module Stripe end def request_headers(api_key, method) + user_agent = "Stripe/v1 RubyBindings/#{Stripe::VERSION}" + unless Stripe.app_info.nil? + user_agent += " " + format_app_info(Stripe.app_info) + end + headers = { - 'User-Agent' => "Stripe/v1 RubyBindings/#{Stripe::VERSION}", + 'User-Agent' => user_agent, 'Authorization' => "Bearer #{api_key}", 'Content-Type' => 'application/x-www-form-urlencoded' } diff --git a/test/stripe/stripe_client_test.rb b/test/stripe/stripe_client_test.rb index 908b7140..5b8ae157 100644 --- a/test/stripe/stripe_client_test.rb +++ b/test/stripe/stripe_client_test.rb @@ -193,14 +193,21 @@ module Stripe stub_request(:post, "#{Stripe.api_base}/v1/account"). with { |req| + assert_equal \ + "Stripe/v1 RubyBindings/#{Stripe::VERSION} " \ + "MyAwesomePlugin/1.2.34 (https://myawesomeplugin.info)", + req.headers["User-Agent"] + data = JSON.parse(req.headers["X-Stripe-Client-User-Agent"], symbolize_names: true) - data["application"] == { + assert_equal({ name: "MyAwesomePlugin", url: "https://myawesomeplugin.info", version: "1.2.34" - } + }, data[:application]) + + true }.to_return(body: JSON.generate(API_FIXTURES.fetch(:account))) client = StripeClient.new