coinbase 0.0.1 → 2.0.0

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of coinbase might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
- SHA256:
3
- metadata.gz: fd20c87743ea6c8d95575db35841ffd10b1dc87376d9d1e2e08803998b8322e8
4
- data.tar.gz: 475ac4213ae2e15c5ea5002441302faa908524ccb067e7a0ce99194d15c5b049
2
+ SHA1:
3
+ metadata.gz: 1f6f837948954a200e21988327ab19e83bacde78
4
+ data.tar.gz: e15a00800ff4529c813ceb1a981195d4e5d3fd1b
5
5
  SHA512:
6
- metadata.gz: 94fadca140def76fb428a824eca188f3eaa77388963f59ec04d70adfc1c1ccf2002c8af791bfc84bb7d83b07753b83fc755cec3af149e6c4549d51c594490ba6
7
- data.tar.gz: 8d04ee71428ee01611445c71f7642a205493e4a12f365826726132fc5fc783015e7aaa7e98e3813da924c914336bc51592198b6e0b3cc77b4e7e0d2226fdc655
6
+ metadata.gz: def938226ec7c66dbd960c38a2bd12a8a07230bb68d26e97d17c35342e3000e9eeafaf6c1949f6df8e0be15e6547ca6dfdd636fdc0d8cae50484bf1d9dec1bd9
7
+ data.tar.gz: 4445e6a817be9f527da079471a9c1afeab4a141750834f9aa30fa0fc24ac4361d23462787b3a374b2061386b306a7dd6330c5410c53b12fe03da906c0ca6e1c3
data/.gitignore ADDED
@@ -0,0 +1,17 @@
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*
data/.travis.yml ADDED
@@ -0,0 +1,5 @@
1
+ rvm:
2
+ - 1.9.2
3
+ - 1.9.3
4
+ - jruby-19mode
5
+ - rbx-19mode
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in 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,330 @@
1
+ # Coinbase
2
+
3
+ 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).
4
+
5
+ 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.
6
+
7
+ ## Installation
8
+
9
+ Add this line to your application's Gemfile:
10
+
11
+ gem 'coinbase'
12
+
13
+ Then execute:
14
+
15
+ $ bundle install
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install coinbase
20
+
21
+ ## Usage
22
+
23
+ ### HMAC Authentication (for accessing your own account)
24
+
25
+ Start by [enabling an API Key on your account](https://coinbase.com/settings/api)
26
+
27
+ Next, create an instance of the client and pass it your API Key + Secret as parameters.
28
+
29
+ ```ruby
30
+ coinbase = Coinbase::Client.new(ENV['COINBASE_API_KEY'], ENV['COINBASE_API_SECRET'])
31
+ ```
32
+
33
+ ### OAuth 2.0 Authentication (for accessing others' accounts)
34
+
35
+ Start by [creating a new OAuth 2.0 application](https://coinbase.com/oauth/applications)
36
+
37
+ ```ruby
38
+ # Obtaining the OAuth credentials is outside the scope of this gem
39
+ user_credentials = {
40
+ :access_token => 'access_token',
41
+ :refresh_token => 'refresh_token',
42
+ :expires_at => Time.now + 1.day
43
+ }
44
+ coinbase = Coinbase::OAuthClient.new(ENV['COINBASE_CLIENT_ID'], ENV['COINBASE_CLIENT_SECRET'], user_credentials)
45
+ ```
46
+
47
+ 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).
48
+
49
+ Now you can call methods on `coinbase` similar to the ones described in the [api reference](https://coinbase.com/api/doc). For example:
50
+
51
+ ```ruby
52
+ coinbase.balance
53
+ => #<Money fractional:20035300000 currency:BTC>
54
+ coinbase.balance.format
55
+ => "200.35300000 BTC"
56
+ coinbase.balance.to_d
57
+ => #<BigDecimal:7ff36b091670,'0.200353E3',18(54)>
58
+ coinbase.balance.to_s
59
+ => 200.35300000 # BTC amount
60
+ ```
61
+
62
+ [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.
63
+
64
+ ## Examples
65
+
66
+ ### Check your balance
67
+
68
+ ```ruby
69
+ coinbase.balance.to_s
70
+ => "200.35300000" # BTC amount
71
+ ```
72
+
73
+ ### Send bitcoin
74
+
75
+ ```ruby
76
+ r = coinbase.send_money 'user@example.com', 1.23
77
+ r.success?
78
+ => true
79
+ r.transaction.status
80
+ => '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
81
+ r.transaction.id
82
+ => '501a1791f8182b2071000087'
83
+ r.transaction.recipient.email
84
+ => 'user@example.com'
85
+ r.to_hash
86
+ => ... # raw hash response
87
+ ```
88
+
89
+ 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.
90
+
91
+ ```ruby
92
+ r = coinbase.send_money 'user@example.com', 1.23.to_money('AUS')
93
+ r.transaction.amount.format
94
+ => "0.06713955 BTC"
95
+ ```
96
+
97
+ 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).
98
+
99
+ ```ruby
100
+ r = coinbase.send_money 'mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x', 2.23.to_money("USD"), "thanks for the coffee!"
101
+ r.transaction.recipient_address
102
+ => "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
103
+ r.transaction.notes
104
+ => "thanks for the coffee!"
105
+ ```
106
+
107
+ ### Request bitcoin
108
+
109
+ This will send an email to the recipient, requesting payment, and give them an easy way to pay.
110
+
111
+ ```ruby
112
+ r = coinbase.request_money 'client@example.com', 50, "contractor hours in January (website redesign for 50 BTC)"
113
+ r.transaction.request?
114
+ => true
115
+ r.transaction.id
116
+ => '501a3554f8182b2754000003'
117
+ r = coinbase.resend_request '501a3554f8182b2754000003'
118
+ r.success?
119
+ => true
120
+ r = coinbase.cancel_request '501a3554f8182b2754000003'
121
+ r.success?
122
+ => true
123
+ # from the other account
124
+ r = coinbase.complete_request '501a3554f8182b2754000003'
125
+ r.success?
126
+ => true
127
+ ```
128
+
129
+ ### List your current transactions
130
+
131
+ 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)`.
132
+
133
+ ```ruby
134
+ r = coinbase.transactions
135
+ r.current_page
136
+ => 1
137
+ r.num_pages
138
+ => 7
139
+ r.transactions.collect{|t| t.transaction.id }
140
+ => ["5018f833f8182b129c00002f", "5018f833f8182b129c00002e", ...]
141
+ r.transactions.collect{|t| t.transaction.amount.format }
142
+ => ["-1.10000000 BTC", "42.73120000 BTC", ...]
143
+ ```
144
+
145
+ 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).
146
+
147
+ ### Get transaction details
148
+
149
+ This will fetch the details/status of a transaction that was made within Coinbase or outside of Coinbase
150
+
151
+ ```ruby
152
+ r = coinbase.transaction '5011f33df8182b142400000e'
153
+ r.transaction.status
154
+ => 'pending'
155
+ r.transaction.recipient_address
156
+ => 'mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x'
157
+ ```
158
+
159
+ ### Check bitcoin prices
160
+
161
+ 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.
162
+
163
+ 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.
164
+
165
+ ```ruby
166
+ coinbase.buy_price(1).format
167
+ => "$17.95"
168
+ coinbase.buy_price(30).format
169
+ => "$539.70"
170
+ ```
171
+
172
+
173
+ ```ruby
174
+ coinbase.sell_price(1).format
175
+ => "$17.93"
176
+ coinbase.sell_price(30).format
177
+ => "$534.60"
178
+ ```
179
+
180
+ 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.
181
+
182
+ ```ruby
183
+ coinbase.spot_price.format
184
+ => "$431.42"
185
+ coinbase.spot_price('EUR').format
186
+ => "€307,40"
187
+ ```
188
+
189
+ ### Buy or Sell bitcoin
190
+
191
+ Buying and selling bitcoin requires you to [add a payment method](https://coinbase.com/buys) through the web app first.
192
+
193
+ Then you can call `buy!` or `sell!` and pass a `quantity` of bitcoin you want to buy (as a float or integer).
194
+
195
+ ```ruby
196
+ r = coinbase.buy!(1)
197
+ r.transfer.code
198
+ => '6H7GYLXZ'
199
+ r.transfer.btc.format
200
+ => "1.00000000 BTC"
201
+ r.transfer.total.format
202
+ => "$17.95"
203
+ r.transfer.payout_date
204
+ => 2013-02-01 18:00:00 -0800
205
+ ```
206
+
207
+
208
+ ```ruby
209
+ r = coinbase.sell!(1)
210
+ r.transfer.code
211
+ => 'RD2OC8AL'
212
+ r.transfer.btc.format
213
+ => "1.00000000 BTC"
214
+ r.transfer.total.format
215
+ => "$17.93"
216
+ r.transfer.payout_date
217
+ => 2013-02-01 18:00:00 -0800
218
+ ```
219
+
220
+ ### Listing Buy/Sell History
221
+
222
+ You can use `transfers` to view past buys and sells.
223
+
224
+ ```ruby
225
+ r = coinbase.transfers
226
+ r.current_page
227
+ => 1
228
+ r.total_count
229
+ => 7
230
+ r.transfers.collect{|t| t.transfer.type }
231
+ => ["Buy", "Buy", ...]
232
+ r.transfers.collect{|t| t.transfer.btc.amount }
233
+ => [0.01, 0.01, ...]
234
+ r.transfers.collect{|t| t.transfer.total.amount }
235
+ => [5.72, 8.35, ...]
236
+ ```
237
+
238
+ ### Create a payment button
239
+
240
+ 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).
241
+
242
+ 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).
243
+
244
+ ```ruby
245
+ r = coinbase.create_button "Your Order #1234", 42.95.to_money('EUR'), "1 widget at €42.95", "my custom tracking code for this order"
246
+ r.button.code
247
+ => "93865b9cae83706ae59220c013bc0afd"
248
+ r.embed_html
249
+ => "<div class=\"coinbase-button\" data-code=\"93865b9cae83706ae59220c013bc0afd\"></div><script src=\"https://coinbase.com/assets/button.js\" type=\"text/javascript\"></script>"
250
+ ```
251
+
252
+ ### Create an order for a button
253
+
254
+ 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).
255
+
256
+ ```ruby
257
+ r = coinbase.create_order_for_button "93865b9cae83706ae59220c013bc0afd"
258
+ => "{\"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}}"
259
+ ```
260
+
261
+ ### Create a new user
262
+
263
+ ```ruby
264
+ r = coinbase.create_user "newuser@example.com", "some password"
265
+ r.user.email
266
+ => "newuser@example.com"
267
+ r.receive_address
268
+ => "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
269
+ ```
270
+
271
+ A receive address is returned also in case you need to send the new user a payment right away.
272
+
273
+ 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.
274
+
275
+ ```ruby
276
+ r = coinbase.create_user "newuser@example.com", "some password", client_id, ['transactions', 'buy', 'sell']
277
+ r.user.email
278
+ => "newuser@example.com"
279
+ r.receive_address
280
+ => "mpJKwdmJKYjiyfNo26eRp4j6qGwuUUnw9x"
281
+ r.oauth.access_token
282
+ => "93865b9cae83706ae59220c013bc0afd93865b9cae83706ae59220c013bc0afd"
283
+ ```
284
+
285
+ ## Adding new methods
286
+
287
+ 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).
288
+
289
+ 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:
290
+
291
+ ```ruby
292
+ coinbase.get('/account/balance').to_hash
293
+ => {"amount"=>"50.00000000", "currency"=>"BTC"}
294
+ ```
295
+
296
+ Or feel free to add a new wrapper method and submit a pull request.
297
+
298
+ ## Security Notes
299
+
300
+ 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.
301
+
302
+ 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.
303
+
304
+ ## Decimal precision
305
+
306
+ 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.
307
+
308
+ 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).
309
+
310
+ For this reason, we provide examples which use BigDecimal as the preferred way to perform arithmetic:
311
+
312
+ ```coinbase.balance.to_d
313
+ => #<BigDecimal:7ff36b091670,'0.200353E3',18(54)>
314
+ ```
315
+
316
+ ## Testing
317
+
318
+ If you'd like to contribute code or modify this gem, you can run the test suite with:
319
+
320
+ ```ruby
321
+ gem install coinbase --dev
322
+ bundle exec rspec # or just 'rspec' may work
323
+ ```
324
+
325
+ ## Contributing
326
+
327
+ 1. Fork this repo and make changes in your own copy
328
+ 2. Add a test if applicable and run the existing tests with `rspec` to make sure they pass
329
+ 3. Commit your changes and push to your fork `git push origin master`
330
+ 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
data/coinbase.gemspec ADDED
@@ -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 'coinbase/version'
5
+
6
+ Gem::Specification.new do |gem|
7
+ gem.name = "coinbase"
8
+ gem.version = Coinbase::VERSION
9
+ gem.authors = ["Brian Armstrong"]
10
+ gem.email = [""]
11
+ gem.description = ["An easy way to buy, send, and accept bitcoin."]
12
+ gem.summary = ["An easy way to buy, send, and accept bitcoin."]
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