balanced 0.7.5 → 0.8.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (119) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +0 -1
  3. data/.rbenv-version +1 -0
  4. data/.ruby-version +1 -0
  5. data/Gemfile +1 -0
  6. data/balanced.gemspec +2 -2
  7. data/examples/bank_account_debits.rb +2 -1
  8. data/examples/customers.rb +8 -4
  9. data/examples/events_and_callbacks.rb +2 -1
  10. data/examples/examples.rb +6 -5
  11. data/examples/exception_handling.rb +2 -1
  12. data/lib/balanced/client.rb +3 -3
  13. data/lib/balanced/resources/account.rb +5 -3
  14. data/lib/balanced/resources/card.rb +3 -1
  15. data/lib/balanced/resources/credit.rb +12 -13
  16. data/lib/balanced/resources/customer.rb +1 -1
  17. data/lib/balanced/resources/debit.rb +1 -1
  18. data/lib/balanced/resources/resource.rb +83 -93
  19. data/lib/balanced/utils.rb +13 -4
  20. data/lib/balanced/version.rb +1 -1
  21. data/scenario.cache +299 -380
  22. data/scenarios/account_add_card/ruby.mako +3 -3
  23. data/scenarios/account_capture_hold/request.rb +1 -1
  24. data/scenarios/account_capture_hold/ruby.mako +1 -1
  25. data/scenarios/account_create/ruby.mako +1 -1
  26. data/scenarios/account_create_buyer/ruby.mako +2 -2
  27. data/scenarios/account_create_debit/request.rb +1 -1
  28. data/scenarios/account_create_debit/ruby.mako +1 -1
  29. data/scenarios/account_create_hold/request.rb +1 -1
  30. data/scenarios/account_create_hold/ruby.mako +1 -1
  31. data/scenarios/account_create_merchant/ruby.mako +3 -3
  32. data/scenarios/account_underwrite_business/ruby.mako +1 -1
  33. data/scenarios/account_underwrite_person/request.rb +1 -1
  34. data/scenarios/account_underwrite_person/ruby.mako +7 -7
  35. data/scenarios/bank_account_create/request.rb +1 -1
  36. data/scenarios/bank_account_create/ruby.mako +5 -5
  37. data/scenarios/bank_account_delete/ruby.mako +2 -2
  38. data/scenarios/bank_account_invalid_routing_number/ruby.mako +2 -8
  39. data/scenarios/bank_account_list/ruby.mako +1 -1
  40. data/scenarios/bank_account_show/ruby.mako +2 -2
  41. data/scenarios/bank_account_verification_create/ruby.mako +2 -2
  42. data/scenarios/bank_account_verification_show/ruby.mako +2 -2
  43. data/scenarios/bank_account_verification_update/ruby.mako +2 -2
  44. data/scenarios/callback_create/definition.rb +1 -1
  45. data/scenarios/callback_create/request.rb +1 -1
  46. data/scenarios/callback_create/ruby.mako +0 -11
  47. data/scenarios/callback_delete/definition.rb +1 -1
  48. data/scenarios/callback_delete/request.rb +1 -1
  49. data/scenarios/callback_delete/ruby.mako +0 -10
  50. data/scenarios/callback_list/definition.rb +1 -1
  51. data/scenarios/callback_list/ruby.mako +0 -10
  52. data/scenarios/callback_show/definition.rb +1 -1
  53. data/scenarios/callback_show/ruby.mako +0 -9
  54. data/scenarios/card_create/request.rb +2 -1
  55. data/scenarios/card_create/ruby.mako +6 -5
  56. data/scenarios/card_delete/ruby.mako +2 -2
  57. data/scenarios/card_invalidate/ruby.mako +2 -2
  58. data/scenarios/card_list/ruby.mako +1 -1
  59. data/scenarios/card_show/ruby.mako +2 -2
  60. data/scenarios/card_update/ruby.mako +2 -2
  61. data/scenarios/credit_bank_account_list/ruby.mako +2 -2
  62. data/scenarios/credit_create_existing_bank_account/ruby.mako +3 -3
  63. data/scenarios/credit_customer_list/ruby.mako +2 -2
  64. data/scenarios/credit_failed_state/request.rb +2 -2
  65. data/scenarios/credit_failed_state/ruby.mako +2 -5
  66. data/scenarios/credit_list/ruby.mako +1 -1
  67. data/scenarios/credit_paid_state/request.rb +2 -2
  68. data/scenarios/credit_paid_state/ruby.mako +2 -5
  69. data/scenarios/credit_pending_state/request.rb +2 -2
  70. data/scenarios/credit_pending_state/ruby.mako +2 -5
  71. data/scenarios/credit_show/ruby.mako +2 -2
  72. data/scenarios/customer_add_bank_account/ruby.mako +3 -3
  73. data/scenarios/customer_add_card/ruby.mako +3 -3
  74. data/scenarios/customer_capture_hold/request.rb +1 -1
  75. data/scenarios/customer_create/ruby.mako +1 -1
  76. data/scenarios/customer_create_debit/request.rb +1 -1
  77. data/scenarios/customer_create_debit/ruby.mako +3 -3
  78. data/scenarios/customer_create_hold/request.rb +1 -1
  79. data/scenarios/customer_create_hold/ruby.mako +1 -1
  80. data/scenarios/customer_credit/request.rb +1 -1
  81. data/scenarios/customer_credit/ruby.mako +2 -2
  82. data/scenarios/customer_delete/ruby.mako +1 -1
  83. data/scenarios/debit_create/ruby.mako +3 -3
  84. data/scenarios/debit_customer_list/ruby.mako +2 -2
  85. data/scenarios/debit_list/ruby.mako +1 -1
  86. data/scenarios/debit_refund/ruby.mako +2 -2
  87. data/scenarios/debit_show/ruby.mako +2 -2
  88. data/scenarios/debit_update/ruby.mako +2 -2
  89. data/scenarios/event_list/ruby.mako +1 -1
  90. data/scenarios/event_show/ruby.mako +2 -2
  91. data/scenarios/helpers.rb +1 -5
  92. data/scenarios/hold_capture/ruby.mako +4 -3
  93. data/scenarios/hold_create/request.rb +2 -2
  94. data/scenarios/hold_create/ruby.mako +6 -6
  95. data/scenarios/hold_customer_list/ruby.mako +2 -2
  96. data/scenarios/hold_list/ruby.mako +1 -1
  97. data/scenarios/hold_show/ruby.mako +2 -2
  98. data/scenarios/hold_update/ruby.mako +2 -2
  99. data/scenarios/hold_void/ruby.mako +2 -2
  100. data/scenarios/refund_create/ruby.mako +2 -2
  101. data/scenarios/refund_customer_list/ruby.mako +2 -2
  102. data/scenarios/refund_list/ruby.mako +1 -1
  103. data/scenarios/refund_show/ruby.mako +2 -2
  104. data/scenarios/refund_update/ruby.mako +2 -2
  105. data/scenarios/render.rb +1 -1
  106. data/scenarios/render_mako.rb +0 -1
  107. data/spec/balanced/error_spec.rb +4 -4
  108. data/spec/balanced/pager_spec.rb +7 -7
  109. data/spec/balanced/resources/account_spec.rb +51 -30
  110. data/spec/balanced/resources/bank_account_spec.rb +2 -2
  111. data/spec/balanced/resources/card_spec.rb +8 -8
  112. data/spec/balanced/resources/customer_spec.rb +11 -11
  113. data/spec/balanced/resources/hold_spec.rb +1 -1
  114. data/spec/balanced/resources/marketplace_spec.rb +12 -10
  115. data/spec/balanced/resources/resource_spec.rb +1 -1
  116. data/spec/scenarios_spec.rb +1 -1
  117. data/spec/spec_helper.rb +1 -1
  118. metadata +16 -15
  119. data/examples/customers-with-meta-example.rb +0 -141
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 702de4ea3bdcd354e312e317d8d065c88a4b40f1
4
- data.tar.gz: 21dbefe6a866777a6a2ef4372d254c3be9d41f05
3
+ metadata.gz: c7186ba853d6660c7502ad3767c186b1df7561a8
4
+ data.tar.gz: 42fbc557f0911e4a666d9df08ba0eb93122eef72
5
5
  SHA512:
