Add app info to User-Agent as well

This commit is contained in:
Brandur 2017-04-17 12:57:16 -07:00
parent 65c5c675ed
commit 06992ef370
3 changed files with 27 additions and 3 deletions

View File

@ -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'

View File

@ -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'
}

View File

@ -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