mercado_bitcoin 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: d58b5492bd7c156af06499208f0f05441a47f201
4
+ data.tar.gz: 3aca71d49547a54bec36ba50acfa63d7f7c70a33
5
+ SHA512:
6
+ metadata.gz: c2735e4917bb2cf5f9b465e732eaeb98adbca26d237a84c6e3a56374ad6f5969f5ef3137786bcdc8e09377940f6c8c8e33167ccfb6a6c0c334979f9f2816af77
7
+ data.tar.gz: fc019bb9f44991e34b5bf0838101640ae93ca70a005d905800a55a01e9172d9032facd8c71f40fc07a48e866a234b759d54e6e84220009e36b77e4eb2cb2f8b6
data/.gitignore ADDED
@@ -0,0 +1,38 @@
1
+ *.gem
2
+ *.rbc
3
+ /.config
4
+ /coverage/
5
+ /InstalledFiles
6
+ /pkg/
7
+ /spec/reports/
8
+ /spec/examples.txt
9
+ /test/tmp/
10
+ /test/version_tmp/
11
+ /tmp/
12
+
13
+ ## Specific to RubyMotion:
14
+ .dat*
15
+ .repl_history
16
+ build/
17
+
18
+ .byebug_history
19
+
20
+ ## Documentation cache and generated files:
21
+ /.yardoc/
22
+ /_yardoc/
23
+ /doc/
24
+ /rdoc/
25
+
26
+ ## Environment normalisation:
27
+ /.bundle/
28
+ /vendor/bundle
29
+ /lib/bundler/man/
30
+
31
+ # for a library or gem, you might want to ignore these files since the code is
32
+ # intended to run in multiple environments; otherwise, check them in:
33
+ Gemfile.lock
34
+ .ruby-version
35
+ .ruby-gemset
36
+
37
+ # unless supporting rvm < 1.11.0 or doing something fancy, ignore this:
38
+ .rvmrc
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format progress
2
+ --color
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.3
4
+ before_install: gem install bundler -v 1.10.6
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, ethnicity, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http://contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in mercado_bitcoin.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Marco Carvalho
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,33 @@
1
+ # MercadoBitcoin
2
+
3
+ Thin layer to MercadoBitcoin.com.br public price api layer.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'mercado_bitcoin'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install mercado_bitcoin
20
+
21
+ ## Usage
22
+
23
+ TODO: missing documentation.
24
+
25
+ ## Contributing
26
+
27
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marcocarvalho/mercado_bitcoin. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](contributor-covenant.org) code of conduct.
28
+
29
+
30
+ ## License
31
+
32
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
33
+
data/Rakefile ADDED
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "mercado_bitcoin"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,17 @@
1
+ class MercadoBitcoin::Api::Data::AskBid
2
+ include Virtus.model
3
+
4
+ attribute :price, Float
5
+ attribute :volume, Float
6
+
7
+ def to_hash
8
+ {
9
+ price: price,
10
+ volume: volume
11
+ }
12
+ end
13
+
14
+ def to_json
15
+ to_hash.to_json
16
+ end
17
+ end
@@ -0,0 +1,7 @@
1
+ module MercadoBitcoin::Api::Data::AskBidRefinement
2
+ refine Array do
3
+ def to_model_hash
4
+ Hash[*[:price, :volume].zip(self).flatten]
5
+ end
6
+ end
7
+ end
@@ -0,0 +1,31 @@
1
+ class MercadoBitcoin::Api::Data::OrderBook
2
+ using MercadoBitcoin::Api::Data::AskBidRefinement
3
+
4
+ attr_reader :asks, :bids
5
+
6
+ def initialize(opts = {})
7
+ self.asks = opts[:asks] || opts['asks']
8
+ self.bids = opts[:bids] || opts['bids']
9
+ end
10
+
11
+ def asks=(v)
12
+ v ||= []
13
+ @asks = v.map { |i| MercadoBitcoin::Api::Data::AskBid.new(i.to_model_hash) }
14
+ end
15
+
16
+ def bids=(v)
17
+ v ||= []
18
+ @bids = v.map { |i| MercadoBitcoin::Api::Data::AskBid.new(i.to_model_hash) }
19
+ end
20
+
21
+ def to_hash
22
+ {
23
+ asks: asks.map { |i| i.to_hash },
24
+ bids: bids.map { |i| i.to_hash }
25
+ }
26
+ end
27
+
28
+ def to_json
29
+ to_hash.to_json
30
+ end
31
+ end
@@ -0,0 +1,27 @@
1
+ class MercadoBitcoin::Api::Data::Ticker
2
+ include Virtus.model
3
+
4
+ attribute :high, Float
5
+ attribute :low, Float
6
+ attribute :vol, Float
7
+ attribute :last, Float
8
+ attribute :buy, Float
9
+ attribute :sell, Float
10
+ attribute :date, Timestamp
11
+
12
+ def to_hash
13
+ {
14
+ high: high,
15
+ low: low,
16
+ vol: vol,
17
+ last: last,
18
+ buy: buy,
19
+ sell: sell,
20
+ date: date
21
+ }
22
+ end
23
+
24
+ def to_json
25
+ to_hash.to_json
26
+ end
27
+ end
@@ -0,0 +1,23 @@
1
+ class MercadoBitcoin::Api::Data::Trade
2
+ include Virtus.model
3
+
4
+ attribute :price, Float
5
+ attribute :amount, Float
6
+ attribute :tid, Integer
7
+ attribute :type, String
8
+ attribute :date, Timestamp
9
+
10
+ def to_hash
11
+ {
12
+ price: price,
13
+ amount: amount,
14
+ tid: tid,
15
+ type: type,
16
+ date: date
17
+ }
18
+ end
19
+
20
+ def to_json
21
+ to_hash.to_json
22
+ end
23
+ end
@@ -0,0 +1,7 @@
1
+ module MercadoBitcoin::Api::Data
2
+ autoload :Ticker, 'mercado_bitcoin/api/data/ticker'
3
+ autoload :Trade, 'mercado_bitcoin/api/data/trade'
4
+ autoload :AskBid, 'mercado_bitcoin/api/data/ask_bid'
5
+ autoload :OrderBook, 'mercado_bitcoin/api/data/order_book'
6
+ autoload :AskBidRefinement, 'mercado_bitcoin/api/data/ask_bid_refinement'
7
+ end
@@ -0,0 +1,3 @@
1
+ module MercadoBitcoin::Api
2
+ require 'mercado_bitcoin/api/data'
3
+ end
@@ -0,0 +1,65 @@
1
+ module MercadoBitcoin
2
+ class BaseApiCall
3
+ attr_accessor :coin, :parser, :rest_client, :parser_error_class
4
+ def initialize(coin = :bitcoin, opts = {})
5
+ @coin = coin.to_s.downcase.to_sym
6
+ raise MercadoBitcoin::CoinTypeError.new("bitcoin or litecoin expected #{coin} received") unless valid_coin?
7
+ @parser = opts[:parser] || JSON
8
+ @rest_client = opts[:rest_client] || RestClient
9
+ @parser_error_class = opts[:parser_error_class] || JSON::ParserError
10
+ end
11
+
12
+ def valid_coin?
13
+ bitcoin? || litecoin?
14
+ end
15
+
16
+ def fetch
17
+ parsed = parse(get(url))
18
+ model.new(parsed)
19
+ end
20
+
21
+ def model
22
+ raise NotImplementedError.new
23
+ end
24
+
25
+ def action
26
+ ''
27
+ end
28
+
29
+ def base_url
30
+ @base_url ||= "https://www.mercadobitcoin.net/api".freeze
31
+ end
32
+
33
+ def bitcoin?
34
+ coin == :bitcoin
35
+ end
36
+
37
+ def litecoin?
38
+ coin == :litecoin
39
+ end
40
+
41
+ def params
42
+ {}
43
+ end
44
+
45
+ def url
46
+ return @url if @url
47
+ ac = action.is_a?(String) && action != '' ? File.join(base_url, action) : base_url
48
+ @url = URI.parse(ac)
49
+ @url.query = URI.encode_www_form(params) if !params.nil? && !params.empty?
50
+ @url = @url.to_s
51
+ end
52
+
53
+ attr_reader :response
54
+
55
+ def get(url)
56
+ @response = rest_client.get(url)
57
+ end
58
+
59
+ def parse(body)
60
+ parser.parse(body)
61
+ rescue parser_error_class
62
+ raise MercadoBitcoin::ParserError.new("#{url} responded an invalid json data")
63
+ end
64
+ end
65
+ end
@@ -0,0 +1,5 @@
1
+ module MercadoBitcoin
2
+ class Error < StandardError; end
3
+ class ParserError < Error; end
4
+ class CoinTypeError < Error; end
5
+ end
@@ -0,0 +1,9 @@
1
+ class MercadoBitcoin::OrderBook < MercadoBitcoin::BaseApiCall
2
+ def model
3
+ MercadoBitcoin::Api::Data::OrderBook
4
+ end
5
+
6
+ def action
7
+ @action ||= bitcoin? ? 'orderbook' : 'orderbook_litecoin'
8
+ end
9
+ end
@@ -0,0 +1,27 @@
1
+ module MercadoBitcoin
2
+ class Ticker < BaseApiCall
3
+
4
+ def initialize(coin, opts = {})
5
+ opts[:parser] = TickerParser
6
+ super(coin, opts)
7
+ end
8
+
9
+ def action
10
+ @action ||= bitcoin? ? 'ticker' : 'ticker_litecoin'
11
+ end
12
+
13
+ def model
14
+ MercadoBitcoin::Api::Data::Ticker
15
+ end
16
+
17
+ class TickerParser
18
+ def self.parse(body)
19
+ new.parse(body)
20
+ end
21
+
22
+ def parse(body)
23
+ JSON.parse(body)['ticker']
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,44 @@
1
+ module MercadoBitcoin
2
+ class Trade < BaseApiCall
3
+ attr_accessor :tid, :from, :to
4
+ def initialize(coin, opts = {})
5
+ @tid = opts[:tid] || opts[:since]
6
+ @from = opts[:from].to_i.to_s if opts[:from].is_a?(Time)
7
+ @to = opts[:to].to_i.to_s if opts[:to].is_a?(Time)
8
+ super(coin, opts)
9
+ end
10
+
11
+ def from_to
12
+ return @from_to if @from_to
13
+ if @from && @to
14
+ @from_to = "#{from}/#{to}"
15
+ else
16
+ @from_to = @from || @to
17
+ end
18
+ end
19
+
20
+ def action
21
+ return @action if @action
22
+ @action = bitcoin? ? 'trades' : 'trades_litecoin'
23
+ if(from_to)
24
+ @action = File.join(@action, from_to).freeze
25
+ end
26
+ @action
27
+ end
28
+
29
+ def params
30
+ return {} unless tid
31
+ { tid: tid }
32
+ end
33
+
34
+ def model
35
+ Array
36
+ end
37
+
38
+ class Array < ::Array
39
+ def initialize(collection)
40
+ collection.each { |i| push(MercadoBitcoin::Api::Data::Trade.new(i)) }
41
+ end
42
+ end
43
+ end
44
+ end
@@ -0,0 +1,3 @@
1
+ module MercadoBitcoin
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,14 @@
1
+ require 'mercado_bitcoin/version'
2
+ require 'virtus'
3
+ require 'virtus/timestamp'
4
+ require 'rest-client'
5
+ require 'json'
6
+
7
+ module MercadoBitcoin
8
+ require 'mercado_bitcoin/errors'
9
+ autoload :Api, 'mercado_bitcoin/api'
10
+ autoload :BaseApiCall, 'mercado_bitcoin/base_api_call'
11
+ autoload :Ticker, 'mercado_bitcoin/ticker'
12
+ autoload :Trade, 'mercado_bitcoin/trade'
13
+ autoload :OrderBook, 'mercado_bitcoin/order_book'
14
+ end
@@ -0,0 +1,14 @@
1
+ class Timestamp < Virtus::Attribute
2
+ def coerce(value)
3
+ case value
4
+ when Fixnum
5
+ Time.at(value)
6
+ when Time
7
+ value
8
+ when NilClass
9
+ value
10
+ else
11
+ raise ArgumentError.new("#{value.class} cannot be coerced to Time")
12
+ end
13
+ end
14
+ end
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'mercado_bitcoin/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "mercado_bitcoin"
8
+ spec.version = MercadoBitcoin::VERSION
9
+ spec.authors = ["Marco Carvalho"]
10
+ spec.email = ["marco@mcorp.io"]
11
+
12
+ spec.summary = %q{Thin layer over public price api of MercadoBitcoin.com.br}
13
+ spec.description = %q{Thin layer over public price api of MercadoBitcoin.com.br for more information about the API see https://www.mercadobitcoin.com.br/api }
14
+ spec.homepage = "https://github.com/marcocarvalho/mercadobitcoin"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency 'virtus', '~> 1.0'
21
+ spec.add_dependency 'rest-client', '~> 1.8'
22
+ spec.add_development_dependency "bundler", "~> 1.10"
23
+ spec.add_development_dependency "byebug", "~> 8.2"
24
+ spec.add_development_dependency "rake", "~> 10.0"
25
+ spec.add_development_dependency "rspec", "~> 3.4"
26
+ end
metadata ADDED
@@ -0,0 +1,155 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: mercado_bitcoin
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marco Carvalho
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-12-26 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: virtus
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rest-client
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.8'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.8'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '1.10'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '1.10'
55
+ - !ruby/object:Gem::Dependency
56
+ name: byebug
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '8.2'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '8.2'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rake
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '10.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '10.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: rspec
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: '3.4'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: '3.4'
97
+ description: 'Thin layer over public price api of MercadoBitcoin.com.br for more information
98
+ about the API see https://www.mercadobitcoin.com.br/api '
99
+ email:
100
+ - marco@mcorp.io
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".travis.yml"
108
+ - CODE_OF_CONDUCT.md
109
+ - Gemfile
110
+ - LICENSE.txt
111
+ - README.md
112
+ - Rakefile
113
+ - bin/console
114
+ - bin/setup
115
+ - lib/mercado_bitcoin.rb
116
+ - lib/mercado_bitcoin/api.rb
117
+ - lib/mercado_bitcoin/api/data.rb
118
+ - lib/mercado_bitcoin/api/data/ask_bid.rb
119
+ - lib/mercado_bitcoin/api/data/ask_bid_refinement.rb
120
+ - lib/mercado_bitcoin/api/data/order_book.rb
121
+ - lib/mercado_bitcoin/api/data/ticker.rb
122
+ - lib/mercado_bitcoin/api/data/trade.rb
123
+ - lib/mercado_bitcoin/base_api_call.rb
124
+ - lib/mercado_bitcoin/errors.rb
125
+ - lib/mercado_bitcoin/order_book.rb
126
+ - lib/mercado_bitcoin/ticker.rb
127
+ - lib/mercado_bitcoin/trade.rb
128
+ - lib/mercado_bitcoin/version.rb
129
+ - lib/virtus/timestamp.rb
130
+ - mercado_bitcoin.gemspec
131
+ homepage: https://github.com/marcocarvalho/mercadobitcoin
132
+ licenses:
133
+ - MIT
134
+ metadata: {}
135
+ post_install_message:
136
+ rdoc_options: []
137
+ require_paths:
138
+ - lib
139
+ required_ruby_version: !ruby/object:Gem::Requirement
140
+ requirements:
141
+ - - ">="
142
+ - !ruby/object:Gem::Version
143
+ version: '0'
144
+ required_rubygems_version: !ruby/object:Gem::Requirement
145
+ requirements:
146
+ - - ">="
147
+ - !ruby/object:Gem::Version
148
+ version: '0'
149
+ requirements: []
150
+ rubyforge_project:
151
+ rubygems_version: 2.4.8
152
+ signing_key:
153
+ specification_version: 4
154
+ summary: Thin layer over public price api of MercadoBitcoin.com.br
155
+ test_files: []