monzo 0.2.0 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: bca691ae63f93ef6c1c6c601bd4b2e0d6f0f8c09
4
- data.tar.gz: 407b2ff8fe2e498c9fe740af03a873186fc10b01
3
+ metadata.gz: 416017c7fb95dddb3f6abbd9b29b5113b854b912
4
+ data.tar.gz: ef730307125fad7edd2467933b27fe90212aa92f
5
5
  SHA512:
6
- metadata.gz: be97766eedfd044f591ace66749efaafe95725eab79317282b786644b866e948e59a987aa2768a3798b3c8f475e16eb0af6dc5fa6b2d7b00cfea92bacaee5186
7
- data.tar.gz: bea7bd7cf6b42d19a8c0140f64d80ba734de11354f01b8066cd15ceeff41e9ddb14713930791e2d7637f7e3f088b7b557132ca63d878b26d7281d1856534b2ba
6
+ metadata.gz: 274dcb2993f5ce88b9b85aad37db9668c2144e3459f73b24df1a942ff378c6c58e61d26beb44afcfac3791d9b1e1a40e9c431047d74b6054054db65ac044fe1d
7
+ data.tar.gz: 104e362c74e3bd84c4c8cb56daf18f22474f4110c80a7b4ebba76482b81bcfae5ec7ec41a44203f1545d0eec046107c2f75ecadf9aaf48872791f89a1522d710
data/README.md CHANGED
@@ -63,6 +63,32 @@ A Pot is a place to keep some money separate from your main spending account.
63
63
  ```ruby
64
64
  # Find all Monzo Pots
65
65
  Monzo::Pot.all
66
+
67
+ # Find a pot with the given pot id.
68
+ Monzo::Pot.find(pot_id)
69
+
70
+ # Move money into a pot
71
+ account_id = Monzo::Account.all.last.id # The account to withdraw from
72
+ pot = Monzo::Pot.all.first # Get the first pot
73
+ pot.balance #=> eg. 5000
74
+
75
+ pot.deposit!(100, account_id)
76
+ pot.balance #=> eg. 5100
77
+
78
+ # Move money out of a pot
79
+ account_id = Monzo::Account.all.last.id
80
+ pot = Monzo::Pot.all.first
81
+ pot.balance #=> eg. 5000
82
+
83
+ pot.withdraw!(100, account_id)
84
+ pot.balance #=> eg. 4900
85
+ ```
86
+
87
+ The `deposit!` and `withdrawl!` methods accept an optional `dedupe_id` parameter. It's used to prevent duplicate transactions and should remain static between retries to ensure only one deposit/withdrawl is created. If you don't provide one, a random string will be generated for each deposit/withdrawl. You should **always** provide this if there is a chance the transaction will be retried.
88
+
89
+ ```ruby
90
+ dedupe_id = 'SomeniqueDeDuplicationString' # Store this and use it for retries.
91
+ pot.deposit!(100, account_id, dedupe_id)
66
92
  ```
67
93
 
68
94
  ### Balance
@@ -1,6 +1,7 @@
1
1
  require "json"
2
2
 
3
3
  require "monzo/version"
4
+ require "monzo/errors"
4
5
  require "monzo/account"
5
6
  require "monzo/balance"
6
7
  require "monzo/transaction"
@@ -65,6 +65,23 @@ module Monzo
65
65
  response = https_client(uri).request(request)
66
66
  end
67
67
 
68
+ # Internal: Perform a PUT request to the Monzo API.
69
+ #
70
+ # path - The URI path to request.
71
+ # data - The form data to send with the request.
72
+ # options - A Hash of query options to include in the URI.
73
+ #
74
+ # Returns a HTTP response.
75
+ def put(path, data, options = {})
76
+ uri = build_uri(path, options)
77
+
78
+ request = Net::HTTP::Put.new(uri.request_uri)
79
+ set_authorisation_header(request)
80
+ request.set_form_data(data)
81
+
82
+ response = https_client(uri).request(request)
83
+ end
84
+
68
85
  # Internal: Perform a DELETE request to the Monzo API.
69
86
  #
70
87
  # path - The URI path to request.
@@ -0,0 +1,6 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Monzo
4
+ # Internal: Exception to raise on reciept of API error.
5
+ APIError = Class.new(StandardError)
6
+ end
@@ -1,3 +1,5 @@
1
+ require "securerandom"
2
+
1
3
  module Monzo
2
4
 
