mtgox-ruby 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,5 @@
1
+ lib/**/*.rb
2
+ bin/*
3
+ -
4
+ features/**/*.feature
5
+ LICENSE.txt
data/Gemfile ADDED
@@ -0,0 +1,11 @@
1
+ source "http://rubygems.org"
2
+ gem "json"
3
+
4
+ group :development do
5
+ gem "shoulda", ">= 0"
6
+ gem "bundler", "~> 1.0.0"
7
+ gem "jeweler", "~> 1.5.2"
8
+ gem "rspec", "> 1.2.3"
9
+ gem "rcov", ">= 0"
10
+ gem 'fakeweb', ">= 0"
11
+ end
@@ -0,0 +1,34 @@
1
+ GEM
2
+ remote: http://rubygems.org/
3
+ specs:
4
+ diff-lcs (1.1.2)
5
+ fakeweb (1.3.0)
6
+ git (1.2.5)
7
+ jeweler (1.5.2)
8
+ bundler (~> 1.0.0)
9
+ git (>= 1.2.5)
10
+ rake
11
+ json (1.5.1)
12
+ rake (0.8.7)
13
+ rcov (0.9.9)
14
+ rspec (2.5.0)
15
+ rspec-core (~> 2.5.0)
16
+ rspec-expectations (~> 2.5.0)
17
+ rspec-mocks (~> 2.5.0)
18
+ rspec-core (2.5.1)
19
+ rspec-expectations (2.5.0)
20
+ diff-lcs (~> 1.1.2)
21
+ rspec-mocks (2.5.0)
22
+ shoulda (2.11.3)
23
+
24
+ PLATFORMS
25
+ ruby
26
+
27
+ DEPENDENCIES
28
+ bundler (~> 1.0.0)
29
+ fakeweb
30
+ jeweler (~> 1.5.2)
31
+ json
32
+ rcov
33
+ rspec (> 1.2.3)
34
+ shoulda
@@ -0,0 +1,49 @@
1
+ require 'rubygems'
2
+ require 'bundler'
3
+ begin
4
+ Bundler.setup(:default, :development)
5
+ rescue Bundler::BundlerError => e
6
+ $stderr.puts e.message
7
+ $stderr.puts "Run `bundle install` to install missing gems"
8
+ exit e.status_code
9
+ end
10
+ require 'rake'
11
+
12
+ require 'jeweler'
13
+ Jeweler::Tasks.new do |gem|
14
+ gem.name = "mtgox-ruby"
15
+ gem.homepage = "http://github.com/paulasmuth/mtgox-ruby"
16
+ gem.license = "MIT"
17
+ gem.summary = "ruby client for the mtgox.com bitcoin exchange api"
18
+ gem.description = "trade bitcoins from ruby"
19
+ gem.email = "paul@23linesofcode.com"
20
+ gem.authors = ["Paul Asmuth"]
21
+ gem.add_development_dependency 'rspec', '> 1.2.3'
22
+ end
23
+ Jeweler::RubygemsDotOrgTasks.new
24
+
25
+ require 'rspec'
26
+ require 'rspec/core/rake_task'
27
+ desc "Run all examples"
28
+ RSpec::Core::RakeTask.new('spec') do |spec|
29
+ spec.pattern = 'spec/*_spec.rb'
30
+ end
31
+
32
+ require 'rcov/rcovtask'
33
+ Rcov::RcovTask.new do |test|
34
+ test.libs << 'test'
35
+ test.pattern = 'test/**/test_*.rb'
36
+ test.verbose = true
37
+ end
38
+
39
+ task :default => :spec
40
+
41
+ require 'rake/rdoctask'
42
+ Rake::RDocTask.new do |rdoc|
43
+ version = File.exist?('VERSION') ? File.read('VERSION') : ""
44
+
45
+ rdoc.rdoc_dir = 'rdoc'
46
+ rdoc.title = "mtgox-ruby #{version}"
47
+ rdoc.rdoc_files.include('README*')
48
+ rdoc.rdoc_files.include('lib/**/*.rb')
49
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.0
@@ -0,0 +1,80 @@
1
+ require 'net/http'
2
+ require 'net/https'
3
+ require 'rubygems'
4
+ require 'json'
5
+
6
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
7
+ require 'mtgox_order'
8
+
9
+ class MtgoxClient
10
+
11
+ MTGOX_API_URL = "https://mtgox.com/code/data/"
12
+
13
+ attr_accessor :api_url
14
+
15
+ def initialize(url=nil)
16
+ self.api_url = url || MTGOX_API_URL
17
+ end
18
+
19
+ def ticker_data
20
+ raise "implement me"
21
+ end
22
+
23
+ def market_depth
24
+ raise "implement me"
25
+ end
26
+
27
+ def recent_trades
28
+ json = JSON.parse(submit_request('getTrades.php'))
29
+ orders = json.map do |trade_json|
30
+ MtgoxOrder.from_json(trade_json)
31
+ end
32
+ return orders
33
+ end
34
+
35
+ def current_balance
36
+ raise "implement me"
37
+ end
38
+
39
+ def open_orders
40
+ raise "implement me"
41
+ end
42
+
43
+ def cancel_order(order)
44
+ raise "implement me"
45
+ end
46
+
47
+ def place_order(order)
48
+ raise "implement me"
49
+ end
50
+
51
+ def send_bitcoin(amount, bitcoin_address)
52
+ raise "implement me"
53
+ end
54
+
55
+ private
56
+
57
+ def submit_request_with_credentials(command, data_hash={})
58
+ submit_request(command, data_hash.merge(
59
+ :user => @username,
60
+ :pass => @password
61
+ ))
62
+ end
63
+
64
+ def submit_request(command, data_string_or_hash="")
65
+ uri = URI::parse(self.api_url+command)
66
+ http = Net::HTTP.new(uri.host, uri.port)
67
+ http.use_ssl = true if uri.scheme == 'https'
68
+ resp, resp_data = http.post(uri.path, build_request_data(data_string_or_hash), nil)
69
+ return resp_data
70
+ end
71
+
72
+ def build_request_data(data_string_or_hash="")
73
+ if data_string_or_hash.is_a?(Hash)
74
+ return "?" + data_string_or_hash.to_a.map{ |param| "#{param[0]}=#{param[1]}" }.join("&")
75
+ else
76
+ return data_string_or_hash
77
+ end
78
+ end
79
+
80
+ end
@@ -0,0 +1,22 @@
1
+ class MtgoxOrder
2
+
3
+ attr_accessor :price, :amount, :time, :order_id, :buy_or_sell
4
+
5
+ def buy?
6
+ self.buy_or_sell == :buy
7
+ end
8
+
9
+ def sell?
10
+ self.buy_or_sell == :sell
11
+ end
12
+
13
+ def self.from_json(json)
14
+ order = self.new
15
+ order.amount = json["amount"].to_f
16
+ order.order_id = json["tid"]
17
+ order.price = json["price"].to_f
18
+ order.time = Time.at(json["date"].to_i)
19
+ return order
20
+ end
21
+
22
+ end
@@ -0,0 +1,36 @@
1
+ = mtgox-ruby
2
+
3
+ ruby api client for the mtgox.com bitcoin exchange
4
+
5
+ {<img src="https://secure.travis-ci.org/paulasmuth/mtgox-ruby.png" />}[http://travis-ci.org/paulasmuth/mtgox-ruby]
6
+
7
+ == Installation
8
+
9
+ In your Gemfile
10
+ gem "mtgox-ruby", :git => "git://github.com/paulasmuth/mtgox-ruby.git"
11
+
12
+ or
13
+ gem install git://github.com/paulasmuth/mtgox-ruby.git
14
+
15
+ == Usage
16
+
17
+ Get recent trades:
18
+
19
+ @mtgox_client = MtgoxClient.new
20
+
21
+ most_recent_trade = @mtgox_client.recent_trades.first
22
+
23
+ most_recent_trade.buy?
24
+ => true
25
+
26
+ most_recent_trade.sell?
27
+ => false
28
+
29
+ most_recent_trade.amount
30
+ => 42
31
+
32
+ most_recent_trade.price
33
+ => 5,23
34
+
35
+ most_recent_trade.order_id
36
+ => 123123123123
@@ -0,0 +1,25 @@
1
+ $: << File.join(File.dirname(__FILE__), '..', 'lib')
2
+ require 'mtgox-ruby'
3
+ require 'fakeweb'
4
+
5
+ FakeWeb.register_uri(:any, 'https://mtgox.com/code/data/getTrades.php',
6
+ :body => ::File.open(::File.expand_path('../get_trades.json', __FILE__)).read
7
+ )
8
+
9
+ describe MtgoxClient do
10
+
11
+ before :each do
12
+ @mtgox_client = MtgoxClient.new
13
+ end
14
+
15
+ it "should receive recent trades as MtgoxOrder objects" do
16
+ recent_trades = @mtgox_client.recent_trades
17
+ recent_trades.should be_a(Array)
18
+ recent_trades.length.should > 0
19
+ recent_trades.each do |trade|
20
+ trade.should be_a(MtgoxOrder)
21
+ trade.amount.should > 0
22
+ end
23
+ end
24
+
25
+ end
metadata ADDED
@@ -0,0 +1,140 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mtgox-ruby
3
+ version: !ruby/object:Gem::Version
4
+ prerelease:
5
+ version: 0.1.0
6
+ platform: ruby
7
+ authors:
8
+ - Paul Asmuth
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+
13
+ date: 2011-05-01 00:00:00 +02:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
17
+ name: json
18
+ prerelease: false
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - ">="
23
+ - !ruby/object:Gem::Version
24
+ version: "0"
25
+ type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: shoulda
29
+ prerelease: false
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ">="
34
+ - !ruby/object:Gem::Version
35
+ version: "0"
36
+ type: :development
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
39
+ name: bundler
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 1.0.0
47
+ type: :development
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: jeweler
51
+ prerelease: false
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 1.5.2
58
+ type: :development
59
+ version_requirements: *id004
60
+ - !ruby/object:Gem::Dependency
61
+ name: rspec
62
+ prerelease: false
63
+ requirement: &id005 !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ">"
67
+ - !ruby/object:Gem::Version
68
+ version: 1.2.3
69
+ type: :development
70
+ version_requirements: *id005
71
+ - !ruby/object:Gem::Dependency
72
+ name: rcov
73
+ prerelease: false
74
+ requirement: &id006 !ruby/object:Gem::Requirement
75
+ none: false
76
+ requirements:
77
+ - - ">="
78
+ - !ruby/object:Gem::Version
79
+ version: "0"
80
+ type: :development
81
+ version_requirements: *id006
82
+ - !ruby/object:Gem::Dependency
83
+ name: rspec
84
+ prerelease: false
85
+ requirement: &id007 !ruby/object:Gem::Requirement
86
+ none: false
87
+ requirements:
88
+ - - ">"
89
+ - !ruby/object:Gem::Version
90
+ version: 1.2.3
91
+ type: :development
92
+ version_requirements: *id007
93
+ description: trade bitcoins from ruby
94
+ email: paul@23linesofcode.com
95
+ executables: []
96
+
97
+ extensions: []
98
+
99
+ extra_rdoc_files:
100
+ - readme.rdoc
101
+ files:
102
+ - .document
103
+ - Gemfile
104
+ - Gemfile.lock
105
+ - readme.rdoc
106
+ - Rakefile
107
+ - VERSION
108
+ - lib/mtgox-ruby.rb
109
+ - lib/mtgox_order.rb
110
+ - spec/mtgox-ruby_spec.rb
111
+ has_rdoc: true
112
+ homepage: http://github.com/paulasmuth/mtgox-ruby
113
+ licenses:
114
+ - MIT
115
+ post_install_message:
116
+ rdoc_options: []
117
+
118
+ require_paths:
119
+ - lib
120
+ required_ruby_version: !ruby/object:Gem::Requirement
121
+ none: false
122
+ requirements:
123
+ - - ">="
124
+ - !ruby/object:Gem::Version
125
+ version: "0"
126
+ required_rubygems_version: !ruby/object:Gem::Requirement
127
+ none: false
128
+ requirements:
129
+ - - ">="
130
+ - !ruby/object:Gem::Version
131
+ version: "0"
132
+ requirements: []
133
+
134
+ rubyforge_project:
135
+ rubygems_version: 1.6.2
136
+ signing_key:
137
+ specification_version: 3
138
+ summary: ruby client for the mtgox.com bitcoin exchange api
139
+ test_files:
140
+ - spec/mtgox-ruby_spec.rb