peakium 0.1.0 → 0.1.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (54) hide show
  1. checksums.yaml +7 -0
  2. data/.gitignore +4 -0
  3. data/.travis.yml +11 -0
  4. data/CONTRIBUTORS +1 -0
  5. data/Gemfile +2 -0
  6. data/History.txt +0 -0
  7. data/LICENSE +21 -0
  8. data/README.rdoc +43 -0
  9. data/Rakefile +16 -0
  10. data/VERSION +1 -0
  11. data/bin/peakium-console +7 -0
  12. data/gemfiles/default-with-activesupport.gemfile +3 -0
  13. data/gemfiles/json.gemfile +4 -0
  14. data/gemfiles/yajl.gemfile +4 -0
  15. data/lib/data/ca-certificates.crt +3828 -0
  16. data/lib/peakium/api_operations/create.rb +16 -0
  17. data/lib/peakium/api_operations/delete.rb +11 -0
  18. data/lib/peakium/api_operations/list.rb +21 -0
  19. data/lib/peakium/api_operations/update.rb +17 -0
  20. data/lib/peakium/api_resource.rb +33 -0
  21. data/lib/peakium/api_resources/customer.rb +25 -0
  22. data/lib/peakium/api_resources/event.rb +28 -0
  23. data/lib/peakium/api_resources/event_webhook.rb +5 -0
  24. data/lib/peakium/api_resources/gateway.rb +19 -0
  25. data/lib/peakium/api_resources/invoice.rb +26 -0
  26. data/lib/peakium/api_resources/payment_session.rb +5 -0
  27. data/lib/peakium/api_resources/submission_form.rb +12 -0
  28. data/lib/peakium/api_resources/subscription.rb +26 -0
  29. data/lib/peakium/api_resources/webhook.rb +21 -0
  30. data/lib/peakium/errors/api_connection_error.rb +4 -0
  31. data/lib/peakium/errors/api_error.rb +4 -0
  32. data/lib/peakium/errors/authentication_error.rb +4 -0
  33. data/lib/peakium/errors/invalid_request_error.rb +10 -0
  34. data/lib/peakium/errors/peakium_error.rb +20 -0
  35. data/lib/peakium/list_object.rb +31 -0
  36. data/lib/peakium/peakium_object.rb +167 -0
  37. data/lib/peakium/util.rb +115 -0
  38. data/lib/peakium/version.rb +3 -0
  39. data/lib/peakium.rb +265 -0
  40. data/peakium.gemspec +27 -0
  41. data/test/peakium/api_resource_test.rb +315 -0
  42. data/test/peakium/customer_test.rb +21 -0
  43. data/test/peakium/event_test.rb +30 -0
  44. data/test/peakium/event_webhook_test.rb +13 -0
  45. data/test/peakium/gateway_test.rb +35 -0
  46. data/test/peakium/invoice_test.rb +21 -0
  47. data/test/peakium/list_object_test.rb +16 -0
  48. data/test/peakium/payment_session_test.rb +13 -0
  49. data/test/peakium/submission_form_test.rb +12 -0
  50. data/test/peakium/subscription_test.rb +13 -0
  51. data/test/peakium/util_test.rb +29 -0
  52. data/test/peakium/webhook_test.rb +20 -0
  53. data/test/test_helper.rb +337 -0
  54. metadata +99 -52