6
- metadata.gz: 2dd3628ee2e6de7a7f88aa21a11ffc097c3cb1dbdcc8f933ee9d3dfb137277b21d4cb203ace1fc4f39de4f385c274430de4b57322ecb06295463f024a2797539
7
- data.tar.gz: 8d19b4684e94ef36beb8bc2d1119d665e7a0f489b1d76a68b932c00af99c784b2aad60cb047867e5e99e503dbe40fa49180bcad4ff24bbbce07efd8105c8ce56
6
+ metadata.gz: bba78d1960941223a2fb01eadc1cb73b1f0b617577889b0705dd11e4d04f83cea273e6696d2eea418ed1b68d7d4e33aa26127411a4b1b6a8f882fdb0ba5b9252
7
+ data.tar.gz: 665f3459a00d622a9d366c4637928f7fed47cf9e73cfaf73d1c0b1ee3e31b6d77415ab632da22a60b69ddaa830d23788113d9734dc0152a950a60243798cfdd1
data/.gitignore CHANGED
@@ -33,4 +33,3 @@ spec/cassettes
33
33
  # OS X
34
34
  .DS_Store
35
35
 
36
- .ruby-version
data/.rbenv-version ADDED
@@ -0,0 +1 @@
1
+ 1.9.2-p290
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.0.0-p247
data/Gemfile CHANGED
@@ -4,6 +4,7 @@ source 'https://rubygems.org'
4
4
 
