money-coinmarketcap-bank 0.1

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: 800bf8f6726bec5bfde37f479051dacb9a906ecf
4
+ data.tar.gz: 20669dcd6ce4b0fa89bf7517ecdbda6deb94c726
5
+ SHA512:
6
+ metadata.gz: e140bd798f49a67990f1e9b9b432537491acd2d5ee84be5d9c76a689b8e9a4bf934808b58b0047b1f53530b31fdffe00f6fe1b01ad85004217774df6ef79aeb4
7
+ data.tar.gz: 9972b43c73220889e4add82ac4f2e39189bc33f694cc3ca233c4edf941032a467211159d60464171451f4bc47c26e9e92fc4fbdc1522fb4ededad9a6ed2b5743
data/.gitignore ADDED
@@ -0,0 +1,2 @@
1
+ *.gem
2
+ .DS_Store
data/README.md ADDED
@@ -0,0 +1,23 @@
1
+ # money-coinmarketcap-bank
2
+ ## CoinMarketCap frontend for ruby-money
3
+
4
+ Attach this gem to ruby-money to use it as bank and get the exchange rate from [http://coinmarketcap.com/](coinmarketcap.com).
5
+
6
+ This gem is basically a clone of [money-uphold-bank](https://github.com/subvisual/money-uphold-bank).
7
+ Almost of all the credits go to its creators.
8
+
9
+ # Usage
10
+
11
+ ```rb
12
+ # Minimal requirements
13
+ require "money/bank/uphold"
14
+
15
+ bank = Money::Bank::Uphold.new
16
+
17
+ # (optional)
18
+ # Set the number of seconds after which the rates are automatically expired.
19
+ # By default, they expire every hour
20
+ bank.ttl_in_seconds = 3600
21
+
22
+ Money.default_bank = bank
23
+ ```
data/Rakefile ADDED
@@ -0,0 +1,12 @@
1
+ #! /usr/bin/env ruby
2
+ require 'rake'
3
+
4
+ task default: %i(build install)
5
+
6
+ task :build do
7
+ sh 'gem build *.gemspec'
8
+ end
9
+
10
+ task :install do
11
+ sh 'gem install *.gem'
12
+ end
@@ -0,0 +1,87 @@
1
+ require "money"
2
+ require "money/bank/variable_exchange"
3
+ require "open-uri"
4
+ require "json"
5
+
6
+ class Money
7
+ module Bank
8
+ class CoinMarketCap < Money::Bank::VariableExchange
9
+ BASE_CURRENCY = "USD".freeze
10
+ EXCHANGE_URL = "http://coinmarketcap.northpole.ro/api/v5/all.json".freeze
11
+
12
+ # Seconds after which the current rates are automatically expired
13
+ attr_accessor :ttl_in_seconds
14
+
15
+ # Rates expiration time
16
+ attr_reader :rates_expire_at
17
+
18
+ def initialize(*args, &block)
19
+ super
20
+
21
+ @ttl_in_seconds = 3600 # 1 hour
22
+ end
23
+
24
+ def update_rates
25
+ store.each_rate do |iso_from, iso_to, _rate|
26
+ add_rate(iso_from, iso_to, nil)
27
+ end
28
+
29
+ add_exchange_rates
30
+ end
31
+
32
+ alias original_get_rate get_rate
33
+
34
+ def get_rate(iso_from, iso_to, opts = {})
35
+ update_rates_if_expired
36
+
37
+ super || get_indirect_rate(iso_from, iso_to, opts)
38
+ end
39
+
40
+ private
41
+
42
+ def get_indirect_rate(iso_from, iso_to, opts = {})
43
+ return 1 if Money::Currency.wrap(iso_from).iso_code == Money::Currency.wrap(iso_to).iso_code
44
+
45
+ rate_to_base = original_get_rate(iso_from, BASE_CURRENCY, opts)
46
+ rate_from_base = original_get_rate(BASE_CURRENCY, iso_to, opts)
47
+
48
+ return unless rate_to_base && rate_from_base
49
+
50
+ rate = rate_to_base * rate_from_base
51
+
52
+ add_rate(iso_from, iso_to, rate)
53
+ add_rate(iso_to, iso_from, 1.0 / rate)
54
+
55
+ rate
56
+ end
57
+
58
+ def coins
59
+ return @coins if @coins
60
+
61
+ response = open(EXCHANGE_URL).read
62
+ @coins = JSON.parse(response)['markets']
63
+ end
64
+
65
+ def add_exchange_rates
66
+ coins.each do |coin|
67
+ iso_from = coin['symbol']
68
+ coin['price'].each do |iso_to, rate|
69
+ next unless Money::Currency.find(iso_from) && Money::Currency.find(iso_to)
70
+ add_rate(iso_from, iso_to, rate)
71
+ add_rate(iso_to, iso_from, 1.0 / rate)
72
+ end
73
+ end
74
+ end
75
+
76
+ def update_rates_if_expired
77
+ update_rates if rates_expired?
78
+ end
79
+
80
+ def rates_expired?
81
+ return true unless rates_expire_at
82
+
83
+ rates_expire_at <= Time.now
84
+ end
85
+ end
86
+ end
87
+ end
@@ -0,0 +1,17 @@
1
+ Gem::Specification.new { |s|
2
+ s.name = 'money-coinmarketcap-bank'
3
+ s.version = '0.1'
4
+ s.author = 'Giovanni Capuano'
5
+ s.email = 'webmaster@giovannicapuano.net'
6
+ s.homepage = 'https://github.com/RoxasShadow'
7
+ s.summary = 'CoinMarketCap frontend for ruby-money'
8
+ s.description = 'Attach this gem to ruby-money to use it as bank and get the exchange rate from coinmarketcap.com'
9
+ s.licenses = 'WTFPL'
10
+
11
+ s.files = `git ls-files`.split("\n")
12
+ s.require_paths = ['lib']
13
+
14
+ s.add_dependency 'money', '~> 6.7'
15
+
16
+ s.add_development_dependency 'rake', '~> 10.0'
17
+ }
metadata ADDED
@@ -0,0 +1,77 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money-coinmarketcap-bank
3
+ version: !ruby/object:Gem::Version
4
+ version: '0.1'
5
+ platform: ruby
6
+ authors:
7
+ - Giovanni Capuano
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2017-08-07 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: money
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '6.7'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '6.7'
27
+ - !ruby/object:Gem::Dependency
28
+ name: rake
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '10.0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '10.0'
41
+ description: Attach this gem to ruby-money to use it as bank and get the exchange
42
+ rate from coinmarketcap.com
43
+ email: webmaster@giovannicapuano.net
44
+ executables: []
45
+ extensions: []
46
+ extra_rdoc_files: []
47
+ files:
48
+ - ".gitignore"
49
+ - README.md
50
+ - Rakefile
51
+ - lib/money/bank/coin_market_cap.rb
52
+ - money-coinmarketcap-bank.gemspec
53
+ homepage: https://github.com/RoxasShadow
54
+ licenses:
55
+ - WTFPL
56
+ metadata: {}
57
+ post_install_message:
58
+ rdoc_options: []
59
+ require_paths:
60
+ - lib
61
+ required_ruby_version: !ruby/object:Gem::Requirement
62
+ requirements:
63
+ - - ">="
64
+ - !ruby/object:Gem::Version
65
+ version: '0'
66
+ required_rubygems_version: !ruby/object:Gem::Requirement
67
+ requirements:
68
+ - - ">="
69
+ - !ruby/object:Gem::Version
70
+ version: '0'
71
+ requirements: []
72
+ rubyforge_project:
73
+ rubygems_version: 2.6.12
74
+ signing_key:
75
+ specification_version: 4
76
+ summary: CoinMarketCap frontend for ruby-money
77
+ test_files: []