energypriceapi 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: 65b54f544d7055c44f25b8f9d0523c14883f2b672a10ad471a5973d24165aa55
4
+ data.tar.gz: 7b438a7173a2b0e1c74fc679b2d8c8f3161f0386929c7711f5937b1600cb32ee
5
+ SHA512:
6
+ metadata.gz: 119a4da72f106d966e4f3998b67905ffcfa0dbb3c790ff641696854ae0e8c6af61c1b6510d323ff7dc4ac1217aa1b559b021c264d9df641bdd77f927eebf4189
7
+ data.tar.gz: 908157480816b3a737aa6a3845e747358ba85a3c8ec4162bf0abe8e8b773aa655e50e58aee1a81e7e0f81a6ac8de705cc456d96be66216410de91c0599e20dd8
data/LICENSE.md ADDED
@@ -0,0 +1,22 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2023 EnergypriceAPI
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
+ # EnergypriceAPI
2
+
3
+ energypriceapi is the official Node.js wrapper for EnergypriceAPI.com. This allows you to quickly integrate our foreign exchange rate API and currency conversion API into your application. Check https://energypriceapi.com documentation for more information.
4
+
5
+ ## Installation
6
+ Add to Gemfile.
7
+
8
+ ```
9
+ gem 'energypriceapi'
10
+ ```
11
+
12
+ ## Usage
13
+
14
+ ```ruby
15
+
16
+ api_key = 'SET_YOUR_API_KEY_HERE'
17
+ client = EnergypriceAPI::Client.new(api_key: api_key)
18
+ ```
19
+ ---
20
+ ## Documentation
21
+
22
+ #### fetchSymbols()
23
+ ```ruby
24
+ client.fetchSymbols()
25
+ ```
26
+
27
+ [Link](https://energypriceapi.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=['BRENT','GASOLINE','NATURALGAS','WTI'])
37
+ ```
38
+
39
+ [Link](https://energypriceapi.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=['BRENT','GASOLINE','NATURALGAS','WTI'])
50
+ ```
51
+
52
+ [Link](https://energypriceapi.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://energypriceapi.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=['BRENT','GASOLINE','NATURALGAS','WTI'])
78
+ ```
79
+
80
+ [Link](https://energypriceapi.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=['BRENT','GASOLINE','NATURALGAS','WTI'])
92
+ ```
93
+
94
+ [Link](https://energypriceapi.com/documentation#api_change)
95
+
96
+ ---
97
+ **[Official documentation](https://energypriceapi.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://energypriceapi.com).
106
+
107
+ - I want more information
108
+
109
+ Checkout our FAQs [here](https://energypriceapi.com/faq).
110
+
111
+
112
+ ## Support
113
+
114
+ For support, get in touch using [this form](https://energypriceapi.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 EnergypriceAPI
4
+ class Client
5
+ include Connection
6
+ include Request
7
+ include Endpoints
8
+
9
+ attr_accessor(*Config::ATTRIBUTES)
10
+
11
+ def initialize(options = {})
12
+ EnergypriceAPI::Config::ATTRIBUTES.each do |key|
13
+ send("#{key}=", options[key] || EnergypriceAPI.config.send(key))
14
+ end
15
+ @logger ||= EnergypriceAPI::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 EnergypriceAPI
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.energypriceapi.com/v1'
23
+ self.api_key = nil
24
+ self.user_agent = "EnergypriceAPI Ruby Client/#{EnergypriceAPI::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
+ EnergypriceAPI::Config.reset
@@ -0,0 +1,40 @@
1
+ # frozen_string_literal: true
2
+
3
+ module EnergypriceAPI
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 ::EnergypriceAPI::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 EnergypriceAPI
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 EnergypriceAPI
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 EnergypriceAPI
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 EnergypriceAPI
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 EnergypriceAPI::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 EnergypriceAPI
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 EnergypriceAPI
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 'energypriceapi/version'
9
+ require_relative 'energypriceapi/logger'
10
+
11
+ require_relative 'energypriceapi/errors/fault'
12
+
13
+ require_relative 'energypriceapi/raise_error'
14
+ require_relative 'energypriceapi/connection'
15
+ require_relative 'energypriceapi/request'
16
+ require_relative 'energypriceapi/config'
17
+ require_relative 'energypriceapi/errors'
18
+ require_relative 'energypriceapi/endpoints'
19
+ require_relative 'energypriceapi/client'
metadata ADDED
@@ -0,0 +1,84 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: energypriceapi
3
+ version: !ruby/object:Gem::Version
4
+ version: 1.0.1
5
+ platform: ruby
6
+ authors:
7
+ - EnergypriceAPI
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@energypriceapi.com
43
+ executables: []
44
+ extensions: []
45
+ extra_rdoc_files: []
46
+ files:
47
+ - LICENSE.md
48
+ - README.md
49
+ - lib/energypriceapi.rb
50
+ - lib/energypriceapi/client.rb
51
+ - lib/energypriceapi/config.rb
52
+ - lib/energypriceapi/connection.rb
53
+ - lib/energypriceapi/endpoints.rb
54
+ - lib/energypriceapi/endpoints/index.rb
55
+ - lib/energypriceapi/errors.rb
56
+ - lib/energypriceapi/errors/fault.rb
57
+ - lib/energypriceapi/logger.rb
58
+ - lib/energypriceapi/raise_error.rb
59
+ - lib/energypriceapi/request.rb
60
+ - lib/energypriceapi/version.rb
61
+ homepage: http://github.com/energypriceapi/energypriceapi-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 EnergypriceAPI Ruby client.
84
+ test_files: []