balanced 0.8.2 → 1.0.beta1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/.rbenv-version +1 -1
- data/.ruby-version +1 -1
- data/Gemfile +7 -3
- data/README.md +2 -10
- data/Rakefile +4 -18
- data/balanced.gemspec +8 -10
- data/lib/balanced.rb +28 -28
- data/lib/balanced/client.rb +15 -6
- data/lib/balanced/error.rb +21 -12
- data/lib/balanced/pager.rb +66 -25
- data/lib/balanced/resources.rb +18 -17
- data/lib/balanced/resources/api_key.rb +4 -0
- data/lib/balanced/resources/bank_account.rb +26 -75
- data/lib/balanced/resources/callback.rb +2 -0
- data/lib/balanced/resources/card.rb +15 -65
- data/lib/balanced/resources/card_hold.rb +38 -0
- data/lib/balanced/resources/credit.rb +6 -50
- data/lib/balanced/resources/customer.rb +33 -71
- data/lib/balanced/resources/debit.rb +6 -14
- data/lib/balanced/resources/event.rb +6 -0
- data/lib/balanced/resources/funding_instrument.rb +11 -0
- data/lib/balanced/resources/hypermedia.rb +28 -0
- data/lib/balanced/resources/marketplace.rb +5 -203
- data/lib/balanced/resources/order.rb +71 -0
- data/lib/balanced/resources/refund.rb +2 -7
- data/lib/balanced/resources/resource.rb +169 -106
- data/lib/balanced/resources/reversal.rb +2 -8
- data/lib/balanced/resources/transfer.rb +11 -0
- data/lib/balanced/response/balanced_exception_middleware.rb +2 -2
- data/lib/balanced/utils.rb +47 -14
- data/lib/balanced/version.rb +1 -1
- data/scenario.cache +526 -380
- data/scenarios/api_key_create/definition.rb +1 -0
- data/scenarios/api_key_create/request.rb +2 -0
- data/scenarios/api_key_create/ruby.mako +21 -0
- data/scenarios/api_key_delete/definition.rb +1 -0
- data/scenarios/api_key_delete/request.rb +3 -0
- data/scenarios/api_key_delete/ruby.mako +12 -0
- data/scenarios/api_key_list/definition.rb +1 -0
- data/scenarios/api_key_list/request.rb +2 -0
- data/scenarios/api_key_list/ruby.mako +28 -0
- data/scenarios/api_key_show/definition.rb +1 -0
- data/scenarios/api_key_show/request.rb +2 -0
- data/scenarios/api_key_show/ruby.mako +18 -0
- data/scenarios/{customer_add_bank_account → bank_account_associate_to_customer}/definition.rb +0 -0
- data/scenarios/bank_account_associate_to_customer/request.rb +3 -0
- data/scenarios/bank_account_associate_to_customer/ruby.mako +49 -0
- data/scenarios/bank_account_create/definition.rb +1 -1
- data/scenarios/bank_account_create/request.rb +1 -1
- data/scenarios/bank_account_create/ruby.mako +44 -7
- data/scenarios/{credit_create_existing_bank_account → bank_account_credit}/definition.rb +0 -0
- data/scenarios/bank_account_credit/request.rb +3 -0
- data/scenarios/bank_account_credit/ruby.mako +42 -0
- data/scenarios/bank_account_debit/definition.rb +1 -0
- data/scenarios/bank_account_debit/request.rb +5 -0
- data/scenarios/bank_account_debit/ruby.mako +50 -0
- data/scenarios/bank_account_delete/request.rb +1 -1
- data/scenarios/bank_account_delete/ruby.mako +5 -3
- data/scenarios/bank_account_list/ruby.mako +76 -2
- data/scenarios/bank_account_show/definition.rb +1 -1
- data/scenarios/bank_account_show/request.rb +1 -1
- data/scenarios/bank_account_show/ruby.mako +41 -5
- data/scenarios/bank_account_update/definition.rb +1 -0
- data/scenarios/bank_account_update/request.rb +6 -0
- data/scenarios/bank_account_update/ruby.mako +56 -0
- data/scenarios/bank_account_verification_create/request.rb +1 -1
- data/scenarios/bank_account_verification_create/ruby.mako +20 -3
- data/scenarios/bank_account_verification_show/definition.rb +1 -1
- data/scenarios/bank_account_verification_show/request.rb +1 -1
- data/scenarios/bank_account_verification_show/ruby.mako +21 -4
- data/scenarios/bank_account_verification_update/definition.rb +1 -1
- data/scenarios/bank_account_verification_update/request.rb +5 -5
- data/scenarios/bank_account_verification_update/ruby.mako +24 -8
- data/scenarios/callback_create/definition.rb +1 -1
- data/scenarios/callback_create/request.rb +2 -2
- data/scenarios/callback_create/ruby.mako +22 -0
- data/scenarios/callback_delete/definition.rb +1 -1
- data/scenarios/callback_delete/request.rb +2 -2
- data/scenarios/callback_delete/ruby.mako +12 -0
- data/scenarios/callback_list/definition.rb +1 -1
- data/scenarios/callback_list/request.rb +1 -2
- data/scenarios/callback_list/ruby.mako +20 -0
- data/scenarios/callback_show/definition.rb +1 -1
- data/scenarios/callback_show/request.rb +1 -1
- data/scenarios/callback_show/ruby.mako +20 -0
- data/scenarios/{customer_add_card → card_associate_to_customer}/definition.rb +0 -0
- data/scenarios/card_associate_to_customer/request.rb +3 -0
- data/scenarios/card_associate_to_customer/ruby.mako +49 -0
- data/scenarios/card_create/request.rb +1 -2
- data/scenarios/card_create/ruby.mako +44 -7
- data/scenarios/card_debit/definition.rb +1 -0
- data/scenarios/card_debit/request.rb +5 -0
- data/scenarios/card_debit/ruby.mako +49 -0
- data/scenarios/card_delete/request.rb +1 -1
- data/scenarios/card_delete/ruby.mako +5 -3
- data/scenarios/card_hold_capture/definition.rb +1 -0
- data/scenarios/card_hold_capture/request.rb +5 -0
- data/scenarios/card_hold_capture/ruby.mako +49 -0
- data/scenarios/card_hold_create/definition.rb +1 -0
- data/scenarios/card_hold_create/request.rb +5 -0
- data/scenarios/card_hold_create/ruby.mako +38 -0
- data/scenarios/card_hold_list/definition.rb +1 -0
- data/scenarios/card_hold_list/request.rb +2 -0
- data/scenarios/card_hold_list/ruby.mako +60 -0
- data/scenarios/card_hold_show/definition.rb +1 -0
- data/scenarios/card_hold_show/request.rb +2 -0
- data/scenarios/card_hold_show/ruby.mako +36 -0
- data/scenarios/card_hold_update/definition.rb +1 -0
- data/scenarios/card_hold_update/request.rb +7 -0
- data/scenarios/card_hold_update/ruby.mako +42 -0
- data/scenarios/card_hold_void/definition.rb +1 -0
- data/scenarios/card_hold_void/request.rb +3 -0
- data/scenarios/card_hold_void/ruby.mako +36 -0
- data/scenarios/card_list/definition.rb +1 -1
- data/scenarios/card_list/request.rb +1 -1
- data/scenarios/card_list/ruby.mako +78 -5
- data/scenarios/card_show/definition.rb +1 -1
- data/scenarios/card_show/request.rb +1 -1
- data/scenarios/card_show/ruby.mako +42 -4
- data/scenarios/card_update/definition.rb +1 -1
- data/scenarios/card_update/request.rb +1 -1
- data/scenarios/card_update/ruby.mako +45 -4
- data/scenarios/credit_list/request.rb +1 -3
- data/scenarios/credit_list/ruby.mako +34 -4
- data/scenarios/{credit_bank_account_list → credit_list_bank_account}/definition.rb +0 -0
- data/scenarios/credit_list_bank_account/request.rb +3 -0
- data/scenarios/credit_list_bank_account/ruby.mako +13 -0
- data/scenarios/credit_show/definition.rb +1 -1
- data/scenarios/credit_show/request.rb +1 -3
- data/scenarios/credit_show/ruby.mako +35 -5
- data/scenarios/{credit_create_new_bank_account → credit_update}/definition.rb +0 -0
- data/scenarios/credit_update/request.rb +7 -0
- data/scenarios/credit_update/ruby.mako +48 -0
- data/scenarios/customer_create/request.rb +4 -1
- data/scenarios/customer_create/ruby.mako +60 -3
- data/scenarios/customer_delete/request.rb +1 -1
- data/scenarios/customer_delete/ruby.mako +4 -2
- data/scenarios/customer_list/definition.rb +1 -0
- data/scenarios/customer_list/request.rb +2 -0
- data/scenarios/customer_list/ruby.mako +105 -0
- data/scenarios/customer_show/definition.rb +1 -0
- data/scenarios/customer_show/request.rb +2 -0
- data/scenarios/customer_show/ruby.mako +58 -0
- data/scenarios/customer_update/definition.rb +1 -0
- data/scenarios/customer_update/request.rb +7 -0
- data/scenarios/customer_update/ruby.mako +64 -0
- data/scenarios/debit_list/request.rb +1 -3
- data/scenarios/debit_list/ruby.mako +70 -4
- data/scenarios/debit_show/definition.rb +1 -1
- data/scenarios/debit_show/request.rb +1 -1
- data/scenarios/debit_show/ruby.mako +39 -4
- data/scenarios/debit_update/definition.rb +1 -1
- data/scenarios/debit_update/request.rb +1 -1
- data/scenarios/debit_update/ruby.mako +39 -4
- data/scenarios/event_list/definition.rb +1 -1
- data/scenarios/event_list/request.rb +1 -2
- data/scenarios/event_list/ruby.mako +54 -5
- data/scenarios/event_show/definition.rb +1 -1
- data/scenarios/event_show/request.rb +1 -1
- data/scenarios/event_show/ruby.mako +54 -4
- data/scenarios/helpers.rb +29 -2
- data/scenarios/order_create/definition.rb +1 -0
- data/scenarios/order_create/request.rb +5 -0
- data/scenarios/order_create/ruby.mako +46 -0
- data/scenarios/order_list/definition.rb +1 -0
- data/scenarios/order_list/request.rb +2 -0
- data/scenarios/order_list/ruby.mako +42 -0
- data/scenarios/order_show/definition.rb +1 -0
- data/scenarios/order_show/request.rb +2 -0
- data/scenarios/order_show/ruby.mako +44 -0
- data/scenarios/order_update/definition.rb +1 -0
- data/scenarios/order_update/request.rb +7 -0
- data/scenarios/order_update/ruby.mako +50 -0
- data/scenarios/refund_create/definition.rb +1 -1
- data/scenarios/refund_create/request.rb +2 -1
- data/scenarios/refund_create/ruby.mako +33 -5
- data/scenarios/refund_list/request.rb +1 -3
- data/scenarios/refund_list/ruby.mako +30 -4
- data/scenarios/refund_show/definition.rb +1 -1
- data/scenarios/refund_show/request.rb +1 -1
- data/scenarios/refund_show/ruby.mako +32 -4
- data/scenarios/refund_update/definition.rb +1 -1
- data/scenarios/refund_update/request.rb +1 -1
- data/scenarios/refund_update/ruby.mako +32 -4
- data/scenarios/render_mako.rb +59 -4
- data/scenarios/reversal_create/definition.rb +1 -0
- data/scenarios/reversal_create/request.rb +9 -0
- data/scenarios/reversal_create/ruby.mako +45 -0
- data/scenarios/reversal_list/definition.rb +1 -0
- data/scenarios/reversal_list/request.rb +2 -0
- data/scenarios/reversal_list/ruby.mako +36 -0
- data/scenarios/reversal_show/definition.rb +1 -0
- data/scenarios/reversal_show/request.rb +2 -0
- data/scenarios/reversal_show/ruby.mako +37 -0
- data/scenarios/reversal_update/definition.rb +1 -0
- data/scenarios/reversal_update/request.rb +7 -0
- data/scenarios/reversal_update/ruby.mako +44 -0
- data/spec/balanced/error_spec.rb +7 -7
- data/spec/balanced/pager_spec.rb +10 -10
- data/spec/balanced/resources/api_key_spec.rb +6 -35
- data/spec/balanced/resources/bank_account_spec.rb +123 -218
- data/spec/balanced/resources/callback_spec.rb +12 -13
- data/spec/balanced/resources/card_hold_spec.rb +103 -0
- data/spec/balanced/resources/card_spec.rb +101 -27
- data/spec/balanced/resources/credit_spec.rb +69 -35
- data/spec/balanced/resources/customer_spec.rb +91 -271
- data/spec/balanced/resources/debit_spec.rb +95 -0
- data/spec/balanced/resources/marketplace_spec.rb +17 -170
- data/spec/balanced/resources/order_spec.rb +364 -0
- data/spec/balanced/resources/resource_spec.rb +25 -32
- data/spec/balanced_spec.rb +18 -36
- data/spec/client_spec.rb +7 -7
- data/spec/spec_helper.rb +19 -18
- data/spec/utils_spec.rb +5 -5
- metadata +123 -165
- data/examples/bank_account_debits.rb +0 -57
- data/examples/customers.rb +0 -114
- data/examples/events_and_callbacks.rb +0 -81
- data/examples/examples.rb +0 -162
- data/examples/exception_handling.rb +0 -44
- data/lib/balanced/resources/account.rb +0 -179
- data/lib/balanced/resources/hold.rb +0 -61
- data/lib/balanced/resources/invoice.rb +0 -11
- data/lib/balanced/resources/merchant.rb +0 -21
- data/lib/balanced/resources/transaction.rb +0 -17
- data/scenarios/account_add_card/definition.rb +0 -1
- data/scenarios/account_add_card/request.rb +0 -3
- data/scenarios/account_add_card/ruby.mako +0 -11
- data/scenarios/account_capture_hold/definition.rb +0 -1
- data/scenarios/account_capture_hold/request.rb +0 -3
- data/scenarios/account_capture_hold/ruby.mako +0 -11
- data/scenarios/account_create/definition.rb +0 -1
- data/scenarios/account_create/request.rb +0 -2
- data/scenarios/account_create/ruby.mako +0 -10
- data/scenarios/account_create_buyer/definition.rb +0 -1
- data/scenarios/account_create_buyer/request.rb +0 -5
- data/scenarios/account_create_buyer/ruby.mako +0 -13
- data/scenarios/account_create_debit/definition.rb +0 -1
- data/scenarios/account_create_debit/request.rb +0 -3
- data/scenarios/account_create_debit/ruby.mako +0 -11
- data/scenarios/account_create_hold/definition.rb +0 -1
- data/scenarios/account_create_hold/request.rb +0 -3
- data/scenarios/account_create_hold/ruby.mako +0 -11
- data/scenarios/account_create_merchant/definition.rb +0 -1
- data/scenarios/account_create_merchant/request.rb +0 -3
- data/scenarios/account_create_merchant/ruby.mako +0 -11
- data/scenarios/account_underwrite_business/definition.rb +0 -1
- data/scenarios/account_underwrite_business/request.rb +0 -17
- data/scenarios/account_underwrite_business/ruby.mako +0 -37
- data/scenarios/account_underwrite_person/definition.rb +0 -1
- data/scenarios/account_underwrite_person/request.rb +0 -18
- data/scenarios/account_underwrite_person/ruby.mako +0 -31
- data/scenarios/bank_account_find_and_credit/definition.rb +0 -1
- data/scenarios/bank_account_find_and_credit/request.rb +0 -5
- data/scenarios/bank_account_find_and_credit/ruby.mako +0 -13
- data/scenarios/bank_account_find_and_delete/definition.rb +0 -1
- data/scenarios/bank_account_find_and_delete/request.rb +0 -3
- data/scenarios/bank_account_find_and_delete/ruby.mako +0 -10
- data/scenarios/bank_account_invalid_routing_number/definition.rb +0 -1
- data/scenarios/bank_account_invalid_routing_number/request.rb +0 -11
- data/scenarios/bank_account_invalid_routing_number/ruby.mako +0 -16
- data/scenarios/card_invalidate/definition.rb +0 -1
- data/scenarios/card_invalidate/request.rb +0 -3
- data/scenarios/card_invalidate/ruby.mako +0 -10
- data/scenarios/credit_account_list/definition.rb +0 -1
- data/scenarios/credit_account_list/request.rb +0 -3
- data/scenarios/credit_account_list/ruby.mako +0 -10
- data/scenarios/credit_account_merchant_create/definition.rb +0 -1
- data/scenarios/credit_account_merchant_create/request.rb +0 -5
- data/scenarios/credit_account_merchant_create/ruby.mako +0 -12
- data/scenarios/credit_bank_account_list/request.rb +0 -5
- data/scenarios/credit_bank_account_list/ruby.mako +0 -12
- data/scenarios/credit_create_existing_bank_account/request.rb +0 -5
- data/scenarios/credit_create_existing_bank_account/ruby.mako +0 -12
- data/scenarios/credit_create_new_bank_account/request.rb +0 -10
- data/scenarios/credit_create_new_bank_account/ruby.mako +0 -21
- data/scenarios/credit_customer_list/definition.rb +0 -1
- data/scenarios/credit_customer_list/request.rb +0 -4
- data/scenarios/credit_customer_list/ruby.mako +0 -12
- data/scenarios/credit_failed_state/definition.rb +0 -1
- data/scenarios/credit_failed_state/request.rb +0 -10
- data/scenarios/credit_failed_state/ruby.mako +0 -18
- data/scenarios/credit_paid_state/definition.rb +0 -1
- data/scenarios/credit_paid_state/request.rb +0 -11
- data/scenarios/credit_paid_state/ruby.mako +0 -19
- data/scenarios/credit_pending_state/definition.rb +0 -1
- data/scenarios/credit_pending_state/request.rb +0 -9
- data/scenarios/credit_pending_state/ruby.mako +0 -17
- data/scenarios/customer_add_bank_account/request.rb +0 -3
- data/scenarios/customer_add_bank_account/ruby.mako +0 -11
- data/scenarios/customer_add_card/request.rb +0 -3
- data/scenarios/customer_add_card/ruby.mako +0 -11
- data/scenarios/customer_capture_hold/definition.rb +0 -1
- data/scenarios/customer_capture_hold/request.rb +0 -3
- data/scenarios/customer_capture_hold/ruby.mako +0 -0
- data/scenarios/customer_create_debit/definition.rb +0 -1
- data/scenarios/customer_create_debit/request.rb +0 -3
- data/scenarios/customer_create_debit/ruby.mako +0 -11
- data/scenarios/customer_create_hold/definition.rb +0 -1
- data/scenarios/customer_create_hold/request.rb +0 -3
- data/scenarios/customer_create_hold/ruby.mako +0 -11
- data/scenarios/customer_credit/definition.rb +0 -1
- data/scenarios/customer_credit/request.rb +0 -2
- data/scenarios/customer_credit/ruby.mako +0 -8
- data/scenarios/debit_account_list/definition.rb +0 -1
- data/scenarios/debit_account_list/request.rb +0 -3
- data/scenarios/debit_account_list/ruby.mako +0 -10
- data/scenarios/debit_create/definition.rb +0 -1
- data/scenarios/debit_create/request.rb +0 -5
- data/scenarios/debit_create/ruby.mako +0 -15
- data/scenarios/debit_customer_list/definition.rb +0 -1
- data/scenarios/debit_customer_list/request.rb +0 -4
- data/scenarios/debit_customer_list/ruby.mako +0 -12
- data/scenarios/debit_refund/definition.rb +0 -1
- data/scenarios/debit_refund/request.rb +0 -3
- data/scenarios/debit_refund/ruby.mako +0 -10
- data/scenarios/delete.rb +0 -7
- data/scenarios/event_replay/definition.rb +0 -0
- data/scenarios/event_replay/request.rb +0 -1
- data/scenarios/event_replay/ruby.mako +0 -0
- data/scenarios/execute.rb +0 -27
- data/scenarios/hold_account_list/definition.rb +0 -1
- data/scenarios/hold_account_list/request.rb +0 -3
- data/scenarios/hold_account_list/ruby.mako +0 -10
- data/scenarios/hold_capture/definition.rb +0 -1
- data/scenarios/hold_capture/request.rb +0 -5
- data/scenarios/hold_capture/ruby.mako +0 -15
- data/scenarios/hold_create/definition.rb +0 -1
- data/scenarios/hold_create/request.rb +0 -5
- data/scenarios/hold_create/ruby.mako +0 -14
- data/scenarios/hold_customer_list/definition.rb +0 -1
- data/scenarios/hold_customer_list/request.rb +0 -3
- data/scenarios/hold_customer_list/ruby.mako +0 -11
- data/scenarios/hold_list/definition.rb +0 -1
- data/scenarios/hold_list/request.rb +0 -4
- data/scenarios/hold_list/ruby.mako +0 -11
- data/scenarios/hold_show/definition.rb +0 -1
- data/scenarios/hold_show/request.rb +0 -2
- data/scenarios/hold_show/ruby.mako +0 -10
- data/scenarios/hold_update/definition.rb +0 -1
- data/scenarios/hold_update/request.rb +0 -7
- data/scenarios/hold_update/ruby.mako +0 -16
- data/scenarios/hold_void/definition.rb +0 -1
- data/scenarios/hold_void/request.rb +0 -3
- data/scenarios/hold_void/ruby.mako +0 -10
- data/scenarios/refund_account_list/definition.rb +0 -1
- data/scenarios/refund_account_list/request.rb +0 -3
- data/scenarios/refund_account_list/ruby.mako +0 -10
- data/scenarios/refund_customer_list/definition.rb +0 -1
- data/scenarios/refund_customer_list/request.rb +0 -3
- data/scenarios/refund_customer_list/ruby.mako +0 -11
- data/scenarios/render.rb +0 -32
- data/scenarios/render_one.rb +0 -32
- data/spec/balanced/resources/account_spec.rb +0 -636
- data/spec/balanced/resources/hold_spec.rb +0 -48
- data/spec/balanced/resources/merchant_spec.rb +0 -6
- data/spec/scenarios_spec.rb +0 -39
- data/upload_docs.rb +0 -40
@@ -13,30 +13,22 @@ module Balanced
|
|
13
13
|
#
|
14
14
|
class Debit
|
15
15
|
include Balanced::Resource
|
16
|
+
include Balanced::HypermediaRegistry
|
16
17
|
|
17
|
-
|
18
|
-
Balanced::Utils.stringify_keys! attributes
|
19
|
-
unless attributes.has_key? 'uri'
|
20
|
-
attributes['uri'] = self.class.uri
|
21
|
-
end
|
22
|
-
super attributes
|
23
|
-
end
|
18
|
+
define_hypermedia_types [:debits]
|
24
19
|
|
25
20
|
# Refunds this Debit. If no amount is specified it will refund the entire
|
26
21
|
# amount of the Debit, you may create many Refunds up to the sum total
|
27
22
|
# of the original Debit's amount.
|
28
23
|
#
|
29
24
|
# @return [Refund]
|
30
|
-
def refund
|
31
|
-
warn_on_positional args
|
25
|
+
def refund(options={})
|
32
26
|
|
33
|
-
|
34
|
-
|
35
|
-
description = args[1] || options.fetch(:description) { nil }
|
27
|
+
amount = options[:amount]
|
28
|
+
description = options[:description]
|
36
29
|
|
37
30
|
refund = Refund.new(
|
38
|
-
:
|
39
|
-
:debit_uri => self.uri,
|
31
|
+
:href => self.refunds.href,
|
40
32
|
:amount => amount,
|
41
33
|
:description => description
|
42
34
|
)
|
@@ -7,21 +7,27 @@ module Balanced
|
|
7
7
|
#
|
8
8
|
class Event
|
9
9
|
include Balanced::Resource
|
10
|
+
include Balanced::HypermediaRegistry
|
10
11
|
|
12
|
+
define_hypermedia_types [:events]
|
11
13
|
end
|
12
14
|
|
13
15
|
# Represents a single event being sent to a callback.
|
14
16
|
#
|
15
17
|
class EventCallback
|
16
18
|
include Balanced::Resource
|
19
|
+
include Balanced::HypermediaRegistry
|
17
20
|
|
21
|
+
define_hypermedia_types [:event_callbacks]
|
18
22
|
end
|
19
23
|
|
20
24
|
# Represents a single log of an event sent to a callback.
|
21
25
|
#
|
22
26
|
class Log
|
23
27
|
include Balanced::Resource
|
28
|
+
include Balanced::HypermediaRegistry
|
24
29
|
|
30
|
+
define_hypermedia_types [:logs]
|
25
31
|
end
|
26
32
|
|
27
33
|
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module Balanced
|
2
|
+
|
3
|
+
module HypermediaRegistry
|
4
|
+
|
5
|
+
def self.included(base) # :nodoc:
|
6
|
+
base.extend ClassMethods
|
7
|
+
end
|
8
|
+
|
9
|
+
module ClassMethods
|
10
|
+
|
11
|
+
def define_hypermedia_types(types)
|
12
|
+
@hypermedia_types = types.map! do |t|
|
13
|
+
t.to_s
|
14
|
+
end.sort!.freeze
|
15
|
+
|
16
|
+
@hypermedia_types.each do |type|
|
17
|
+
Balanced.hypermedia_registry[type] = self
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# @return [Array, nil] The list of hypermedia types this class is
|
22
|
+
# mapped to for interacting with the resource.
|
23
|
+
attr_reader :hypermedia_types
|
24
|
+
end
|
25
|
+
|
26
|
+
end
|
27
|
+
|
28
|
+
end
|
@@ -3,8 +3,9 @@ module Balanced
|
|
3
3
|
#
|
4
4
|
class Marketplace
|
5
5
|
include Balanced::Resource
|
6
|
+
include Balanced::HypermediaRegistry
|
6
7
|
|
7
|
-
|
8
|
+
define_hypermedia_types [:marketplaces]
|
8
9
|
|
9
10
|
class << self
|
10
11
|
|
@@ -13,37 +14,13 @@ module Balanced
|
|
13
14
|
#
|
14
15
|
# @return [Marketplace]
|
15
16
|
def my_marketplace
|
16
|
-
|
17
|
-
@@marketplace_uri = marketplace.uri if marketplace
|
18
|
-
marketplace
|
19
|
-
end
|
20
|
-
alias mine my_marketplace
|
21
|
-
|
22
|
-
# @return [String, nil] the marketplace's URI
|
23
|
-
def marketplace_uri
|
24
|
-
if !@@marketplace_uri and Balanced.is_configured_with_api_key?
|
25
|
-
begin
|
26
|
-
mine
|
27
|
-
rescue Balanced::Error
|
28
|
-
# do nothing..
|
29
|
-
end
|
30
|
-
end
|
31
|
-
@@marketplace_uri
|
17
|
+
Balanced::Marketplace.where.first
|
32
18
|
end
|
33
19
|
|
34
|
-
|
35
|
-
!!marketplace_uri
|
36
|
-
end
|
20
|
+
alias mine my_marketplace
|
37
21
|
|
38
22
|
end
|
39
23
|
|
40
|
-
# @return [Marketplace]
|
41
|
-
def save
|
42
|
-
marketplace = super
|
43
|
-
@@marketplace_uri = marketplace.uri
|
44
|
-
marketplace
|
45
|
-
end
|
46
|
-
|
47
24
|
# Returns an instance representing the marketplace associated with
|
48
25
|
# the current API key.
|
49
26
|
#
|
@@ -51,183 +28,8 @@ module Balanced
|
|
51
28
|
def my_marketplace
|
52
29
|
self.class.my_marketplace
|
53
30
|
end
|
54
|
-
alias mine my_marketplace
|
55
|
-
|
56
|
-
# Create an Account associated with this Marketplace. This account
|
57
|
-
# will have no roles associated with it.
|
58
|
-
#
|
59
|
-
# @return [Account]
|
60
|
-
def create_account options={}
|
61
|
-
email_address = options.fetch(:email_address) { nil }
|
62
|
-
name = options.fetch(:name) { nil }
|
63
|
-
meta = options.fetch(:meta) { nil }
|
64
|
-
|
65
|
-
account_attributes = {
|
66
|
-
:uri => self.accounts_uri,
|
67
|
-
:email_address => email_address,
|
68
|
-
:name => name,
|
69
|
-
:meta => meta
|
70
|
-
}
|
71
|
-
|
72
|
-
account = Account.new account_attributes
|
73
|
-
account.save
|
74
|
-
end
|
75
|
-
|
76
|
-
|
77
|
-
# Create a buyer Account associated with this Marketplace.
|
78
|
-
#
|
79
|
-
# @return [Account]
|
80
|
-
def create_buyer *args
|
81
|
-
warn_on_positional args
|
82
31
|
|
83
|
-
|
84
|
-
email_address = args[0] || options.fetch(:email_address) { nil }
|
85
|
-
card = args[1] || options.fetch(:card_uri) { options.fetch(:card) { nil} }
|
86
|
-
name = args[2] || options.fetch(:name) { nil }
|
87
|
-
meta = args[3] || options.fetch(:meta) { nil }
|
88
|
-
|
89
|
-
account_attributes = {
|
90
|
-
:uri => self.accounts_uri,
|
91
|
-
:email_address => email_address,
|
92
|
-
:name => name,
|
93
|
-
:meta => meta,
|
94
|
-
}
|
95
|
-
|
96
|
-
if card.respond_to? :keys
|
97
|
-
account_attributes[:card] = card
|
98
|
-
else
|
99
|
-
account_attributes[:card_uri] = card
|
100
|
-
end
|
101
|
-
|
102
|
-
account = Account.new account_attributes
|
103
|
-
account.save
|
104
|
-
end
|
105
|
-
|
106
|
-
# Creates a Merchant Account associated with this Marketplace.
|
107
|
-
#
|
108
|
-
# This method may return 300 if you have not supplied enough information
|
109
|
-
# for Balanced to identify the Merchant. You may re-submit the request
|
110
|
-
# with more information, or redirect the Merchant to the supplied url
|
111
|
-
# so they may manually sign up.
|
112
|
-
#
|
113
|
-
# When you receive a merchant_uri from balanced, just pass it in:
|
114
|
-
#
|
115
|
-
# @example
|
116
|
-
# Account.create_merchant('bob@example.com', '/v1/TEST-MRxxxx')
|
117
|
-
#
|
118
|
-
# @return [Account]
|
119
|
-
def create_merchant *args
|
120
|
-
warn_on_positional args
|
121
|
-
|
122
|
-
options = args.last.is_a?(Hash) ? args.pop : {}
|
123
|
-
email_address = args[0] || options.fetch(:email_address) { nil }
|
124
|
-
merchant = args[1] || options.fetch(:merchant) { nil }
|
125
|
-
bank_account_uri = args[2] || options.fetch(:bank_account_uri) { nil }
|
126
|
-
name = args[3] || options.fetch(:name) { nil }
|
127
|
-
meta = args[4] || options.fetch(:meta) { nil }
|
128
|
-
bank_account = args[5] || options.fetch(:bank_account) { nil }
|
129
|
-
|
130
|
-
account_attributes = {
|
131
|
-
:uri => self.accounts_uri,
|
132
|
-
:email_address => email_address,
|
133
|
-
:bank_account_uri => bank_account_uri,
|
134
|
-
:name => name,
|
135
|
-
:meta => meta,
|
136
|
-
:bank_account => bank_account
|
137
|
-
}
|
138
|
-
|
139
|
-
if merchant.respond_to? :keys
|
140
|
-
account_attributes[:merchant] = merchant
|
141
|
-
else
|
142
|
-
account_attributes[:merchant_uri] = merchant
|
143
|
-
end
|
144
|
-
|
145
|
-
account = Account.new account_attributes
|
146
|
-
account.save
|
147
|
-
end
|
148
|
-
|
149
|
-
# Creates a BankAccount object tied to this marketplace, for use with
|
150
|
-
# accounts
|
151
|
-
#
|
152
|
-
# @return [BankAccount]
|
153
|
-
def create_bank_account *args
|
154
|
-
warn_on_positional args
|
155
|
-
|
156
|
-
options = args.last.is_a?(Hash) ? args.pop : {}
|
157
|
-
name = args[0] || options.fetch(:name) { }
|
158
|
-
account_number = args[1] || options.fetch(:account_number) { nil }
|
159
|
-
bank_code = args[2] || options.fetch(:bank_code) {
|
160
|
-
options.fetch(:routing_number) {
|
161
|
-
nil
|
162
|
-
}
|
163
|
-
}
|
164
|
-
meta = args[3] || options.fetch(:meta) { nil }
|
165
|
-
|
166
|
-
bank_account = BankAccount.new(
|
167
|
-
:uri => self.bank_accounts_uri,
|
168
|
-
:name => name,
|
169
|
-
:account_number => account_number,
|
170
|
-
:bank_code => bank_code,
|
171
|
-
:meta => meta
|
172
|
-
)
|
173
|
-
|
174
|
-
bank_account.save
|
175
|
-
end
|
176
|
-
|
177
|
-
# Creates a Card object tied to this marketplace, for use with
|
178
|
-
# accounts
|
179
|
-
#
|
180
|
-
# @return [Card]
|
181
|
-
def create_card *args
|
182
|
-
warn_on_positional args
|
183
|
-
|
184
|
-
options = args.last.is_a?(Hash) ? args.pop : {}
|
185
|
-
card_number = args[0] || options.fetch(:card_number) { nil }
|
186
|
-
expiration_month = args[1] || options.fetch(:expiration_month) { nil }
|
187
|
-
expiration_year = args[2] || options.fetch(:expiration_year) { nil }
|
188
|
-
security_code = args[3] || options.fetch(:security_code) { nil }
|
189
|
-
postal_code = args[4] || options.fetch(:postal_code) { nil }
|
190
|
-
name = args[5] || options.fetch(:name) { nil }
|
191
|
-
phone_number = args[6] || options.fetch(:phone_number) { nil }
|
192
|
-
street_address = args[7] || options.fetch(:street_address) { nil }
|
193
|
-
city = args[8] || options.fetch(:city) { nil }
|
194
|
-
country_code = args[9] || options.fetch(:country_code) { nil }
|
195
|
-
meta = args[10] || options.fetch(:meta) { nil }
|
196
|
-
|
197
|
-
card = Card.new(
|
198
|
-
:uri => self.cards_uri,
|
199
|
-
:card_number => card_number,
|
200
|
-
:expiration_month => expiration_month,
|
201
|
-
:expiration_year => expiration_year,
|
202
|
-
:name => name,
|
203
|
-
:security_code => security_code,
|
204
|
-
:street_address => street_address,
|
205
|
-
:city => city,
|
206
|
-
:postal_code => postal_code,
|
207
|
-
:country_code => country_code,
|
208
|
-
:phone_number => phone_number,
|
209
|
-
:meta => meta
|
210
|
-
)
|
211
|
-
card.save
|
212
|
-
end
|
213
|
-
|
214
|
-
def create_customer(options = {})
|
215
|
-
customer = Customer.new(options)
|
216
|
-
customer.save
|
217
|
-
end
|
218
|
-
|
219
|
-
def create_callback *args
|
220
|
-
warn_on_positional args
|
221
|
-
|
222
|
-
options = args.last.is_a?(Hash) ? args.pop : {}
|
223
|
-
url = args[0] || options.fetch(:url) { nil }
|
224
|
-
|
225
|
-
callback = Callback.new(
|
226
|
-
'uri' => self.callbacks_uri,
|
227
|
-
'url' => url
|
228
|
-
)
|
229
|
-
callback.save
|
230
|
-
end
|
32
|
+
alias mine my_marketplace
|
231
33
|
|
232
34
|
end
|
233
35
|
|
@@ -0,0 +1,71 @@
|
|
1
|
+
module Balanced
|
2
|
+
|
3
|
+
class Error
|
4
|
+
|
5
|
+
class OrderCancelFailure < Balanced::StandardError;
|
6
|
+
|
7
|
+
end
|
8
|
+
|
9
|
+
end
|
10
|
+
|
11
|
+
class Order
|
12
|
+
include Balanced::Resource
|
13
|
+
include Balanced::HypermediaRegistry
|
14
|
+
|
15
|
+
define_hypermedia_types [:orders]
|
16
|
+
|
17
|
+
def cancel
|
18
|
+
reload
|
19
|
+
|
20
|
+
# shouldn't fail unless there's a network outage
|
21
|
+
# let's just hope there's not (for the sake of time)
|
22
|
+
self.debits.map { |d|
|
23
|
+
begin
|
24
|
+
d.refund rescue d.refunds.to_a
|
25
|
+
end }.flatten
|
26
|
+
end
|
27
|
+
|
28
|
+
alias refund cancel
|
29
|
+
|
30
|
+
def debit_from(options={})
|
31
|
+
Balanced::Utils.assert_required_keys(options, :required => [:source, :amount])
|
32
|
+
|
33
|
+
source = options[:source]
|
34
|
+
amount = options[:amount]
|
35
|
+
|
36
|
+
debit = source.debit(
|
37
|
+
:amount => amount,
|
38
|
+
:order => self.href
|
39
|
+
)
|
40
|
+
|
41
|
+
debit
|
42
|
+
end
|
43
|
+
|
44
|
+
def credit_to(options={})
|
45
|
+
Balanced::Utils.assert_required_keys(options, :required => [:destination, :amount])
|
46
|
+
destination = options[:destination]
|
47
|
+
amount = options[:amount]
|
48
|
+
# should have a way here to disburse more funds to another account, but not required
|
49
|
+
destination.credit(:amount => amount, :order => self.href)
|
50
|
+
end
|
51
|
+
|
52
|
+
private
|
53
|
+
|
54
|
+
def has_initiated_credits?
|
55
|
+
self.credits.total != 0
|
56
|
+
end
|
57
|
+
|
58
|
+
def has_initiated_refunds?
|
59
|
+
self.refunds.total != 0
|
60
|
+
end
|
61
|
+
|
62
|
+
def has_initiated_reversals?
|
63
|
+
self.reversals.total != 0
|
64
|
+
end
|
65
|
+
|
66
|
+
def has_initiated_debits?
|
67
|
+
self.debits.total != 0
|
68
|
+
end
|
69
|
+
|
70
|
+
end
|
71
|
+
end
|
@@ -6,14 +6,9 @@ module Balanced
|
|
6
6
|
#
|
7
7
|
class Refund
|
8
8
|
include Balanced::Resource
|
9
|
+
include Balanced::HypermediaRegistry
|
9
10
|
|
10
|
-
|
11
|
-
Balanced::Utils.stringify_keys! attributes
|
12
|
-
unless attributes.has_key? 'uri'
|
13
|
-
attributes['uri'] = self.class.uri
|
14
|
-
end
|
15
|
-
super attributes
|
16
|
-
end
|
11
|
+
define_hypermedia_types [:refunds]
|
17
12
|
|
18
13
|
end
|
19
14
|
|
@@ -1,95 +1,160 @@
|
|
1
1
|
require File.expand_path('../../pager', __FILE__)
|
2
|
+
require File.expand_path('../../utils', __FILE__)
|
3
|
+
require 'addressable/template'
|
4
|
+
require 'pp'
|
5
|
+
|
2
6
|
|
3
7
|
module Balanced
|
8
|
+
|
4
9
|
module Resource
|
10
|
+
|
5
11
|
attr_accessor :attributes
|
12
|
+
attr_accessor :hyperlinks
|
13
|
+
|
14
|
+
attr_accessor :id
|
15
|
+
attr_accessor :href
|
16
|
+
attr_accessor :links
|
17
|
+
|
18
|
+
def initialize(attributes = {})
|
19
|
+
@attributes = Utils.indifferent_read_access attributes
|
20
|
+
@hyperlinks = {}
|
21
|
+
end
|
22
|
+
|
23
|
+
def id
|
24
|
+
(attributes[:id] ||= nil)
|
25
|
+
end
|
6
26
|
|
7
|
-
def
|
8
|
-
|
27
|
+
def id=(value)
|
28
|
+
attributes[:id] = value
|
29
|
+
end
|
30
|
+
|
31
|
+
def href
|
32
|
+
(attributes[:href] ||= self.class.collection_name)
|
33
|
+
end
|
34
|
+
|
35
|
+
def links
|
36
|
+
(attributes[:links] ||= {})
|
37
|
+
end
|
38
|
+
|
39
|
+
def links=(value)
|
40
|
+
attributes[:links] = value
|
41
|
+
end
|
42
|
+
|
43
|
+
def hydrate(links, meta)
|
44
|
+
# links here is 'marketplaces.events'
|
45
|
+
links.each do |key, uri|
|
46
|
+
# key is 'marketplaces.events'
|
47
|
+
property = key.sub(/.*?\./, '')
|
48
|
+
# property here is events
|
49
|
+
# if marketplace.links is a hash
|
50
|
+
# for each property and uri, we want to set them on the instance
|
51
|
+
# as pagers.
|
52
|
+
#
|
53
|
+
# right?
|
54
|
+
if self.links.include? property
|
55
|
+
link_value = self.links[property]
|
56
|
+
# expands /customers/{marketplaces.owner_customer} to /customers/ACxxxxx
|
57
|
+
template = Addressable::Template.new(uri)
|
58
|
+
uri = template.expand("#{key}" => link_value).to_s
|
59
|
+
@hyperlinks[property] = Balanced::Utils.callable(
|
60
|
+
link_value.nil? ? link_value : lambda { self.class.find(uri) }
|
61
|
+
)
|
62
|
+
else
|
63
|
+
unless uri.nil? || uri.is_a?(Hash)
|
64
|
+
# matches something like '/blah/{class.attribute}/bliakdf'
|
65
|
+
begin
|
66
|
+
match_data = /\{(#{self.class.collection_name}\.(\w+))\}/.match(uri)
|
67
|
+
rescue TypeError => ex
|
68
|
+
puts 'what the fuck'
|
69
|
+
raise ex
|
70
|
+
end
|
71
|
+
unless match_data.nil?
|
72
|
+
attribute_path = match_data[1]
|
73
|
+
attribute_name = match_data[2]
|
74
|
+
template = Addressable::Template.new(uri)
|
75
|
+
uri = template.expand("#{attribute_path}" => @attributes[attribute_name]).to_s
|
76
|
+
end
|
77
|
+
end
|
78
|
+
@hyperlinks[property] = Balanced::Utils.callable(Balanced::Pager.new(uri, {}))
|
79
|
+
end
|
80
|
+
end
|
9
81
|
end
|
10
82
|
|
11
83
|
# delegate the query to the pager module
|
12
84
|
|
13
|
-
def
|
85
|
+
def fetch(*arguments)
|
14
86
|
self.class.find *arguments
|
15
87
|
end
|
88
|
+
|
89
|
+
alias find fetch
|
16
90
|
|
17
91
|
def save
|
18
|
-
|
92
|
+
href = @attributes.delete('href')
|
19
93
|
method = :post
|
20
|
-
if
|
21
|
-
|
22
|
-
elsif !Balanced.is_collection(
|
94
|
+
if href.nil?
|
95
|
+
href = self.class.collection_path
|
96
|
+
elsif !Balanced.is_collection(href)
|
23
97
|
method = :put
|
24
98
|
end
|
99
|
+
|
25
100
|
attributes_to_submit = self.sanitize
|
26
|
-
|
101
|
+
begin
|
102
|
+
@response = Balanced.send(method, href, attributes_to_submit)
|
103
|
+
rescue Balanced::Error
|
104
|
+
# restore the href on the instance if there was an exception
|
105
|
+
# this will allow us to try to fix any attributes and save again
|
106
|
+
@attributes['href'] = href
|
107
|
+
raise
|
108
|
+
end
|
109
|
+
|
27
110
|
reload @response
|
28
111
|
end
|
29
112
|
|
30
113
|
def sanitize
|
31
114
|
to_submit = {}
|
32
115
|
@attributes.each do |key, value|
|
33
|
-
|
116
|
+
unless value.is_a? Balanced::Resource
|
34
117
|
to_submit[key] = value
|
35
118
|
end
|
36
119
|
end
|
37
120
|
to_submit
|
38
121
|
end
|
39
122
|
|
40
|
-
def warn_on_positional args
|
41
|
-
msg = <<-WARNING
|
42
|
-
Called from: #{caller[1]}
|
43
|
-
#############################################################
|
44
|
-
# WARNING! WARNING! WARNING! WARNING! WARNING! WARNING! #
|
45
|
-
#############################################################
|
46
|
-
|
47
|
-
Using positional arguments is **DEPRECATED**. Please use the
|
48
|
-
keyword options pattern instead. Version __0.7.0__ of the
|
49
|
-
Ruby client will not support positional arguments.
|
50
|
-
|
51
|
-
If you need help, please hop on irc.freenode.net #balanced
|
52
|
-
or contact support@balancedpayments.com
|
53
|
-
WARNING
|
54
|
-
# warn if [...] otherwise, it's ok if it's: [], [{}] or [{...}]
|
55
|
-
unless (args.size == 1 and args.last.is_a? Hash) or (args.size == 0)
|
56
|
-
warn msg
|
57
|
-
end
|
58
|
-
end
|
59
|
-
|
60
123
|
def response
|
61
124
|
@response
|
62
125
|
end
|
63
|
-
private :response
|
64
126
|
|
65
|
-
|
66
|
-
Balanced.delete @attributes[:uri]
|
67
|
-
end
|
127
|
+
private :response
|
68
128
|
|
69
129
|
def unstore
|
70
|
-
|
130
|
+
Balanced.unstore @attributes[:href]
|
71
131
|
end
|
132
|
+
alias_method :destroy, :unstore
|
72
133
|
|
73
|
-
def reload
|
134
|
+
def reload(the_response = nil)
|
74
135
|
if the_response
|
75
136
|
return if the_response.body.to_s.length.zero?
|
76
137
|
fresh = self.class.construct_from_response the_response.body
|
77
138
|
else
|
78
|
-
fresh = self.find(@attributes[:
|
139
|
+
fresh = self.find(@attributes[:href])
|
79
140
|
end
|
80
141
|
fresh and copy_from fresh
|
81
142
|
self
|
82
143
|
end
|
83
144
|
|
84
|
-
def copy_from
|
145
|
+
def copy_from(other)
|
85
146
|
other.instance_variables.each do |ivar|
|
86
147
|
instance_variable_set ivar, other.instance_variable_get(ivar)
|
87
148
|
end
|
88
149
|
end
|
89
150
|
|
90
151
|
def method_missing(method, *args, &block)
|
152
|
+
if @attributes.has_key?(method.to_s)
|
153
|
+
return @attributes[method.to_s]
|
154
|
+
end
|
155
|
+
|
91
156
|
case method.to_s
|
92
|
-
when /(.+)
|
157
|
+
when /(.+)=$/
|
93
158
|
attr = method.to_s.chop
|
94
159
|
@attributes[attr] = args[0]
|
95
160
|
else
|
@@ -105,29 +170,36 @@ module Balanced
|
|
105
170
|
# method requests that are essentially #{method}_uri.
|
106
171
|
#
|
107
172
|
# This solves the acute problem, for now.
|
108
|
-
if @
|
109
|
-
value = @
|
110
|
-
|
111
|
-
|
112
|
-
# it ends in a symbol then we should allow a lazy executor of
|
113
|
-
# the query pager
|
114
|
-
if Balanced.is_collection(value)
|
115
|
-
pager = Balanced::Pager.new value, {}
|
116
|
-
return pager.to_a
|
117
|
-
else
|
118
|
-
return values_class.find(value)
|
119
|
-
end
|
173
|
+
if @hyperlinks.has_key? "#{method}"
|
174
|
+
value = @hyperlinks["#{method}"]
|
175
|
+
result = value.call
|
176
|
+
return result
|
120
177
|
else
|
121
178
|
super
|
122
179
|
end
|
123
180
|
end
|
124
181
|
end
|
125
|
-
|
182
|
+
|
183
|
+
if RUBY_VERSION < '1.9'
|
184
|
+
def respond_to?(method_name, include_private=false)
|
185
|
+
does_resource_respond_to?(method_name) || super
|
186
|
+
end
|
187
|
+
else
|
188
|
+
def respond_to_missing?(method_name, include_private=false)
|
189
|
+
does_resource_respond_to?(method_name) || super
|
190
|
+
end
|
191
|
+
end
|
192
|
+
|
193
|
+
def does_resource_respond_to?(method_name)
|
194
|
+
@attributes.has_key?(method_name.to_s) or @hyperlinks.has_key?(method_name.to_s)
|
195
|
+
end
|
196
|
+
|
126
197
|
def self.included(base)
|
127
198
|
base.extend ClassMethods
|
128
199
|
end
|
129
200
|
|
130
201
|
module ClassMethods
|
202
|
+
|
131
203
|
def resource_name
|
132
204
|
Utils.demodulize name
|
133
205
|
end
|
@@ -137,86 +209,77 @@ module Balanced
|
|
137
209
|
end
|
138
210
|
|
139
211
|
def collection_path
|
140
|
-
|
212
|
+
# this is to just support version1 stuff, but in reality, if we get here
|
213
|
+
# we should throw an exception since we do not want to use v1 ever.
|
214
|
+
if Balanced.config[:version] == '1'
|
215
|
+
# XXX: we should raise an exception if we get here.
|
216
|
+
["/v#{Balanced.config[:version]}", collection_name].compact.join '/'
|
217
|
+
else
|
218
|
+
collection_name
|
219
|
+
end
|
141
220
|
end
|
142
221
|
|
143
|
-
def
|
144
|
-
|
222
|
+
def href
|
223
|
+
collection_path
|
145
224
|
end
|
146
225
|
|
147
|
-
|
148
|
-
|
149
|
-
# @example A Balanced::Account resource
|
150
|
-
# Balanced::Account.uri # => "/v1/marketplaces/TEST-MP72zVdg2j9IiqRffW9lczRZ/accounts"
|
151
|
-
#
|
152
|
-
# @return [String] the uri of the instance or the class
|
153
|
-
def uri
|
154
|
-
# the uri of a particular resource depends if there's a marketplace
|
155
|
-
# created or not. if there's a marketplace, then all resources have their
|
156
|
-
# own uri from there and the top level ones. if there's not a marketplace
|
157
|
-
#
|
158
|
-
# if there's an api key, then the merchant is available
|
159
|
-
# if there's no api key, the only resources exposed are purely top level
|
160
|
-
if self == Balanced::Merchant || self == Balanced::Marketplace || self == Balanced::ApiKey
|
161
|
-
collection_path
|
162
|
-
else
|
163
|
-
unless Balanced::Marketplace.marketplace_exists?
|
164
|
-
raise Balanced::StandardError, "#{self.name} is nested under a marketplace, which is not created or configured."
|
165
|
-
end
|
166
|
-
|
167
|
-
Balanced::Marketplace.marketplace_uri + "/#{collection_name}"
|
168
|
-
end
|
226
|
+
def member_name
|
227
|
+
Utils.underscore resource_name
|
169
228
|
end
|
170
229
|
|
171
|
-
def construct_from_response
|
172
|
-
payload = Balanced::Utils.
|
173
|
-
return payload if payload[:uri].nil?
|
174
|
-
klass = Balanced.from_uri(payload[:uri])
|
175
|
-
instance = klass.new payload
|
230
|
+
def construct_from_response(payload)
|
231
|
+
payload = Balanced::Utils.indifferent_read_access payload
|
176
232
|
|
177
|
-
|
178
|
-
|
233
|
+
links = payload.delete('links') || {}
|
234
|
+
meta = payload.delete('meta') || {}
|
179
235
|
|
180
|
-
payload.
|
236
|
+
if payload.length > 1
|
237
|
+
raise 'NOT SUPPORTED YET'
|
238
|
+
end
|
181
239
|
|
182
|
-
|
183
|
-
|
184
|
-
|
185
|
-
|
186
|
-
|
240
|
+
instance = nil
|
241
|
+
# the remaining keys here are just hypermedia resources
|
242
|
+
payload.each do |key, value|
|
243
|
+
# > If the length of an array at a resource key is greater than one,
|
244
|
+
# the value represents a list of documents.
|
245
|
+
if value.length > 1
|
246
|
+
# key is a collection, this should never happen
|
247
|
+
raise 'This should basically never happen'
|
187
248
|
end
|
188
|
-
|
189
|
-
|
190
|
-
|
191
|
-
|
192
|
-
instance
|
193
|
-
|
194
|
-
instance.class.send(:define_method, "#{name}?", proc{ !!@attributes[name] })
|
195
|
-
|
196
|
-
instance.send("#{name}=".to_s, value)
|
249
|
+
# > Singular resources are represented as JSON objects. However,
|
250
|
+
# they are still wrapped inside an array:
|
251
|
+
resource_body = value.first
|
252
|
+
cls = Balanced.from_hypermedia_registry(key)
|
253
|
+
instance = cls.new resource_body
|
254
|
+
instance.hydrate(links, meta)
|
197
255
|
end
|
198
256
|
instance
|
199
257
|
end
|
200
258
|
|
201
|
-
def
|
202
|
-
scope
|
259
|
+
def fetch(*arguments)
|
260
|
+
scope = arguments.slice!(0)
|
203
261
|
options = arguments.slice!(0) || {}
|
204
262
|
case scope
|
205
|
-
when :all
|
206
|
-
|
263
|
+
when :all then
|
264
|
+
all(options)
|
265
|
+
when :first then
|
266
|
+
paginate(options).first
|
207
267
|
else
|
208
268
|
response = Balanced.get scope, options
|
209
269
|
construct_from_response response.body
|
210
270
|
end
|
211
271
|
end
|
212
272
|
|
213
|
-
|
214
|
-
|
273
|
+
alias find fetch
|
274
|
+
|
275
|
+
def paginate(options = {})
|
276
|
+
Balanced::Pager.new href, options
|
215
277
|
end
|
278
|
+
|
216
279
|
alias scoped paginate
|
217
|
-
alias where
|
280
|
+
alias where paginate
|
218
281
|
|
219
|
-
def all
|
282
|
+
def all(options = {})
|
220
283
|
pager = paginate(options)
|
221
284
|
pager.to_a
|
222
285
|
end
|