Skip to content

Commit

Permalink
refactor rest connection
Browse files Browse the repository at this point in the history
  • Loading branch information
erubboli committed Feb 9, 2016
1 parent 049830f commit bcf62be
Show file tree
Hide file tree
Showing 35 changed files with 339 additions and 166 deletions.
9 changes: 8 additions & 1 deletion Gemfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,11 @@
source 'https://rubygems.org'

# Specify your gem's dependencies in bitfinexrb.gemspec
gemspec

group :test do
gem 'rspec'
gem 'simplecov'
gem 'webmock', '~> 1.22.6'
gem 'pry'
end

10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,28 +1,28 @@
# Bitfinexrb
# Bitfinex API client Library


TODO: Write a gem description

## Installation

Add this line to your application's Gemfile:

gem 'bitfinexrb'
gem 'bitfinex'

And then execute:

$ bundle

Or install it yourself as:

$ gem install bitfinexrb
$ gem install bitfinex

## Usage

TODO: Write usage instructions here

## Contributing

1. Fork it ( https://github.com/[my-github-username]/bitfinexrb/fork )
1. Fork it ( https://github.com/[my-github-username]/bitfinex/fork )
2. Create your feature branch (`git checkout -b my-new-feature`)
3. Commit your changes (`git commit -am 'Add some feature'`)
4. Push to the branch (`git push origin my-new-feature`)
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,6 @@ require "bundler/gem_tasks"

desc "Preloaded Ruby Shell"
task :console do
sh "irb -rubygems -I lib -r bitfinexrb.rb"
sh "irb -rubygems -I lib -r bitfinex.rb"
end

10 changes: 7 additions & 3 deletions bin/bfxws_example
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
#!/usr/bin/env ruby -Ilib

require 'bitfinexrb'
require 'bitfinex'

@ws = Bitfinexrb::Websocket::EMClient.new
@ws = Bitfinex::Websocket::EMClient.new

@ws.on(:open) do |msg|
@ws.send({Event: 'subscribe', Pair: 'BTCUSD', Channel: 'book', Prec: 'P0', Len: 'full'})
@ws.send({event: 'subscribe',
pair: 'BTCUSD',
channel: 'book',
prec: 'P0',
len: '25'})
end

@ws.on(:message) do |msg|
Expand Down
13 changes: 4 additions & 9 deletions bitfinexrb.gemspec → bitfinex.gemspec
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
# coding: utf-8
lib = File.expand_path('../lib', __FILE__)
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
require 'bitfinexrb/version'
require 'bitfinex/version'

Gem::Specification.new do |spec|
spec.name = 'bitfinex-api-rb'
spec.version = Bitfinexrb::VERSION
spec.version = Bitfinex::VERSION
spec.authors = ['Bitfinex']
spec.email = ['developers@bitfinex.com']
spec.summary = %q{Bitfinex API Wrapper}
Expand All @@ -18,11 +18,6 @@ Gem::Specification.new do |spec|
spec.test_files = spec.files.grep(%r{^(test|spec|features)/})
spec.require_paths = ['lib']

spec.add_dependency 'httparty'
spec.add_dependency 'eventmachine'
spec.add_dependency 'faye-websocket'
spec.add_dependency 'json'

spec.add_development_dependency "bundler", "~> 1.8"
spec.add_development_dependency 'rake'
spec.add_dependency 'faraday'
spec.add_development_dependency "bundler"
end
8 changes: 8 additions & 0 deletions lib/bitfinex.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
require 'faraday'
require 'bitfinex/configurable'
require 'bitfinex/base_resource'
require 'bitfinex/connection_errors'
require 'bitfinex/connection'
require 'bitfinex/ticker'
require 'bitfinex/client'

Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
module Bitfinex

module Bitfinexrb

class AccountInfo < Authenticated
class AccountInfo
def all
uri = "/#{@api_version}/account_infos"
self.class.post(uri, headers: headers_for(uri)).parsed_response
Expand Down
3 changes: 1 addition & 2 deletions lib/bitfinexrb/balances.rb → lib/bitfinex/balances.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

module Bitfinexrb
module Bitfinex

class Balances < Authenticated
def all
Expand Down
27 changes: 27 additions & 0 deletions lib/bitfinex/base_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
module Bitfinex
class BaseResource
class << self
def set_properties *props
@properties = props
props.each do |val|
class_eval %{
def #{val}=(v);@#{val}=v;end
def #{val};@#{val};end
}
end
end

def properties
@properties
end
end

def initialize(obj)
obj.each do |k,v|
if self.class.properties.include?(k.to_sym)
send((k+'=').to_sym,v)
end
end
end
end
end
7 changes: 7 additions & 0 deletions lib/bitfinex/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
module Bitfinex
class Client
include Bitfinex::RestConnection
include Bitfinex::TickerClient
include Bitfinex::Configurable
end
end
26 changes: 26 additions & 0 deletions lib/bitfinex/configurable.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Bitfinex
module Configurable
def self.included(base)
base.extend(ClassMethods)
end

def config
self.class.config
end

module ClassMethods
def configure
yield config
end

def config
@configuration ||= Configuration.new
end
end
end

class Configuration
attr_accessor :api_endpoint
end

end
22 changes: 22 additions & 0 deletions lib/bitfinex/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
module Bitfinex
# Network Layer for API Rest client
module RestConnection

# Make an HTTP GET request
def get(url, options={})
rest.get url, parse_params(options)
end

private
def rest
@conn ||= new_rest_connection
end

def new_rest_connection
Faraday.new(url: config.api_endpoint) do |builder|
builder.use Bitfinex::CheckResponse
end
end

end
end
24 changes: 24 additions & 0 deletions lib/bitfinex/connection_errors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Bitfinex

class Error
def self.check_response(response)
case response.status.to_i
when 400 then BitFinex::BadRequest
end
end
end

# Check Faraday Response and raise the appropriate exception
# in case of failure
class CheckResponse < Faraday::Response::Middleware
private
def on_complete(response)
if error = Bitfinex::Error.check_response(response)
raise error
end
end
end

class ClientError < Error; end
class BadRequest < ClientError; end
end
3 changes: 1 addition & 2 deletions lib/bitfinexrb/credits.rb → lib/bitfinex/credits.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

module Bitfinexrb
module Bitfinex

class Credits < Authenticated
def all
Expand Down
3 changes: 2 additions & 1 deletion lib/bitfinexrb/lendbook.rb → lib/bitfinex/lendbook.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Bitfinexrb
module Bitfinex

class Lendbook < Base

attr_accessor :bids, :rate, :amount, :period, :timestamp, :frr, :asks, :limit_bids, :limit_asks
Expand Down
3 changes: 2 additions & 1 deletion lib/bitfinexrb/lends.rb → lib/bitfinex/lends.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
module Bitfinexrb
module Bitfinex

class Lend
attr_accessor :rate, :amount, :timestamp

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@

module Bitfinexrb
module Bitfinex

class MarginInfo < Authenticated
def all
Expand Down
2 changes: 1 addition & 1 deletion lib/bitfinexrb/orderbook.rb → lib/bitfinex/orderbook.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Bitfinexrb
module Bitfinex
class Orderbook < Base

attr_accessor :bids, :asks
Expand Down
2 changes: 1 addition & 1 deletion lib/bitfinexrb/orders.rb → lib/bitfinex/orders.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Bitfinexrb
module Bitfinex
#
class Orders < Authenticated
def all
Expand Down
2 changes: 1 addition & 1 deletion lib/bitfinexrb/pairs.rb → lib/bitfinex/pairs.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Bitfinexrb
module Bitfinex
#
class Pairs < Base
def all
Expand Down
2 changes: 1 addition & 1 deletion lib/bitfinexrb/parser.rb → lib/bitfinex/parser.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
module Bitfinexrb
module Bitfinex
class JSONParser < HTTParty::Parser
SupportedFormats.merge!({ 'application/json' => :to_json})

Expand Down
3 changes: 2 additions & 1 deletion lib/bitfinexrb/positions.rb → lib/bitfinex/positions.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
module Bitfinexrb
module Bitfinex

class Positions < Authenticated

def all
uri = "/#{@api_version}/positions"
self.class.post(uri, headers: headers_for(uri)).parsed_response
end

end
end
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@

module Bitfinexrb
module Bitfinex

class TakenFunds < Authenticated
def all
Expand Down
15 changes: 15 additions & 0 deletions lib/bitfinex/ticker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
module Bitfinex
module TickerClient
def ticker(symbol='btcusd')
resp = rest.get("/pubticker/#{symbol}")
if resp.success?
Ticker.new(JSON.parse(resp.body))
end
end
end

class Ticker < BaseResource
set_properties :mid, :bid, :ask, :last_price, :low, :volume, :timestamp
end

end
19 changes: 19 additions & 0 deletions lib/bitfinex/trades.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
module Bitfinex

module TradeClient
TRADE_PARAMS = %w{timestamp limit_trades}

def trades symbol, params={}
resp = rest.get("/trades/#{symbol}", check_params(params, TRADE_PARAMS))

if resp.success?
Trade.new(JSON.parse(resp.body))
end
end
end

class Trade
values :tid, :timestamp, :price, :amount, :exchange, :type
end

end
2 changes: 1 addition & 1 deletion lib/bitfinexrb/version.rb → lib/bitfinex/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module Bitfinexrb
module Bitfinex
VERSION = "0.0.1"
end
Loading

0 comments on commit bcf62be

Please sign in to comment.