Skip to content

Commit

Permalink
Add new endpoint for crypto
Browse files Browse the repository at this point in the history
  • Loading branch information
Stefano Martin committed Apr 8, 2020
1 parent e6f29eb commit 723e4f7
Show file tree
Hide file tree
Showing 11 changed files with 56 additions and 41 deletions.
2 changes: 1 addition & 1 deletion AlphavantageRB.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

Gem::Specification.new do |s|
s.name = "alphavantagerb"
s.version = "1.3.3"
s.version = "1.4.0"
s.authors = ["Stefano Martin"]
s.email = ["[email protected]"]
s.homepage = "https://github.com/StefanoMartin/AlphaVantageRB"
Expand Down
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
AlphavantageRB [![Gem Version](https://badge.fury.io/rb/alphavantagerb.svg)](https://badge.fury.io/rb/alphavantagerb)
=========================================================

Last update: 7/4/2020
Last update: 8/4/2020

[Alpha Vantage](https://www.alphavantage.co/) is a great API for retrieving Stock
market data in JSON or CSV format.
Expand Down Expand Up @@ -736,6 +736,7 @@ These two creation commands are equivalent:
``` ruby
crypto = client.crypto symbol: "BTC", market: "DKK"
crypto = Alphavantage::Crypto.new symbol: "BTC", market: "DKK", key: "YOURKEY"
crypto.rating # Retrieve crypto ratings instance
```

Note that the initialization owns different entry:
Expand Down Expand Up @@ -832,8 +833,8 @@ exchange = Alphavantage::Exchange.new from: "USD", to: "DKK", key: "YOURKEY"

Note that the initialization owns different entry:

* from: input currency you want to check the value
* to: output currency you want to see the value
* from: input currency you want to check the value (can be a crypto currency)
* to: output currency you want to see the value (can be a crypto currency)
* symbol: it is a string that denote the stock you want to retrieve.
* key: authentication key. This value cannot be setup if you are initializing a Stock class from a client
* verbose: used to see the request to Alpha Vantage (default false). This value cannot be setup if you are initializing a timeseries from a stock
Expand Down
1 change: 1 addition & 0 deletions lib/Client.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ def request(url)
end
data = response.body
begin
puts data if @verbose
data = JSON.parse(data)
rescue StandardError => e
raise Alphavantage::Error.new message: "Parsing failed",
Expand Down
5 changes: 5 additions & 0 deletions lib/Crypto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,11 @@ def datatype=(datatype)
@datatype = datatype
end

def rating
url = "function=CRYPTO_RATING&symbol=#{@symbol}"
return open_struct(url, "Crypto Rating (FCAS)")
end

def timeseries type: "daily", market: @market, file: nil, datatype: @datatype
Alphavantage::Crypto_Timeseries.new type: type, market: market,
symbol: @symbol, datatype: datatype, file: file, key: @client
Expand Down
2 changes: 2 additions & 0 deletions lib/Exchange.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ def initialize from:, to:, key:, verbose: false,
datatype: "json"
check_argument([true, false], verbose, "verbose")
@client = return_client(key, verbose)
from = from.symbol if from.is_a?(Alphavantage::Crypto)
to = to.symbol if to.is_a?(Alphavantage::Crypto)
@from = from
@to = to
check_argument(["json", "csv"], datatype, "datatype")
Expand Down
16 changes: 8 additions & 8 deletions lib/Exchange_Timeseries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,31 +23,31 @@ def initialize type: "intraday", from:, to:, datatype: "json", file: nil,
return @client.download(url, file) if datatype == "csv"
@output = @client.request(url)
metadata = @output.dig("Meta Data") || {}
metadata.each do |key, val|
key_sym = key.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
metadata.each do |k, val|
key_sym = k.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
define_singleton_method(key_sym) do
return val
end
end
@open = []; @high = []; @low = []; @close = [];

begin
time_series = @output.find{|key, val| key.include?("Time Series")}[1]
time_series = @output.find{|k, val| k.include?("Time Series")}[1]
rescue StandardError => e
raise Alphavantage::Error.new message: "No Time Series found: #{e.message}",
data: @output
end

series = {}
convert_key = {}
time_series.values[0].keys.each do |key|
key_sym = recreate_metadata_key(key)
time_series.values[0].keys.each do |k|
key_sym = recreate_metadata_key(k)
series[key_sym] = []
convert_key[key] = key_sym
convert_key[k] = key_sym
end
time_series.each do |time, ts_hash|
ts_hash.each do |key, value|
series[convert_key[key]] << [time, value]
ts_hash.each do |k, value|
series[convert_key[k]] << [time, value]
end
end
series.keys.each do |key_sym|
Expand Down
16 changes: 8 additions & 8 deletions lib/Indicator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -96,30 +96,30 @@ def initialize function:, symbol:, interval: "daily", time_period: "60",

@output = @client.request(url)
metadata = @output.dig("Meta Data") || {}
metadata.each do |key, val|
key_sym = recreate_metadata_key(key)
metadata.each do |k, val|
key_sym = recreate_metadata_key(k)
define_singleton_method(key_sym) do
return val
end
end

begin
time_series = @output.find{|key, val| key.include?("Technical Analysis")}[1]
time_series = @output.find{|k, val| k.include?("Technical Analysis")}[1]
rescue StandardError => e
raise Alphavantage::Error.new message: "No Time Series found: #{e.message}",
data: @output
end

series = {}
convert_key = {}
time_series.values[0].keys.each do |key|
key_sym = key.downcase.gsub(/[.\/]/, "").lstrip.gsub(" ", "_").to_sym
time_series.values[0].keys.each do |k|
key_sym = k.downcase.gsub(/[.\/]/, "").lstrip.gsub(" ", "_").to_sym
series[key_sym] = []
convert_key[key] = key_sym
convert_key[k] = key_sym
end
time_series.each do |time, ts_hash|
ts_hash.each do |key, value|
series[convert_key[key]] << [time, value]
ts_hash.each do |k, value|
series[convert_key[k]] << [time, value]
end
end
series.keys.each do |key_sym|
Expand Down
22 changes: 11 additions & 11 deletions lib/Sector.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,22 +7,22 @@ def initialize key:, verbose: false
@client = return_client(key, verbose)
@output = @client.request("function=SECTOR")
metadata = @output.dig("Meta Data") || {}
metadata.each do |key, val|
key_sym = key.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
metadata.each do |k, val|
key_sym = k.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
define_singleton_method(key_sym) do
return val
end
end
@output.each do |key, val|
next if key == "Meta Data"
key = key.split(":")[1].lstrip
key = key.split(" ")
if key[0].to_i != 0
key[0] = key[0].to_i.humanize
@output.each do |k, val|
next if k == "Meta Data"
k = k.split(":")[1].lstrip
k = k.split(" ")
if k[0].to_i != 0
k[0] = k[0].to_i.humanize
end
key.delete_if{|k| k.include?("(")}
key = key.join("_")
key_sym = key.downcase.gsub("-", "_").to_sym
k.delete_if{|ka| ka.include?("(")}
k = k.join("_")
key_sym = k.downcase.gsub("-", "_").to_sym
define_singleton_method(key_sym) do
return val
end
Expand Down
16 changes: 8 additions & 8 deletions lib/Timeseries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,30 @@ def initialize type: "daily", interval: nil, outputsize: "compact",
return @client.download(url, file) if datatype == "csv"
@output = @client.request(url)
metadata = @output.dig("Meta Data") || {}
metadata.each do |key, val|
key_sym = recreate_metadata_key(key)
metadata.each do |k, val|
key_sym = recreate_metadata_key(k)
define_singleton_method(key_sym) do
return val
end
end

begin
time_series = @output.find{|key, val| key.include?("Time Series")}[1]
time_series = @output.find{|k, val| k.include?("Time Series")}[1]
rescue StandardError => e
raise Alphavantage::Error.new message: "No Time Series found: #{e.message}",
data: @output
end

series = {}
convert_key = {}
time_series.values[0].keys.each do |key|
key_sym = key.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
time_series.values[0].keys.each do |k|
key_sym = k.downcase.gsub(/[0-9.]/, "").lstrip.gsub(" ", "_").to_sym
series[key_sym] = []
convert_key[key] = key_sym
convert_key[k] = key_sym
end
time_series.each do |time, ts_hash|
ts_hash.each do |key, value|
series[convert_key[key]] << [time, value]
ts_hash.each do |k, value|
series[convert_key[k]] << [time, value]
end
end
series.keys.each do |key_sym|
Expand Down
8 changes: 7 additions & 1 deletion spec/lib/1.0.0/crypto.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
bool << "error"
end
stock.datatype = "csv"
bool <<stock.datatype
bool << stock.datatype
stock.datatype = "json"
expect(bool).to eq ["json", "error", "csv"]
end
Expand All @@ -32,5 +32,11 @@
timeseries = stock.timeseries
expect(timeseries.class).to eq Alphavantage::Crypto_Timeseries
end

it "can check its rating" do
stock = @client.crypto symbol: "BTC", market: "DKK"
rating = stock.rating
expect(rating.symbol).to eq "BTC"
end
end
end
2 changes: 1 addition & 1 deletion spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
require "openssl"
OpenSSL::SSL::VERIFY_PEER = OpenSSL::SSL::VERIFY_NONE
require "alphavantagerb"
# require_relative File.expand_path('../../lib/alphavantagerb', __FILE__)
# require_relative "../lib/alphavantagerb"

RSpec.configure do |config|
config.color = true
Expand Down

0 comments on commit 723e4f7

Please sign in to comment.