Skip to content

Commit

Permalink
Initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
sferik committed Jun 16, 2011
0 parents commit faa878d
Show file tree
Hide file tree
Showing 22 changed files with 454 additions and 0 deletions.
1 change: 1 addition & 0 deletions .autotest
@@ -0,0 +1 @@
require 'autotest/bundler'
Empty file added .gemtest
Empty file.
41 changes: 41 additions & 0 deletions .gitignore
@@ -0,0 +1,41 @@
!.gitignore
*.gem
*.rbc
*.sw[a-p]
*.tmproj
*.tmproject
*.un~
*~
.DS_Store
.Spotlight-V100
.Trashes
._*
.bundle
.config
.directory
.elc
.redcar
.yardoc
/.emacs.desktop
/.emacs.desktop.lock
Desktop.ini
Gemfile.lock
Icon?
InstalledFiles
Session.vim
Thumbs.db
\#*\#
_yardoc
auto-save-list
coverage
doc/
lib/bundler/man
pkg
pkg/*
rdoc
spec/reports
test/tmp
test/version_tmp
tmp
tmtags
tramp
3 changes: 3 additions & 0 deletions .rspec
@@ -0,0 +1,3 @@
--color
--format=nested
--backtrace
7 changes: 7 additions & 0 deletions .travis.yml
@@ -0,0 +1,7 @@
rvm:
- 1.8.7
- 1.9.1
- 1.9.2
- jruby
- ree
- ruby-head
3 changes: 3 additions & 0 deletions .yardopts
@@ -0,0 +1,3 @@
--markup markdown
-
LICENSE.md
7 changes: 7 additions & 0 deletions Gemfile
@@ -0,0 +1,7 @@
source 'http://rubygems.org'

platforms :jruby do
gem 'jruby-openssl', '~> 0.7'
end

gemspec
20 changes: 20 additions & 0 deletions LICENSE.md
@@ -0,0 +1,20 @@
Copyright (c) 2011 Erik Michaels-Ober

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
130 changes: 130 additions & 0 deletions README.md
@@ -0,0 +1,130 @@
# Ruby wrapper for the Mt. Gox Trade API

Mt. Gox allows you to trade US Dollars (USD) for Bitcoins (BTC) or Bitcoins for
US Dollars.

Installation
------------
gem install tradehill

Alias
-----
After installing the gem, you can get the current price for 1 BTC in USD by
typing `btc` in your bash shell simply by setting the following alias:

alias btc='ruby -r rubygems -r tradehill -e "puts TradeHill.ticker.last"'

Documentation
-------------
[http://rdoc.info/gems/tradehill](http://rdoc.info/gems/tradehill)

Donate
------
If you find this library useful, please consider sending a donation to the
author, which you can do using the following script:

require 'rubygems'
require 'tradehill'

# Certain methods require authentication
TradeHill.configure do |config|
config.name = YOUR_MTGOX_USERNAME
config.pass = YOUR_MTGOX_PASSWORD
end

TradeHill.send 1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L"

Continuous Integration
----------------------
[![Build Status](http://travis-ci.org/sferik/tradehill.png)](http://travis-ci.org/sferik/tradehill)

Usage Examples
--------------
require 'rubygems'
require 'tradehill'

# Fetch the latest price for 1 BTC in USD
puts TradeHill.ticker.last

# Fetch open asks
puts TradeHill.asks

# Fetch open bids
puts TradeHill.bids

# Fetch the last 48 hours worth of trades (takes a minute)
puts TradeHill.trades

# Certain methods require authentication
TradeHill.configure do |config| [TODO]
config.name = YOUR_MTGOX_USERNAME
config.pass = YOUR_MTGOX_PASSWORD
end

# Get your current balance
puts TradeHill.balance [TODO]

# Place an order to buy 1 BTC for 20 USD (returns a list of your open orders)
puts TradeHill.buy 1.0, 20.0 [TODO]

# Place an order to sell 1 BTC for 20 USD (returns a list of your open orders)
puts TradeHill.sell 1.0, 20.0 [TODO]

# Cancel order #1234567890
puts TradeHill.cancel 1234567890 [TODO]

# Send 1 BTC to the author of this gem
puts TradeHill.send 1.0, "1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L" [TODO]

Contributing
------------
In the spirit of [free
software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is
encouraged to help improve this project.

Here are some ways *you* can contribute:

* by using alpha, beta, and prerelease versions
* by reporting bugs
* by suggesting new features
* by writing or editing documentation
* by writing specifications
* by writing code (**no patch is too small**: fix typos, add comments, clean up
inconsistent whitespace)
* by refactoring code
* by closing [issues](https://github.com/sferik/tradehill/issues)
* by reviewing patches
* by financially (please send bitcoin donations to
1KxSo9bGBfPVFEtWNLpnUK1bfLNNT4q31L)

Submitting an Issue
-------------------
We use the [GitHub issue tracker](https://github.com/sferik/tradehill/issues) to
track bugs and features. Before submitting a bug report or feature request,
check to make sure it hasn't already been submitted. You can indicate support
for an existing issuse by voting it up. When submitting a bug report, please
include a [Gist](https://gist.github.com/) that includes a stack trace and any
details that may be necessary to reproduce the bug, including your gem version,
Ruby version, and operating system. Ideally, a bug report should include a pull
request with failing specs.

Submitting a Pull Request
-------------------------
1. Fork the project.
2. Create a topic branch.
3. Implement your feature or bug fix.
4. Add documentation for your feature or bug fix.
5. Run <tt>bundle exec rake doc:yard</tt>. If your changes are not 100%
documented, go back to step 4.
6. Add specs for your feature or bug fix.
7. Run <tt>bundle exec rake spec</tt>. If your changes are not 100% covered, go
back to step 6.
8. Commit and push your changes.
9. Submit a pull request. Please do not include changes to the gemspec,
version, or history file. (If you want to create your own version for some
reason, please do so in a separate commit.)

Copyright
---------
Copyright (c) 2011 Erik Michaels-Ober.
See [LICENSE](https://github.com/sferik/tradehill/blob/master/LICENSE.md) for details.
18 changes: 18 additions & 0 deletions Rakefile
@@ -0,0 +1,18 @@
#!/usr/bin/env rake

require 'bundler'
Bundler::GemHelper.install_tasks

require 'rspec/core/rake_task'
RSpec::Core::RakeTask.new(:spec)

task :default => :spec
task :test => :spec

require 'yard'
namespace :doc do
YARD::Rake::YardocTask.new do |task|
task.files = ['LICENSE.md', 'lib/**/*.rb']
task.options = ['--markup', 'markdown']
end
end
3 changes: 3 additions & 0 deletions lib/foo.rb
@@ -0,0 +1,3 @@
Thread.new{puts "HELLO"}
Thread.new{puts "GOODBYE"}
sleep 2
20 changes: 20 additions & 0 deletions lib/tradehill.rb
@@ -0,0 +1,20 @@
require 'TradeHill/client'