3
5
  # Public: Retrieve information about a pot. A Pot is a place to keep
@@ -25,13 +27,88 @@ module Monzo
25
27
  #
26
28
  # Returns An Array of Monzo::Pot
27
29
  def self.all
28
- client = Monzo.client
29
- response = client.get("/pots/listV1")
30
+ response = Monzo.client.get("/pots")
30
31
  parsed_response = JSON.parse(response.body, :symbolize_names => true)
31
32
 
32
33
  parsed_response[:pots].map do |item|
33
34
  Monzo::Pot.new(item)
34
35
  end
35
36
  end
37
+
38
+ # Public: Find a pot with the given pot id.
39
+ #
40
+ # pot_id - The id to find.
41
+ #
42
+ # Returns an instance of Monzo::Pot.
43
+ def self.find(pot_id)
44
+ response = Monzo.client.get("/pots/#{pot_id}")
45
+ parsed_response = JSON.parse(response.body, :symbolize_names => true)
46
+ Monzo::Pot.new(parsed_response)
47
+ end
48
+
49
+ # Public: Deposit Money in a pot
50
+ #
51
+ # amount - The amount to deposit, in pennies.
52
+ # source_account_id - The account_id of the account to withdraw from.
53
+ # dedupe_id (optional) - A random string, to prevent duplicate deposits.
54
+ #
55
+ # Returns self: a single Monzo::Pot
56
+ def deposit!(amount, source_account_id, dedupe_id = SecureRandom.uuid)
57
+ data = {
58
+ amount: amount,
59
+ source_account_id: source_account_id,
60
+ dedupe_id: dedupe_id,
61
+ }
62
+
63
+ response = Monzo.client.put("/pots/#{@id}/deposit", data)
64
+ parsed_response = parse_response(response)
65
+ update_self(parsed_response)
66
+ end
67
+
68
+ # Public: Withdraw Money from a pot
69
+ #
70
+ # amount - The amount to withdraw, in pennies.
71
+ # destination_account_id - The account_id of the account to deposit into.
72
+ # dedupe_id (optional) - A random string, to prevent duplicate deposits.
73
+ #
74
+ # Returns self: a single Monzo::Pot
75
+ def withdraw!(amount, destination_account_id, dedupe_id = SecureRandom.uuid)
76
+ data = {
77
+ amount: amount,
78
+ destination_account_id: destination_account_id,
79
+ dedupe_id: dedupe_id,
80
+ }
81
+
82
+ response = Monzo.client.put("/pots/#{@id}/withdraw", data)
83
+ parsed_response = parse_response(response)
84
+ update_self(parsed_response)
85
+ end
86
+
87
+ private
88
+
89
+ # Private: Parse the API response
90
+ #
91
+ # response - A Net::HTTPResponse provided by Monzo::Client
92
+ #
93
+ # Returns a hash representing the response or raises a Monzo::APIError
94
+ def parse_response(response)
95
+ parsed_response = JSON.parse(response.body, :symbolize_names => true)
96
+ if response.code.to_i.between?(400,599)
97
+ raise Monzo::APIError, "#{parsed_response[:code]}: #{parsed_response[:error]}"
98
+ end
99
+ parsed_response
100
+ end
101
+
102
+ # Private: Update the Pot instance variables
103
+ #
104
+ # parsed_response - a hash whose keys exactly match the instance variables for Monzo::Pot.
105
+ #
106
+ # Returns self (an instance of Monzo::Pot)
107
+ def update_self(parsed_response)
108
+ instance_variables.each do |iv|
109
+ instance_variable_set(iv, parsed_response[iv.to_s[1..-1].to_sym])
110
+ end
111
+ self
112
+ end
36
113
  end
37
114
  end
@@ -1,4 +1,4 @@
1
1
  module Monzo
2
2
  # Public: The version number of the gem.
3
- VERSION = "0.2.0"
3
+ VERSION = "0.3.0"
4
4
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: monzo
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Murray Summers
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2017-11-27 00:00:00.000000000 Z
11
+ date: 2018-04-06 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -117,6 +117,7 @@ files:
117
117
  - lib/monzo/balance.rb
118
118
  - lib/monzo/client.rb
119
119
  - lib/monzo/configuration.rb
120
+ - lib/monzo/errors.rb
120
121
  - lib/monzo/feed_item.rb
121
122
  - lib/monzo/pot.rb
122
123
  - lib/monzo/transaction.rb