├── lib ├── bitcoinaverage │ ├── version.rb │ ├── market_ticker.rb │ ├── global_ticker.rb │ ├── available_currencies.csv │ ├── ticker.rb │ └── requester.rb └── bitcoinaverage.rb ├── .travis.yml ├── .gitignore ├── Rakefile ├── spec ├── fixtures │ ├── market_EUR.json │ ├── market_USD.json │ ├── global_EUR.json │ └── global_USD.json ├── spec_helper.rb └── bitcoinaverage_spec.rb ├── Gemfile ├── bitcoinaverage.gemspec ├── LICENSE └── README.md /lib/bitcoinaverage/version.rb: -------------------------------------------------------------------------------- 1 | module BitcoinAverage 2 | VERSION = "0.0.2" 3 | end 4 | -------------------------------------------------------------------------------- /.travis.yml: -------------------------------------------------------------------------------- 1 | bundler_args: --without development 2 | language: ruby 3 | rvm: 4 | - 1.9.3-p374 5 | - 2.0.0-p353 6 | - 2.1.0 7 | -------------------------------------------------------------------------------- /.gitignore: -------------------------------------------------------------------------------- 1 | Gemfile.lock 2 | coverage 3 | coverage/* 4 | /coverage/ 5 | coverage.data 6 | log/* 7 | *.gem 8 | mysession 9 | mysession.vim 10 | -------------------------------------------------------------------------------- /Rakefile: -------------------------------------------------------------------------------- 1 | require "bundler/gem_tasks" 2 | require "rspec/core/rake_task" 3 | 4 | RSpec::Core::RakeTask.new(:spec) 5 | 6 | task :test => :spec 7 | task :default => :spec 8 | -------------------------------------------------------------------------------- /spec/fixtures/market_EUR.json: -------------------------------------------------------------------------------- 1 | { 2 | "24h_avg": 419.29, 3 | "ask": 420.16, 4 | "bid": 417.84, 5 | "last": 418.17, 6 | "timestamp": "Fri, 21 Feb 2014 16:24:27 -0000", 7 | "total_vol": 4335.29 8 | } -------------------------------------------------------------------------------- /spec/fixtures/market_USD.json: -------------------------------------------------------------------------------- 1 | { 2 | "24h_avg": 565.89, 3 | "ask": 562.54, 4 | "bid": 559.7, 5 | "last": 560.54, 6 | "timestamp": "Fri, 21 Feb 2014 16:24:27 -0000", 7 | "total_vol": 100667.13 8 | } -------------------------------------------------------------------------------- /spec/fixtures/global_EUR.json: -------------------------------------------------------------------------------- 1 | { 2 | "24h_avg": 414.43, 3 | "ask": 411.77, 4 | "bid": 410.41, 5 | "last": 410.77, 6 | "timestamp": "Fri, 21 Feb 2014 16:23:25 -0000", 7 | "volume_btc": 4337.36, 8 | "volume_percent": 3.43 9 | } -------------------------------------------------------------------------------- /spec/fixtures/global_USD.json: -------------------------------------------------------------------------------- 1 | { 2 | "24h_avg": 568.44, 3 | "ask": 566.15, 4 | "bid": 563.83, 5 | "last": 564.61, 6 | "timestamp": "Fri, 21 Feb 2014 16:22:17 -0000", 7 | "volume_btc": 100496.08, 8 | "volume_percent": 79.49 9 | } -------------------------------------------------------------------------------- /lib/bitcoinaverage/market_ticker.rb: -------------------------------------------------------------------------------- 1 | require 'bitcoinaverage/ticker' 2 | require 'singleton' 3 | 4 | module BitcoinAverage 5 | class MarketTicker < BitcoinAverage::Ticker 6 | include Singleton 7 | attr_accessor :total_vol 8 | def avg_type 9 | 'market' 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /lib/bitcoinaverage/global_ticker.rb: -------------------------------------------------------------------------------- 1 | require 'bitcoinaverage/ticker' 2 | require 'singleton' 3 | 4 | module BitcoinAverage 5 | class GlobalTicker < BitcoinAverage::Ticker 6 | include Singleton 7 | attr_accessor :volume_btc, :volume_percent 8 | def avg_type 9 | 'global' 10 | end 11 | end 12 | end 13 | -------------------------------------------------------------------------------- /Gemfile: -------------------------------------------------------------------------------- 1 | # A sample Gemfile 2 | source "https://rubygems.org" 3 | 4 | gem 'rake' 5 | gem 'httparty' 6 | gem 'json' 7 | group :development do 8 | gem 'pry' 9 | gem 'pry-nav' 10 | end 11 | 12 | group :test do 13 | gem 'rspec', '>= 2.14' 14 | gem 'coveralls', require: false 15 | gem 'webmock' 16 | gem 'simplecov' 17 | end 18 | 19 | gemspec 20 | -------------------------------------------------------------------------------- /lib/bitcoinaverage.rb: -------------------------------------------------------------------------------- 1 | require 'bitcoinaverage/market_ticker' 2 | require 'bitcoinaverage/global_ticker' 3 | 4 | module BitcoinAverage 5 | def self.global(currency="USD") 6 | GlobalTicker.instance.request_info currency 7 | end 8 | def self.market(currency="USD") 9 | MarketTicker.instance.request_info currency 10 | end 11 | end 12 | -------------------------------------------------------------------------------- /spec/spec_helper.rb: -------------------------------------------------------------------------------- 1 | #require 'pry' 2 | require 'simplecov' 3 | require 'coveralls' 4 | 5 | SimpleCov.start 6 | SimpleCov.formatter = SimpleCov::Formatter::MultiFormatter[ 7 | SimpleCov::Formatter::HTMLFormatter, 8 | Coveralls::SimpleCov::Formatter 9 | ] 10 | #Coveralls.wear! 11 | 12 | 13 | require 'rubygems' 14 | require 'rspec' 15 | require 'webmock/rspec' 16 | WebMock.disable_net_connect!(allow:'coverwalls.io',allow_localhost:true) 17 | 18 | def fixture_path 19 | File.expand_path('../fixtures', __FILE__) 20 | end 21 | def fixture(file) 22 | File.new(fixture_path + '/' + file) 23 | end 24 | -------------------------------------------------------------------------------- /lib/bitcoinaverage/available_currencies.csv: -------------------------------------------------------------------------------- 1 | AED,AFN,ALL,AMD,ANG,AOA,ARS,AUD,AWG,AZN,BAM,BBD,BDT,BGN,BHD,BIF,BMD,BND,BOB,BRL,BSD,BTC,BTN,BWP,BYR,BZD,CAD,CDF,CHF,CLF,CLP,CNY,COP,CRC,CUP,CVE,CZK,DJF,DKK,DOP,DZD,EEK,EGP,ERN,ETB,EUR,FJD,FKP,GBP,GEL,GHS,GIP,GMD,GNF,GTQ,GYD,HKD,HNL,HRK,HTG,HUF,IDR,ILS,INR,IQD,IRR,ISK,JEP,JMD,JOD,JPY,KES,KGS,KHR,KMF,KPW,KRW,KWD,KYD,KZT,LAK,LBP,LKR,LRD,LSL,LTL,LVL,LYD,MAD,MDL,MGA,MKD,MMK,MNT,MOP,MRO,MTL,MUR,MVR,MWK,MXN,MYR,MZN,NAD,NGN,NIO,NOK,NPR,NZD,OMR,PAB,PEN,PGK,PHP,PKR,PLN,PYG,QAR,RON,RSD,RUB,RWF,SAR,SBD,SCR,SDG,SEK,SGD,SHP,SLL,SOS,SRD,STD,SVC,SYP,SZL,THB,TJS,TMT,TND,TOP,TRY,TTD,TWD,TZS,UAH,UGX,USD,UYU,UZS,VEF,VND,VUV,WST,XAF,XAG,XAU,XCD,XDR,XOF,XPF,YER,ZAR,ZMK,ZMW,ZWL 2 | -------------------------------------------------------------------------------- /lib/bitcoinaverage/ticker.rb: -------------------------------------------------------------------------------- 1 | require 'bitcoinaverage/requester' 2 | require 'json' 3 | 4 | module BitcoinAverage 5 | class Ticker 6 | include BitcoinAverage::Requester 7 | attr_accessor :avg_24h, :ask, :bid, :last, :timestamp 8 | 9 | def request_info currency='USD' 10 | response= sendrequest self.avg_type, currency 11 | if response.success? 12 | #Small patch since an attribute can't be named "24h_avg" 13 | response=JSON.load(response.to_s) unless response.is_a?(Hash) 14 | self.avg_24h = response["24h_avg"] 15 | response.delete("24h_avg") 16 | #binding.pry 17 | response.each do |key,value| 18 | send("#{key}=",value) 19 | end 20 | self 21 | else 22 | raise "Error receiving response"#<- to be extended 23 | end 24 | end 25 | end 26 | end 27 | -------------------------------------------------------------------------------- /bitcoinaverage.gemspec: -------------------------------------------------------------------------------- 1 | $:.push File.expand_path("../lib", __FILE__) 2 | require 'bitcoinaverage/version' 3 | 4 | Gem::Specification.new do |s| 5 | s.name = 'bitcoinaverage' 6 | s.version = BitcoinAverage::VERSION 7 | s.summary = "Ruby Wrapper for the Bitcoinaverage API" 8 | s.description = "Ruby wrapper for the Bitcoinaverage API. Bitcoinaverage is an independent 'globally averaged bitcoin price' provider" 9 | s.author = ["Toni Urcola"] 10 | s.email = 'unmail.toni@gmail.com' 11 | s.files = `git ls-files`.split("\n") 12 | s.test_files = `git ls-files -- {test,spec}/*`.split("\n") 13 | s.require_paths = ["lib"] 14 | s.required_ruby_version = '>= 1.9.3' 15 | s.add_dependency 'httparty' 16 | s.add_dependency 'json' 17 | s.add_development_dependency 'rspec' 18 | s.add_development_dependency 'bundler' 19 | s.homepage = 'http://rubygems.org/gems/bitcoinaverage' 20 | s.license = 'MIT' 21 | end 22 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | Copyright (c) 2014 Toni Urcola 2 | 3 | Permission is hereby granted, free of charge, to any person obtaining 4 | a copy of this software and associated documentation files (the 5 | "Software"), to deal in the Software without restriction, including 6 | without limitation the rights to use, copy, modify, merge, publish, 7 | distribute, sublicense, and/or sell copies of the Software, and to 8 | permit persons to whom the Software is furnished to do so, subject to 9 | the following conditions: 10 | 11 | The above copyright notice and this permission notice shall be 12 | included in all copies or substantial portions of the Software. 13 | 14 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, 15 | EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF 16 | MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND 17 | NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE 18 | LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION 19 | OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION 20 | WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 21 | -------------------------------------------------------------------------------- /lib/bitcoinaverage/requester.rb: -------------------------------------------------------------------------------- 1 | require 'httparty' 2 | 3 | module BitcoinAverage 4 | module Requester 5 | def sendrequest(average,currency='USD') 6 | raise "#{currency} is not a known currency" unless known_currency?(currency) 7 | base_url='https://api.bitcoinaverage.com/ticker/' 8 | avg_url= average == 'global'? average+'/' : '' 9 | ccy_url=currency 10 | final_url= base_url+avg_url+ccy_url 11 | 12 | response=HTTParty.get final_url 13 | end 14 | 15 | def known_currency?(currency) 16 | av_currencies=File.open('lib/bitcoinaverage/available_currencies.csv','r') 17 | .read 18 | .parse_csv 19 | true unless !av_currencies.include? currency 20 | end 21 | 22 | #Method to obtain all the available currencies 23 | # 24 | #Note: it's not run repeatedly, rather the result 25 | #was written to a file. 26 | #Note 2: "require 'csv' " is necessary for this method 27 | # 28 | # 29 | #def available_currencies 30 | # all_ccy=HTTParty.get 'https://api.bitcoinaverage.com/ticker/global/all' 31 | # all_ccy.map!{|k,v| k} 32 | # file= File.open 'currencies_file.csv','w' 33 | # file.write all_ccy[0..-2].to_csv 34 | # file.close 35 | #end 36 | end 37 | end 38 | -------------------------------------------------------------------------------- /spec/bitcoinaverage_spec.rb: -------------------------------------------------------------------------------- 1 | require 'spec_helper' 2 | require 'bitcoinaverage' 3 | 4 | SimpleCov.start 5 | describe BitcoinAverage do 6 | describe '.global ' do 7 | before do 8 | stub_request(:get, "https://api.bitcoinaverage.com/ticker/global/USD"). 9 | to_return(status:200, body: fixture('global_USD.json'),headers:{}) 10 | @response=BitcoinAverage.global 11 | end 12 | it 'returns a GlobalTicker object properly filled' do 13 | #binding.pry 14 | #expect(BitcoinAverage.global).to be_a BitcoinAverage::GlobalTicker 15 | expect(@response).to be_a BitcoinAverage::GlobalTicker 16 | @response.avg_24h.should == 568.44 17 | @response.ask.should == 566.15 18 | @response.bid.should == 563.83 19 | @response.last.should == 564.61 20 | @response.timestamp.should == 'Fri, 21 Feb 2014 16:22:17 -0000' 21 | @response.volume_btc.should == 100496.08 22 | @response.volume_percent.should == 79.49 23 | end 24 | end 25 | describe '.market ' do 26 | before do 27 | stub_request(:get, "https://api.bitcoinaverage.com/ticker/USD"). 28 | to_return(status:200, body: fixture('market_USD.json'),headers:{}) 29 | @response=BitcoinAverage.market 30 | end 31 | it 'returns a GlobalTicker object properly filled' do 32 | #binding.pry 33 | #expect(BitcoinAverage.global).to be_a BitcoinAverage::GlobalTicker 34 | expect(@response).to be_a BitcoinAverage::MarketTicker 35 | @response.avg_24h.should == 565.89 36 | @response.ask.should == 562.54 37 | @response.bid.should == 559.7 38 | @response.last.should == 560.54 39 | @response.timestamp.should == 'Fri, 21 Feb 2014 16:24:27 -0000' 40 | @response.total_vol.should == 100667.13 41 | end 42 | end 43 | describe 'unknown currency' do 44 | before do 45 | stub_request(:get, "https://api.bitcoinaverage.com/ticker/EEE"). 46 | to_return(status:404, body:"", headers:{}) 47 | end 48 | it 'raises an error' do 49 | expect{BitcoinAverage.market('EEE')}.to raise_error("EEE is not a known currency") 50 | end 51 | end 52 | end 53 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Ruby wrapper for the BitcoinAverage API 2 | 3 | [![Gem Version](https://badge.fury.io/rb/bitcoinaverage.png)](http://badge.fury.io/rb/bitcoinaverage) 4 | [![Coverage Status](https://coveralls.io/repos/git-toni/bitcoinaverage/badge.png)](https://coveralls.io/r/git-toni/bitcoinaverage) 5 | [![Build Status](https://travis-ci.org/git-toni/bitcoinaverage.png?branch=master)](https://travis-ci.org/git-toni/bitcoinaverage) 6 | 7 | 8 | Bitcoinaverage is an independent 'globally averaged bitcoin price' provider. They provide on one hand a **Global** price, 9 | which is based on the volume and price of BTC in all currencies. And on the other hand they provide a **Market** price, which only takes into account 10 | the volume of BTC directly exchangeable using the chosen specific currency. 11 | For more information on the methods BitcoinAverage uses to calculate the price, please refer to their website, in 12 | particular [this page](https://bitcoinaverage.com/explain.htm). 13 | 14 | ## Important first note 15 | The development of this gem has been *deeply* inspired by Erik Michaels-Ober's great [mtgox](https://github.com/sferik/mtgox) gem ( specially this README ;) ). 16 | 17 | ## Installation 18 | gem install bitcoinaverage 19 | 20 | 21 | ## Documentation 22 | TODO 23 | 24 | ## Usage 25 | ```ruby 26 | require 'rubygems' 27 | require 'bitcoinaverage' 28 | 29 | # Obtain the BitcoinAverage's global price 30 | BitcoinAverage.global 31 | 32 | # Obtain the BitcoinAverage's market price 33 | BitcoinAverage.market 34 | 35 | # Obtain the BitcoinAverage's global price in EUR 36 | BitcoinAverage.global 'EUR' 37 | 38 | # Obtain the BitcoinAverage's market price in GBP 39 | BitcoinAverage.market 'GBP' 40 | 41 | # Obtain the specific fields of BitcoinAverage's global price 42 | BitcoinAverage.global.last 43 | BitcoinAverage.global.bid 44 | BitcoinAverage.global.ask 45 | BitcoinAverage.global.volume_btc 46 | BitcoinAverage.global.volume_percent 47 | ``` 48 | 49 | ## Tested Ruby Versions 50 | This library aims to support and is [tested against](https://travis-ci.org/git-toni/bitcoinaverage) the following Ruby 51 | implementations: 52 | 53 | * ruby-1.9.3-p374 54 | * ruby-2.0.0-p353 55 | * ruby-2.1.0 56 | 57 | ## Contributing 58 | In the spirit of [free software](http://www.fsf.org/licensing/essays/free-sw.html), **everyone** is encouraged to help improve this project. 59 | 60 | Here are some ways *you* can contribute: 61 | 62 | * by using alpha, beta, and prerelease versions 63 | * by reporting bugs 64 | * by suggesting new features 65 | * by writing or editing documentation 66 | * by writing specifications 67 | * by writing code (**no patch is too small**: fix typos, add comments, clean up inconsistent whitespace) 68 | * by refactoring code 69 | * by closing [issues](http://github.com/Instagram/instagram-ruby-gem/issues) 70 | * by reviewing patches 71 | 72 | ## Copyright 73 | Copyright (c) 2014 Toni Urcola. See [LICENSE](https://github.com/git-toni/bitcoinaverage/LICENSE) for details. 74 | --------------------------------------------------------------------------------