5
5
  gem "faraday"
6
6
  gem "faraday_middleware"
7
+ gem "json"
7
8
 
8
9
  group :development do
9
10
  gem "yard"
data/balanced.gemspec CHANGED
@@ -13,7 +13,7 @@ Gem::Specification.new do |gem|
13
13
  gem.summary = %q{Sign up on https://balancedpayments.com/}
14
14
  gem.homepage = "https://balancedpayments.com"
15
15
 
16
- gem.add_dependency("faraday", '~> 0.8.6')
16
+ gem.add_dependency("faraday", '>= 0.8.6')
17
17
  gem.add_dependency("faraday_middleware", '~> 0.9.0')
18
18
 
19
19
  gem.files = `git ls-files`.split($\)
@@ -22,4 +22,4 @@ Gem::Specification.new do |gem|
22
22
  gem.name = "balanced"
23
23
  gem.require_paths = %w(lib)
24
24
  gem.version = Balanced::VERSION
25
- end
25
+ end
@@ -1,8 +1,9 @@
1
1
  """
2
2
  Learn how to authenticate a bank account so you can debit with it.
3
3
  """
4
- cwd = File.dirname(File.dirname(File.absolute_path(__FILE__)))
4
+ cwd = File.dirname(File.dirname(File.expand_path(__FILE__)))
5
5
  $:.unshift(cwd + "/lib")
6
+ require 'rubygems'
6
7
  require 'balanced'
7
8
 
8
9
  host = ENV.fetch('BALANCED_HOST') { nil }
@@ -1,8 +1,8 @@
1
- cwd = File.dirname(File.dirname(File.absolute_path(__FILE__)))
1
+ cwd = File.dirname(File.dirname(File.expand_path(__FILE__)))
2
2
  $:.unshift(cwd + "/lib")
3
+ require 'rubygems'
3
4
  require 'balanced'
4
5
 
5
-
6
6
  begin
7
7
  Balanced::Card
8
8
  rescue NameError
@@ -45,7 +45,7 @@ customer = marketplace.create_customer(
45
45
  :line1 => "1234 1st Street",
46
46
  :city => "San Francisco",
47
47
  :state => "CA"
48
- }
48
+ }
49
49
  ).save
