mirror of
https://github.com/stripe/stripe-ruby.git
synced 2025-12-06 00:00:29 -05:00
Add a StripeResponse to objects created by API calls
This commit is contained in:
parent
15f77a6e47
commit
843ea88219
@ -23,6 +23,7 @@ require 'stripe/api_operations/request'
|
|||||||
require 'stripe/errors'
|
require 'stripe/errors'
|
||||||
require 'stripe/util'
|
require 'stripe/util'
|
||||||
require 'stripe/stripe_object'
|
require 'stripe/stripe_object'
|
||||||
|
require 'stripe/stripe_response'
|
||||||
require 'stripe/list_object'
|
require 'stripe/list_object'
|
||||||
require 'stripe/api_resource'
|
require 'stripe/api_resource'
|
||||||
require 'stripe/singleton_api_resource'
|
require 'stripe/singleton_api_resource'
|
||||||
@ -203,9 +204,15 @@ module Stripe
|
|||||||
:method => method, :open_timeout => open_timeout,
|
:method => method, :open_timeout => open_timeout,
|
||||||
:payload => payload, :url => url, :timeout => read_timeout)
|
:payload => payload, :url => url, :timeout => read_timeout)
|
||||||
|
|
||||||
response = execute_request_with_rescues(request_opts, api_base_url)
|
http_resp = execute_request_with_rescues(request_opts, api_base_url)
|
||||||
|
|
||||||
[parse(response), api_key]
|
begin
|
||||||
|
resp = StripeResponse.from_rest_client_response(http_resp)
|
||||||
|
rescue JSON::ParserError
|
||||||
|
raise general_api_error(http_resp.code, http_resp.body)
|
||||||
|
end
|
||||||
|
|
||||||
|
[resp, api_key]
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.max_network_retries
|
def self.max_network_retries
|
||||||
@ -218,20 +225,6 @@ module Stripe
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def self.api_error(error, resp, error_obj)
|
|
||||||
APIError.new(error[:message], resp.code, resp.body, error_obj, resp.headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.authentication_error(error, resp, error_obj)
|
|
||||||
AuthenticationError.new(error[:message], resp.code, resp.body, error_obj,
|
|
||||||
resp.headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.card_error(error, resp, error_obj)
|
|
||||||
CardError.new(error[:message], error[:param], error[:code],
|
|
||||||
resp.code, resp.body, error_obj, resp.headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.execute_request(opts)
|
def self.execute_request(opts)
|
||||||
RestClient::Request.execute(opts)
|
RestClient::Request.execute(opts)
|
||||||
end
|
end
|
||||||
@ -274,9 +267,9 @@ module Stripe
|
|||||||
response
|
response
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.general_api_error(rcode, rbody)
|
def self.general_api_error(status, body)
|
||||||
APIError.new("Invalid response object from API: #{rbody.inspect} " +
|
APIError.new("Invalid response object from API: #{body.inspect} " +
|
||||||
"(HTTP response code was #{rcode})", rcode, rbody)
|
"(HTTP response code was #{status})", status, body)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.get_uname
|
def self.get_uname
|
||||||
@ -310,32 +303,45 @@ module Stripe
|
|||||||
"uname lookup failed"
|
"uname lookup failed"
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.handle_api_error(resp)
|
def self.handle_api_error(http_resp)
|
||||||
begin
|
begin
|
||||||
error_obj = JSON.parse(resp.body)
|
resp = StripeResponse.from_rest_client_response(http_resp)
|
||||||
error_obj = Util.symbolize_names(error_obj)
|
error = resp.data[:error]
|
||||||
error = error_obj[:error]
|
|
||||||
raise StripeError.new unless error && error.is_a?(Hash)
|
raise StripeError.new unless error && error.is_a?(Hash)
|
||||||
|
|
||||||
rescue JSON::ParserError, StripeError
|
rescue JSON::ParserError, StripeError
|
||||||
raise general_api_error(resp.code, resp.body)
|
raise general_api_error(http_resp.code, http_resp.body)
|
||||||
end
|
end
|
||||||
|
|
||||||
case resp.code
|
case resp.http_status
|
||||||
when 400, 404
|
when 400, 404
|
||||||
raise invalid_request_error(error, resp, error_obj)
|
error = InvalidRequestError.new(
|
||||||
|
error[:message], error[:param],
|
||||||
|
resp.http_status, resp.http_body, resp.data, resp.http_headers)
|
||||||
when 401
|
when 401
|
||||||
raise authentication_error(error, resp, error_obj)
|
error = AuthenticationError.new(
|
||||||
|
error[:message],
|
||||||
|
resp.http_status, resp.http_body, resp.data, resp.http_headers)
|
||||||
when 402
|
when 402
|
||||||
raise card_error(error, resp, error_obj)
|
error = CardError.new(
|
||||||
|
error[:message], error[:param], error[:code],
|
||||||
|
resp.http_status, resp.http_body, resp.data, resp.http_headers)
|
||||||
when 403
|
when 403
|
||||||
raise permission_error(error, resp, error_obj)
|
error = PermissionError.new(
|
||||||
|
error[:message],
|
||||||
|
resp.http_status, resp.http_body, resp.data, resp.http_headers)
|
||||||
when 429
|
when 429
|
||||||
raise rate_limit_error(error, resp, error_obj)
|
error = RateLimitError.new(
|
||||||
|
error[:message],
|
||||||
|
resp.http_status, resp.http_body, resp.data, resp.http_headers)
|
||||||
else
|
else
|
||||||
raise api_error(error, resp, error_obj)
|
error = APIError.new(
|
||||||
|
error[:message],
|
||||||
|
resp.http_status, resp.http_body, resp.data, resp.http_headers)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
error.response = resp
|
||||||
|
raise(error)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.handle_restclient_error(e, request_opts, retry_count, api_base_url=nil)
|
def self.handle_restclient_error(e, request_opts, retry_count, api_base_url=nil)
|
||||||
@ -383,32 +389,6 @@ module Stripe
|
|||||||
raise APIConnectionError.new(message + "\n\n(Network error: #{e.message})")
|
raise APIConnectionError.new(message + "\n\n(Network error: #{e.message})")
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.invalid_request_error(error, resp, error_obj)
|
|
||||||
InvalidRequestError.new(error[:message], error[:param], resp.code,
|
|
||||||
resp.body, error_obj, resp.headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.parse(response)
|
|
||||||
begin
|
|
||||||
# Would use :symbolize_names => true, but apparently there is
|
|
||||||
# some library out there that makes symbolize_names not work.
|
|
||||||
response = JSON.parse(response.body)
|
|
||||||
rescue JSON::ParserError
|
|
||||||
raise general_api_error(response.code, response.body)
|
|
||||||
end
|
|
||||||
|
|
||||||
Util.symbolize_names(response)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.permission_error(error, resp, error_obj)
|
|
||||||
PermissionError.new(error[:message], resp.code, resp.body, error_obj, resp.headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.rate_limit_error(error, resp, error_obj)
|
|
||||||
RateLimitError.new(error[:message], resp.code, resp.body, error_obj,
|
|
||||||
resp.headers)
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.request_headers(api_key, method)
|
def self.request_headers(api_key, method)
|
||||||
headers = {
|
headers = {
|
||||||
'User-Agent' => "Stripe/v1 RubyBindings/#{Stripe::VERSION}",
|
'User-Agent' => "Stripe/v1 RubyBindings/#{Stripe::VERSION}",
|
||||||
|
|||||||
@ -37,8 +37,8 @@ module Stripe
|
|||||||
|
|
||||||
def reject(params={}, opts={})
|
def reject(params={}, opts={})
|
||||||
opts = Util.normalize_opts(opts)
|
opts = Util.normalize_opts(opts)
|
||||||
response, opts = request(:post, resource_url + '/reject', params, opts)
|
self.response, opts = request(:post, resource_url + '/reject', params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Somewhat unfortunately, we attempt to do a special encoding trick when
|
# Somewhat unfortunately, we attempt to do a special encoding trick when
|
||||||
@ -93,9 +93,9 @@ module Stripe
|
|||||||
|
|
||||||
def deauthorize(client_id, opts={})
|
def deauthorize(client_id, opts={})
|
||||||
opts = {:api_base => Stripe.connect_base}.merge(Util.normalize_opts(opts))
|
opts = {:api_base => Stripe.connect_base}.merge(Util.normalize_opts(opts))
|
||||||
response, opts = request(:post, '/oauth/deauthorize', { 'client_id' => client_id, 'stripe_user_id' => self.id }, opts)
|
resp, opts = request(:post, '/oauth/deauthorize', { 'client_id' => client_id, 'stripe_user_id' => self.id }, opts)
|
||||||
opts.delete(:api_base) # the api_base here is a one-off, don't persist it
|
opts.delete(:api_base) # the api_base here is a one-off, don't persist it
|
||||||
Util.convert_to_stripe_object(response, opts)
|
Util.convert_to_stripe_object(resp.data, opts, response: resp)
|
||||||
end
|
end
|
||||||
|
|
||||||
ARGUMENT_NOT_PROVIDED = Object.new
|
ARGUMENT_NOT_PROVIDED = Object.new
|
||||||
|
|||||||
@ -2,8 +2,8 @@ module Stripe
|
|||||||
module APIOperations
|
module APIOperations
|
||||||
module Create
|
module Create
|
||||||
def create(params={}, opts={})
|
def create(params={}, opts={})
|
||||||
response, opts = request(:post, resource_url, params, opts)
|
resp, opts = request(:post, resource_url, params, opts)
|
||||||
Util.convert_to_stripe_object(response, opts)
|
Util.convert_to_stripe_object(resp.data, opts, response: resp)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -3,8 +3,8 @@ module Stripe
|
|||||||
module Delete
|
module Delete
|
||||||
def delete(params={}, opts={})
|
def delete(params={}, opts={})
|
||||||
opts = Util.normalize_opts(opts)
|
opts = Util.normalize_opts(opts)
|
||||||
response, opts = request(:delete, resource_url, params, opts)
|
self.response, opts = request(:delete, resource_url, params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@ -4,8 +4,9 @@ module Stripe
|
|||||||
def list(filters={}, opts={})
|
def list(filters={}, opts={})
|
||||||
opts = Util.normalize_opts(opts)
|
opts = Util.normalize_opts(opts)
|
||||||
|
|
||||||
response, opts = request(:get, resource_url, filters, opts)
|
resp, opts = request(:get, resource_url, filters, opts)
|
||||||
obj = ListObject.construct_from(response, opts)
|
obj = ListObject.construct_from(resp.data, opts)
|
||||||
|
obj.response = resp
|
||||||
|
|
||||||
# set filters so that we can fetch the same limit, expansions, and
|
# set filters so that we can fetch the same limit, expansions, and
|
||||||
# predicates when accessing the next and previous pages
|
# predicates when accessing the next and previous pages
|
||||||
|
|||||||
@ -12,7 +12,7 @@ module Stripe
|
|||||||
api_base = headers.delete(:api_base)
|
api_base = headers.delete(:api_base)
|
||||||
# Assume all remaining opts must be headers
|
# Assume all remaining opts must be headers
|
||||||
|
|
||||||
response, opts[:api_key] = Stripe.request(method, url, api_key, params, headers, api_base)
|
resp, opts[:api_key] = Stripe.request(method, url, api_key, params, headers, api_base)
|
||||||
|
|
||||||
# Hash#select returns an array before 1.9
|
# Hash#select returns an array before 1.9
|
||||||
opts_to_persist = {}
|
opts_to_persist = {}
|
||||||
@ -22,7 +22,7 @@ module Stripe
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
[response, opts_to_persist]
|
[resp, opts_to_persist]
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -21,8 +21,8 @@ module Stripe
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
response, opts = request(:post, "#{resource_url}/#{id}", params, opts)
|
resp, opts = request(:post, "#{resource_url}/#{id}", params, opts)
|
||||||
Util.convert_to_stripe_object(response, opts)
|
Util.convert_to_stripe_object(resp.data, opts, response: resp)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@ -57,10 +57,8 @@ module Stripe
|
|||||||
# generated a uri for this object with an identifier baked in
|
# generated a uri for this object with an identifier baked in
|
||||||
values.delete(:id)
|
values.delete(:id)
|
||||||
|
|
||||||
response, opts = request(:post, save_url, values, opts)
|
self.response, opts = request(:post, save_url, values, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
|
|
||||||
self
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.included(base)
|
def self.included(base)
|
||||||
|
|||||||
@ -2,6 +2,13 @@ module Stripe
|
|||||||
class APIResource < StripeObject
|
class APIResource < StripeObject
|
||||||
include Stripe::APIOperations::Request
|
include Stripe::APIOperations::Request
|
||||||
|
|
||||||
|
# Response contains a structure that has some basic information about the
|
||||||
|
# response that conveyed the API resource.
|
||||||
|
#
|
||||||
|
# Note that this is only set on the top-level API resource returned by an
|
||||||
|
# API operation. It will hold a nil value on all others.
|
||||||
|
attr_accessor :response
|
||||||
|
|
||||||
# A flag that can be set a behavior that will cause this resource to be
|
# A flag that can be set a behavior that will cause this resource to be
|
||||||
# encoded and sent up along with an update of its parent resource. This is
|
# encoded and sent up along with an update of its parent resource. This is
|
||||||
# usually not desirable because resources are updated individually on their
|
# usually not desirable because resources are updated individually on their
|
||||||
@ -54,8 +61,8 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
def refresh
|
def refresh
|
||||||
response, opts = request(:get, resource_url, @retrieve_params)
|
self.response, opts = request(:get, resource_url, @retrieve_params)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.retrieve(id, opts={})
|
def self.retrieve(id, opts={})
|
||||||
|
|||||||
@ -5,8 +5,8 @@ module Stripe
|
|||||||
extend Stripe::APIOperations::List
|
extend Stripe::APIOperations::List
|
||||||
|
|
||||||
def verify(params={}, opts={})
|
def verify(params={}, opts={})
|
||||||
response, opts = request(:post, resource_url + '/verify', params, opts)
|
self.response, opts = request(:post, resource_url + '/verify', params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def resource_url
|
def resource_url
|
||||||
|
|||||||
@ -20,41 +20,41 @@ module Stripe
|
|||||||
# from the server
|
# from the server
|
||||||
self.refresh
|
self.refresh
|
||||||
else
|
else
|
||||||
response, opts = request(:post, refund_url, params, opts)
|
self.response, opts = request(:post, refund_url, params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def capture(params={}, opts={})
|
def capture(params={}, opts={})
|
||||||
response, opts = request(:post, capture_url, params, opts)
|
self.response, opts = request(:post, capture_url, params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_dispute(params={}, opts={})
|
def update_dispute(params={}, opts={})
|
||||||
response, opts = request(:post, dispute_url, params, opts)
|
self.response, opts = request(:post, dispute_url, params, opts)
|
||||||
initialize_from({ :dispute => response }, opts, true)
|
initialize_from({ :dispute => response.data }, opts, true)
|
||||||
dispute
|
dispute
|
||||||
end
|
end
|
||||||
|
|
||||||
def close_dispute(params={}, opts={})
|
def close_dispute(params={}, opts={})
|
||||||
response, opts = request(:post, close_dispute_url, params, opts)
|
self.response, opts = request(:post, close_dispute_url, params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark_as_fraudulent
|
def mark_as_fraudulent
|
||||||
params = {
|
params = {
|
||||||
:fraud_details => { :user_report => 'fraudulent' }
|
:fraud_details => { :user_report => 'fraudulent' }
|
||||||
}
|
}
|
||||||
response, opts = request(:post, resource_url, params)
|
self.response, opts = request(:post, resource_url, params)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def mark_as_safe
|
def mark_as_safe
|
||||||
params = {
|
params = {
|
||||||
:fraud_details => { :user_report => 'safe' }
|
:fraud_details => { :user_report => 'safe' }
|
||||||
}
|
}
|
||||||
response, opts = request(:post, resource_url, params)
|
self.response, opts = request(:post, resource_url, params)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@ -38,25 +38,25 @@ module Stripe
|
|||||||
end
|
end
|
||||||
|
|
||||||
def cancel_subscription(params={}, opts={})
|
def cancel_subscription(params={}, opts={})
|
||||||
response, opts = request(:delete, subscription_url, params, opts)
|
self.response, opts = request(:delete, subscription_url, params, opts)
|
||||||
initialize_from({ :subscription => response }, opts, true)
|
initialize_from({ :subscription => response.data }, opts, true)
|
||||||
subscription
|
subscription
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_subscription(params={}, opts={})
|
def update_subscription(params={}, opts={})
|
||||||
response, opts = request(:post, subscription_url, params, opts)
|
self.response, opts = request(:post, subscription_url, params, opts)
|
||||||
initialize_from({ :subscription => response }, opts, true)
|
initialize_from({ :subscription => response.data }, opts, true)
|
||||||
subscription
|
subscription
|
||||||
end
|
end
|
||||||
|
|
||||||
def create_subscription(params={}, opts={})
|
def create_subscription(params={}, opts={})
|
||||||
response, opts = request(:post, subscriptions_url, params, opts)
|
self.response, opts = request(:post, subscriptions_url, params, opts)
|
||||||
initialize_from({ :subscription => response }, opts, true)
|
initialize_from({ :subscription => response.data }, opts, true)
|
||||||
subscription
|
subscription
|
||||||
end
|
end
|
||||||
|
|
||||||
def delete_discount
|
def delete_discount
|
||||||
_, opts = request(:delete, discount_url)
|
self.response, opts = request(:delete, discount_url)
|
||||||
initialize_from({ :discount => nil }, opts, true)
|
initialize_from({ :discount => nil }, opts, true)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
@ -5,8 +5,8 @@ module Stripe
|
|||||||
include Stripe::APIOperations::Save
|
include Stripe::APIOperations::Save
|
||||||
|
|
||||||
def close(params={}, opts={})
|
def close(params={}, opts={})
|
||||||
response, opts = request(:post, close_url, params, opts)
|
self.response, opts = request(:post, close_url, params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def close_url
|
def close_url
|
||||||
|
|||||||
@ -3,12 +3,24 @@ module Stripe
|
|||||||
# errors derive.
|
# errors derive.
|
||||||
class StripeError < StandardError
|
class StripeError < StandardError
|
||||||
attr_reader :message
|
attr_reader :message
|
||||||
attr_reader :http_status
|
|
||||||
|
# Response contains a structure that has some basic information about the
|
||||||
|
# response that conveyed the error.
|
||||||
|
attr_accessor :response
|
||||||
|
|
||||||
|
# These fields are now available as part of #response and that usage should
|
||||||
|
# be preferred.
|
||||||
attr_reader :http_body
|
attr_reader :http_body
|
||||||
attr_reader :http_headers
|
attr_reader :http_headers
|
||||||
|
attr_reader :http_status
|
||||||
|
attr_reader :json_body # equivalent to #data
|
||||||
attr_reader :request_id
|
attr_reader :request_id
|
||||||
attr_reader :json_body
|
|
||||||
|
|
||||||
|
# Initializes a StripeError.
|
||||||
|
#
|
||||||
|
# Note: We should try to move away from the very heavy constructors ordered
|
||||||
|
# parameters to each just setting accessor values directly or optional
|
||||||
|
# arguments.
|
||||||
def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil,
|
def initialize(message=nil, http_status=nil, http_body=nil, json_body=nil,
|
||||||
http_headers=nil)
|
http_headers=nil)
|
||||||
@message = message
|
@message = message
|
||||||
|
|||||||
@ -5,13 +5,13 @@ module Stripe
|
|||||||
extend Stripe::APIOperations::Create
|
extend Stripe::APIOperations::Create
|
||||||
|
|
||||||
def self.upcoming(params, opts={})
|
def self.upcoming(params, opts={})
|
||||||
response, opts = request(:get, upcoming_url, params, opts)
|
resp, opts = request(:get, upcoming_url, params, opts)
|
||||||
Util.convert_to_stripe_object(response, opts)
|
Util.convert_to_stripe_object(resp.data, opts, response: resp)
|
||||||
end
|
end
|
||||||
|
|
||||||
def pay(opts={})
|
def pay(opts={})
|
||||||
response, opts = request(:post, pay_url, {}, opts)
|
self.response, opts = request(:post, pay_url, {}, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@ -10,6 +10,10 @@ module Stripe
|
|||||||
# expansions, and predicates as a user pages through resources.
|
# expansions, and predicates as a user pages through resources.
|
||||||
attr_accessor :filters
|
attr_accessor :filters
|
||||||
|
|
||||||
|
# Response contains a structure that has some basic information about the
|
||||||
|
# response that conveyed the list resource.
|
||||||
|
attr_accessor :response
|
||||||
|
|
||||||
# An empty list object. This is returned from +next+ when we know that
|
# An empty list object. This is returned from +next+ when we know that
|
||||||
# there isn't a next page in order to replicate the behavior of the API
|
# there isn't a next page in order to replicate the behavior of the API
|
||||||
# when it attempts to return a page beyond the last.
|
# when it attempts to return a page beyond the last.
|
||||||
@ -64,8 +68,8 @@ module Stripe
|
|||||||
|
|
||||||
def retrieve(id, opts={})
|
def retrieve(id, opts={})
|
||||||
id, retrieve_params = Util.normalize_id(id)
|
id, retrieve_params = Util.normalize_id(id)
|
||||||
response, opts = request(:get,"#{resource_url}/#{CGI.escape(id)}", retrieve_params, opts)
|
resp, opts = request(:get,"#{resource_url}/#{CGI.escape(id)}", retrieve_params, opts)
|
||||||
Util.convert_to_stripe_object(response, opts)
|
Util.convert_to_stripe_object(resp.data, opts, response: resp)
|
||||||
end
|
end
|
||||||
|
|
||||||
# Fetches the next page in the resource list (if there is one).
|
# Fetches the next page in the resource list (if there is one).
|
||||||
|
|||||||
@ -5,13 +5,13 @@ module Stripe
|
|||||||
include Stripe::APIOperations::Save
|
include Stripe::APIOperations::Save
|
||||||
|
|
||||||
def pay(params, opts={})
|
def pay(params, opts={})
|
||||||
response, opts = request(:post, pay_url, params, opts)
|
self.response, opts = request(:post, pay_url, params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
|
|
||||||
def return_order(params, opts={})
|
def return_order(params, opts={})
|
||||||
response, opts = request(:post, returns_url, params, opts)
|
resp, opts = request(:post, returns_url, params, opts)
|
||||||
Util.convert_to_stripe_object(response, opts)
|
Util.convert_to_stripe_object(resp.data, opts, response: resp)
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|||||||
@ -4,8 +4,8 @@ module Stripe
|
|||||||
include Stripe::APIOperations::Save
|
include Stripe::APIOperations::Save
|
||||||
|
|
||||||
def verify(params={}, opts={})
|
def verify(params={}, opts={})
|
||||||
response, opts = request(:post, resource_url + '/verify', params, opts)
|
self.response, opts = request(:post, resource_url + '/verify', params, opts)
|
||||||
initialize_from(response, opts)
|
initialize_from(response.data, opts)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
34
lib/stripe/stripe_response.rb
Normal file
34
lib/stripe/stripe_response.rb
Normal file
@ -0,0 +1,34 @@
|
|||||||
|
module Stripe
|
||||||
|
# StripeResponse encapsulates some vitals of a response that came back from
|
||||||
|
# the Stripe API.
|
||||||
|
class StripeResponse
|
||||||
|
# The data contained by the HTTP body of the response deserialized from
|
||||||
|
# JSON.
|
||||||
|
attr_accessor :data
|
||||||
|
|
||||||
|
# The raw HTTP body of the response.
|
||||||
|
attr_accessor :http_body
|
||||||
|
|
||||||
|
# A Hash of the HTTP headers of the response.
|
||||||
|
attr_accessor :http_headers
|
||||||
|
|
||||||
|
# The integer HTTP status code of the response.
|
||||||
|
attr_accessor :http_status
|
||||||
|
|
||||||
|
# The Stripe request ID of the response.
|
||||||
|
attr_accessor :request_id
|
||||||
|
|
||||||
|
# Initializes a StripeResponse object from a RestClient HTTP response
|
||||||
|
# object.
|
||||||
|
#
|
||||||
|
# This may throw JSON::ParserError if the response body is not valid JSON.
|
||||||
|
def self.from_rest_client_response(http_resp)
|
||||||
|
resp = StripeResponse.new
|
||||||
|
resp.data = JSON.parse(http_resp.body, symbolize_names: true)
|
||||||
|
resp.http_body = http_resp.body
|
||||||
|
resp.http_headers = http_resp.headers
|
||||||
|
resp.http_status = http_resp.code
|
||||||
|
resp
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
@ -5,8 +5,8 @@ module Stripe
|
|||||||
include Stripe::APIOperations::Save
|
include Stripe::APIOperations::Save
|
||||||
|
|
||||||
def cancel
|
def cancel
|
||||||
response, api_key = self.request(:post, cancel_url)
|
self.response, api_key = self.request(:post, cancel_url)
|
||||||
initialize_from(response, api_key)
|
initialize_from(response.data, api_key)
|
||||||
end
|
end
|
||||||
|
|
||||||
def cancel_url
|
def cancel_url
|
||||||
|
|||||||
@ -68,19 +68,28 @@ module Stripe
|
|||||||
#
|
#
|
||||||
# ==== Attributes
|
# ==== Attributes
|
||||||
#
|
#
|
||||||
# * +resp+ - Hash of fields and values to be converted into a StripeObject.
|
# * +data+ - Hash of fields and values to be converted into a StripeObject.
|
||||||
# * +opts+ - Options for +StripeObject+ like an API key that will be reused
|
# * +opts+ - Options for +StripeObject+ like an API key that will be reused
|
||||||
# on subsequent API calls.
|
# on subsequent API calls.
|
||||||
def self.convert_to_stripe_object(resp, opts)
|
# * +response+ - An object containing information about the API response
|
||||||
case resp
|
# that produced the data which is hydrating the StripeObject.
|
||||||
|
def self.convert_to_stripe_object(data, opts, response: nil)
|
||||||
|
obj = case data
|
||||||
when Array
|
when Array
|
||||||
resp.map { |i| convert_to_stripe_object(i, opts) }
|
data.map { |i| convert_to_stripe_object(i, opts) }
|
||||||
when Hash
|
when Hash
|
||||||
# Try converting to a known object class. If none available, fall back to generic StripeObject
|
# Try converting to a known object class. If none available, fall back to generic StripeObject
|
||||||
object_classes.fetch(resp[:object], StripeObject).construct_from(resp, opts)
|
object_classes.fetch(data[:object], StripeObject).construct_from(data, opts)
|
||||||
else
|
else
|
||||||
resp
|
data
|
||||||
end
|
end
|
||||||
|
|
||||||
|
case obj
|
||||||
|
when APIResource, ListObject
|
||||||
|
obj.response = response
|
||||||
|
end
|
||||||
|
|
||||||
|
obj
|
||||||
end
|
end
|
||||||
|
|
||||||
def self.file_readable(file)
|
def self.file_readable(file)
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user