crypto_val 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: 94ab7c7da8be8b0a4a3204d92438acfd6ee9859c
4
+ data.tar.gz: a3b4cc9ad0bca88b896a63cf114115fe0183ee24
5
+ SHA512:
6
+ metadata.gz: edefe54a285a2bbcaac0ebca703629d6073f8be825c5c1d76e02595fc202387a7d76aff3820dc672b6dcde074d7a76874303fa95e49f075b4162b39d71a5b315
7
+ data.tar.gz: 3f51e2d50c333e4395282783a1873a2662529323ea472af35b49921ca8bf6b789d1aeb0aee60ba60bebcfb13ebe3afd06334298fe660a17e0d4d2039e77d08dc
data/.travis.yml ADDED
@@ -0,0 +1,4 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.2.5
4
+ before_install: gem install bundler -v 1.13.2
@@ -0,0 +1,74 @@
1
+ # Contributor Covenant Code of Conduct
2
+
3
+ ## Our Pledge
4
+
5
+ In the interest of fostering an open and welcoming environment, we as
6
+ contributors and maintainers pledge to making participation in our project and
7
+ our community a harassment-free experience for everyone, regardless of age, body
8
+ size, disability, ethnicity, gender identity and expression, level of experience,
9
+ nationality, personal appearance, race, religion, or sexual identity and
10
+ orientation.
11
+
12
+ ## Our Standards
13
+
14
+ Examples of behavior that contributes to creating a positive environment
15
+ include:
16
+
17
+ * Using welcoming and inclusive language
18
+ * Being respectful of differing viewpoints and experiences
19
+ * Gracefully accepting constructive criticism
20
+ * Focusing on what is best for the community
21
+ * Showing empathy towards other community members
22
+
23
+ Examples of unacceptable behavior by participants include:
24
+
25
+ * The use of sexualized language or imagery and unwelcome sexual attention or
26
+ advances
27
+ * Trolling, insulting/derogatory comments, and personal or political attacks
28
+ * Public or private harassment
29
+ * Publishing others' private information, such as a physical or electronic
30
+ address, without explicit permission
31
+ * Other conduct which could reasonably be considered inappropriate in a
32
+ professional setting
33
+
34
+ ## Our Responsibilities
35
+
36
+ Project maintainers are responsible for clarifying the standards of acceptable
37
+ behavior and are expected to take appropriate and fair corrective action in
38
+ response to any instances of unacceptable behavior.
39
+
40
+ Project maintainers have the right and responsibility to remove, edit, or
41
+ reject comments, commits, code, wiki edits, issues, and other contributions
42
+ that are not aligned to this Code of Conduct, or to ban temporarily or
43
+ permanently any contributor for other behaviors that they deem inappropriate,
44
+ threatening, offensive, or harmful.
45
+
46
+ ## Scope
47
+
48
+ This Code of Conduct applies both within project spaces and in public spaces
49
+ when an individual is representing the project or its community. Examples of
50
+ representing a project or community include using an official project e-mail
51
+ address, posting via an official social media account, or acting as an appointed
52
+ representative at an online or offline event. Representation of a project may be
53
+ further defined and clarified by project maintainers.
54
+
55
+ ## Enforcement
56
+
57
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be
58
+ reported by contacting the project team at marvin.baltazar@gmail.com. All
59
+ complaints will be reviewed and investigated and will result in a response that
60
+ is deemed necessary and appropriate to the circumstances. The project team is
61
+ obligated to maintain confidentiality with regard to the reporter of an incident.
62
+ Further details of specific enforcement policies may be posted separately.
63
+
64
+ Project maintainers who do not follow or enforce the Code of Conduct in good
65
+ faith may face temporary or permanent repercussions as determined by other
66
+ members of the project's leadership.
67
+
68
+ ## Attribution
69
+
70
+ This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
71
+ available at [http://contributor-covenant.org/version/1/4][version]
72
+
73
+ [homepage]: http://contributor-covenant.org
74
+ [version]: http://contributor-covenant.org/version/1/4/
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in crypto_val.gemspec
4
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,22 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ crypto_val (0.1.0)
5
+
6
+ GEM
7
+ remote: https://rubygems.org/
8
+ specs:
9
+ minitest (5.10.1)
10
+ rake (10.4.2)
11
+
12
+ PLATFORMS
13
+ ruby
14
+
15
+ DEPENDENCIES
16
+ bundler (~> 1.13)
17
+ crypto_val!
18
+ minitest (~> 5.0)
19
+ rake (~> 10.0)
20
+
21
+ BUNDLED WITH
22
+ 1.13.2
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2017 Marvin Baltazar
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,76 @@
1
+ # CryptoVal
2
+
3
+ CryptoVal is a Ruby gem that fetches the latest value of a Cryptocurrency. In addition, it also provides exchange rate conversion for several currencies.
4
+
5
+ [![Gem Version](https://badge.fury.io/rb/cryptoval.svg)](https://badge.fury.io/rb/cryptoval)
6
+ [![Build Status](https://travis-ci.org/marvs/cryptoval.png)](https://travis-ci.org/marvs/cryptoval)
7
+
8
+ ## Installation
9
+
10
+ Add this line to your application's Gemfile:
11
+
12
+ ```ruby
13
+ gem 'crypto_val'
14
+ ```
15
+
16
+ And then execute:
17
+
18
+ $ bundle
19
+
20
+ Or install it yourself as:
21
+
22
+ $ gem install crypto_val
23
+
24
+ ## Usage
25
+
26
+ ### Basic Usage
27
+
28
+ The basic usage of this gem is to get the latest USD value of Bitcoin (BTC):
29
+
30
+ `CryptoVal::Latest.fetch`
31
+
32
+ This will return a Hash similar to:
33
+
34
+ `{:symbol=>"BTC", :currency=>"USD", :value=>4121.19}`
35
+
36
+ Additional options can be provided:
37
+
38
+ * symbol - the symbol of the Cryptocurrency (e.g. "LTC", "XRP")
39
+ * currency - the fiat currency that will be used
40
+
41
+ For example, to get the PHP value of Ripple (XRP):
42
+
43
+ `CryptoVal::Latest.fetch(symbol: "XRP", currency: "PHP")`
44
+
45
+ This returns the following:
46
+
47
+ `{:symbol=>"XRP", :currency=>"PHP", :value=>12.03}`
48
+
49
+ ### Exchange Rate
50
+
51
+ It is possible to just get the current exchange rate for different currencies. This method can be used for a more performant code by fetching the current exchange rate only once for all of the Cryptocurrency values.
52
+
53
+ `CryptoVal::FiatExchange.fetch(source_currency: "USD", target_currency: "PHP")`
54
+
55
+ * source_currency - the fiat symbol of the source currency (defaults to "USD")
56
+ * target_currency - the fiat symbol of the target currency
57
+
58
+ This returns the date and the current exchange rate:
59
+
60
+ `{:source_currency=>"USD", :target_currency=>"PHP", :date=>#<Date: 2017-08-24 ((2457990j,0s,0n),+0s,2299161j)>, :rate=>51.08}`
61
+
62
+ ## Development
63
+
64
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
65
+
66
+ To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
67
+
68
+ ## Contributing
69
+
70
+ Bug reports and pull requests are welcome on GitHub at https://github.com/marvs/cryptoval. 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.
71
+
72
+
73
+ ## License
74
+
75
+ The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
76
+
data/Rakefile ADDED
@@ -0,0 +1,10 @@
1
+ require "bundler/gem_tasks"
2
+ require "rake/testtask"
3
+
4
+ Rake::TestTask.new(:test) do |t|
5
+ t.libs << "test"
6
+ t.libs << "lib"
7
+ t.test_files = FileList['test/**/*_test.rb']
8
+ end
9
+
10
+ task :default => :test
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "crypto_val"
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,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,27 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'crypto_val/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "crypto_val"
8
+ spec.version = CryptoVal::VERSION
9
+ spec.authors = ["Marvin Baltazar"]
10
+ spec.email = ["marvin.baltazar@gmail.com"]
11
+
12
+ spec.summary = %q{Ruby wrapper for the latest valuations of various cryptocurrencies.}
13
+ spec.description = %q{Ruby wrapper for the latest valuations of various cryptocurrencies.}
14
+ spec.homepage = "https://github.com/marvs/cryptoval"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject do |f|
18
+ f.match(%r{^(test|spec|features)/})
19
+ end
20
+ spec.bindir = "exe"
21
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
22
+ spec.require_paths = ["lib"]
23
+
24
+ spec.add_development_dependency "bundler", "~> 1.13"
25
+ spec.add_development_dependency "rake", "~> 10.0"
26
+ spec.add_development_dependency "minitest", "~> 5.0"
27
+ end
data/lib/crypto_val.rb ADDED
@@ -0,0 +1,11 @@
1
+ require 'date'
2
+ require 'net/http'
3
+ require 'json'
4
+ require "crypto_val/version"
5
+ require "crypto_val/crypto_exchange/cryptonator"
6
+ require "crypto_val/fiat_exchange"
7
+ require "crypto_val/fiat_exchange/fixer"
8
+ require "crypto_val/latest"
9
+
10
+ module CryptoVal
11
+ end
@@ -0,0 +1,56 @@
1
+ module CryptoVal::CryptoExchange
2
+ class Cryptonator
3
+ DEFAULT_SYMBOL = "BTC"
4
+ DEFAULT_CURRENCY = "USD"
5
+
6
+ attr_reader :symbol, :currency
7
+
8
+ def initialize opts={}
9
+ @symbol = opts[:symbol] || DEFAULT_SYMBOL
10
+ @currency = opts[:currency] || DEFAULT_CURRENCY
11
+ end
12
+
13
+ def fetch
14
+ format_response
15
+ end
16
+
17
+ def self.fetch opts={}
18
+ new(opts).fetch
19
+ end
20
+
21
+ private
22
+
23
+ def base_url
24
+ "https://api.cryptonator.com"
25
+ end
26
+
27
+ def url
28
+ "#{base_url}/api/ticker/#{@symbol}-#{@currency}"
29
+ end
30
+
31
+ def response
32
+ @response ||= get_response
33
+ end
34
+
35
+ def get_response
36
+ uri = URI.parse(url)
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ http.use_ssl = true
39
+ request = Net::HTTP::Get.new(uri.request_uri)
40
+ http.request(request).body
41
+ end
42
+
43
+ def parsed
44
+ @parsed ||= JSON.parse(response)
45
+ end
46
+
47
+ def format_response
48
+ {
49
+ symbol: parsed["ticker"]["base"],
50
+ currency: parsed["ticker"]["target"],
51
+ price: parsed["ticker"]["price"].to_f
52
+ }
53
+ end
54
+
55
+ end
56
+ end
@@ -0,0 +1,31 @@
1
+ module CryptoVal
2
+ class FiatExchange
3
+ DEFAULT_CURRENCY = "USD"
4
+
5
+ def initialize opts={}
6
+ @source_currency = opts[:source_currency] || DEFAULT_CURRENCY
7
+ @target_currency = opts[:target_currency]
8
+ @exchange_klass = opts[:exchange_class] || CryptoVal::FiatExchange::Fixer
9
+ end
10
+
11
+ def fetch
12
+ {
13
+ source_currency: exchange[:source],
14
+ target_currency: exchange[:target],
15
+ date: exchange[:date],
16
+ rate: exchange[:rate]
17
+ }
18
+ end
19
+
20
+ def self.fetch opts={}
21
+ new(opts).fetch
22
+ end
23
+
24
+ private
25
+
26
+ def exchange
27
+ @exchange ||= @exchange_klass.fetch({ source_currency: @source_currency, target_currency: @target_currency })
28
+ end
29
+
30
+ end
31
+ end
@@ -0,0 +1,62 @@
1
+ module CryptoVal
2
+ class FiatExchange::Fixer
3
+ DEFAULT_SOURCE = "USD"
4
+ DEFAULT_TARGET = "PHP"
5
+
6
+ attr_reader :source_currency, :target_currency
7
+
8
+ def initialize opts={}
9
+ @source_currency = opts[:source_currency] || DEFAULT_SOURCE
10
+ @target_currency = opts[:target_currency] || DEFAULT_TARGET
11
+ end
12
+
13
+ def fetch
14
+ format_response
15
+ end
16
+
17
+ def self.fetch opts={}
18
+ new(opts).fetch
19
+ end
20
+
21
+ private
22
+
23
+ def base_url
24
+ "https://api.fixer.io"
25
+ end
26
+
27
+ def url
28
+ "#{base_url}/latest?base=#{@source_currency}&symbols=#{@target_currency}"
29
+ end
30
+
31
+ def response
32
+ @response ||= get_response
33
+ end
34
+
35
+ def get_response
36
+ uri = URI.parse(url)
37
+ http = Net::HTTP.new(uri.host, uri.port)
38
+ http.use_ssl = true
39
+ request = Net::HTTP::Get.new(uri.request_uri)
40
+ http.request(request).body
41
+ end
42
+
43
+ def parsed
44
+ @parsed ||= JSON.parse(response)
45
+ end
46
+
47
+ def parsed_date
48
+ Date.parse(parsed["date"]) rescue nil
49
+ end
50
+
51
+ def format_response
52
+ {
53
+ source: parsed["base"],
54
+ target: @target_currency,
55
+ date: parsed_date,
56
+ rate: parsed["rates"][@target_currency].to_f
57
+ }
58
+ end
59
+
60
+ end
61
+
62
+ end
@@ -0,0 +1,49 @@
1
+ module CryptoVal
2
+ class Latest
3
+ # Gets the latest value of a cryptocurrency
4
+ DEFAULT_CRYPTO = "BTC"
5
+ DEFAULT_CURRENCY = "USD"
6
+
7
+ def initialize opts={}
8
+ @symbol = opts[:symbol] || DEFAULT_CRYPTO
9
+ @currency = opts[:currency] || DEFAULT_CURRENCY
10
+ @crypto_klass = opts[:crypto_klass] || CryptoVal::CryptoExchange::Cryptonator
11
+ @fiat_klass = opts[:fiat_klass] || CryptoVal::FiatExchange
12
+ end
13
+
14
+ def fetch
15
+ {
16
+ symbol: @symbol,
17
+ currency: @currency,
18
+ value: currency_value
19
+ }
20
+ end
21
+
22
+ def self.fetch opts={}
23
+ new(opts).fetch
24
+ end
25
+
26
+ private
27
+
28
+ def crypto
29
+ @crypto ||= @crypto_klass.fetch({ symbol: @symbol })
30
+ end
31
+
32
+ def fiat
33
+ @fiat ||= @fiat_klass.fetch({ target_currency: @currency })
34
+ end
35
+
36
+ def crypto_value
37
+ @crypto_value ||= crypto[:price]
38
+ end
39
+
40
+ def exchange_rate
41
+ exchange_rate ||= fiat[:rate]
42
+ end
43
+
44
+ def currency_value
45
+ @currency != DEFAULT_CURRENCY ? (crypto_value * exchange_rate).to_f.round(2) : crypto_value.to_f.round(2)
46
+ end
47
+
48
+ end
49
+ end
@@ -0,0 +1,3 @@
1
+ module CryptoVal
2
+ VERSION = "0.1.0"
3
+ end
metadata ADDED
@@ -0,0 +1,102 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: crypto_val
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Marvin Baltazar
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2017-09-01 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: bundler
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.13'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.13'
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
+ - !ruby/object:Gem::Dependency
42
+ name: minitest
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '5.0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '5.0'
55
+ description: Ruby wrapper for the latest valuations of various cryptocurrencies.
56
+ email:
57
+ - marvin.baltazar@gmail.com
58
+ executables: []
59
+ extensions: []
60
+ extra_rdoc_files: []
61
+ files:
62
+ - ".travis.yml"
63
+ - CODE_OF_CONDUCT.md
64
+ - Gemfile
65
+ - Gemfile.lock
66
+ - LICENSE.txt
67
+ - README.md
68
+ - Rakefile
69
+ - bin/console
70
+ - bin/setup
71
+ - crypto_val.gemspec
72
+ - lib/crypto_val.rb
73
+ - lib/crypto_val/crypto_exchange/cryptonator.rb
74
+ - lib/crypto_val/fiat_exchange.rb
75
+ - lib/crypto_val/fiat_exchange/fixer.rb
76
+ - lib/crypto_val/latest.rb
77
+ - lib/crypto_val/version.rb
78
+ homepage: https://github.com/marvs/cryptoval
79
+ licenses:
80
+ - MIT
81
+ metadata: {}
82
+ post_install_message:
83
+ rdoc_options: []
84
+ require_paths:
85
+ - lib
86
+ required_ruby_version: !ruby/object:Gem::Requirement
87
+ requirements:
88
+ - - ">="
89
+ - !ruby/object:Gem::Version
90
+ version: '0'
91
+ required_rubygems_version: !ruby/object:Gem::Requirement
92
+ requirements:
93
+ - - ">="
94
+ - !ruby/object:Gem::Version
95
+ version: '0'
96
+ requirements: []
97
+ rubyforge_project:
98
+ rubygems_version: 2.4.8
99
+ signing_key:
100
+ specification_version: 4
101
+ summary: Ruby wrapper for the latest valuations of various cryptocurrencies.
102
+ test_files: []