50
50
 
51
51
  puts "our customer uri is #{customer.uri}"
@@ -61,7 +61,7 @@ bank_account = marketplace.create_bank_account(
61
61
  card = marketplace.create_card(
62
62
  :card_number => "4111111111111111",
63
63
  :expiration_month => "12",
64
- :expiration_year => "2015",
64
+ :expiration_year => "2015"
65
65
  ).save
66
66
 
67
67
  puts "our bank account uri is #{bank_account.uri}"
@@ -108,3 +108,7 @@ raise "active card is incorrect" unless customer.active_card.id == card.id
108
108
  puts "check to see what is the active bank_account for a customer"
109
109
 
110
110
  raise "active bank account is incorrect" unless customer.active_bank_account.id == bank_account.id
111
+
112
+
113
+
114
+
@@ -3,8 +3,9 @@ Welcome weary traveller. Sick of polling for state changes? Well today have I
3
3
  got good news for you. Run this example below to see how to get yourself some
4
4
  callback goodness and to understand how events work.
5
5
  """
6
- cwd = File.dirname(File.dirname(File.absolute_path(__FILE__)))
6
+ cwd = File.dirname(File.dirname(File.expand_path(__FILE__)))
7
7
  $:.unshift(cwd + "/lib")
8
+ require 'rubygems'
8
9
  require 'balanced'
9
10
  require 'net/http'
10
11
  require 'uri'
data/examples/examples.rb CHANGED
@@ -1,5 +1,6 @@
1
- cwd = File.dirname(File.dirname(File.absolute_path(__FILE__)))
1
+ cwd = File.dirname(File.dirname(File.expand_path(__FILE__)))
2
2
  $:.unshift(cwd + "/lib")
3
+ require 'rubygems'
3
4
  require 'balanced'
4
5
 
5
6
  begin
@@ -52,7 +53,7 @@ puts "cool! let's create a new card."
52
53
  card = Balanced::Card.new(
53
54
  :card_number => "5105105105105100",
54
55
  :expiration_month => "12",
55
- :expiration_year => "2015",
56
+ :expiration_year => "2015"
56
57
  ).save
57
58
  puts "Our card uri: #{card.uri}"
58
59
 
@@ -83,7 +84,7 @@ puts "ok, we have a merchant that's signing up, let's create an account for them
83
84
  bank_account = marketplace.create_bank_account(
84
85
  :account_number => "1234567890",
85
86
  :routing_number => "322271627",
86
- :name => "Jack Q Merchant",
87
+ :name => "Jack Q Merchant"
87
88
  )
88
89
 
89
90
  merchant = marketplace.create_merchant(
@@ -95,10 +96,10 @@ merchant = marketplace.create_merchant(
95
96
  :postal_code => "94301",
96
97
  :country => "USA",
97
98
  :dob => "1842-01",
98
- :phone_number => "+16505551234",
99
+ :phone_number => "+16505551234"
99
100
  },
100
101
  :bank_account_uri => bank_account.uri,
101
- :name => "Jack Q Merchant",
102
+ :name => "Jack Q Merchant"
102
103
  )
103
104
 
104
105
  puts "oh our buyer is interested in buying something for 530.00$"
@@ -1,5 +1,6 @@
1
- cwd = File.dirname(File.dirname(File.absolute_path(__FILE__)))
1
+ cwd = File.dirname(File.dirname(File.expand_path(__FILE__)))
2
2
  $:.unshift(cwd + "/lib")
3
+ require 'rubygems'
3
4
  require 'balanced'
4
5
 
5
6
  begin
@@ -70,9 +70,9 @@ module Balanced
70
70
  def url
71
71
  builder = (config[:scheme] == 'http') ? URI::HTTP : URI::HTTPS
72
72
 
73
- builder.build ({:host => config[:host],
74
- :port => config[:port],
75
- :scheme => config[:scheme]})
73
+ builder.build({:host => config[:host],
74
+ :port => config[:port],
75
+ :scheme => config[:scheme]})
76
76
  end
77
77
 
78
78
  def method_missing(method, *args, &block)
@@ -60,7 +60,7 @@ module Balanced
60
60
  :meta => meta,
61
61
  :description => description,
62
62
  :source_uri => source_uri,
63
- :on_behalf_of_uri => on_behalf_of,
63
+ :on_behalf_of_uri => on_behalf_of
64
64
  )
65
65
  debit.save
66
66
  end
@@ -81,12 +81,14 @@ module Balanced
81
81
  amount = args[0] || options.fetch(:amount) { }
82
82
  meta = args[1] || options.fetch(:meta) { nil }
83
83
  source_uri = args[2] || options.fetch(:source_uri) { nil }
84
+ description = args[3] || options.fetch(:description) { nil }
84
85
 
85
86
  hold = Hold.new(
86
87
  :uri => self.holds_uri,
87
88
  :amount => amount,
88
89
  :meta => meta,
89
90
  :source_uri => source_uri,
91
+ :description => description
90
92
  )
91
93
  hold.save
92
94
  end
@@ -102,9 +104,9 @@ module Balanced
102
104
  warn_on_positional args
103
105
 
104
106
  if args.last.is_a? Hash
105
- args.last.merge! uri: self.credits_uri
107
+ args.last.merge! :uri => self.credits_uri
106
108
  else
107
- args << { uri: self.credits_uri }
109
+ args << { :uri => self.credits_uri }
108
110
  end
109
111
  Credit.new(*args).save
110
112
  end
@@ -56,13 +56,15 @@ module Balanced
56
56
  options = args.last.is_a?(Hash) ? args.pop : {}
57
57
  amount = args[0] || options.fetch(:amount) { nil }
58
58
  meta = args[1] || options.fetch(:meta) { nil }
59
+ description = args[3] || options.fetch(:description) { nil }
59
60
 
60
61
  ensure_associated_to_account!
61
62
 
62
63
  self.account.hold(
63
64
  :amount => amount,
64
65
  :meta => meta,
65
- :source_uri => self.uri
66
+ :source_uri => self.uri,
67
+ :description => description
66
68
  )
67
69
  end
68
70
 
@@ -27,23 +27,23 @@ module Balanced
27
27
  unless bank_account.nil?
28
28
  # Accountless bank account
29
29
  attributes = {
30
- uri: uri,
31
- amount: amount,
32
- description: description,
33
- bank_account: bank_account,
34
- meta: nil
30
+ :uri => uri,
31
+ :amount => amount,
32
+ :description => description,
33
+ :bank_account => bank_account,
34
+ :meta => nil
35
35
  }
36
36
  else
37
37
  meta = args[2] || options.fetch(:meta) { nil }
38
38
  destination_uri = args[3] || options.fetch(:destination_uri) { nil }
39
39
  appears_on_statement_as = args[4] || options.fetch(:appears_on_statement_as) { nil }
40
40
  attributes = {
41
- uri: uri,
42
- amount: amount,
43
- meta: meta,
44
- description: description,
45
- destination_uri: destination_uri,
46
- appears_on_statement_as: appears_on_statement_as
41
+ :uri => uri,
42
+ :amount => amount,
43
+ :meta => meta,
44
+ :description => description,
45
+ :destination_uri => destination_uri,
46
+ :appears_on_statement_as => appears_on_statement_as
47
47
  }
48
48
  end
49
49
 
@@ -63,9 +63,8 @@ module Balanced
63
63
  :uri => self.reversals_uri,
64
64
  :credit_uri => self.uri,
65
65
  :amount => amount,
66
- :description => description,
66
+ :description => description
67
67
  )
68
-
69
68
  reversal.save
70
69
  end
71
70
 
@@ -61,7 +61,7 @@ module Balanced
61
61
  :meta => meta,
62
62
  :description => description,
63
63
  :source_uri => source_uri,
64
- :on_behalf_of_uri => on_behalf_of,
64
+ :on_behalf_of_uri => on_behalf_of
65
65
  )
66
66
  debit.save
67
67
  end
@@ -38,7 +38,7 @@ module Balanced
38
38
  :uri => self.refunds_uri,
39
39
  :debit_uri => self.uri,
40
40
  :amount => amount,
41
- :description => description,
41
+ :description => description
42
42
  )
43
43
  refund.save
44
44
  end
@@ -1,5 +1,4 @@
1
- require_relative "../pager"
2
-
1
+ require File.expand_path('../../pager', __FILE__)
3
2
 
4
3
  module Balanced
5
4
  module Resource
@@ -89,7 +88,7 @@ module Balanced
89
88
  end
90
89
 
91
90
  def method_missing(method, *args, &block)
92
- case method
91
+ case method.to_s
93
92
  when /(.+)\=$/
94
93
  attr = method.to_s.chop
95
94
  @attributes[attr] = args[0]
@@ -107,16 +106,8 @@ module Balanced
107
106
  #
108
107
  # This solves the acute problem, for now.
109
108
  if @attributes.has_key? "#{method}_uri"
110
-
111
109
  value = @attributes["#{method}_uri"]
112
- # what if the server returns a _uri that we don't know how to
113
- # construct? Welp, we catch that NameError and return to super.
114
- begin
115
- values_class = Balanced.from_uri(value)
116
- rescue NameError
117
- super
118
- end
119
-
110
+ values_class = Balanced.from_uri(value)
120
111
  # if uri is a collection -> this would definitely be if
121
112
  # it ends in a symbol then we should allow a lazy executor of
122
113
  # the query pager
@@ -126,112 +117,111 @@ module Balanced
126
117
  else
127
118
  return values_class.find(value)
128
119
  end
129
-
130
120
  else
131
121
  super
132
122
  end
133
123
  end
134
124
  end
135
-
136
- def self.included(base)
137
- base.extend ClassMethods
138
- end
139
-
140
- module ClassMethods
141
- def resource_name
142
- Utils.demodulize name
125
+
126
+ def self.included(base)
127
+ base.extend ClassMethods
143
128
  end
144
129
 
145
- def collection_name
146
- Utils.pluralize Utils.underscore(resource_name)
147
- end
130
+ module ClassMethods
131
+ def resource_name
132
+ Utils.demodulize name
133
+ end
148
134
 
149
- def collection_path
150
- ["/v#{Balanced.config[:version]}", collection_name].compact.join '/'
151
- end
135
+ def collection_name
136
+ Utils.pluralize Utils.underscore(resource_name)
137
+ end
152
138
 
153
- def member_name
154
- Utils.underscore resource_name
155
- end
139
+ def collection_path
140
+ ["/v#{Balanced.config[:version]}", collection_name].compact.join '/'
141
+ end
156
142
 
157
- # Returns the resource URI for a given class.
158
- #
159
- # @example A Balanced::Account resource
160
- # Balanced::Account.uri # => "/v1/marketplaces/TEST-MP72zVdg2j9IiqRffW9lczRZ/accounts"
161
- #
162
- # @return [String] the uri of the instance or the class
163
- def uri
164
- # the uri of a particular resource depends if there's a marketplace
165
- # created or not. if there's a marketplace, then all resources have their
166
- # own uri from there and the top level ones. if there's not a marketplace
143
+ def member_name
144
+ Utils.underscore resource_name
145
+ end
146
+
147
+ # Returns the resource URI for a given class.
167
148
  #
168
- # if there's an api key, then the merchant is available
169
- # if there's no api key, the only resources exposed are purely top level
170
- if self == Balanced::Merchant || self == Balanced::Marketplace || self == Balanced::ApiKey
171
- collection_path
172
- else
173
- unless Balanced::Marketplace.marketplace_exists?
174
- raise Balanced::StandardError, "#{self.name} is nested under a marketplace, which is not created or configured."
175
- end
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
176
166
 
177
- Balanced::Marketplace.marketplace_uri + "/#{collection_name}"
167
+ Balanced::Marketplace.marketplace_uri + "/#{collection_name}"
168
+ end
178
169
  end
179
- end
180
170
 
181
- def construct_from_response payload
182
- payload = Balanced::Utils.hash_with_indifferent_read_access payload
183
- return payload if payload[:uri].nil?
184
- klass = Balanced.from_uri(payload[:uri])
185
- instance = klass.new payload
171
+ def construct_from_response payload
172
+ payload = Balanced::Utils.hash_with_indifferent_read_access payload
173
+ return payload if payload[:uri].nil?
174
+ klass = Balanced.from_uri(payload[:uri])
175
+ instance = klass.new payload
186
176
 
187
- # http://stackoverflow.com/a/2495650/133514
188
- instance_eigen = class << instance; self; end
177
+ # http://stackoverflow.com/a/2495650/133514
178
+ instance_eigen = class << instance; self; end
189
179
 
190
- payload.each do |name, value|
180
+ payload.each do |name, value|
191
181
 
192
- # here is where our interpretations will begin.
193
- # if the value is a sub-resource, lets instantiate the class
194
- # and set it correctly
195
- if value.instance_of? Hash and value.has_key? 'uri'
196
- value = construct_from_response value
197
- end
182
+ # here is where our interpretations will begin.
183
+ # if the value is a sub-resource, lets instantiate the class
184
+ # and set it correctly
185
+ if value.instance_of? Hash and value.has_key? 'uri'
186
+ value = construct_from_response value
187
+ end
198
188
 
199
- # Get attribute
200
- instance.class.send(:define_method, name, proc{@attributes[name]})
201
- # Set attribute
202
- instance.class.send(:define_method, "#{name}=", proc{ |value| @attributes[name] = value })
203
- # Is attribute present?
204
- instance.class.send(:define_method, "#{name}?", proc{ !!@attributes[name] })
189
+ # Get attribute
190
+ instance.class.send(:define_method, name, proc{@attributes[name]})
191
+ # Set attribute
192
+ instance.class.send(:define_method, "#{name}=", proc{ |value| @attributes[name] = value })
193
+ # Is attribute present?
194
+ instance.class.send(:define_method, "#{name}?", proc{ !!@attributes[name] })
205
195
 
206
- instance.send("#{name}=".to_s, value)
196
+ instance.send("#{name}=".to_s, value)
197
+ end
198
+ instance
207
199
  end
208
- instance
209
- end
210
200
 
211
- def find *arguments
212
- scope = arguments.slice!(0)
213
- options = arguments.slice!(0) || {}
214
- case scope
215
- when :all then all(options)
216
- when :first then paginate(options).first
217
- else
218
- response = Balanced.get scope, options
219
- construct_from_response response.body
201
+ def find *arguments
202
+ scope = arguments.slice!(0)
203
+ options = arguments.slice!(0) || {}
204
+ case scope
205
+ when :all then all(options)
206
+ when :first then paginate(options).first
207
+ else
208
+ response = Balanced.get scope, options
209
+ construct_from_response response.body
210
+ end
220
211
  end
221
- end
222
212
 
223
- def paginate options = {}
224
- Balanced::Pager.new uri, options
225
- end
226
- alias scoped paginate
227
- alias where paginate
213
+ def paginate options = {}
214
+ Balanced::Pager.new uri, options
215
+ end
216
+ alias scoped paginate
217
+ alias where paginate
228
218
 
229
- def all options = {}
230
- pager = paginate(options)
231
- pager.to_a
232
- end
219
+ def all options = {}
220
+ pager = paginate(options)
221
+ pager.to_a
222
+ end
233
223
 
234
- end
224
+ end
235
225
 
236
226
  end
237
227
  end