better-coinbase 0.1.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 6a3f56cb9f3b0472313175a38e24361a6f469f3b
4
+ data.tar.gz: 4531b765ab8be02f1c43f3fcdb4888c4f281c87e
5
+ SHA512:
6
+ metadata.gz: 12755651583c981814e8d96e2dddd92fde78a28d2fe0cac954107d5ccbf7ebfab3302218fc0a886d526a4fd0fd6e9dc88346698aedae3d2f8e5dc45c311f9504
7
+ data.tar.gz: 9f5dca6e2332381caf8622fc812c4a255bd808f1b0e37c8343a76ec57d7715fe69f46132ce14b70407391f142a2f334b69085a62c3df6e4ff8b3a6a3f42dcb0e
data/.gitignore ADDED
@@ -0,0 +1,19 @@
1
+ *.gem
2
+ *.rbc
3
+ .bundle
4
+ .config
5
+ .yardoc
6
+ Gemfile.lock
7
+ InstalledFiles
8
+ _yardoc
9
+ coverage
10
+ doc/
11
+ lib/bundler/man
12
+ pkg
13
+ rdoc
14
+ test/tmp
15
+ test/version_tmp
16
+ tmp
17
+ .tddium*
18
+ .idea/
19
+ .idea
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 1.9.3
4
+ - 2.0.0
5
+ - 2.1.1
6
+ - jruby-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in better-coinbase-ruby.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2013 Coinbase
2
+
3
+ MIT License
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,336 @@
1
+ # Better! Coinbase
2
+
3
+ The Coinbase gem was not maintained and missing some key features from their API. This frustrated me.
4
+
5
+ So here's what I think is a better version - I will maintain it.
6
+
7
+ -
8
+
9
+ An easy way to buy, send, and accept [bitcoin](http://en.wikipedia.org/wiki/Bitcoin) through the [Coinbase API](https://coinbase.com/docs/api/overview).
10
+
11
+ This gem is a wrapper around the [Coinbase JSON API](https://coinbase.com/api/doc). It supports both the the [api key + secret authentication method](https://coinbase.com/docs/api/authentication) as well as OAuth 2.0 for performing actions on other people's account.
12
+
13
+ ## Installation
14
+
15
+ Add this line to your application's Gemfile:
16
+
17
+ gem 'better-coinbase'
18
+
19
+ Then execute:
20
+
21
+ $ bundle install
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install better-coinbase
26
+
27
+ ## Usage
28
+
29
+ ### HMAC Authentication (for accessing your own account)
30
+
31
+ Start by [enabling an API Key on your account](https://coinbase.com/settings/api)
32
+
33
+ Next, create an instance of the client and pass it your API Key + Secret as parameters.
34
+
35
+ ```ruby
36
+ coinbase = BetterCoinbase::Client.new(ENV['COINBASE_API_KEY'], ENV['COINBASE_API_SECRET'])
37
+ ```
38
+
39
+ ### OAuth 2.0 Authentication (for accessing others' accounts)
40
+
41
+ Start by [creating a new OAuth 2.0 application](https://coinbase.com/oauth/applications)
42
+
43
+ ```ruby
44
+ # Obtaining the OAuth credentials is outside the scope of this gem
45
+ user_credentials = {
46
+ :access_token => 'access_token',
47
+ :refresh_token => 'refresh_token',
48
+ :expires_at => Time.now + 1.day
49
+ }
50
+ coinbase = BetterCoinbase::OAuthClient.new(ENV['COINBASE_CLIENT_ID'], ENV['COINBASE_CLIENT_SECRET'], user_credentials)
51
+ ```
52
+
53
+ Notice here that we did not hard code the API keys into our codebase, but set it in an environment variable instead. This is just one example, but keeping your credentials separate from your code base is a good [security practice](https://coinbase.com/docs/api/authentication#security).
54
+
55
+ Now you can call methods on `coinbase` similar to the ones described in the [api reference](https://coinbase.com/api/doc). For example:
56
+
57
+ ```ruby
58
+ coinbase.balance
59
+ => #<Money fractional:20035300000 currency:BTC>
60
+ coinbase.balance.format
61
+ => "200.35300000 BTC"
62
+ coinbase.balance.to_d
63
+ => #<BigDecimal:7ff36b091670,'0.200353E3',18(54)>
64
+ coinbase.balance.to_s
65
+ => 200.35300000 # BTC amount
66
+ ```
67
+
68
+ [Money objects](https://github.com/RubyMoney/money) are returned for most amounts dealing with currency. You can call `to_d`, `format`, or perform math operations on money objects.
69
+
70
+ ## Examples
71
+
72
+ ### Check your balance
73
+
74
+ ```ruby
75
+ coinbase.balance.to_s
76
+ => "200.35300000" # BTC amount
77
+ ```
78
+
79
+ ### Send bitcoin
80
+
81
+ ```ruby
82
+ r = coinbase.send_money 'user@example.com', 1.23
83
+ r.success?
84
+ => true
85
+ r.transaction.status
86
+ => 'pending' # this will change to 'complete' in a few seconds if you are sending coinbase-to-coinbase, otherwise it will take about 1 hour, 'complete' means it cannot be reversed or canceled
87
+ r.transaction.id
88
+ => '501a1791f8182b2071000087'
89
+ r.transaction.recipient.email
90
+ => 'user@example.com'
91
+ r.to_hash
92
+ => ... # raw hash response
93
+ ```
94
+
95
+ You can also send money in [a number of currencies](https://github.com/coinbase/coinbase-ruby/blob/master/supported_currencies.json). The amount will be automatically converted to the correct BTC amount using the current exchange rate.
96
+
97
+ ```ruby
98
+ r = coinbase.send_money 'user@example.com', 1.23.to_money('AUS')
99
+ r.transaction.amount.format
100
+ => "0.06713955 BTC"
101
+ ```
102
+
103
+ The first parameter can also be a bitcoin address and the third parameter can be a note or description of the transaction. Descriptions are only visible on Coinbase (not on the general bitcoin network).
104
+
105
+ ```ruby
106
+ r = coinbase.send_money 'mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x', 2.23.to_money("USD"), "thanks for the coffee!"
107
+ r.transaction.recipient_address
108
+ => "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
109
+ r.transaction.notes
110
+ => "thanks for the coffee!"
111
+ ```
112
+
113
+ ### Request bitcoin
114
+
115
+ This will send an email to the recipient, requesting payment, and give them an easy way to pay.
116
+
117
+ ```ruby
118
+ r = coinbase.request_money 'client@example.com', 50, "contractor hours in January (website redesign for 50 BTC)"
119
+ r.transaction.request?
120
+ => true
121
+ r.transaction.id
122
+ => '501a3554f8182b2754000003'
123
+ r = coinbase.resend_request '501a3554f8182b2754000003'
124
+ r.success?
125
+ => true
126
+ r = coinbase.cancel_request '501a3554f8182b2754000003'
127
+ r.success?
128
+ => true
129
+ # from the other account
130
+ r = coinbase.complete_request '501a3554f8182b2754000003'
131
+ r.success?
132
+ => true
133
+ ```
134
+
135
+ ### List your current transactions
136
+
137
+ Sorted in descending order by timestamp, 30 per page. You can pass an integer as the first param to page through results, for example `coinbase.transactions(2)`.
138
+
139
+ ```ruby
140
+ r = coinbase.transactions
141
+ r.current_page
142
+ => 1
143
+ r.num_pages
144
+ => 7
145
+ r.transactions.collect{|t| t.transaction.id }
146
+ => ["5018f833f8182b129c00002f", "5018f833f8182b129c00002e", ...]
147
+ r.transactions.collect{|t| t.transaction.amount.format }
148
+ => ["-1.10000000 BTC", "42.73120000 BTC", ...]
149
+ ```
150
+
151
+ Transactions will always have an `id` attribute which is the primary way to identity them through the Coinbase api. They will also have a `hsh` (bitcoin hash) attribute once they've been broadcast to the network (usually within a few seconds).
152
+
153
+ ### Get transaction details
154
+
155
+ This will fetch the details/status of a transaction that was made within Coinbase or outside of Coinbase
156
+
157
+ ```ruby
158
+ r = coinbase.transaction '5011f33df8182b142400000e'
159
+ r.transaction.status
160
+ => 'pending'
161
+ r.transaction.recipient_address
162
+ => 'mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x'
163
+ ```
164
+
165
+ ### Check bitcoin prices
166
+
167
+ Check the buy or sell price by passing a `quantity` of bitcoin that you'd like to buy or sell. This price includes Coinbase's fee of 1% and the bank transfer fee of $0.15.
168
+
169
+ The `buy_price` and `sell_price` per Bitcoin will increase and decrease respectively as `quantity` increases. This [slippage](http://en.wikipedia.org/wiki/Slippage_(finance)) is normal and is influenced by the [market depth](http://en.wikipedia.org/wiki/Market_depth) on the exchanges we use.
170
+
171
+ ```ruby
172
+ coinbase.buy_price(1).format
173
+ => "$17.95"
174
+ coinbase.buy_price(30).format
175
+ => "$539.70"
176
+ ```
177
+
178
+
179
+ ```ruby
180
+ coinbase.sell_price(1).format
181
+ => "$17.93"
182
+ coinbase.sell_price(30).format
183
+ => "$534.60"
184
+ ```
185
+
186
+ Check the spot price of Bitcoin in a given `currency`. This is usually somewhere in between the buy and sell price, current to within a few minutes and does not include any Coinbase or bank transfer fees. The default currency is USD.
187
+
188
+ ```ruby
189
+ coinbase.spot_price.format
190
+ => "$431.42"
191
+ coinbase.spot_price('EUR').format
192
+ => "€307,40"
193
+ ```
194
+
195
+ ### Buy or Sell bitcoin
196
+
197
+ Buying and selling bitcoin requires you to [add a payment method](https://coinbase.com/buys) through the web app first.
198
+
199
+ Then you can call `buy!` or `sell!` and pass a `quantity` of bitcoin you want to buy (as a float or integer).
200
+
201
+ ```ruby
202
+ r = coinbase.buy!(1)
203
+ r.transfer.code
204
+ => '6H7GYLXZ'
205
+ r.transfer.btc.format
206
+ => "1.00000000 BTC"
207
+ r.transfer.total.format
208
+ => "$17.95"
209
+ r.transfer.payout_date
210
+ => 2013-02-01 18:00:00 -0800
211
+ ```
212
+
213
+
214
+ ```ruby
215
+ r = coinbase.sell!(1)
216
+ r.transfer.code
217
+ => 'RD2OC8AL'
218
+ r.transfer.btc.format
219
+ => "1.00000000 BTC"
220
+ r.transfer.total.format
221
+ => "$17.93"
222
+ r.transfer.payout_date
223
+ => 2013-02-01 18:00:00 -0800
224
+ ```
225
+
226
+ ### Listing Buy/Sell History
227
+
228
+ You can use `transfers` to view past buys and sells.
229
+
230
+ ```ruby
231
+ r = coinbase.transfers
232
+ r.current_page
233
+ => 1
234
+ r.total_count
235
+ => 7
236
+ r.transfers.collect{|t| t.transfer.type }
237
+ => ["Buy", "Buy", ...]
238
+ r.transfers.collect{|t| t.transfer.btc.amount }
239
+ => [0.01, 0.01, ...]
240
+ r.transfers.collect{|t| t.transfer.total.amount }
241
+ => [5.72, 8.35, ...]
242
+ ```
243
+
244
+ ### Create a payment button
245
+
246
+ This will create the code for a payment button (and modal window) that you can use to accept bitcoin on your website. You can read [more about payment buttons here and try a demo](https://coinbase.com/docs/merchant_tools/payment_buttons).
247
+
248
+ The method signature is `def create_button name, price, description=nil, custom=nil, options={}`. The `custom` param will get passed through in [callbacks](https://coinbase.com/docs/merchant_tools/callbacks) to your site. The list of valid `options` [are described here](https://coinbase.com/api/doc/1.0/buttons/create.html).
249
+
250
+ ```ruby
251
+ r = coinbase.create_button "Your Order #1234", 42.95.to_money('EUR'), "1 widget at €42.95", "my custom tracking code for this order"
252
+ r.button.code
253
+ => "93865b9cae83706ae59220c013bc0afd"
254
+ r.embed_html
255
+ => "<div class=\"coinbase-button\" data-code=\"93865b9cae83706ae59220c013bc0afd\"></div><script src=\"https://coinbase.com/assets/button.js\" type=\"text/javascript\"></script>"
256
+ ```
257
+
258
+ ### Create an order for a button
259
+
260
+ This will generate an order associated with a button. You can read [more about creating an order for a button here](https://coinbase.com/api/doc/1.0/buttons/create_order.html).
261
+
262
+ ```ruby
263
+ r = coinbase.create_order_for_button "93865b9cae83706ae59220c013bc0afd"
264
+ => "{\"success\"=>true, \"order\"=>{\"id\"=>\"ASXTKPZM\", \"created_at\"=>\"2013-12-13T01:36:47-08:00\", \"status\"=>\"new\", \"total_btc\"=>{\"cents\"=>6859115, \"currency_iso\"=>\"BTC\"}, \"total_native\"=>{\"cents\"=>4295, \"currency_iso\"=>\"EUR\"}, \"custom\"=>\"my custom tracking code for this order\", \"receive_address\"=>\"mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x\", \"button\"=>{\"type\"=>\"buy_now\", \"name\"=>\"Your Order #1234\", \"description\"=>\"1 widget at 42.95\", \"id\"=>\"93865b9cae83706ae59220c013bc0afd\"}, \"transaction\"=>nil}}"
265
+ ```
266
+
267
+ ### Create a new user
268
+
269
+ ```ruby
270
+ r = coinbase.create_user "newuser@example.com", "some password"
271
+ r.user.email
272
+ => "newuser@example.com"
273
+ r.receive_address
274
+ => "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
275
+ ```
276
+
277
+ A receive address is returned also in case you need to send the new user a payment right away.
278
+
279
+ You can optionally pass in a client_id parameter that corresponds to your OAuth2 application and an array of permissions. When these are provided, the generated user will automatically have the permissions you’ve specified granted for your application. See the [API Reference](https://coinbase.com/api/doc/1.0/users/create.html) for more details.
280
+
281
+ ```ruby
282
+ r = coinbase.create_user "newuser@example.com", "some password", client_id, ['transactions', 'buy', 'sell']
283
+ r.user.email
284
+ => "newuser@example.com"
285
+ r.receive_address
286
+ => "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
287
+ r.oauth.access_token
288
+ => "93865b9cae83706ae59220c013bc0afd93865b9cae83706ae59220c013bc0afd"
289
+ ```
290
+
291
+ ## Adding new methods
292
+
293
+ You can see a [list of method calls here](https://github.com/coinbase/coinbase-ruby/blob/master/lib/coinbase/client.rb) and how they are implemented. They are a wrapper around the [Coinbase JSON API](https://coinbase.com/api/doc).
294
+
295
+ If there are any methods listed in the [API Reference](https://coinbase.com/api/doc) that haven't been added to the gem yet, you can also call `get`, `post`, `put`, or `delete` with a `path` and optional `params` hash for a quick implementation. The raw response will be returned. For example:
296
+
297
+ ```ruby
298
+ coinbase.get('/account/balance').to_hash
299
+ => {"amount"=>"50.00000000", "currency"=>"BTC"}
300
+ ```
301
+
302
+ Or feel free to add a new wrapper method and submit a pull request.
303
+
304
+ ## Security Notes
305
+
306
+ If someone gains access to your API Key they will have complete control of your Coinbase account. This includes the abillity to send all of your bitcoins elsewhere.
307
+
308
+ For this reason, API access is disabled on all Coinbase accounts by default. If you decide to enable API key access you should take precautions to store your API key securely in your application. How to do this is application specific, but it's something you should [research](http://programmers.stackexchange.com/questions/65601/is-it-smart-to-store-application-keys-ids-etc-directly-inside-an-application) if you have never done this before.
309
+
310
+ ## Decimal precision
311
+
312
+ This gem relies on the [Money](https://github.com/RubyMoney/money) gem, which by default uses the [BigDecimal](www.ruby-doc.org/stdlib-2.0/libdoc/bigdecimal/rdoc/BigDecimal.html) class for arithmetic to maintain decimal precision for all values returned.
313
+
314
+ When working with currency values in your application, it's important to remember that floating point arithmetic is prone to [rounding errors](http://en.wikipedia.org/wiki/Round-off_error).
315
+
316
+ For this reason, we provide examples which use BigDecimal as the preferred way to perform arithmetic:
317
+
318
+ ```coinbase.balance.to_d
319
+ => #<BigDecimal:7ff36b091670,'0.200353E3',18(54)>
320
+ ```
321
+
322
+ ## Testing
323
+
324
+ If you'd like to contribute code or modify this gem, you can run the test suite with:
325
+
326
+ ```ruby
327
+ gem install coinbase --dev
328
+ bundle exec rspec # or just 'rspec' may work
329
+ ```
330
+
331
+ ## Contributing
332
+
333
+ 1. Fork this repo and make changes in your own copy
334
+ 2. Add a test if applicable and run the existing tests with `rspec` to make sure they pass
335
+ 3. Commit your changes and push to your fork `git push origin master`
336
+ 4. Create a new pull request and submit it back to us!
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require 'rspec/core/rake_task'
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,33 @@
1
+ # -*- encoding: utf-8 -*-
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'better-coinbase/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "better-coinbase"
8
+ gem.version = BetterCoinbase::VERSION
9
+ gem.authors = "James Larisch, Brian Armstrong"
10
+ gem.email = "root@jameslarisch.com"
11
+ gem.description = "The Coinbase API, wrapped, maintained, and better."
12
+ gem.summary = "The Coinbase API, wrapped, maintained, and better."
13
+ gem.homepage = "https://coinbase.com/api/doc"
14
+
15
+ gem.files = `git ls-files`.split($/)
16
+ gem.executables = gem.files.grep(%r{^bin/}).map{ |f| File.basename(f) }
17
+ gem.test_files = gem.files.grep(%r{^(test|spec|features)/})
18
+ gem.require_paths = ["lib"]
19
+
20
+
21
+ # Gems that must be intalled for sift to compile and build
22
+ gem.add_development_dependency "rspec", "~> 2.12"
23
+ gem.add_development_dependency "fakeweb", "~> 1.3.0"
24
+ gem.add_development_dependency "rake"
25
+
26
+ # Gems that must be intalled for sift to work
27
+ gem.add_dependency "httparty", ">= 0.8.3"
28
+ gem.add_dependency "multi_json", ">= 1.3.4"
29
+ gem.add_dependency "money", "~> 6.0"
30
+ gem.add_dependency "monetize", "~> 0.3.0"
31
+ gem.add_dependency "hashie", ">= 1.2.0"
32
+ gem.add_dependency "oauth2", "~> 0.9"
33
+ end
@@ -0,0 +1,5 @@
1
+ require "json"
2
+ require "money"
3
+ require "better-coinbase/version"
4
+ require "better-coinbase/client"
5
+ require "better-coinbase/oauth_client"