rbtce 0.1

Sign up to get free protection for your applications and to get access to all the features.
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
+ spec/reports
15
+ test/tmp
16
+ test/version_tmp
17
+ tmp
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in rbtce.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,22 @@
1
+ Copyright (c) 2014 Omer Jakobinsky
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,61 @@
1
+ # Rbtce
2
+
3
+ Learn about the BTC-e API at
4
+ [https://btc-e.com/api/3/documentation](https://btc-e.com/api/3/documentation).
5
+
6
+ ## Installation
7
+
8
+ gem install rbtce
9
+
10
+ ## Usage
11
+
12
+ Note that for the time being only the public API is avaliable in v3.
13
+
14
+ ### Info Endpoint
15
+
16
+ ```ruby
17
+ require 'btce'
18
+
19
+ Rbtce::Info.fetch
20
+ ```
21
+
22
+ ### Ticker Endpoint
23
+
24
+ ```ruby
25
+ require 'btce'
26
+
27
+ Rbtce::Ticker.from( Rbtce::Currency::LTC ).to( Rbtce::Currency::BTC ).
28
+ and.from( Rbtce::Currency::LTC ).to( Rbtce::Currency::USD ).
29
+ fetch
30
+ ```
31
+
32
+ ### Trades Endpoint
33
+
34
+ ```ruby
35
+ require 'btce'
36
+
37
+ Rbtce::Trades.from( Rbtce::Currency::LTC ).to( Rbtce::Currency::BTC ).
38
+ fetch( limit: 2000 )
39
+ ```
40
+
41
+ ### Depth Endpoint
42
+
43
+ ```ruby
44
+ require 'btce'
45
+
46
+ Rbtce::Depth.from( Rbtce::Currency::LTC ).to( Rbtce::Currency::BTC ).fetch
47
+ ```
48
+
49
+ ## Contributing
50
+
51
+ 1. Fork it
52
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
53
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
54
+ 4. Push to the branch (`git push origin my-new-feature`)
55
+ 5. Create new Pull Request
56
+
57
+ ## Donate
58
+
59
+ **BTC:** 1J6urRH6xbMYUdk6shTshrjtRu3PbhupRk
60
+ **LTC:** LX7nFCTEsygsEXpx4k2TAupiJMgrZqsrUr
61
+ **XDG:** DUPGsYEMBsWrYzPxbjyu4mQ7ujhAZj58mu
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/lib/rbtce.rb ADDED
@@ -0,0 +1,14 @@
1
+ require 'json'
2
+ require 'net/http'
3
+ require 'uri'
4
+ require 'cgi'
5
+
6
+ require_relative 'rbtce/version'
7
+ require_relative 'rbtce/client'
8
+ require_relative 'rbtce/currency'
9
+ require_relative 'rbtce/pair_builder'
10
+ require_relative 'rbtce/base_model'
11
+ require_relative 'rbtce/ticker'
12
+ require_relative 'rbtce/trades'
13
+ require_relative 'rbtce/depth'
14
+ require_relative 'rbtce/info'
@@ -0,0 +1,50 @@
1
+ module Rbtce
2
+ class BaseModel
3
+ attr_reader :symbol
4
+
5
+ def initialize( symbol, options )
6
+ @symbol = symbol
7
+ @_options = options
8
+ required_fields.each do |field_name|
9
+ val = options.fetch( field_name )
10
+ val = Time.at( val ) if date_fields.include?( field_name )
11
+ instance_variable_set( "@#{field_name}", val )
12
+ end
13
+ end
14
+
15
+ def self.from( currency )
16
+ PairBuilder.new( end_point: end_point, class: self ).from( currency )
17
+ end
18
+
19
+ def to_json( options={} )
20
+ include_root = options.fetch( :include_root, false )
21
+ include_root ? Hash[symbol, @_options].to_json : @_options.to_json
22
+ end
23
+ alias :to_s :to_json
24
+
25
+ def self.build( pairs, response )
26
+ elements = pairs.map { |pair|
27
+ symbol = "#{pair[:from]}_#{pair[:to]}"
28
+ symbol_element = response.fetch( symbol )
29
+ if symbol_element.is_a?( Hash )
30
+ new( symbol, symbol_element )
31
+ else
32
+ symbol_element.map { |options| new( symbol, options ) }
33
+ end
34
+ }
35
+ elements.length < 2 ? elements.first : elements
36
+ end
37
+
38
+ private
39
+
40
+ def self.end_point
41
+ raise MethodNotImplemented
42
+ end
43
+
44
+ def required_fields
45
+ raise MethodNotImplemented
46
+ end
47
+
48
+ def date_fields; []; end
49
+ end
50
+ end
@@ -0,0 +1,30 @@
1
+ module Rbtce
2
+ module Client
3
+ class << self
4
+ BASE_URI = 'https://btc-e.com/api/3'
5
+
6
+ def get( end_point, options={} )
7
+ uri = URI.parse( [BASE_URI, end_point, parse_params( options )].join )
8
+ http = Net::HTTP.new( uri.host, uri.port )
9
+ http.use_ssl = true
10
+ http.verify_mode = OpenSSL::SSL::VERIFY_NONE
11
+ request = Net::HTTP::Get.new( uri.request_uri )
12
+ response = http.request( request )
13
+ if response.code == "200"
14
+ JSON.parse( response.body )
15
+ else
16
+ raise "#{response.message} (#{response.code})"
17
+ end
18
+ end
19
+
20
+ private
21
+
22
+ def parse_params( params )
23
+ return '' if params.empty?
24
+ '?' + params.map { |k,v|
25
+ "#{CGI.escape( k.to_s )}=#{CGI.escape( v.to_s )}"
26
+ }.join( '&' )
27
+ end
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,15 @@
1
+ module Rbtce
2
+ module Currency
3
+ BTC = :btc
4
+ EUR = :eur
5
+ FTC = :ftc
6
+ LTC = :ltc
7
+ NMC = :nmc
8
+ NVC = :nvc
9
+ PPC = :ppc
10
+ RUR = :rur
11
+ TRC = :trc
12
+ USD = :usd
13
+ XPM = :xpm
14
+ end
15
+ end
@@ -0,0 +1,13 @@
1
+ module Rbtce
2
+ class Depth < BaseModel
3
+ private
4
+
5
+ def self.end_point
6
+ '/depth'
7
+ end
8
+
9
+ def required_fields
10
+ %w{ asks bids }
11
+ end
12
+ end
13
+ end
data/lib/rbtce/info.rb ADDED
@@ -0,0 +1,23 @@
1
+ module Rbtce
2
+ class Info < BaseModel
3
+ def self.fetch( options={} )
4
+ build( nil, Client.get( end_point, options ) )
5
+ end
6
+
7
+ def self.build( _, response )
8
+ response['pairs'].map { |symbol, options|
9
+ new( symbol, options )
10
+ }
11
+ end
12
+
13
+ private
14
+
15
+ def self.end_point
16
+ '/info'
17
+ end
18
+
19
+ def required_fields
20
+ %w{ decimal_places min_price max_price min_amount hidden fee }
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,69 @@
1
+ module Rbtce
2
+ class PairBuilder
3
+ VALID_PAIRS = [
4
+ { from: Currency::BTC, to: Currency::USD },
5
+ { from: Currency::BTC, to: Currency::EUR },
6
+ { from: Currency::BTC, to: Currency::RUR },
7
+ { from: Currency::EUR, to: Currency::USD },
8
+ { from: Currency::FTC, to: Currency::BTC },
9
+ { from: Currency::LTC, to: Currency::BTC },
10
+ { from: Currency::LTC, to: Currency::EUR },
11
+ { from: Currency::LTC, to: Currency::RUR },
12
+ { from: Currency::LTC, to: Currency::USD },
13
+ { from: Currency::NMC, to: Currency::BTC },
14
+ { from: Currency::NMC, to: Currency::USD },
15
+ { from: Currency::NVC, to: Currency::BTC },
16
+ { from: Currency::NVC, to: Currency::USD },
17
+ { from: Currency::PPC, to: Currency::BTC },
18
+ { from: Currency::PPC, to: Currency::USD },
19
+ { from: Currency::TRC, to: Currency::BTC },
20
+ { from: Currency::USD, to: Currency::RUR },
21
+ { from: Currency::XPM, to: Currency::BTC },
22
+ ]
23
+
24
+ def initialize( options )
25
+ @end_point = options.fetch( :end_point )
26
+ @target_class = options.fetch( :class )
27
+ @pairs = []
28
+ reset_current_pair
29
+ end
30
+
31
+ def from( currency )
32
+ @current_pair[:from] = currency
33
+ self
34
+ end
35
+
36
+ def to( currency )
37
+ @current_pair[:to] = currency
38
+ self
39
+ end
40
+
41
+ def and( options={} )
42
+ return self if @pairs.include?( @current_pair )
43
+ validate_pair!( @current_pair ) if options.fetch( :validate, true )
44
+ @pairs << @current_pair
45
+ reset_current_pair
46
+ self
47
+ end
48
+ alias :flash_pair :and
49
+
50
+ def fetch( options={} )
51
+ flash_pair
52
+ @target_class.build( @pairs, Client.get( "#{@end_point}/#{pairs_path}", options ) )
53
+ end
54
+
55
+ private
56
+
57
+ def reset_current_pair
58
+ @current_pair = { from: nil, to: nil }
59
+ end
60
+
61
+ def validate_pair!( pair )
62
+ raise "Invalid pair: '#{pair.inspect}'" unless VALID_PAIRS.include?( pair )
63
+ end
64
+
65
+ def pairs_path
66
+ @pairs.map { |pair| "#{pair[:from]}_#{pair[:to]}" }.join( '-' )
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,17 @@
1
+ module Rbtce
2
+ class Ticker < BaseModel
3
+ private
4
+
5
+ def self.end_point
6
+ '/ticker'
7
+ end
8
+
9
+ def required_fields
10
+ %w{ high low avg vol vol_cur last buy sell updated }
11
+ end
12
+
13
+ def date_fields
14
+ %w{ updated }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,17 @@
1
+ module Rbtce
2
+ class Trades < BaseModel
3
+ private
4
+
5
+ def self.end_point
6
+ '/trades'
7
+ end
8
+
9
+ def required_fields
10
+ %w{ timestamp price amount tid type }
11
+ end
12
+
13
+ def date_fields
14
+ %w{ timestamp }
15
+ end
16
+ end
17
+ end
@@ -0,0 +1,3 @@
1
+ module Rbtce
2
+ VERSION = "0.1"
3
+ end
data/rbtce.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'rbtce/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "rbtce"
8
+ spec.version = Rbtce::VERSION
9
+ spec.authors = ["Omer Jakobinsky"]
10
+ spec.email = ["omer@jakobinsky.com"]
11
+ spec.description = %q{Ruby wrapper for BTC-e API (v3)}
12
+ spec.summary = %q{Ruby wrapper for BTC-e API (v3)}
13
+ spec.homepage = "https://github.com/omer/rbtce"
14
+ spec.license = "MIT"
15
+
16
+ spec.files = `git ls-files`.split($/)
17
+ spec.executables = spec.files.grep(%r{^bin/}) { |f| File.basename(f) }
18
+ spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
19
+ spec.require_paths = ["lib"]
20
+
21
+ spec.add_development_dependency "bundler", "~> 1.3"
22
+ spec.add_development_dependency "rake"
23
+ end
metadata ADDED
@@ -0,0 +1,95 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: rbtce
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Omer Jakobinsky
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2014-02-16 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: bundler
16
+ requirement: !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '1.3'
22
+ type: :development
23
+ prerelease: false
24
+ version_requirements: !ruby/object:Gem::Requirement
25
+ none: false
26
+ requirements:
27
+ - - ~>
28
+ - !ruby/object:Gem::Version
29
+ version: '1.3'
30
+ - !ruby/object:Gem::Dependency
31
+ name: rake
32
+ requirement: !ruby/object:Gem::Requirement
33
+ none: false
34
+ requirements:
35
+ - - ! '>='
36
+ - !ruby/object:Gem::Version
37
+ version: '0'
38
+ type: :development
39
+ prerelease: false
40
+ version_requirements: !ruby/object:Gem::Requirement
41
+ none: false
42
+ requirements:
43
+ - - ! '>='
44
+ - !ruby/object:Gem::Version
45
+ version: '0'
46
+ description: Ruby wrapper for BTC-e API (v3)
47
+ email:
48
+ - omer@jakobinsky.com
49
+ executables: []
50
+ extensions: []
51
+ extra_rdoc_files: []
52
+ files:
53
+ - .gitignore
54
+ - Gemfile
55
+ - LICENSE.txt
56
+ - README.md
57
+ - Rakefile
58
+ - lib/rbtce.rb
59
+ - lib/rbtce/base_model.rb
60
+ - lib/rbtce/client.rb
61
+ - lib/rbtce/currency.rb
62
+ - lib/rbtce/depth.rb
63
+ - lib/rbtce/info.rb
64
+ - lib/rbtce/pair_builder.rb
65
+ - lib/rbtce/ticker.rb
66
+ - lib/rbtce/trades.rb
67
+ - lib/rbtce/version.rb
68
+ - rbtce.gemspec
69
+ homepage: https://github.com/omer/rbtce
70
+ licenses:
71
+ - MIT
72
+ post_install_message:
73
+ rdoc_options: []
74
+ require_paths:
75
+ - lib
76
+ required_ruby_version: !ruby/object:Gem::Requirement
77
+ none: false
78
+ requirements:
79
+ - - ! '>='
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ required_rubygems_version: !ruby/object:Gem::Requirement
83
+ none: false
84
+ requirements:
85
+ - - ! '>='
86
+ - !ruby/object:Gem::Version
87
+ version: '0'
88
+ requirements: []
89
+ rubyforge_project:
90
+ rubygems_version: 1.8.23
91
+ signing_key:
92
+ specification_version: 3
93
+ summary: Ruby wrapper for BTC-e API (v3)
94
+ test_files: []
95
+ has_rdoc: