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 +4 -4
- data/README.md +26 -0
- data/lib/monzo.rb +1 -0
- data/lib/monzo/client.rb +17 -0
- data/lib/monzo/errors.rb +6 -0
- data/lib/monzo/pot.rb +79 -2
- data/lib/monzo/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 416017c7fb95dddb3f6abbd9b29b5113b854b912
|
4
|
+
data.tar.gz: ef730307125fad7edd2467933b27fe90212aa92f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
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
|
data/lib/monzo.rb
CHANGED
data/lib/monzo/client.rb
CHANGED
@@ -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.
|
data/lib/monzo/errors.rb
ADDED
data/lib/monzo/pot.rb
CHANGED
@@ -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
|
-
|
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
|
data/lib/monzo/version.rb
CHANGED
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.
|
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:
|
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
|