money-uphold-bank 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 06b97ddab0656fba08ddc1e6bc35b21b0fb77270
4
+ data.tar.gz: a95bb0c3911f74e1a001ba645b2a04abda39868a
5
+ SHA512:
6
+ metadata.gz: 588490db739884bde71916f384a3fdec0fb9026c0d6f03b606e2a5815ae8669fd567e05cb03e371a1130c756d814579c993702ba146532c4c0afe289b06d4038
7
+ data.tar.gz: 0d535317af0db2dbbe4cc7c8be93a735e3dd98e524cd5ba25be72227775f6f0a514a25c3d6f896d051cb5fa5800c092b110d5ecf2ff8c7b5cf36ab7f731f7358
@@ -0,0 +1,9 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --format documentation
2
+ --color
@@ -0,0 +1,64 @@
1
+ AllCops:
2
+ Include:
3
+ - "**/Rakefile"
4
+ - "**/config.ru"
5
+ Exclude:
6
+ - "db/**/*"
7
+ - "config/**/*"
8
+ - "script/**/*"
9
+ - "bin/**/*"
10
+ - "vendor/**/*"
11
+ - "node_modules/**/*"
12
+ - "spec/factories.rb"
13
+ - "slack_bot/**/*"
14
+ TargetRubyVersion: 2.3
15
+
16
+ Metrics/LineLength:
17
+ Enabled: false
18
+
19
+ Style/AlignParameters:
20
+ EnforcedStyle: with_fixed_indentation
21
+
22
+ Style/ClassAndModuleChildren:
23
+ Enabled: false
24
+
25
+ Style/Documentation:
26
+ Enabled: false
27
+
28
+ Style/DotPosition:
29
+ EnforcedStyle: trailing
30
+
31
+ Style/IfUnlessModifier:
32
+ Enabled: false
33
+
34
+ Style/PredicateName:
35
+ NamePrefixBlacklist:
36
+ - is_
37
+ - have_
38
+
39
+ Style/StringLiterals:
40
+ EnforcedStyle: double_quotes
41
+
42
+ Style/NumericLiterals:
43
+ Enabled: false
44
+
45
+ Style/SingleLineBlockParams:
46
+ Enabled: false
47
+
48
+ Style/MultilineOperationIndentation:
49
+ EnforcedStyle: indented
50
+
51
+ Style/MultilineMethodCallIndentation:
52
+ EnforcedStyle: indented
53
+
54
+ Style/TrailingCommaInArguments:
55
+ EnforcedStyleForMultiline: comma
56
+
57
+ Style/TrailingCommaInLiteral:
58
+ EnforcedStyleForMultiline: comma
59
+
60
+ Style/FrozenStringLiteralComment:
61
+ Enabled: false
62
+
63
+ Style/MutableConstant:
64
+ Enabled: false
@@ -0,0 +1,15 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.0
4
+ - 2.1.5
5
+ - 2.0.0
6
+ - ruby-head
7
+ - jruby-1.6.8
8
+ - rbx-2.2.10
9
+ before_install:
10
+ - gem install bundler -v 1.11.2
11
+ matrix:
12
+ allow_failures:
13
+ - rvm: ruby-head
14
+ - rvm: jruby-1.6.8
15
+ - rvm: rbx-2.2.10
data/Gemfile ADDED
@@ -0,0 +1,6 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in money-uphold-bank.gemspec
4
+ gemspec
5
+
6
+ gem "pry"
@@ -0,0 +1,69 @@
1
+ # Money Uphold Bank
2
+
3
+ [![Build Status](https://travis-ci.org/subvisual/money-uphold-bank.svg?branch=master)](https://travis-ci.org/subvisual/money-uphold-bank) [![Code Climate](https://codeclimate.com/github/subvisual/money-uphold-bank/badges/gpa.svg)](https://codeclimate.com/github/subvisual/money-uphold-bank)
4
+
5
+ A gem that calculates the exchange rate using published rates from https://uphold.com
6
+
7
+ To be used as an exchange rate provider for the [money gem](https://github.com/RubyMoney/money)
8
+
9
+ ## Uphold API
10
+
11
+ ```json
12
+ [{
13
+ "ask": "1",
14
+ "bid": "1",
15
+ "currency": "BTC",
16
+ "pair": "BTCBTC"
17
+ }, {
18
+ "ask": "440.99",
19
+ "bid": "440",
20
+ "currency": "USD",
21
+ "pair": "BTCUSD"
22
+ }]
23
+ ```
24
+
25
+ Full description of the ticker endpoint: https://uphold.com/en/developer/api/documentation/#get-all-tickers
26
+ Full Uphold API specification: https://uphold.com/en/developer/api/documentation
27
+
28
+ ## Features
29
+
30
+ * Calculates exchange rates based on the mid market price, as [specified by Uphold itself](https://support.uphold.com/hc/en-us/articles/203664225-How-does-Uphold-set-its-conversion-rates-)
31
+ * Supports all Uphold currencies, including Bitcoin, Litecoin, and Voxelus
32
+ * Caches API response for a customizable time
33
+
34
+ ## Installation
35
+
36
+ Add this line to your application's Gemfile:
37
+
38
+ ```ruby
39
+ gem 'money-uphold-bank'
40
+ ```
41
+
42
+ And then execute:
43
+
44
+ $ bundle
45
+
46
+ Or install it yourself as:
47
+
48
+ $ gem install money-uphold-bank
49
+
50
+ ## Usage
51
+
52
+ ```ruby
53
+ # Minimal requirements
54
+ require "money/bank/uphold"
55
+
56
+ bank = Money::Bank::Uphold.new
57
+
58
+ # (optional)
59
+ # Set the number of seconds after which the rates are automatically expired.
60
+ # By default, they expire every hour
61
+ bank.ttl_in_seconds = 3600
62
+
63
+ Money.default_bank = bank
64
+ ```
65
+
66
+ ## Contributing
67
+
68
+ Bug reports and pull requests are welcome on GitHub at https://github.com/subvisual/money-uphold-bank. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
69
+
@@ -0,0 +1,8 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+ require "rubocop/rake_task"
4
+
5
+ RuboCop::RakeTask.new
6
+ RSpec::Core::RakeTask.new(:spec)
7
+
8
+ task default: [:rubocop, :spec]
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "money/uphold/bank"
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
@@ -0,0 +1,8 @@
1
+ #!/usr/bin/env bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+ set -vx
5
+
6
+ bundle install
7
+
8
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,94 @@
1
+ require "money/bank/variable_exchange"
2
+ require "open-uri"
3
+
4
+ class Money
5
+ module Bank
6
+ class Uphold < Money::Bank::VariableExchange
7
+ UPHOLD_TICKERS_URL = "https://api.uphold.com/v0/ticker".freeze
8
+ UPHOLD_BASE_CURRENCY = "USD".freeze
9
+
10
+ # Seconds after which the current rates are automatically expired
11
+ attr_accessor :ttl_in_seconds
12
+
13
+ # Parsed UpholdBank result as a Hash
14
+ attr_reader :tickers
15
+
16
+ # Rates expiration time
17
+ attr_reader :rates_expire_at
18
+
19
+ def initialize(*args, &block)
20
+ super
21
+ ttl_in_seconds = 3600 # 1 hour
22
+ end
23
+
24
+ # Update all rates from UpholdBank JSON
25
+ def update_rates
26
+ # clear all existing rates, even inferred ones
27
+ store.each_rate do |iso_from, iso_to, _rate|
28
+ add_rate(iso_from, iso_to, nil)
29
+ end
30
+ exchange_rates.each do |ticker|
31
+ add_exchange_rates_from_ticker(ticker)
32
+ end
33
+ end
34
+
35
+ alias original_get_rate get_rate
36
+
37
+ def get_rate(iso_from, iso_to, opts = {})
38
+ update_rates_if_expired
39
+
40
+ super || get_indirect_rate(iso_from, iso_to, opts)
41
+ end
42
+
43
+ private
44
+
45
+ def get_indirect_rate(iso_from, iso_to, opts = {})
46
+ return 1 if Currency.wrap(iso_from).iso_code == Currency.wrap(iso_to).iso_code
47
+
48
+ rate_to_base = original_get_rate(iso_from, UPHOLD_BASE_CURRENCY, opts)
49
+ rate_from_base = original_get_rate(UPHOLD_BASE_CURRENCY, iso_to, opts)
50
+
51
+ return unless rate_to_base && rate_from_base
52
+
53
+ rate = rate_to_base * rate_from_base
54
+
55
+ add_rate(iso_from, iso_to, rate)
56
+ add_rate(iso_to, iso_from, 1.0 / rate)
57
+
58
+ rate
59
+ end
60
+
61
+ def add_exchange_rates_from_ticker(ticker)
62
+ iso_from, iso_to = ticker["pair"].scan(/.{3}/)
63
+ rate = (ticker["ask"].to_f + ticker["bid"].to_f) * 0.5
64
+
65
+ return unless Money::Currency.find(iso_from) && Money::Currency.find(iso_to)
66
+
67
+ add_rate(iso_from, iso_to, rate)
68
+ add_rate(iso_to, iso_from, 1.0 / rate)
69
+ end
70
+
71
+ def update_rates_if_expired
72
+ update_rates if rates_expired?
73
+ end
74
+
75
+ def rates_expired?
76
+ return true unless rates_expire_at
77
+
78
+ rates_expire_at <= Time.now
79
+ end
80
+
81
+ def exchange_rates
82
+ raw_rates = JSON.parse(read_from_url)
83
+ @rates_expire_at = Time.now + ttl_in_seconds
84
+ raw_rates
85
+ rescue JSON::ParserError
86
+ []
87
+ end
88
+
89
+ def read_from_url
90
+ open(UPHOLD_TICKERS_URL).read
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+
5
+ Gem::Specification.new do |spec|
6
+ spec.name = "money-uphold-bank"
7
+ spec.version = "0.0.1"
8
+ spec.authors = ["Miguel Palhas"]
9
+ spec.email = ["miguel@subvisual.co"]
10
+
11
+ spec.description = %q(A gem that calculates the exchange rate using published rates from uphold.com. Compatible with the money gem)
12
+ spec.summary = %q(A gem that calculates the exchange rate using published rates from uphold.com.)
13
+ spec.homepage = "https://github.com/subvisual/#{spec.name}"
14
+
15
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
16
+ spec.bindir = "exe"
17
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
18
+ spec.require_paths = ["lib"]
19
+
20
+ spec.add_dependency "money", "~> 6.7"
21
+
22
+ spec.add_development_dependency "bundler", "~> 1.11"
23
+ spec.add_development_dependency "rake", "~> 10.0"
24
+ spec.add_development_dependency "rspec", "~> 3.0"
25
+ spec.add_development_dependency "rubocop", "~> 0.37.2"
26
+ spec.add_development_dependency "webmock", "~> 2.1.0"
27
+ end
metadata ADDED
@@ -0,0 +1,139 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: money-uphold-bank
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Miguel Palhas
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2016-06-16 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: bundler
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '1.11'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '1.11'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '10.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '10.0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - "~>"
60
+ - !ruby/object:Gem::Version
61
+ version: '3.0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - "~>"
67
+ - !ruby/object:Gem::Version
68
+ version: '3.0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: rubocop
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: 0.37.2
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: 0.37.2
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - "~>"
88
+ - !ruby/object:Gem::Version
89
+ version: 2.1.0
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - "~>"
95
+ - !ruby/object:Gem::Version
96
+ version: 2.1.0
97
+ description: A gem that calculates the exchange rate using published rates from uphold.com.
98
+ Compatible with the money gem
99
+ email:
100
+ - miguel@subvisual.co
101
+ executables: []
102
+ extensions: []
103
+ extra_rdoc_files: []
104
+ files:
105
+ - ".gitignore"
106
+ - ".rspec"
107
+ - ".rubocop.yml"
108
+ - ".travis.yml"
109
+ - Gemfile
110
+ - README.md
111
+ - Rakefile
112
+ - bin/console
113
+ - bin/setup
114
+ - lib/money/bank/uphold.rb
115
+ - money-uphold-bank.gemspec
116
+ homepage: https://github.com/subvisual/money-uphold-bank
117
+ licenses: []
118
+ metadata: {}
119
+ post_install_message:
120
+ rdoc_options: []
121
+ require_paths:
122
+ - lib
123
+ required_ruby_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ required_rubygems_version: !ruby/object:Gem::Requirement
129
+ requirements:
130
+ - - ">="
131
+ - !ruby/object:Gem::Version
132
+ version: '0'
133
+ requirements: []
134
+ rubyforge_project:
135
+ rubygems_version: 2.6.2
136
+ signing_key:
137
+ specification_version: 4
138
+ summary: A gem that calculates the exchange rate using published rates from uphold.com.
139
+ test_files: []