module TradeHill
# Alias for TradeHill::Client.new
#
# @return [TradeHill::Client]
def self.new
TradeHill::Client.new
end

# Delegate to TradeHill::Client
def self.method_missing(method, *args, &block)
return super unless new.respond_to?(method)
new.send(method, *args, &block)
end

def self.respond_to?(method, include_private=false)
new.respond_to?(method, include_private) || super(method, include_private)
end
end
46 changes: 46 additions & 0 deletions lib/tradehill/client.rb
@@ -0,0 +1,46 @@
require 'tradehill/connection'
require 'tradehill/request'

module TradeHill
class Client
include TradeHill::Connection
include TradeHill::Request

# Fetch open asks
#
# @return [Array<Numeric>]
# @example
# TradeHill.asks
def asks
get('/API/USD/Orderbook')['asks'].each do |o|
o[0] = o[0].to_f
o[1] = o[1].to_f
end
end

# Fetch open bids
#
# @return [Array<Numeric>]
# @example
# TradeHill.bids
def bids
get('/API/USD/Orderbook')['bids'].each do |o|
o[0] = o[0].to_f
o[1] = o[1].to_f
end
end

# Fetch recent trades
#
# @return [Array<Hashie::Rash>]
# @example
# TradeHill.trades
def trades
get('/API/USD/Trades').each do |t|
t['date'] = Time.at(t['date'].to_i)
t['price'] = t['price'].to_f
t['amount'] = t['amount'].to_f
end
end
end
end
22 changes: 22 additions & 0 deletions lib/tradehill/connection.rb
@@ -0,0 +1,22 @@
require 'faraday_middleware'

module TradeHill
module Connection
private

def connection(options={})
options = {
:ssl => {:verify => false},
:url => 'https://api.tradehill.com',
}.merge(options)

Faraday.new(options) do |connection|
connection.use Faraday::Request::UrlEncoded
connection.use Faraday::Response::RaiseError
connection.use Faraday::Response::Rashify
connection.use Faraday::Response::ParseJson
connection.adapter(Faraday.default_adapter)
end
end
end
end
16 changes: 16 additions & 0 deletions lib/tradehill/request.rb
@@ -0,0 +1,16 @@
module TradeHill
module Request
def get(path, options={})
request(:get, path, options)
end

private

