forexrateapi 1.0.1

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.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: '09786c38fab46aa7cc8a8d4563d5d19557843ed5487ab87fced0467f08b367bd'
4
+ data.tar.gz: da6ec735343f79837df80ab761df8eebad9b380b798086dfbfa08ac25bb3bb0c
5
+ SHA512:
6
+ metadata.gz: 5dd1698e4512b742a5a7fc68a66a62776d45af15c283fd0716f47c88a36eeca50b0761cb1aa5c29ce15b908f3b59ebbd4d3154504adef47693c88e095584fbf7
7
+ data.tar.gz: 6e77fd48b33cdb459d05a06b6a5b41eabe281128066317acd483e9e305af89302a91e84c0ff1e0847a837e3f19e1cd6c69c86511623df2883ff1ffed58caab31
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 ForexRateAPI
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining
6
+ a copy of this software and associated documentation files (the
7
+ "Software"), to deal in the Software without restriction, including
8
+ without limitation the rights to use, copy, modify, merge, publish,
9
+ distribute, sublicense, and/or sell copies of the Software, and to
10
+ permit persons to whom the Software is furnished to do so, subject to
11
+ the following conditions:
12
+
13
+ The above copyright notice and this permission notice shall be
14
+ included in all copies or substantial portions of the Software.
15
+
16
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
+ EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18
+ MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
+ NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
20
+ LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
21
+ OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
22
+ WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,119 @@
1
+ # ForexRateAPI
2
+
3
+ forexrateapi is the official Node.js wrapper for ForexRateAPI.com. This allows you to quickly integrate our foreign exchange rate API and currency conversion API into your application. Check https://forexrateapi.com documentation for more information.
4
+
5
+ ## Installation
6
+ Add to Gemfile.
7
+
8
+ ```
9
+ gem 'forexrateapi'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ruby
15
+
16
+ api_key = 'SET_YOUR_API_KEY_HERE'
17
+ client = ForexRateAPI::Client.new(api_key: api_key)
18
+ ```
19
+ ---
20
+ ## Documentation
21
+
22
+ #### fetchSymbols()
23
+ ```ruby
24
+ client.fetchSymbols()
25
+ ```
26
+
27
+ [Link](https://forexrateapi.com/documentation#api_symbol)
28
+
29
+ ---
30
+ #### fetchLive(base, currencies)
31
+
32
+ - `base` <[string]> Optional. Pass in a base currency, defaults to USD.
33
+ - `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
34
+
35
+ ```ruby
36
+ client.fetchLive(base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
37
+ ```
38
+
39
+ [Link](https://forexrateapi.com/documentation#api_realtime)
40
+
41
+ ---
42
+ #### fetchHistorical(date, base, currencies)
43
+
44
+ - `date` <[string]> Required. Pass in a string with format `YYYY-MM-DD`
45
+ - `base` <[string]> Optional. Pass in a base currency, defaults to USD.
46
+ - `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
47
+
48
+ ```ruby
49
+ client.fetchHistorical(date='2021-04-05', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
50
+ ```
51
+
52
+ [Link](https://forexrateapi.com/documentation#api_historical)
53
+
54
+ ---
55
+ #### convert(from_currency, to_currency, amount, date)
56
+
57
+ - `from_currency` <[string]> Optional. Pass in a base currency, defaults to USD.
58
+ - `to_currency` <[string]> Required. Specify currency you would like to convert to.
59
+ - `amount` <[number]> Required. The amount to convert.
60
+ - `date` <[string]> Optional. Specify date to use historical midpoint value for conversion with format `YYYY-MM-DD`. Otherwise, it will use live exchange rate date if value not passed in.
61
+
62
+ ```ruby
63
+ client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2021-04-05')
64
+ ```
65
+
66
+ [Link](https://forexrateapi.com/documentation#api_convert)
67
+
68
+ ---
69
+ #### timeframe(start_date, end_date, base, currencies)
70
+
71
+ - `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
72
+ - `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
73
+ - `base` <[string]> Optional. Pass in a base currency, defaults to USD.
74
+ - `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
75
+
76
+ ```ruby
77
+ client.timeframe(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
78
+ ```
79
+
80
+ [Link](https://forexrateapi.com/documentation#api_timeframe)
81
+
82
+ ---
83
+ #### change(start_date, end_date, base, currencies)
84
+
85
+ - `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
86
+ - `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
87
+ - `base` <[string]> Optional. Pass in a base currency, defaults to USD.
88
+ - `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.
89
+
90
+ ```ruby
91
+ client.change(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['AUD', 'CAD', 'GBP', 'JPY'])
92
+ ```
93
+
94
+ [Link](https://forexrateapi.com/documentation#api_change)
95
+
96
+ ---
97
+ **[Official documentation](https://forexrateapi.com/documentation)**
98
+
99
+
100
+ ---
101
+ ## FAQ
102
+
103
+ - How do I get an API Key?
104
+
105
+ Free API Keys are available [here](https://forexrateapi.com).
106
+
107
+ - I want more information
108
+
109
+ Checkout our FAQs [here](https://forexrateapi.com/faq).
110
+
111
+
112
+ ## Support
113
+
114
+ For support, get in touch using [this form](https://forexrateapi.com/contact).
115
+
116
+
117
+ [Array]: https://www.geeksforgeeks.org/ruby-data-types/ 'Array'
118
+ [number]: https://www.geeksforgeeks.org/ruby-data-types/ 'Number'
119
+ [string]: https://apidock.com/ruby/String 'String'
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ class Client
5
+ include Connection
6
+ include Request
7
+ include Endpoints
8
+
9
+ attr_accessor(*Config::ATTRIBUTES)
10
+
11
+ def initialize(options = {})
12
+ ForexRateAPI::Config::ATTRIBUTES.each do |key|
13
+ send("#{key}=", options[key] || ForexRateAPI.config.send(key))
14
+ end
15
+ @logger ||= ForexRateAPI::Logger.logger
16
+ end
17
+
18
+ class << self
19
+ def configure
20
+ block_given? ? yield(Config) : Config
21
+ end
22
+
23
+ def config
24
+ Config
25
+ end
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,45 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ module Config
5
+ extend self
6
+
7
+ ATTRIBUTES = %i[
8
+ endpoint
9
+ api_key
10
+ proxy
11
+ user_agent
12
+ ca_path
13
+ ca_file
14
+ logger
15
+ timeout
16
+ open_timeout
17
+ ].freeze
18
+
19
+ attr_accessor(*Config::ATTRIBUTES)
20
+
21
+ def reset
22
+ self.endpoint = 'https://api.forexrateapi.com/v1'
23
+ self.api_key = nil
24
+ self.user_agent = "ForexRateAPI Ruby Client/#{ForexRateAPI::VERSION}"
25
+ self.ca_path = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_DIR : nil
26
+ self.ca_file = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_FILE : nil
27
+ self.proxy = nil
28
+ self.logger = nil
29
+ self.timeout = nil
30
+ self.open_timeout = nil
31
+ end
32
+ end
33
+
34
+ class << self
35
+ def configure
36
+ block_given? ? yield(Config) : Config
37
+ end
38
+
39
+ def config
40
+ Config
41
+ end
42
+ end
43
+ end
44
+
45
+ ForexRateAPI::Config.reset
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ module Connection
5
+ private
6
+
7
+ def headers
8
+ {}
9
+ end
10
+
11
+ def connection
12
+ @connection ||= begin
13
+ options = {
14
+ headers: headers.merge(
15
+ 'Accept' => 'application/json; charset=utf-8',
16
+ 'Content-Type' => 'application/json'
17
+ )
18
+ }
19
+
20
+ options[:headers]['User-Agent'] = user_agent if user_agent
21
+ options[:proxy] = proxy if proxy
22
+ options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file
23
+
24
+ request_options = {}
25
+ request_options[:timeout] = timeout if timeout
26
+ request_options[:open_timeout] = open_timeout if open_timeout
27
+ options[:request] = request_options if request_options.any?
28
+
29
+ ::Faraday::Connection.new(endpoint, options) do |connection|
30
+ connection.use ::Faraday::Request::Multipart
31
+ connection.use ::Faraday::Request::UrlEncoded
32
+ connection.use ::ForexRateAPI::Response::RaiseError
33
+ connection.use ::FaradayMiddleware::ParseJson, content_type: /\bjson$/
34
+ connection.response :logger, logger if logger
35
+ connection.adapter ::Faraday.default_adapter
36
+ end
37
+ end
38
+ end
39
+ end
40
+ end
@@ -0,0 +1,66 @@
1
+ # # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ module Endpoints
5
+ def fetchSymbols
6
+ get('symbols')
7
+ end
8
+
9
+ def fetchLive(base = nil, currencies = nil)
10
+ options = removeEmpty({
11
+ base: base,
12
+ currencies: (currencies || []).join(',')
13
+ })
14
+ get('latest', options)
15
+ end
16
+
17
+ def fetchHistorical(date, base = nil, currencies = nil)
18
+ options = removeEmpty({
19
+ base: base,
20
+ currencies: (currencies || []).join(',')
21
+ })
22
+ get(date, options)
23
+ end
24
+
25
+ def convert(from_currency = nil, to_currency = nil, amount = nil, date = nil)
26
+ options = removeEmpty({
27
+ 'from': from_currency,
28
+ 'to': to_currency,
29
+ 'amount': amount,
30
+ 'date': date
31
+ })
32
+ get('convert', options)
33
+ end
34
+
35
+ def timeframe(start_date, end_date, base = nil, currencies = nil)
36
+ options = removeEmpty({
37
+ 'start_date': start_date,
38
+ 'end_date': end_date,
39
+ 'base': base,
40
+ 'currencies': (currencies || []).join(',')
41
+ })
42
+ get('timeframe', options)
43
+ end
44
+
45
+ def change(start_date, end_date, base = '', currencies = nil)
46
+ options = removeEmpty({
47
+ 'start_date': start_date,
48
+ 'end_date': end_date,
49
+ 'base': base,
50
+ 'currencies': (currencies || []).join(',')
51
+ })
52
+ get('change', options)
53
+ end
54
+
55
+ private
56
+
57
+ def removeEmpty(options)
58
+ options.each do |key, value|
59
+ if (!value || value == '')
60
+ options.delete(key)
61
+ end
62
+ end
63
+ options
64
+ end
65
+ end
66
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'endpoints/index'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ module Errors
5
+ class Fault < ::Faraday::ClientError
6
+ def message
7
+ response[:body]['message'] || super
8
+ end
9
+
10
+ def headers
11
+ response[:headers]
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,3 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative 'errors/fault'
@@ -0,0 +1,15 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'logger'
4
+
5
+ module ForexRateAPI
6
+ class Logger < ::Logger
7
+ def self.logger
8
+ @logger ||= begin
9
+ logger = new STDOUT
10
+ logger.level = Logger::WARN
11
+ logger
12
+ end
13
+ end
14
+ end
15
+ end
@@ -0,0 +1,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ module Response
5
+ class RaiseError < ::Faraday::Response::Middleware
6
+ def on_complete(env)
7
+ case env[:status]
8
+ when 404
9
+ raise Faraday::ResourceNotFound, response_values(env)
10
+ when 407
11
+ # mimic the behavior that we get with proxy requests with HTTPS
12
+ raise Faraday::ConnectionFailed, %(407 "Proxy Authentication Required ")
13
+ when (400...600).freeze
14
+ raise ForexRateAPI::Errors::Fault, response_values(env)
15
+ end
16
+ end
17
+
18
+ def response_values(env)
19
+ {
20
+ status: env.status,
21
+ headers: env.response_headers,
22
+ body: env.body
23
+ }
24
+ end
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ module Request
5
+ def get(path, options = {})
6
+ request(:get, path, options)
7
+ end
8
+
9
+ def post(path, options = {})
10
+ request(:post, path, options)
11
+ end
12
+
13
+ def put(path, options = {})
14
+ request(:put, path, options)
15
+ end
16
+
17
+ def delete(path, options = {})
18
+ request(:delete, path, options)
19
+ end
20
+
21
+ private
22
+
23
+ #
24
+ # @param [Symbol] method - Faraday HTTP method.
25
+ # @param [String] path - URL to send.
26
+ # @param [Hash] options - :appid, :lang, :units, :endpoint, :body keys will configure the request.
27
+ # The rest will be converted to query params for GET/DELETE, or jsonified for POST/PUT.
28
+ #
29
+ # @return [Object] - the Faraday::Response#body.
30
+ #
31
+ def request(method, path, options)
32
+ options = options.dup
33
+ options[:api_key] ||= api_key if !api_key.nil?
34
+ root = options.delete(:endpoint) || endpoint
35
+ path = [root, path].join('/')
36
+ response = connection.send(method) do |request|
37
+ case method
38
+ when :get, :delete
39
+ request.url(path, options)
40
+ when :post, :put
41
+ request.path = path
42
+ request.params = { appid: options.delete(:appid) }
43
+ if options.key?(:body)
44
+ request.body = options.delete(:body).to_json
45
+ elsif !options.empty?
46
+ request.body = options.to_json
47
+ end
48
+ end
49
+ request.options.merge!(options.delete(:request)) if options.key?(:request)
50
+ end
51
+ response.body
52
+ end
53
+ end
54
+ end
@@ -0,0 +1,5 @@
1
+ # frozen_string_literal: true
2
+
3
+ module ForexRateAPI
4
+ VERSION = '1.0.1'
5
+ end
@@ -0,0 +1,19 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'faraday'
4
+ require 'faraday_middleware'
5
+ require 'json'
6
+ require 'logger'
7
+
8
+ require_relative 'forexrateapi/version'
9
+ require_relative 'forexrateapi/logger'
10
+
11
+ require_relative 'forexrateapi/errors/fault'
12
+
13
+ require_relative 'forexrateapi/raise_error'
14
+ require_relative 'forexrateapi/connection'
15
+ require_relative 'forexrateapi/request'
16
+ require_relative 'forexrateapi/config'
17
+ require_relative 'forexrateapi/errors'
18
+ require_relative 'forexrateapi/endpoints'
19
+ require_relative 'forexrateapi/client'
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: forexrateapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - ForexRateAPI
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2023-01-19 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.0.0
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - ">="
25
+ - !ruby/object:Gem::Version
26
+ version: 1.0.0
27
+ - !ruby/object:Gem::Dependency
28
+ name: faraday_middleware
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ description:
42
+ email: contact@forexrateapi.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE.md
48
+ - README.md
49
+ - lib/forexrateapi.rb
50
+ - lib/forexrateapi/client.rb
51
+ - lib/forexrateapi/config.rb
52
+ - lib/forexrateapi/connection.rb
53
+ - lib/forexrateapi/endpoints.rb
54
+ - lib/forexrateapi/endpoints/index.rb
55
+ - lib/forexrateapi/errors.rb
56
+ - lib/forexrateapi/errors/fault.rb
57
+ - lib/forexrateapi/logger.rb
58
+ - lib/forexrateapi/raise_error.rb
59
+ - lib/forexrateapi/request.rb
60
+ - lib/forexrateapi/version.rb
61
+ homepage: http://github.com/forexrateapi/forexrateapi-ruby
62
+ licenses:
63
+ - MIT
64
+ metadata: {}
65
+ post_install_message:
66
+ rdoc_options: []
67
+ require_paths:
68
+ - lib
69
+ required_ruby_version: !ruby/object:Gem::Requirement
70
+ requirements:
71
+ - - ">="
72
+ - !ruby/object:Gem::Version
73
+ version: '0'
74
+ required_rubygems_version: !ruby/object:Gem::Requirement
75
+ requirements:
76
+ - - ">="
77
+ - !ruby/object:Gem::Version
78
+ version: '0'
79
+ requirements: []
80
+ rubygems_version: 3.1.6
81
+ signing_key:
82
+ specification_version: 4
83
+ summary: Official ForexRateAPI Ruby client.
84
+ test_files: []