gold_silver_api 1.0.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 96d09225cb5edbc0614fecaa5799226f8adc0f1c1fd93620e34f48323f926f38
4
+ data.tar.gz: 498889025595d3b4139b9c01cddac9041cea0be71e6366a02bb41beb13ef513e
5
+ SHA512:
6
+ metadata.gz: e33b359b17eb8218acd22fdd1da79b8c5c5304e81da0229d3865faf687974c9f6a06d008b6df402ad149a536f75dc0461e8b4fb7fc8988dd0ea599a14493275b
7
+ data.tar.gz: 3b14b6e3afd1e260591f5953ec732385bb8c8dcd8df494b90983fa9aaeb7f134cd4d317ee0cc569063ae62d2f4d908e78de441ba77b940fa9fcd61175648a295
@@ -0,0 +1 @@
1
+ .DS_Store
data/CHANGES ADDED
@@ -0,0 +1 @@
1
+ 1.0.0 Initial Release
@@ -0,0 +1,76 @@
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, sex characteristics, gender identity and expression,
9
+ level of experience, education, socio-economic status, nationality, personal
10
+ appearance, race, religion, or sexual identity and 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 andreimerlescu@protonmail.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 https://www.contributor-covenant.org/version/1/4/code-of-conduct.html
72
+
73
+ [homepage]: https://www.contributor-covenant.org
74
+
75
+ For answers to common questions about this code of conduct, see
76
+ https://www.contributor-covenant.org/faq
@@ -0,0 +1 @@
1
+ Open a pull request.
data/LICENSE ADDED
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2019 Merlescu Codez
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 all
13
+ 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 THE
21
+ SOFTWARE.
@@ -0,0 +1,214 @@
1
+ # GoldAPI Gem
2
+
3
+ If you wish to use the price of Gold and Silver on your Rails application and you wish to use GoldApi.io as your data-source, then use this gem.
4
+
5
+ ## Installation
6
+
7
+ Add this to your `Gemfile`:
8
+
9
+ ```ruby
10
+ gem 'gold_silver_api', '~> 1.0'
11
+ ```
12
+
13
+ Or install it locally to your system:
14
+
15
+ ```bash
16
+ gem install gold_silver_api
17
+ ```
18
+
19
+ ## Configuration
20
+
21
+ If you're using Rails, then add a new file `config/initializers/gold_silver_api.rb` with:
22
+
23
+ ```ruby
24
+ require 'gold_silver_api'
25
+ GoldSilver.configure do |config|
26
+ config.access_token = "goldapi-<access_token>-io"
27
+ end
28
+ ```
29
+
30
+ > Replace `goldapi-<access_token>-io` with your access token, which can be found **[On GoldApi.io](https://www.goldapi.io/dashboard)**.
31
+
32
+ ## Usage
33
+
34
+ Make a request:
35
+
36
+ ```ruby
37
+ class BullionController < ActionController::Base
38
+
39
+ def index
40
+ @gold_price = GoldSilver::API.fetch("XAU", "USD")
41
+ @gold_on_november_3rd_2020 = GoldSilver::API.fetch("XAU", "USD", "20201103")
42
+ end #/def
43
+
44
+ end #/class
45
+ ```
46
+
47
+ Here is a table of argument potential values:
48
+
49
+ | `metal` | `currency` | `date` |
50
+ |:--------|:-----------|:-------|
51
+ | `XAU` - Gold | `AED` - U.A.E. Dirham | `YYYYMMDD` |
52
+ | `XAG` - Silver | `AUD` - Austrialian Dollar | |
53
+ | `XPT` - Platinum | `BTC` - Bitcoin | |
54
+ | `XPD` - Palladium | `CAD` - Canadian Dollar | |
55
+ | | `CHF` - Swiss Franc | |
56
+ | | `CNY` - Chinese/Yuan renminbi | |
57
+ | | `CZK` - Czech Krona | |
58
+ | | `EUR` - European Euro | |
59
+ | | `GBP` - British Pound | |
60
+ | | `INR` - Indian Rupee | |
61
+ | | `JPY` - Japanese Yen | |
62
+ | | `KWD` - Kuwaiti Dinar | |
63
+ | | `MYR` - Malaysian Ringgit | |
64
+ | | `PLN` - Polish Zloty | |
65
+ | | `RUB` - Russian Ruble | |
66
+ | | `SGD` - Singapore Dollar | |
67
+ | | `THB` - Thai Baht | |
68
+ | | `USD` - United States Dollar | |
69
+ | | `XAG` - Gold/Silver Ratio | |
70
+ | | `ZAR` - South African Rand | |
71
+
72
+ > Invalid metals and currencies will be rejected automatically by raising an `ArgumentError`. **NOTE**: `date` is not validated.
73
+
74
+ ## Advanced Usage
75
+
76
+ You might want to cache your value with Redis (update the connection request to your system)...
77
+
78
+ ```ruby
79
+ class BullionController < ActionController::Base
80
+
81
+ def index
82
+
83
+ @gold_price = get_or_fetch_price("XAU", "USD") # redis.get("gold_silver_api-xau-usd")
84
+ end #/def
85
+
86
+ protected
87
+
88
+ def get_or_fetch_price metal, currency
89
+ # cert = File.open(ENV['REDIS_CERT_FILE'], 'r').read
90
+ # key = File.open(ENV['REDIS_KEY_FILE'], 'r').read
91
+ redis = Redis.new(
92
+ url: "#{ENV['REDIS_DSN']}/5",
93
+ network_timeout: 9#,
94
+ # ssl: true,
95
+ # ssl_params: {
96
+ # :ca_file => ENV['REDIS_CA_FILE'],
97
+ # :cert => OpenSSL::X509::Certificate.new(cert),
98
+ # :key => OpenSSL::PKey::RSA.new(key),
99
+ # :verify_mode => OpenSSL::SSL::VERIFY_NONE
100
+ # }
101
+ )
102
+
103
+ result = redis.get("gold_silver_api-#{metal}-#{currency}")
104
+ if result.nil?
105
+ call = GoldSilver::API.fetch(metal, currency)
106
+ unless call.nil?
107
+ result = call[:result]
108
+ unless result.nil?
109
+ redis.set("gold_silver_api-#{metal}-#{currency}", result[:price])
110
+ return result[:price]
111
+ else
112
+ return nil
113
+ end #/unless-else
114
+ else
115
+ return nil
116
+ end #/unless-else
117
+ else
118
+ return result
119
+ end #/if-else
120
+ end #/def
121
+ end #/class
122
+ ```
123
+
124
+ ## Response from API
125
+
126
+ Request `GoldSilver::API.fetch("XAU", "USD")` to get the price of Gold (Au) in US Dollars (USD) will return with the following result
127
+
128
+ ```json
129
+ {
130
+ "timestamp":1608354836,
131
+ "metal":"XAU",
132
+ "currency":"USD",
133
+ "exchange":"FOREXCOM",
134
+ "symbol":"FOREXCOM:XAUUSD",
135
+ "prev_close_price":1885.56,
136
+ "open_price":1885.56,
137
+ "low_price":1877.53,
138
+ "high_price":1889.71,
139
+ "open_time":1608242400,
140
+ "price":1881.24,
141
+ "ch":-4.32,
142
+ "chp":-0.23,
143
+ "ask":1882.28,
144
+ "bid":1880.28
145
+ }
146
+ ```
147
+
148
+ To access the price:
149
+
150
+ ```ruby
151
+ gold_price_usd = nil
152
+ call = GoldSilver::API.fetch("XAU", "USD")
153
+ if call
154
+ result = call[:result]
155
+ if result
156
+ gold_price_usd = result["price"]
157
+ end #/if
158
+ end #/if
159
+ if gold_price_usd.nil?
160
+ # error we were unable to get the price
161
+ end #/if
162
+ # use gold_price_usd as much as you wish
163
+ ```
164
+
165
+ ## Testing
166
+
167
+ ```bash
168
+ ruby spec/gold_silver_api_spec.rb
169
+ ```
170
+
171
+ Expected output should be:
172
+
173
+ ```
174
+ Run options: --seed 21855
175
+
176
+ # Running:
177
+
178
+ .
179
+
180
+ Finished in 0.171850s, 5.8190 runs/s, 5.8190 assertions/s.
181
+ 1 runs, 1 assertions, 0 failures, 0 errors, 0 skips
182
+ ```
183
+
184
+ ## Errors
185
+
186
+ If you are missing the `access_token`, then if you attempt to run the tests, you might get the following kind of output:
187
+
188
+ ```
189
+ Run options: --seed 17828
190
+
191
+ # Running:
192
+
193
+ F
194
+
195
+ Failure:
196
+ GoldSilver::Interact with GoldApi.io#test_0001_should return the current price for gold [spec/gold_silver_api_spec.rb:14]:
197
+ Expected nil to be an instance of Float, not NilClass.
198
+
199
+
200
+ rails test spec/gold_silver_api_spec.rb:12
201
+
202
+
203
+
204
+ Finished in 0.151836s, 6.5861 runs/s, 6.5861 assertions/s.
205
+ 1 runs, 1 assertions, 1 failures, 0 errors, 0 skips
206
+ ```
207
+
208
+ ## Credit
209
+
210
+ This gem was created by [Andrei Merlescu](https://github.com/sponsors/patriotphoenix).
211
+
212
+ ## License
213
+
214
+ [MIT License](LICENSE)
@@ -0,0 +1,18 @@
1
+ # Security Policy
2
+
3
+ ## Supported Versions
4
+
5
+ Use this section to tell people about which versions of your project are
6
+ currently being supported with security updates.
7
+
8
+ | Version | Supported |
9
+ | ------- | ------------------ |
10
+ | > 1.0.1 | :x: |
11
+
12
+ ## Reporting a Vulnerability
13
+
14
+ Use this section to tell people how to report a vulnerability.
15
+
16
+ Tell them where to go, how often they can expect to get an update on a
17
+ reported vulnerability, what to expect if the vulnerability is accepted or
18
+ declined, etc.
@@ -0,0 +1,17 @@
1
+ # require File.expand_path('../lib/gold_silver_api/version', __FILE__)
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "gold_silver_api/version"
4
+
5
+
6
+ Gem::Specification.new do |s|
7
+ s.authors = ["Andrei Merlescu"]
8
+ s.email = ["andrei+github@merlescu.net"]
9
+ s.description = s.summary = "Simple interface to access GoldApi.io"
10
+ s.homepage = "https://github.com/amerlescucodez/goldsilver-api"
11
+ s.license = "MIT"
12
+ s.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
13
+ s.name = "gold_silver_api"
14
+ s.require_paths = ["lib"]
15
+ s.version = GoldSilver::VERSION
16
+ s.add_runtime_dependency 'logging', '~> 2.2', '>= 2.2.2'
17
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+ require 'logging'
3
+
4
+ module GoldSilver
5
+ end
6
+
7
+ begin
8
+ require 'rails'
9
+ rescue LoadError
10
+ # do nothing
11
+ end #/begin-rescue
12
+
13
+ require_relative 'gold_silver_api/config'
14
+ require_relative 'gold_silver_api/logger'
15
+ require_relative 'gold_silver_api/api'
16
+
17
+ if defined? ::Rails::Railtie
18
+ require_relative 'gold_silver_api/railtie'
19
+ end #/if
@@ -0,0 +1,46 @@
1
+ require 'net/http'
2
+ require 'json'
3
+ require_relative 'consts'
4
+
5
+ module GoldSilver
6
+ class API
7
+ class << self
8
+ def fetch metal, currency, date=nil
9
+ raise ArgumentError, "missing metal argument" if metal.nil?
10
+ raise ArgumentError, "invalid metal argument" unless GSAPI_METALS.include?(metal)
11
+ raise ArgumentError, "missing currency argument" if currency.nil?
12
+ raise ArgumentError, "invalid currency argument" unless GSAPI_CURRENCIES.include?(currency)
13
+
14
+ GoldSilver.logger.debug "Attempting to fetch #{metal} in #{currency}."
15
+
16
+ uri = nil
17
+ if date
18
+ uri = URI.parse("https://www.goldapi.io/api/#{metal&.upcase}/#{currency&.upcase}/#{date&.upcase}")
19
+ else
20
+ uri = URI.parse("https://www.goldapi.io/api/#{metal&.upcase}/#{currency&.upcase}")
21
+ end #/if-else
22
+
23
+ response = nil
24
+ result = nil
25
+ Net::HTTP.start(uri.host, uri.port, use_ssl: GoldSilver.config.use_ssl) do |http|
26
+ req = Net::HTTP::Get.new(uri)
27
+ req['Content-Type'] = 'application/json'
28
+ req['x-access-token'] = GoldSilver.config.access_token
29
+ response = http.request(req)
30
+ result = response&.body
31
+ end #/block
32
+
33
+ {
34
+ success: response.instance_of?(Net::HTTPOK),
35
+ response: response,
36
+ result: JSON.parse(result)
37
+ }
38
+ # rescue
39
+ # {
40
+ # success: false,
41
+ # response: nil
42
+ # }
43
+ end #/def
44
+ end #/class
45
+ end #/class
46
+ end #/class
@@ -0,0 +1,21 @@
1
+ module GoldSilver
2
+ class << self
3
+ def configure
4
+ yield config
5
+ end #/def
6
+
7
+ def config
8
+ @_config ||= Config.new
9
+ end #/def
10
+ end #/class
11
+
12
+ class Config
13
+ attr_accessor :access_token
14
+ attr_accessor :use_ssl
15
+
16
+ def initialize
17
+ @access_token = nil
18
+ @use_ssl = true
19
+ end #/def
20
+ end #/class
21
+ end #/module
@@ -0,0 +1,2 @@
1
+ GSAPI_METALS=["XAU", "XAG", "XPT", "XPD"]
2
+ GSAPI_CURRENCIES=['AED', 'AUD', 'BTC', 'CAD', 'CHF', 'CNY', 'CZK', 'EUR', 'GBP', 'INR', 'JPY', 'KWD', 'MYR', 'PLN', 'RUB', 'SGD', 'THB', 'USD', 'XAG', 'ZAR']
@@ -0,0 +1,10 @@
1
+ require 'logging'
2
+ module GoldSilver
3
+ class << self
4
+ def logger
5
+ @logger ||= Logging.logger(STDOUT)
6
+ @logger.level = :warn
7
+ return @logger
8
+ end #/def
9
+ end #/class
10
+ end #/module
@@ -0,0 +1,9 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GoldSilver
4
+ class Railtie < ::Rails::Railtie #:nodoc:
5
+ # Doesn't actually do anything. Just keeping this hook point, mainly for compatibility
6
+ initializer 'gold_silver' do
7
+ end
8
+ end
9
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module GoldSilver
4
+ VERSION = '1.0.0'
5
+ end #/module
metadata ADDED
@@ -0,0 +1,78 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: gold_silver_api
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.0
5
+ platform: ruby
6
+ authors:
7
+ - Andrei Merlescu
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-12-19 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: logging
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '2.2'
20
+ - - ">="
21
+ - !ruby/object:Gem::Version
22
+ version: 2.2.2
23
+ type: :runtime
24
+ prerelease: false
25
+ version_requirements: !ruby/object:Gem::Requirement
26
+ requirements:
27
+ - - "~>"
28
+ - !ruby/object:Gem::Version
29
+ version: '2.2'
30
+ - - ">="
31
+ - !ruby/object:Gem::Version
32
+ version: 2.2.2
33
+ description: Simple interface to access GoldApi.io
34
+ email:
35
+ - andrei+github@merlescu.net
36
+ executables: []
37
+ extensions: []
38
+ extra_rdoc_files: []
39
+ files:
40
+ - ".gitignore"
41
+ - CHANGES
42
+ - CODE_OF_CONDUCT.md
43
+ - CONTRIBUTING.md
44
+ - LICENSE
45
+ - README.md
46
+ - SECURITY.md
47
+ - gold_silver_api.gemspec
48
+ - lib/gold_silver_api.rb
49
+ - lib/gold_silver_api/api.rb
50
+ - lib/gold_silver_api/config.rb
51
+ - lib/gold_silver_api/consts.rb
52
+ - lib/gold_silver_api/logger.rb
53
+ - lib/gold_silver_api/railtie.rb
54
+ - lib/gold_silver_api/version.rb
55
+ homepage: https://github.com/amerlescucodez/goldsilver-api
56
+ licenses:
57
+ - MIT
58
+ metadata: {}
59
+ post_install_message:
60
+ rdoc_options: []
61
+ require_paths:
62
+ - lib
63
+ required_ruby_version: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - ">="
66
+ - !ruby/object:Gem::Version
67
+ version: '0'
68
+ required_rubygems_version: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - ">="
71
+ - !ruby/object:Gem::Version
72
+ version: '0'
73
+ requirements: []
74
+ rubygems_version: 3.0.3
75
+ signing_key:
76
+ specification_version: 4
77
+ summary: Simple interface to access GoldApi.io
78
+ test_files: []