def request(method, path, options)
response = connection.send(method) do |request|
request.url(path, options)
end
response.body
end
end
end
3 changes: 3 additions & 0 deletions lib/tradehill/version.rb
@@ -0,0 +1,3 @@
module TradeHill
VERSION = "0.0.1"
end
1 change: 1 addition & 0 deletions spec/fixtures/orderbook.json
@@ -0,0 +1 @@
{"bids": [["16.40000000", "10.18000000"], ["14.60000000", "1.00000000"], ["6.05000000", "10.00000000"], ["16.00000000", "84.20000000"], ["15.00000000", "40.00000000"], ["0.01000000", "100000.00000000"], ["17.50000000", "10.00000000"], ["15.90000000", "15.60000000"], ["18.10000000", "10.00000000"], ["13.00000000", "0.10000000"], ["10.00000000", "0.19000000"], ["17.00000000", "0.21200000"], ["16.25000000", "0.15000000"], ["10.00000000", "29.00000000"], ["17.00000000", "9.00000000"], ["18.00000000", "3.00000000"], ["12.00000000", "29.00000000"], ["15.00000000", "9.00000000"], ["17.00000000", "20.00000000"], ["18.90000000", "19.00000000"], ["18.85000000", "2.00000000"], ["18.95000000", "2.00000000"], ["16.80000000", "20.00000000"], ["16.00000000", "20.00000000"], ["18.01000000", "5.00000000"], ["18.00000000", "9.89000000"], ["18.90000000", "2.00000000"], ["12.00000000", "20.00000000"], ["13.20000000", "2.00000000"], ["15.00000000", "8.00000000"], ["14.60000000", "1.30000000"], ["15.00000000", "3.95000000"], ["15.00000000", "10.00000000"], ["17.00000000", "25.00000000"], ["18.25000000", "125.00000000"], ["8.90000000", "1.00000000"], ["18.20000000", "13.00000000"], ["9.94000000", "30.00000000"], ["10.05000000", "10.00000000"], ["12.00000000", "24.90000000"], ["7.13500000", "1.00000000"], ["2.00000000", "800.00000000"], ["18.51000000", "2.66200000"], ["7.00000000", "8.50000000"], ["12.00000000", "0.40000000"], ["18.70000000", "0.09000000"], ["15.12000000", "15.00000000"], ["15.00000000", "5.00000000"], ["18.10000000", "10.00000000"], ["16.05000000", "10.00000000"], ["15.03000000", "5.43050000"], ["17.50000000", "13.00000000"], ["11.12000000", "2.00000000"], ["15.00000000", "10.00000000"], ["17.50000000", "20.00000000"], ["15.00000000", "4.00000000"], ["17.01000000", "0.11000000"], ["11.00000000", "30.00000000"], ["6.50000000", "0.20000000"], ["12.05000000", "10.00000000"], ["10.00000000", "9.80000000"], ["7.10000000", "1.45000000"], ["15.05000000", "3.50000000"], ["8.00000000", "2.40000000"], ["15.00000000", "0.40000000"], ["18.20000000", "20.00000000"], ["18.00000000", "3.00000000"], ["14.00000000", "2.00000000"], ["18.00000000", "5.52000000"], ["17.01000000", "3.00000000"], ["12.00000000", "35.00000000"], ["17.00000000", "23.00000000"], ["18.50000000", "75.00000000"]], "asks": [["20.00000000", "37.17100000"], ["19.60000000", "1.00000000"], ["30.00000000", "8.00000000"], ["33.00000000", "0.20000000"], ["27.00000000", "3.00000000"], ["19.99000000", "1.02000000"], ["26.00000000", "7.50000000"], ["19.50000000", "50.00000000"], ["29.92000000", "1.00000000"], ["29.80000000", "1.00000000"], ["22.49000000", "4.00000000"], ["21.10000000", "1.00000000"], ["19.75000000", "50.00000000"], ["21.00000000", "2.00000000"], ["19.30000000", "17.12618356"], ["20.99000000", "6.36000000"], ["19.87000000", "30.71228300"], ["29.98000000", "20.00000000"], ["30.00000000", "0.10000000"], ["22.50000000", "2.00000000"], ["21.77000000", "1.00000000"], ["30.00000000", "8.00000000"], ["26.00000000", "1.00000000"], ["25.00000000", "2.00000000"], ["26.38000000", "0.10000000"], ["21.00000000", "1.00000000"], ["50.00000000", "10.00000000"], ["19.69000000", "8.56500000"], ["19.49000000", "1.50000000"], ["20.00000000", "50.00000000"], ["30.00000000", "1.37000000"], ["34.00000000", "8.00000000"], ["23.00000000", "0.39000000"], ["25.00000000", "1.00000000"], ["29.00000000", "1.00000000"], ["50.00000000", "0.40000000"], ["23.00000000", "5.00000000"], ["20.00000000", "10.00000000"], ["22.00000000", "1.00000000"], ["20.45000000", "0.55000000"], ["25.00000000", "1.00000000"], ["23.00000000", "0.15000000"], ["19.95000000", "0.25000000"], ["21.98000000", "50.00000000"], ["19.98000000", "50.00000000"], ["19.25000000", "49.85000000"], ["27.70000000", "25.00000000"], ["21.00000000", "12.00000000"], ["25.00000000", "9.00000000"], ["19.45000000", "1.98000000"], ["34.43000000", "1.00000000"], ["34.77000000", "1.00000000"], ["21.50000000", "0.18000000"], ["22.49000000", "2.00000000"], ["27.00000000", "2.00000000"], ["21.00000000", "0.15000000"], ["20.05000000", "5.00000000"], ["35.00000000", "0.10000000"], ["38.00000000", "0.10000000"], ["20.00000000", "1.00000000"], ["29.50000000", "10.00000000"], ["21.50000000", "0.16700000"], ["20.00000000", "3.00000000"], ["20.30000000", "1.00000000"], ["24.00000000", "1.00000000"], ["25.00000000", "12.00000000"], ["19.50000000", "0.25000000"], ["29.98000000", "5.00000000"], ["33.00000000", "10.00000000"], ["20.12000000", "1.04400000"], ["20.00000000", "1.00000000"], ["30.00000000", "10.00000000"], ["23.00000000", "3.00000000"], ["19.79990000", "2.00000000"], ["20.31000000", "20.00000000"], ["30.00000000", "1.01000000"], ["30.00000000", "2.00000000"], ["28.99000000", "4.00000000"], ["50.00000000", "0.40000000"], ["38.00000000", "50.00000000"], ["42.00000000", "25.00000000"], ["24.97000000", "9.93000000"], ["40.00000000", "0.25000000"], ["35.00000000", "50.00000000"], ["40.00000000", "60.00000000"], ["30.00000000", "20.00000000"], ["28.00000000", "1.00000000"], ["32.00000000", "0.20000000"], ["47.00000000", "30.00000000"], ["30.00000000", "1.00000000"], ["28.00000000", "4.00000000"], ["30.00000000", "4.00000000"], ["35.00000000", "5.00000000"], ["21.00000000", "200.00000000"], ["25.00000000", "2.00000000"], ["19.99000000", "1.00000000"], ["20.00000000", "1.00000000"], ["23.00000000", "2.00000000"], ["25.00000000", "25.00000000"], ["30.00000000", "40.00000000"], ["25.00000000", "0.20000000"], ["19.49000000", "6.56279704"], ["32.00000000", "10.00000000"], ["200.00000000", "19.30000000"], ["20.50000000", "15.00000000"], ["30.00000000", "27.96000000"], ["24.49880000", "8.64000000"], ["29.68000000", "1.00000000"], ["32.00000000", "0.10000000"], ["50.00000000", "0.25000000"], ["33.00000000", "5.99000000"], ["24.98000000", "50.00000000"], ["20.00000000", "2.00000000"], ["21.00000000", "20.00000000"], ["19.24999900", "100.00000000"], ["23.90000000", "3.00000000"], ["24.00000000", "3.00000000"], ["19.70000000", "54.37000000"], ["19.99000000", "15.00000000"], ["35.00000000", "25.00000000"], ["25.00000000", "5.00000000"], ["25.00000000", "1.00000000"], ["50.00000000", "5.00000000"], ["20.13000000", "1.00000000"], ["31.00000000", "0.10000000"], ["20.12000000", "1.00000000"], ["19.75000000", "1.00000000"], ["21.00000000", "25.00000000"], ["25.00000000", "7.45730000"], ["20.50000000", "50.00000000"], ["37.00000000", "100.00000000"], ["29.89000000", "1.00000000"], ["30.00000000", "500.00000000"], ["30.00000000", "1.00000000"]]}
1 change: 1 addition & 0 deletions spec/fixtures/trades.json

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions spec/helper.rb
@@ -0,0 +1,23 @@
$:.unshift File.expand_path('..', __FILE__)
$:.unshift File.expand_path('../../lib', __FILE__)
require 'simplecov'
SimpleCov.start
require 'tradehill'
require 'rspec'
require 'webmock/rspec'

def a_get(path)
a_request(:get, 'https://api.tradehill.com' + path)
end

def stub_get(path)
stub_request(:get, 'https://api.tradehill.com' + path)
end

def fixture_path
File.expand_path('../fixtures', __FILE__)
end

def fixture(file)
File.new(fixture_path + '/' + file)
end

0 comments on commit faa878d

Please sign in to comment.