Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
energypriceapi committed Jan 19, 2023
0 parents commit fed9e30
Show file tree
Hide file tree
Showing 19 changed files with 524 additions and 0 deletions.
7 changes: 7 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
.env
pkg
Gemfile.lock
.DS_Store
.bundle
.idea
*.gem
5 changes: 5 additions & 0 deletions Gemfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

source 'http://rubygems.org'

gemspec
22 changes: 22 additions & 0 deletions LICENSE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
MIT License

Copyright (c) 2023 EnergypriceAPI

Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:

The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
119 changes: 119 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,119 @@
# EnergypriceAPI

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.

## Installation
Add to Gemfile.

```
gem 'energypriceapi'
```

## Usage

```ruby

api_key = 'SET_YOUR_API_KEY_HERE'
client = EnergypriceAPI::Client.new(api_key: api_key)
```
---
## Documentation

#### fetchSymbols()
```ruby
client.fetchSymbols()
```

[Link](https://energypriceapi.com/documentation#api_symbol)

---
#### fetchLive(base, currencies)

- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.

```ruby
client.fetchLive(base='USD', currencies=['BRENT','GASOLINE','NATURALGAS','WTI'])
```

[Link](https://energypriceapi.com/documentation#api_realtime)

---
#### fetchHistorical(date, base, currencies)

- `date` <[string]> Required. Pass in a string with format `YYYY-MM-DD`
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.

```ruby
client.fetchHistorical(date='2021-04-05', base='USD', currencies=['BRENT','GASOLINE','NATURALGAS','WTI'])
```

[Link](https://energypriceapi.com/documentation#api_historical)

---
#### convert(from_currency, to_currency, amount, date)

- `from_currency` <[string]> Optional. Pass in a base currency, defaults to USD.
- `to_currency` <[string]> Required. Specify currency you would like to convert to.
- `amount` <[number]> Required. The amount to convert.
- `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.

```ruby
client.convert(from_currency='USD', to_currency='EUR', amount=100, date='2021-04-05')
```

[Link](https://energypriceapi.com/documentation#api_convert)

---
#### timeframe(start_date, end_date, base, currencies)

- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.

```ruby
client.timeframe(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['BRENT','GASOLINE','NATURALGAS','WTI'])
```

[Link](https://energypriceapi.com/documentation#api_timeframe)

---
#### change(start_date, end_date, base, currencies)

- `start_date` <[string]> Required. Specify the start date of your timeframe using the format `YYYY-MM-DD`.
- `end_date` <[string]> Required. Specify the end date of your timeframe using the format `YYYY-MM-DD`.
- `base` <[string]> Optional. Pass in a base currency, defaults to USD.
- `currencies` <[Array]<[string]>> Optional. Pass in an array of currencies to return values for.

```ruby
client.change(start_date='2021-04-05', end_date='2021-04-06', base='USD', currencies=['BRENT','GASOLINE','NATURALGAS','WTI'])
```

[Link](https://energypriceapi.com/documentation#api_change)

---
**[Official documentation](https://energypriceapi.com/documentation)**


---
## FAQ

- How do I get an API Key?

Free API Keys are available [here](https://energypriceapi.com).

- I want more information

Checkout our FAQs [here](https://energypriceapi.com/faq).


## Support

For support, get in touch using [this form](https://energypriceapi.com/contact).


[Array]: https://www.geeksforgeeks.org/ruby-data-types/ 'Array'
[number]: https://www.geeksforgeeks.org/ruby-data-types/ 'Number'
[string]: https://apidock.com/ruby/String 'String'
7 changes: 7 additions & 0 deletions Rakefile
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# frozen_string_literal: true

require 'rubygems'
require 'bundler'
require 'bundler/gem_tasks'

Bundler.setup :default, :development
17 changes: 17 additions & 0 deletions energypriceapi.gemspec
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
# frozen_string_literal: true

require 'energypriceapi/version'

Gem::Specification.new do |s|
s.name = 'energypriceapi'
s.version = EnergypriceAPI::VERSION
s.authors = ['EnergypriceAPI']
s.email = '[email protected]'
s.files = Dir['{bin,lib}/**/*'] + ['README.md', 'LICENSE.md']
s.require_paths = ['lib']
s.homepage = 'http://github.com/energypriceapi/energypriceapi-ruby'
s.licenses = ['MIT']
s.summary = 'Official EnergypriceAPI Ruby client.'
s.add_dependency 'faraday', '>= 1.0.0'
s.add_dependency 'faraday_middleware'
end
27 changes: 27 additions & 0 deletions example/index.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require_relative '../lib/energypriceapi'

API_KEY = 'REPLACE_ME'

EnergypriceAPI::VERSION

client = EnergypriceAPI::Client.new(
api_key: API_KEY
)

data = client.fetchSymbols()
puts data

data = client.fetchLive()
puts data

data = client.fetchHistorical('2021-04-05', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI'])
puts data

data = client.convert('USD', 'EUR', 100, '2021-04-05')
puts data

data = client.timeframe('2021-04-05', '2021-04-06', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI'])
puts data

data = client.change('2021-04-05', '2021-04-06', 'USD', ['BRENT','GASOLINE','NATURALGAS','WTI'])
puts data
19 changes: 19 additions & 0 deletions lib/energypriceapi.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
# frozen_string_literal: true

require 'faraday'
require 'faraday_middleware'
require 'json'
require 'logger'

require_relative 'energypriceapi/version'
require_relative 'energypriceapi/logger'

require_relative 'energypriceapi/errors/fault'

require_relative 'energypriceapi/raise_error'
require_relative 'energypriceapi/connection'
require_relative 'energypriceapi/request'
require_relative 'energypriceapi/config'
require_relative 'energypriceapi/errors'
require_relative 'energypriceapi/endpoints'
require_relative 'energypriceapi/client'
28 changes: 28 additions & 0 deletions lib/energypriceapi/client.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# frozen_string_literal: true

module EnergypriceAPI
class Client
include Connection
include Request
include Endpoints

attr_accessor(*Config::ATTRIBUTES)

def initialize(options = {})
EnergypriceAPI::Config::ATTRIBUTES.each do |key|
send("#{key}=", options[key] || EnergypriceAPI.config.send(key))
end
@logger ||= EnergypriceAPI::Logger.logger
end

class << self
def configure
block_given? ? yield(Config) : Config
end

def config
Config
end
end
end
end
45 changes: 45 additions & 0 deletions lib/energypriceapi/config.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
# frozen_string_literal: true

module EnergypriceAPI
module Config
extend self

ATTRIBUTES = %i[
endpoint
api_key
proxy
user_agent
ca_path
ca_file
logger
timeout
open_timeout
].freeze

attr_accessor(*Config::ATTRIBUTES)

def reset
self.endpoint = 'https://api.energypriceapi.com/v1'
self.api_key = nil
self.user_agent = "EnergypriceAPI Ruby Client/#{EnergypriceAPI::VERSION}"
self.ca_path = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_DIR : nil
self.ca_file = defined?(OpenSSL) ? OpenSSL::X509::DEFAULT_CERT_FILE : nil
self.proxy = nil
self.logger = nil
self.timeout = nil
self.open_timeout = nil
end
end

class << self
def configure
block_given? ? yield(Config) : Config
end

def config
Config
end
end
end

EnergypriceAPI::Config.reset
40 changes: 40 additions & 0 deletions lib/energypriceapi/connection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module EnergypriceAPI
module Connection
private

def headers
{}
end

def connection
@connection ||= begin
options = {
headers: headers.merge(
'Accept' => 'application/json; charset=utf-8',
'Content-Type' => 'application/json'
)
}

options[:headers]['User-Agent'] = user_agent if user_agent
options[:proxy] = proxy if proxy
options[:ssl] = { ca_path: ca_path, ca_file: ca_file } if ca_path || ca_file

request_options = {}
request_options[:timeout] = timeout if timeout
request_options[:open_timeout] = open_timeout if open_timeout
options[:request] = request_options if request_options.any?

::Faraday::Connection.new(endpoint, options) do |connection|
connection.use ::Faraday::Request::Multipart
connection.use ::Faraday::Request::UrlEncoded
connection.use ::EnergypriceAPI::Response::RaiseError
connection.use ::FaradayMiddleware::ParseJson, content_type: /\bjson$/
connection.response :logger, logger if logger
connection.adapter ::Faraday.default_adapter
end
end
end
end
end
3 changes: 3 additions & 0 deletions lib/energypriceapi/endpoints.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
# frozen_string_literal: true

require_relative 'endpoints/index'
Loading

0 comments on commit fed9e30

Please sign in to comment.