@@ -0,0 +1,35 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Peakium
5
+ class CustomerTest < Test::Unit::TestCase
6
+ should "gateways should be listable" do
7
+ @mock.expects(:get).once.returns(test_response(test_gateway_array))
8
+ g = Peakium::Gateway.all.data
9
+ assert g.kind_of? Array
10
+ assert g[0].kind_of? Peakium::Gateway
11
+ end
12
+
13
+ should "gateway should have GatewayModule objects associated with their module property" do
14
+ @mock.expects(:get).once.returns(test_response(test_gateway))
15
+ g = Peakium::Gateway.retrieve("gw_test_gateway")
16
+ assert g.module.kind_of?(Peakium::PeakiumObject) && g.module.object == 'gateway_module'
17
+ end
18
+
19
+ should "gateways should be updateable" do
20
+ @mock.expects(:get).once.returns(test_response(test_gateway({:mnemonic => "foo"})))
21
+ @mock.expects(:post).once.returns(test_response(test_gateway({:mnemonic => "bar"})))
22
+ g = Peakium::Gateway.new("test_gateway").refresh
23
+ assert_equal g.mnemonic, "foo"
24
+ g.mnemonic = "bar"
25
+ g.save
26
+ assert_equal g.mnemonic, "bar"
27
+ end
28
+
29
+ should "create should return a new gateway" do
30
+ @mock.expects(:post).once.returns(test_response(test_gateway))
31
+ g = Peakium::Gateway.create
32
+ assert_equal "gw_test_gateway", g.id
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,21 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Peakium
5
+ class InvoiceTest < Test::Unit::TestCase
6
+ should "invoices should be listable" do
7
+ @mock.expects(:get).once.returns(test_response(test_invoice_array))
8
+ i = Peakium::Invoice.all.data
9
+ assert i.kind_of? Array
10
+ assert i[0].kind_of? Peakium::Invoice
11
+ end
12
+
13
+ should "be able to attempt payment of an invoice" do
14
+ @mock.expects(:get).once.returns(test_response(test_invoice))
15
+ i = Peakium::Invoice.retrieve("in_test_invoice")
16
+
17
+ @mock.expects(:post).once.with("#{Peakium.api_base}/v1/invoices/in_test_invoice/pay", nil, '').returns(test_response(test_invoice()))
18
+ i = i.pay;
19
+ end
20
+ end
21
+ end
@@ -0,0 +1,16 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Peakium
4
+ class ListObjectTest < Test::Unit::TestCase
5
+ should "be able to retrieve full lists given a listobject" do
6
+ @mock.expects(:get).twice.returns(test_response(test_gateway_array))
7
+ g = Peakium::Gateway.all
8
+ assert g.kind_of?(Peakium::ListObject)
9
+ assert_equal('/v1/gateways', g.endpoint_url)
10
+ all = g.all
11
+ assert all.kind_of?(Peakium::ListObject)
12
+ assert_equal('/v1/gateways', all.endpoint_url)
13
+ assert all.data.kind_of?(Array)
14
+ end
15
+ end
16
+ end
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Peakium
5
+ class PaymentSessionTest < Test::Unit::TestCase
6
+ should "payment sessions should be listable" do
7
+ @mock.expects(:get).once.returns(test_response(test_payment_session_array))
8
+ ps = Peakium::PaymentSession.all.data
9
+ assert ps.kind_of? Array
10
+ assert ps[0].kind_of? Peakium::PaymentSession
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,12 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Peakium
5
+ class SubmissionFormTest < Test::Unit::TestCase
6
+ should "create should return a new submission form" do
7
+ @mock.expects(:post).once.returns(test_response(test_submission_form))
8
+ sf = Peakium::SubmissionForm.build('create_subscription', {:customer => "test_customer"})
9
+ assert_equal "<html></html>", sf.html
10
+ end
11
+ end
12
+ end
@@ -0,0 +1,13 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Peakium
5
+ class SubscriptionTest < Test::Unit::TestCase
6
+ should "subscriptions should be listable" do
7
+ @mock.expects(:get).once.returns(test_response(test_subscription_array))
8
+ s = Peakium::Subscription.all.data
9
+ assert s.kind_of? Array
10
+ assert s[0].kind_of? Peakium::Subscription
11
+ end
12
+ end
13
+ end
@@ -0,0 +1,29 @@
1
+ require File.expand_path('../../test_helper', __FILE__)
2
+
3
+ module Peakium
4
+ class UtilTest < Test::Unit::TestCase
5
+ should "symbolize_names should convert names to symbols" do
6
+ start = {
7
+ 'foo' => 'bar',
8
+ 'array' => [{ 'foo' => 'bar' }],
9
+ 'nested' => {
10
+ 1 => 2,
11
+ :symbol => 9,
12
+ 'string' => nil
13
+ }
14
+ }
15
+ finish = {
16
+ :foo => 'bar',
17
+ :array => [{ :foo => 'bar' }],
18
+ :nested => {
19
+ 1 => 2,
20
+ :symbol => 9,
21
+ :string => nil
22
+ }
23
+ }
24
+
25
+ symbolized = Peakium::Util.symbolize_names(start)
26
+ assert_equal(finish, symbolized)
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,20 @@
1
+ # -*- coding: utf-8 -*-
2
+ require File.expand_path('../../test_helper', __FILE__)
3
+
4
+ module Peakium
5
+ class WebhookTest < Test::Unit::TestCase
6
+ should "webhooks should be listable" do
7
+ @mock.expects(:get).once.returns(test_response(test_webhook_array))
8
+ w = Peakium::Webhook.all.data
9
+ assert w.kind_of? Array
10
+ assert w[0].kind_of? Peakium::Webhook
11
+ end
12
+
13
+ should "create should return a new webhook" do
14
+ @mock.expects(:post).once.returns(test_response(test_webhook))
15
+ w = Peakium::Webhook.create
16
+
17
+ assert_equal "http://example.com/webhook_endpoint/", w.url
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,337 @@
1
+ require 'peakium'
2
+ require 'test/unit'
3
+ require 'mocha/setup'
4
+ require 'shoulda'
5
+ require 'stringio'
6
+
7
+ #monkeypatch request methods
8
+ module Peakium
9
+ @mock_rest_client = nil
10
+
11
+ def self.mock_rest_client=(mock_client)
12
+ @mock_rest_client = mock_client
13
+ end
14
+
15
+ def self.execute_request(opts)
16
+ get_params = (opts[:headers] || {})[:params]
17
+ post_params = opts[:payload]
18
+ case opts[:method]
19
+ when :get then @mock_rest_client.get opts[:url], get_params, post_params
20
+ when :post then @mock_rest_client.post opts[:url], get_params, post_params
21
+ when :delete then @mock_rest_client.delete opts[:url], get_params, post_params
22
+ end
23
+ end
24
+ end
25
+
26
+ def test_response(body, code=200)
27
+ # When an exception is raised, restclient clobbers method_missing. Hence we
28
+ # can't just use the stubs interface.
29
+ body = JSON.generate(body) if !(body.kind_of? String)
30
+ m = mock
31
+ m.instance_variable_set('@peakium_values', { :body => body, :code => code })
32
+ def m.body; @peakium_values[:body]; end
33
+ def m.code; @peakium_values[:code]; end
34
+ m
35
+ end
36
+
37
+ def test_charge
38
+ {
39
+ :livemode => false,
40
+ :object => "charge",
41
+ :amount => 7200,
42
+ :currency => "USD",
43
+ :paid => true,
44
+ :timestamp_paid => 1375217068,
45
+ :status => "completed",
46
+ }
47
+ end
48
+
49
+ def test_customer(params={})
50
+ {
51
+ :livemode => false,
52
+ :object => "customer",
53
+ :id => "test_customer",
54
+ :created => 1375217068,
55
+ :balances => [],
56
+ }.merge(params)
57
+ end
58
+
59
+ def test_customer_array
60
+ {
61
+ :data => [test_customer, test_customer, test_customer],
62
+ :object => 'list'
63
+ }
64
+ end
65
+
66
+ def test_event(params={})
67
+ {
68
+ :livemode => false,
69
+ :object => "event",
70
+ :id => "ev_test_event",
71
+ :created => 1375217068,
72
+ :event => "subscription_created",
73
+ :data => test_subscription,
74
+ }.merge(params)
75
+ end
76
+
77
+ def test_event_array
78
+ {
79
+ :data => [test_event, test_event, test_event],
80
+ :object => 'list'
81
+ }
82
+ end
83
+
84
+ def test_event_webhook()
85
+ {
86
+ :livemode => false,
87
+ :object => "event_webhook",
88
+ :created => 1375217068,
89
+ :number_of_attempted_deliveries => 1,
90
+ :delivered => true,
91
+ :delivered_timestamp => 1375217068,
92
+ :webhook => test_webhook,
93
+ :event => test_event
94
+ }
95
+ end
96
+
97
+ def test_event_webhook_array
98
+ {
99
+ :data => [test_event_webhook, test_event_webhook, test_event_webhook],
100
+ :object => 'list'
101
+ }
102
+ end
103
+
104
+ def test_gateway(params={})
105
+ {
106
+ :livemode => false,
107
+ :object => "gateway",
108
+ :id => "gw_test_gateway",
109
+ :created => 1375217068,
110
+ :name => "Paypal",
111
+ :active => true,
112
+ :default => false,
113
+ :module => {
114
+ :object => "gateway_module",
115
+ :name => "Paypal_Website_Payments_Standard",
116
+ :required_fields => [
117
+ "paypal_email"
118
+ ]
119
+ },
120
+ :settings => [
121
+ :paypal_email => "paypal@example.com"
122
+ ]
123
+ }.merge(params)
124
+ end
125
+
126
+ def test_gateway_array
127
+ {
128
+ :data => [test_gateway, test_gateway, test_gateway],
129
+ :object => 'list'
130
+ }
131
+ end
132
+
133
+ def test_invoice
134
+ {
135
+ :livemode => false,
136
+ :object => "invoice",
137
+ :id => "in_test_invoice",
138
+ :created => 1375217068,
139
+ :due => 1375217068,
140
+ :locked => true,
141
+ :timestamp_locked => 1375217068,
142
+ :paid => true,
143
+ :timestamp_paid => 1375217068,
144
+ :retracted => true,
145
+ :timestamp_retracted => 1375217068,
146
+ :type => "single",
147
+ :calculated_total => {
148
+ :currency => "USD",
149
+ :amount => 7200
150
+ },
151
+ :attempt_automatic_charge => true,
152
+ :items => [
153
+ {
154
+ :livemode => false,
155
+ :object => "invoice_item",
156
+ :type => "credit",
157
+ :description => "Plus Plan",
158
+ :item_id => 101,
159
+ :unit_amount => 7200,
160
+ :currency => "USD",
161
+ :quantity => 1,
162
+ :date_range_start => 1375217068,
163
+ :date_range_end => 1375217068,
164
+ :discount => 0.0,
165
+ :total_amount => 7200,
166
+ :reference => test_subscription
167
+ }
168
+ ],
169
+ :customer => test_customer
170
+ }
171
+ end
172
+
173
+ def test_invoice_array
174
+ {
175
+ :data => [test_invoice, test_invoice, test_invoice],
176
+ :object => 'list'
177
+ }
178
+ end
179
+
180
+ def test_overdue_invoice_array
181
+ {
182
+ :data => [test_invoice],
183
+ :object => 'list'
184
+ }
185
+ end
186
+
187
+ def test_invoice_customer_array
188
+ {
189
+ :data => [test_invoice],
190
+ :object => 'list'
191
+ }
192
+ end
193
+
194
+ def test_payment_session
195
+ {
196
+ :livemode => false,
197
+ :object => "payment_session",
198
+ :id => "ps_test_payment_session",
199
+ :created => 1375217068,
200
+ :handled => true,
201
+ :timestamp_handled => 1375217068,
202
+ :locked => true,
203
+ :payment_amount => 7200,
204
+ :payment_currency => "USD",
205
+ :action => "subscription-update",
206
+ :ip => "172.16.254.1",
207
+ :user_agent => "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_8_3) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/27.0.1453.93 Safari/537.36",
208
+ :type => "single",
209
+ :posted_values => {
210
+ :customer => "test_customer",
211
+ :subscription => "test_subscription",
212
+ :item_description => "Test Subscription",
213
+ :item_id => 101,
214
+ :period_amount => 1,
215
+ :period_type => "month",
216
+ :payment_amount => 7200,
217
+ :payment_currency => "USD",
218
+ :payment_discount => "USD",
219
+ :return_url_ok => "USD",
220
+ :return_url_error => "USD",
221
+ :publishable_key => "test_publishable_key",
222
+ :gateway => "gw_test_gateway",
223
+ :integrity_value => "test_integrity",
224
+ },
225
+ :gateway => test_gateway,
226
+ :customer => test_customer,
227
+ :charge => test_charge
228
+ }
229
+ end
230
+
231
+ def test_payment_session_array
232
+ {
233
+ :data => [test_payment_session, test_payment_session, test_payment_session],
234
+ :object => 'list'
235
+ }
236
+ end
237
+
238
+ def test_subscription()
239
+ {
240
+ :livemode => false,
241
+ :object => "subscription",
242
+ :created => 1375217068,
243
+ :token => "test_subscription",
244
+ :display_name => "Test Subscription",
245
+ :plan => {
246
+ :period_amount => 1,
247
+ :period_type => "month",
248
+ :amount => 7200,
249
+ :currency => "USD",
250
+ :discount => 0.0,
251
+ :item_id => 101,
252
+ },
253
+ :cycle_number => 1,
254
+ :period_start => 1375217068,
255
+ :period_end => 1375217068,
256
+ :next_charge => 1375217068,
257
+ :status => "active",
258
+ :customer => test_customer
259
+ }
260
+ end
261
+
262
+ def test_subscription_array
263
+ {
264
+ :data => [test_subscription, test_subscription, test_subscription],
265
+ :object => 'list'
266
+ }
267
+ end
268
+
269
+ def test_subcription_customer_array
270
+ {
271
+ :data => [test_subscription],
272
+ :object => 'list'
273
+ }
274
+ end
275
+
276
+ def test_submission_form
277
+ {
278
+ :livemode => false,
279
+ :html => "<html></html>"
280
+ }
281
+ end
282
+
283
+ def test_webhook
284
+ {
285
+ :object => "webhook",
286
+ :url => "http://example.com/webhook_endpoint/",
287
+ }
288
+ end
289
+
290
+ def test_webhook_array
291
+ {
292
+ :data => [test_webhook, test_webhook, test_webhook],
293
+ :object => 'list'
294
+ }
295
+ end
296
+
297
+ def test_invalid_api_key_error
298
+ {
299
+ "error" => {
300
+ "type" => "invalid_request_error",
301
+ "message" => "Invalid API Key provided: invalid"
302
+ }
303
+ }
304
+ end
305
+
306
+ def test_missing_id_error
307
+ {
308
+ :error => {
309
+ :param => "id",
310
+ :type => "invalid_request_error",
311
+ :message => "Missing id"
312
+ }
313
+ }
314
+ end
315
+
316
+ def test_api_error
317
+ {
318
+ :error => {
319
+ :type => "api_error"
320
+ }
321
+ }
322
+ end
323
+
324
+ class Test::Unit::TestCase
325
+ include Mocha
326
+
327
+ setup do
328
+ @mock = mock
329
+ Peakium.mock_rest_client = @mock
330
+ Peakium.api_key="foo"
331
+ end
332
+
333
+ teardown do
334
+ Peakium.mock_rest_client = nil
335
+ Peakium.api_key=nil
336
+ end
337
+ end