lib/recurly/client.rb in recurly-3.0.0.beta.3 vs lib/recurly/client.rb in recurly-3.0.0.beta.4
- old
+ new
@@ -63,11 +63,11 @@
yield(self) if block_given?
end
def next_page(pager)
run_request(:get, pager.next, nil, headers).tap do |response|
- raise_api_error!(response) if response.status != 200
+ raise_api_error!(response) unless (200...300).include?(response.status)
end
end
protected
@@ -75,42 +75,48 @@
Pager.new(client: self, path: path, options: options)
end
def get(path, **options)
response = run_request(:get, path, nil, headers)
- raise_api_error!(response) if response.status != 200
+ raise_api_error!(response) unless (200...300).include?(response.status)
JSONParser.parse(self, response.body)
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
def post(path, request_data, request_class, **options)
request = request_class.new(request_data)
request.validate!
logger.info("POST BODY #{JSON.dump(request_data)}")
response = run_request(:post, path, JSON.dump(request.attributes), headers)
- raise_api_error!(response) if response.status != 201
+ raise_api_error!(response) unless (200...300).include?(response.status)
JSONParser.parse(self, response.body)
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
- def put(path, request_data, request_class, **options)
- request = request_class.new(request_data)
- request.validate!
- logger.info("PUT BODY #{JSON.dump(request_data)}")
- response = run_request(:put, path, JSON.dump(request_data), headers)
- raise_api_error!(response) if ![200, 201].include?(response.status)
+ def put(path, request_data=nil, request_class=nil, **options)
+ response = if request_data
+ request = request_class.new(request_data)
+ request.validate!
+ logger.info("PUT BODY #{JSON.dump(request_data)}")
+ run_request(:put, path, JSON.dump(request_data), headers)
+ else
+ run_request(:put, path, nil, headers)
+ end
+ raise_api_error!(response) unless (200...300).include?(response.status)
JSONParser.parse(self, response.body)
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
def delete(path, **options)
response = run_request(:delete, path, nil, headers)
- raise_api_error!(response) if ![200, 201].include?(response.status)
- JSONParser.parse(self, response.body)
+ raise_api_error!(response) unless (200...300).include?(response.status)
+ if response.body && !response.body.empty?
+ JSONParser.parse(self, response.body)
+ end
rescue Faraday::ClientError => ex
raise_network_error!(ex)
end
protected
@@ -150,10 +156,13 @@
def read_headers(response)
@rate_limit = response.headers['x-ratelimit-limit'].to_i
@rate_limit_remaining = response.headers['x-ratelimit-remaining'].to_i
@rate_limit_reset = Time.at(response.headers['x-ratelimit-reset'].to_i).to_datetime
+ if !@_ignore_deprecation_warning && response.headers['Recurly-Deprecated']&.upcase == 'TRUE'
+ puts "[recurly-client-ruby] WARNING: Your current API version \"#{api_version}\" is deprecated and will be sunset on #{response.headers['Recurly-Sunset-Date']}"
+ end
response
end
def headers
{
@@ -162,10 +171,19 @@
'User-Agent' => "Recurly/#{VERSION}; #{RUBY_DESCRIPTION}"
}.merge(@extra_headers)
end
def interpolate_path(path, **options)
+ options.each do |k, v|
+ unless [String, Symbol, Integer, Float].include?(v.class)
+ message = "We cannot build the url with the given argument #{k}=#{v.inspect}."
+ if k =~ /_id$/
+ message << " Since this appears to be an id, perhaps you meant to pass in a String?"
+ end
+ raise ArgumentError, message
+ end
+ end
path = path.gsub("{", "%{")
path % options
end
def set_site_id(site_id, subdomain)
@@ -177,11 +195,22 @@
raise ArgumentError, "You must pass a site_id or subdomain argument to initialize the Client"
end
end
def set_faraday_connection(api_key)
- @conn = Faraday.new(url: BASE_URL) do |faraday|
- if @log_level == Logger::INFO
+ options = {
+ url: BASE_URL,
+ request: { timeout: 60, open_timeout: 50 },
+ ssl: { verify: true }
+ }
+ # Let's not use the bundled cert in production yet
+ # but we will use these certs for any other staging or dev environment
+ unless BASE_URL.end_with?('.recurly.com')
+ options[:ssl][:ca_file] = File.join(File.dirname(__FILE__), '../data/ca-certificates.crt')
+ end
+
+ @conn = Faraday.new(options) do |faraday|
+ if [Logger::DEBUG, Logger::INFO].include?(@log_level)
faraday.response :logger
end
faraday.basic_auth(api_key, '')
configure_net_adapter(faraday)
end