alphavantage 1.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
+ SHA256:
3
+ metadata.gz: 388988a73e15b388053059bfd2208107755d7fce7ada4ffb590e4543083914f7
4
+ data.tar.gz: 535540dd99e453b36d629bd4581fb8dd3018799eeb3aa96db8ae562202af238a
5
+ SHA512:
6
+ metadata.gz: 90c22c7bc4b7d3490190a2db69af44e9e1b385cfc8db48bd7b3264c1e2f3289a87172fcb522142b58a7890eedba125557101bfd2548d308d2b8405d0024976ba
7
+ data.tar.gz: b73e4ef35ddd4730a18f77e0d1dc7ff2ccf672b33a38c4ba67ec2e2a5af81b032d6a2660e405ad3078b437f65dd7b4b33fd0c8f3ac2c6060a5722acd52330a93
data/.gitignore ADDED
@@ -0,0 +1,14 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ # rspec failure tracking
11
+ .rspec_status
12
+
13
+ .byebug_history
14
+ .DS_Store
data/.rspec ADDED
@@ -0,0 +1,3 @@
1
+ --format documentation
2
+ --color
3
+ --require spec_helper
data/.travis.yml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ sudo: false
3
+ language: ruby
4
+ cache: bundler
5
+ rvm:
6
+ - 2.6.3
7
+ before_install: gem install bundler -v 1.17.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. 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,6 @@
1
+ source "https://rubygems.org"
2
+
3
+ git_source(:github) {|repo_name| "https://github.com/#{repo_name}" }
4
+
5
+ # Specify your gem's dependencies in alphavantage_ruby.gemspec
6
+ gemspec
data/Gemfile.lock ADDED
@@ -0,0 +1,63 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ alphavantage (1.0.1)
5
+ faraday (~> 1.4)
6
+ hashie (~> 4.1)
7
+
8
+ GEM
9
+ remote: https://rubygems.org/
10
+ specs:
11
+ addressable (2.7.0)
12
+ public_suffix (>= 2.0.2, < 5.0)
13
+ byebug (11.1.3)
14
+ crack (0.4.5)
15
+ rexml
16
+ diff-lcs (1.4.4)
17
+ faraday (1.4.1)
18
+ faraday-excon (~> 1.1)
19
+ faraday-net_http (~> 1.0)
20
+ faraday-net_http_persistent (~> 1.1)
21
+ multipart-post (>= 1.2, < 3)
22
+ ruby2_keywords (>= 0.0.4)
23
+ faraday-excon (1.1.0)
24
+ faraday-net_http (1.0.1)
25
+ faraday-net_http_persistent (1.1.0)
26
+ hashdiff (1.0.1)
27
+ hashie (4.1.0)
28
+ multipart-post (2.1.1)
29
+ public_suffix (4.0.6)
30
+ rake (13.0.3)
31
+ rexml (3.2.5)
32
+ rspec (3.10.0)
33
+ rspec-core (~> 3.10.0)
34
+ rspec-expectations (~> 3.10.0)
35
+ rspec-mocks (~> 3.10.0)
36
+ rspec-core (3.10.1)
37
+ rspec-support (~> 3.10.0)
38
+ rspec-expectations (3.10.1)
39
+ diff-lcs (>= 1.2.0, < 2.0)
40
+ rspec-support (~> 3.10.0)
41
+ rspec-mocks (3.10.2)
42
+ diff-lcs (>= 1.2.0, < 2.0)
43
+ rspec-support (~> 3.10.0)
44
+ rspec-support (3.10.2)
45
+ ruby2_keywords (0.0.4)
46
+ webmock (3.12.2)
47
+ addressable (>= 2.3.6)
48
+ crack (>= 0.3.2)
49
+ hashdiff (>= 0.4.0, < 2.0.0)
50
+
51
+ PLATFORMS
52
+ x86_64-darwin-19
53
+
54
+ DEPENDENCIES
55
+ alphavantage!
56
+ bundler (~> 2.2)
57
+ byebug
58
+ rake (>= 12.3.3)
59
+ rspec (~> 3.0)
60
+ webmock
61
+
62
+ BUNDLED WITH
63
+ 2.2.16
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2021 Adrian Teh
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,111 @@
1
+ # Alpha Vantage Ruby Library
2
+
3
+ The Alpha Vantage Ruby library provides convenient access to the [Alpha Vantage API](https://www.alphavantage.co/documentation/) from applications written in the Ruby language.
4
+
5
+ ## Installation
6
+
7
+ Add this line to your application's Gemfile:
8
+
9
+ ```ruby
10
+ gem 'alphavantage'
11
+ ```
12
+
13
+ And then execute:
14
+
15
+ $ bundle
16
+
17
+ Or install it yourself as:
18
+
19
+ $ gem install alphavantage
20
+
21
+ ## Usage
22
+
23
+ The library needs to be configured with your account's api key which you can obtain from https://www.alphavantage.co/support/#api-key.
24
+ Set the `Alphavantage.configuration.api_key` to its value. If you are using Rails, you can configure this in an initializer.
25
+
26
+ ```
27
+ require 'alphavantage'
28
+
29
+ Alphavantage.configure do |config|
30
+ config.api_key = 'your-api-key'
31
+ end
32
+ ```
33
+
34
+ ### Accessing a response object
35
+ All JSON responses are converted to pseudo-objects that have method-like accessors for hash keys
36
+ ```
37
+ quote = Alphavantage::TimeSeries.new(symbol: 'TSLA').quote
38
+ quote.previous_close #=> "719.6900"
39
+ quote.volume #=> "27879033"
40
+ ```
41
+
42
+ All hash keys are also normalized to provide clean and consistent access to values since the Alphavantage API returns arbitrarily formatted keys with numbers, spaces, letters and symbols (i.e. "Crypto Rating (FCAS)", "3. fcas rating", "4. Last Refreshed", "Time Series FX (5min)", "1a. open (CNY)")
43
+
44
+ With this normalization, you can now access via
45
+
46
+ `intraday.time_series_fx_5min`
47
+
48
+ instead of
49
+
50
+ `intraday["Time Series FX (5min)"]`
51
+
52
+ ### Stock Time Series
53
+
54
+ ```
55
+ Alphavantage::TimeSeries.search(keywords: 'Tesla')
56
+
57
+ stock_timeseries = Alphavantage::TimeSeries.new(symbol: 'TSLA')
58
+ stock_timeseries.quote
59
+ stock_timeseries.monthly
60
+ stock_timeseries.monthly(adjusted: true)
61
+ stock_timeseries.weekly
62
+ stock_timeseries.weekly(adjusted: true)
63
+ stock_timeseries.daily(outputsize: 'compact')
64
+ stock_timeseries.daily(adjusted: true, outputsize: 'full')
65
+ stock_timeseries.intraday(adjusted: true, outputsize: 'compact', interval: '5min')
66
+ ```
67
+ ### Fundamental Data
68
+ ```
69
+ company = Alphavantage::Fundamental.new(symbol: 'TSLA')
70
+ company.overview
71
+ company.earnings
72
+ company.income_statement
73
+ company.balance_sheet
74
+ company.cash_flow
75
+ ```
76
+ ### Forex
77
+ ```
78
+ forex = Alphavantage::Forex.new(from_symbol: 'USD', to_symbol: 'JPY')
79
+ forex.exchange_rates
80
+ forex.intraday(interval: '5min', outputsize: 'compact')
81
+ forex.daily(outputsize: 'compact')
82
+ forex.weekly
83
+ forex.monthly
84
+ ```
85
+ ### Crypto Currencies
86
+ ```
87
+ Alphavantage::Crypto.health_index(symbol: 'BTC')
88
+
89
+ crypto = Alphavantage::Crypto.new(symbol: 'BTC', market: 'USD')
90
+ crypto.intraday(interval: '5min')
91
+ crypto.daily
92
+ crypto.weekly
93
+ crypto.monthly
94
+ ```
95
+ ## Development
96
+
97
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
98
+
99
+ 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).
100
+
101
+ ## Contributing
102
+
103
+ Bug reports and pull requests are welcome on GitHub at https://github.com/codespore/alphavantage_ruby. 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.
104
+
105
+ ## License
106
+
107
+ The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
108
+
109
+ ## Code of Conduct
110
+
111
+ Everyone interacting in the Alphavantage project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/codespore/alphavantage_ruby/blob/master/CODE_OF_CONDUCT.md).
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
@@ -0,0 +1,46 @@
1
+
2
+ lib = File.expand_path("../lib", __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require "alphavantage/version"
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "alphavantage"
8
+ spec.version = Alphavantage::VERSION
9
+ spec.authors = ["Adrian Teh"]
10
+ spec.email = ["ateh.dev@gmail.com"]
11
+
12
+ spec.summary = "Ruby library for Alpha Vantage API"
13
+ spec.description = "Ruby library for the Alpha Vantage API, a leading provider of stock APIs as well as forex (FX) and cryptocurrency data feeds."
14
+ spec.homepage = "https://github.com/codespore/alphavantage_ruby"
15
+ spec.license = "MIT"
16
+
17
+ # Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
18
+ # to allow pushing to a single host or delete this section to allow pushing to any host.
19
+ if spec.respond_to?(:metadata)
20
+ spec.metadata["homepage_uri"] = spec.homepage
21
+ spec.metadata["source_code_uri"] = "https://github.com/codespore/alphavantage_ruby"
22
+ spec.metadata["github_repo"] = "ssh://github.com/codespore/alphavantage_ruby"
23
+ spec.metadata["bug_tracker_uri"] = "https://github.com/codespore/alphavantage_ruby/issues"
24
+ else
25
+ raise "RubyGems 2.0 or newer is required to protect against " \
26
+ "public gem pushes."
27
+ end
28
+
29
+ # Specify which files should be added to the gem when it is released.
30
+ # The `git ls-files -z` loads the files in the RubyGem that have been added into git.
31
+ spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
32
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
33
+ end
34
+ spec.bindir = "exe"
35
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
36
+ spec.require_paths = ["lib"]
37
+
38
+ spec.add_dependency "faraday", "~> 1.4"
39
+ spec.add_dependency "hashie", "~> 4.1"
40
+
41
+ spec.add_development_dependency "bundler", "~> 2.2"
42
+ spec.add_development_dependency "rake", ">= 12.3.3"
43
+ spec.add_development_dependency "rspec", "~> 3.0"
44
+ spec.add_development_dependency "webmock"
45
+ spec.add_development_dependency "byebug"
46
+ end
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "alphavantage_ruby"
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(__FILE__)
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,12 @@
1
+ require "faraday"
2
+ require "hashie"
3
+ require "alphavantage/version"
4
+ require "alphavantage/error"
5
+ require "alphavantage/configuration"
6
+ require "alphavantage/normalize_key"
7
+ require "alphavantage/client"
8
+ require "alphavantage/validations"
9
+ require "alphavantage/time_series"
10
+ require "alphavantage/fundamental"
11
+ require "alphavantage/forex"
12
+ require "alphavantage/crypto"
@@ -0,0 +1,27 @@
1
+ module Alphavantage
2
+ class Client
3
+
4
+ class << self
5
+ def get(params:)
6
+ default_params = { apikey: Alphavantage.configuration.api_key }
7
+ response = Faraday.get('https://www.alphavantage.co/query') do |req|
8
+ req.params = default_params.merge(params)
9
+ req.headers['Content-Type'] = 'application/json'
10
+ end
11
+ Hashie::Mash.new(convert_hash_keys(JSON.parse(response.body)))
12
+ end
13
+
14
+ def convert_hash_keys(value)
15
+ case value
16
+ when Array
17
+ value.map { |v| convert_hash_keys(v) }
18
+ when Hash
19
+ Hash[value.map { |k, v| [ NormalizeKey.new(key: k).call, convert_hash_keys(v) ] }]
20
+ else
21
+ value
22
+ end
23
+ end
24
+ end
25
+
26
+ end
27
+ end
@@ -0,0 +1,15 @@
1
+ module Alphavantage
2
+ class << self
3
+ def configuration
4
+ @configuration ||= Configuration.new
5
+ end
6
+
7
+ def configure
8
+ yield(configuration)
9
+ end
10
+ end
11
+
12
+ class Configuration
13
+ attr_accessor :api_key
14
+ end
15
+ end
@@ -0,0 +1,45 @@
1
+ module Alphavantage
2
+ class Crypto
3
+ include Validations
4
+
5
+ FUNCTIONS = {
6
+ health_index: 'CRYPTO_RATING',
7
+ intraday: 'CRYPTO_INTRADAY',
8
+ daily: 'DIGITAL_CURRENCY_DAILY',
9
+ weekly: 'DIGITAL_CURRENCY_WEEKLY',
10
+ monthly: 'DIGITAL_CURRENCY_MONTHLY'
11
+ }
12
+
13
+ def self.health_index(symbol:)
14
+ Client.get(params: {
15
+ function: self::FUNCTIONS[__method__],
16
+ symbol: symbol
17
+ }).crypto_rating_fcas
18
+ end
19
+
20
+ def initialize(symbol:,market:)
21
+ @symbol = symbol
22
+ @market = market
23
+ end
24
+
25
+ def intraday(interval: '5min')
26
+ Client.get(params: {
27
+ function: FUNCTIONS[__method__],
28
+ symbol: @symbol,
29
+ market: @market,
30
+ interval: validate_interval(interval)
31
+ })
32
+ end
33
+
34
+ def daily
35
+ Client.get(params: {
36
+ function: FUNCTIONS[__callee__],
37
+ symbol: @symbol,
38
+ market: @market
39
+ })
40
+ end
41
+ alias :weekly :daily
42
+ alias :monthly :daily
43
+
44
+ end
45
+ end
@@ -0,0 +1,3 @@
1
+ module Alphavantage
2
+ class Error < StandardError; end
3
+ end
@@ -0,0 +1,54 @@
1
+ module Alphavantage
2
+ class Forex
3
+ include Validations
4
+
5
+ FUNCTIONS = {
6
+ exchange_rates: 'CURRENCY_EXCHANGE_RATE',
7
+ intraday: 'FX_INTRADAY',
8
+ daily: 'FX_DAILY',
9
+ weekly: 'FX_WEEKLY',
10
+ monthly: 'FX_MONTHLY'
11
+ }
12
+
13
+ def initialize(from_symbol:,to_symbol:)
14
+ @from_symbol = from_symbol
15
+ @to_symbol = to_symbol
16
+ end
17
+
18
+ def exchange_rates
19
+ Client.get(params: {
20
+ function: FUNCTIONS[__method__],
21
+ from_currency: @from_symbol,
22
+ to_currency: @to_symbol
23
+ }).realtime_currency_exchange_rate
24
+ end
25
+
26
+ def intraday(interval: '5min', outputsize: 'compact')
27
+ Client.get(params: {
28
+ function: FUNCTIONS[__method__],
29
+ from_symbol: @from_symbol,
30
+ to_symbol: @to_symbol,
31
+ interval: validate_interval(interval),
32
+ outputsize: validate_outputsize(outputsize)
33
+ })
34
+ end
35
+
36
+ def daily(outputsize: 'compact')
37
+ Client.get(params: {
38
+ function: FUNCTIONS[__method__],
39
+ from_symbol: @from_symbol,
40
+ to_symbol: @to_symbol,
41
+ outputsize: validate_outputsize(outputsize)
42
+ })
43
+ end
44
+
45
+ def weekly
46
+ Client.get(params: {
47
+ function: FUNCTIONS[__callee__],
48
+ from_symbol: @from_symbol,
49
+ to_symbol: @to_symbol
50
+ })
51
+ end
52
+ alias :monthly :weekly
53
+ end
54
+ end
@@ -0,0 +1,41 @@
1
+ module Alphavantage
2
+ class Fundamental
3
+ FUNCTIONS = {
4
+ overview: 'OVERVIEW',
5
+ earnings: 'EARNINGS',
6
+ income_statement: 'INCOME_STATEMENT',
7
+ balance_sheet: 'BALANCE_SHEET',
8
+ cash_flow: 'CASH_FLOW'
9
+ }
10
+
11
+ def initialize(symbol:)
12
+ @symbol = symbol
13
+ end
14
+
15
+ def overview
16
+ response(FUNCTIONS[__method__])
17
+ end
18
+
19
+ def earnings
20
+ response(FUNCTIONS[__method__]).annual_earnings
21
+ end
22
+
23
+ def income_statement
24
+ response(FUNCTIONS[__method__]).annual_reports
25
+ end
26
+
27
+ def balance_sheet
28
+ response(FUNCTIONS[__method__]).annual_reports
29
+ end
30
+
31
+ def cash_flow
32
+ response(FUNCTIONS[__method__]).annual_reports
33
+ end
34
+
35
+ private
36
+
37
+ def response(function)
38
+ Client.get(params: { function: function, symbol: @symbol })
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,30 @@
1
+ module Alphavantage
2
+ class NormalizeKey
3
+ def initialize(key:)
4
+ @key = key
5
+ end
6
+
7
+ def call
8
+ return @key if is_date?(@key)
9
+ underscore_key(sanitize_key(@key))
10
+ end
11
+
12
+ private
13
+
14
+ def underscore_key(key)
15
+ key.to_s.gsub(/::/, '/').
16
+ gsub(/([A-Z]+)([A-Z][a-z])/,'\1_\2').
17
+ gsub(/([a-z\d])([A-Z])/,'\1_\2').
18
+ tr("-", "_").
19
+ downcase.to_sym
20
+ end
21
+
22
+ def sanitize_key(key)
23
+ key.tr('.()','').gsub(/^\d+.?\s/, "").tr(' ','_')
24
+ end
25
+
26
+ def is_date?(key)
27
+ !/(\d{4}-\d{2}-\d{2})/.match(key.to_s).nil?
28
+ end
29
+ end
30
+ end
@@ -0,0 +1,67 @@
1
+ module Alphavantage
2
+ class TimeSeries
3
+ include Validations
4
+
5
+ FUNCTIONS = {
6
+ search: 'SYMBOL_SEARCH',
7
+ quote: 'GLOBAL_QUOTES',
8
+ monthly: 'TIME_SERIES_MONTHLY',
9
+ monthly_adjusted: 'TIME_SERIES_MONTHLY_ADJUSTED',
10
+ weekly: 'TIME_SERIES_WEEKLY',
11
+ weekly_adjusted: 'TIME_SERIES_WEEKLY_ADJUSTED',
12
+ daily: 'TIME_SERIES_DAILY',
13
+ daily_adjusted: 'TIME_SERIES_DAILY_ADJUSTED',
14
+ intraday: 'TIME_SERIES_INTRADAY',
15
+ intraday_extended_history: 'TIME_SERIES_INTRADAY_EXTENDED'
16
+ }
17
+
18
+ def self.search(keywords:)
19
+ Client.get(params: { function: self::FUNCTIONS[__method__], keywords: keywords }).best_matches
20
+ end
21
+
22
+ def initialize(symbol:)
23
+ @symbol = symbol
24
+ end
25
+
26
+ def quote
27
+ Client.get(params: { function: FUNCTIONS[__method__], symbol: @symbol }).global_quote
28
+ end
29
+
30
+ def monthly(adjusted: false)
31
+ function = adjusted ? FUNCTIONS[:monthly_adjusted] : FUNCTIONS[__method__]
32
+ Client.get(params: { function: function, symbol: @symbol })
33
+ end
34
+
35
+ def weekly(adjusted: false)
36
+ function = adjusted ? FUNCTIONS[:weekly_adjusted] : FUNCTIONS[__method__]
37
+ Client.get(params: { function: function, symbol: @symbol })
38
+ end
39
+
40
+ def daily(adjusted: false, outputsize: 'compact')
41
+ function = adjusted ? FUNCTIONS[:daily_adjusted] : FUNCTIONS[__method__]
42
+ Client.get(params: { function: function, symbol: @symbol, outputsize: validate_outputsize(outputsize) })
43
+ end
44
+
45
+ def intraday(adjusted: true, outputsize: 'compact', interval: '5min', extended_history: false, slice: 'year1month1')
46
+ if extended_history
47
+ raise Alphavantage::Error, "Extended history returns a CSV which will be supported at a later time."
48
+ # params = {
49
+ # function: FUNCTIONS[:intraday_extended_history],
50
+ # symbol: @symbol,
51
+ # slice: validate_slice(slice),
52
+ # interval: validate_interval(interval),
53
+ # adjusted: adjusted
54
+ # }
55
+ else
56
+ params = {
57
+ function: FUNCTIONS[__method__],
58
+ symbol: @symbol,
59
+ outputsize: validate_outputsize(outputsize),
60
+ interval: validate_interval(interval),
61
+ adjusted: adjusted
62
+ }
63
+ end
64
+ Client.get(params: params)
65
+ end
66
+ end
67
+ end
@@ -0,0 +1,29 @@
1
+ module Alphavantage
2
+ module Validations
3
+ VALID_SLICES = (1..2).map do |year|
4
+ (1..12).map do |month|
5
+ "year#{year}month#{month}"
6
+ end
7
+ end.flatten
8
+
9
+ VALID_INTERVALS = %w{ 1min 5min 15min 30min 60min }
10
+ VALID_OUTPUTSIZES = %{ compact full }
11
+
12
+ private
13
+
14
+ def validate_slice(slice)
15
+ raise Alphavantage::Error, "Invalid slice given." unless VALID_SLICES.include?(slice)
16
+ slice
17
+ end
18
+
19
+ def validate_interval(interval)
20
+ raise Alphavantage::Error, "Invalid interval given." unless VALID_INTERVALS.include?(interval)
21
+ interval
22
+ end
23
+
24
+ def validate_outputsize(outputsize)
25
+ raise Alphavantage::Error, "Invalid outputsize given." unless VALID_OUTPUTSIZES.include?(outputsize)
26
+ outputsize
27
+ end
28
+ end
29
+ end
@@ -0,0 +1,3 @@
1
+ module Alphavantage
2
+ VERSION = "1.0.1"
3
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: alphavantage
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Adrian Teh
8
+ autorequire:
9
+ bindir: exe
10
+ cert_chain: []
11
+ date: 2021-04-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: faraday
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.4'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.4'
27
+ - !ruby/object:Gem::Dependency
28
+ name: hashie
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4.1'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4.1'
41
+ - !ruby/object:Gem::Dependency
42
+ name: bundler
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '2.2'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '2.2'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rake
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: 12.3.3
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: 12.3.3
69
+ - !ruby/object:Gem::Dependency
70
+ name: rspec
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - "~>"
74
+ - !ruby/object:Gem::Version
75
+ version: '3.0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - "~>"
81
+ - !ruby/object:Gem::Version
82
+ version: '3.0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: webmock
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: byebug
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ description: Ruby library for the Alpha Vantage API, a leading provider of stock APIs
112
+ as well as forex (FX) and cryptocurrency data feeds.
113
+ email:
114
+ - ateh.dev@gmail.com
115
+ executables: []
116
+ extensions: []
117
+ extra_rdoc_files: []
118
+ files:
119
+ - ".gitignore"
120
+ - ".rspec"
121
+ - ".travis.yml"
122
+ - CODE_OF_CONDUCT.md
123
+ - Gemfile
124
+ - Gemfile.lock
125
+ - LICENSE.txt
126
+ - README.md
127
+ - Rakefile
128
+ - alphavantage.gemspec
129
+ - bin/console
130
+ - bin/setup
131
+ - lib/alphavantage.rb
132
+ - lib/alphavantage/client.rb
133
+ - lib/alphavantage/configuration.rb
134
+ - lib/alphavantage/crypto.rb
135
+ - lib/alphavantage/error.rb
136
+ - lib/alphavantage/forex.rb
137
+ - lib/alphavantage/fundamental.rb
138
+ - lib/alphavantage/normalize_key.rb
139
+ - lib/alphavantage/time_series.rb
140
+ - lib/alphavantage/validations.rb
141
+ - lib/alphavantage/version.rb
142
+ homepage: https://github.com/codespore/alphavantage_ruby
143
+ licenses:
144
+ - MIT
145
+ metadata:
146
+ homepage_uri: https://github.com/codespore/alphavantage_ruby
147
+ source_code_uri: https://github.com/codespore/alphavantage_ruby
148
+ github_repo: ssh://github.com/codespore/alphavantage_ruby
149
+ bug_tracker_uri: https://github.com/codespore/alphavantage_ruby/issues
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: '0'
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubygems_version: 3.1.2
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Ruby library for Alpha Vantage API
169
+ test_files: []