Skip to content

Commit

Permalink
Commit
Browse files Browse the repository at this point in the history
Initial commit

remove
  • Loading branch information
adamcarlile committed Feb 7, 2023
1 parent c48630e commit 7118197
Show file tree
Hide file tree
Showing 26 changed files with 406 additions and 22 deletions.
1 change: 1 addition & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,7 @@ GEM
concurrent-ruby (~> 1.0)

PLATFORMS
arm64-darwin-21
x86_64-darwin-20

DEPENDENCIES
Expand Down
52 changes: 45 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# Cephalopod

Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/cephalopod`. To experiment with that code, run `bin/console` for an interactive prompt.

TODO: Delete this and the text above, and describe your gem
Cephalopod is an Octopus, and also a gem designed to query the Octopus Energy REST API.

## Installation

Expand All @@ -16,8 +14,48 @@ If bundler is not being used to manage dependencies, install the gem by executin

## Usage

TODO: Write usage instructions here

### Configuration

```ruby
Cephalopod.configure do |config|
config[:api_key] = "Your API key"
config[:base_url] = "https://api.octopus.energy"
end
```

You will need an Octopus API key, this can be generated from your customer dashboard: https://octopus.energy/dashboard/developer/

### Runtime

Products and tariffs can be retrieved
```ruby
products = Cephalopod.products(query: {is_variable: true, available_at: DateTime.now})
# Available query params:
# is_variable (boolean, optional)
# is_green (boolean, optional)
# is_tracker (boolean, optional)
# is_prepay (boolean, optional)
# is_business (boolean, default: false)
# available_at (datetime, default: now)

detailed_product = products.first.details
```
However to make sense of the tariffs that are available to you, you will need to determine your GSP (Grid Supply Point), which is a key like `_J`. These GSP's are based on your geographic location, as provided by a postcode lookup
```ruby
gsp = Cephalopod.grid_supply_point(postcode: "W1D 1NN")
```

You can also grab consumption data, if supported on your account
```ruby
electrical_consumption = Cephalopod.electrical_consumption(mpan: "MPAN", serial_number: "METER SERIAL", query: {})
gas_consumption = Cephalopod.gas_consumption(mpan: "MPAN", serial_number: "METER SERIAL", query: {})
# Available query params
# period_from (datetime, optional)
# period_to (datetime, optional)
# page_size (int, optional)
# order_by (string, optional)
# group_by (string, optional)
```
## Development

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.
Expand All @@ -26,12 +64,12 @@ To install this gem onto your local machine, run `bundle exec rake install`. To

## Contributing

Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/cephalopod. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/cephalopod/blob/master/CODE_OF_CONDUCT.md).
Bug reports and pull requests are welcome on GitHub at https://github.com/adamcarlile/cephalopod. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/adamcarlile/cephalopod/blob/master/CODE_OF_CONDUCT.md).

## License

The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).

## Code of Conduct

Everyone interacting in the Cephalopod project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/cephalopod/blob/master/CODE_OF_CONDUCT.md).
Everyone interacting in the Cephalopod project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/adamcarlile/cephalopod/blob/master/CODE_OF_CONDUCT.md).
43 changes: 41 additions & 2 deletions lib/cephalopod.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'dry-struct'
require 'faraday'
require "mustermann"
require 'pry'

require "cephalopod/version"

Expand All @@ -13,10 +12,33 @@

require "cephalopod/models/base"
require "cephalopod/models/link"
require "cephalopod/models/charge"
require "cephalopod/models/products/dual_register_charge"
require "cephalopod/models/products/single_register_charge"
require "cephalopod/models/product_summary"
require "cephalopod/models/product"
require "cephalopod/models/meter_point"
require "cephalopod/models/consumption"
require "cephalopod/models/gsp"

require "cephalopod/repos/base"
require "cephalopod/repos/v1/products/collection"
require "cephalopod/repos/v1/products/resource"

require "cephalopod/repos/v1/products/tariffs/electricity/day_unit_rates/collection"
require "cephalopod/repos/v1/products/tariffs/electricity/night_unit_rates/collection"
require "cephalopod/repos/v1/products/tariffs/electricity/standard_unit_rates/collection"
require "cephalopod/repos/v1/products/tariffs/electricity/standing_charges/collection"
require "cephalopod/repos/v1/products/tariffs/gas/standard_unit_rates/collection"
require "cephalopod/repos/v1/products/tariffs/gas/standing_charges/collection"

require "cephalopod/repos/v1/electricity_meter_points/resource"
require "cephalopod/repos/v1/electricity_meter_points/consumption/collection"
require "cephalopod/repos/v1/gas_meter_points/resource"
require "cephalopod/repos/v1/gas_meter_points/consumption/collection"

require "cephalopod/repos/v1/gsp/resource"


module Cephalopod
class Error < StandardError; end
Expand All @@ -29,11 +51,28 @@ def configure(&block)
def config
@config ||= {
base_url: 'https://api.octopus.energy',
api_key: 'sk_live_w823Gn24vek120vYwZUBBPMp'
api_key: 'api_key'
}
end

def client
@client ||= Cephalopod::Adaptor.new(**config)
end

def products(*args)
Cephalopod::Repos::V1::Products::Collection.new.get(*args)
end

def electrical_consumption(mpan:, serial_number:, **args)
Cephalopod::Repos::V1::ElectricityMeterPoints::Consumption::Collection.new.get(mpan: mpan, serial_number: serial_number, **args)
end

def gas_consumption(mpan:, serial_number:, **args)
Cephalopod::Repos::V1::GasMeterPoints::Consumption::Collection.new.get(mpan: mpan, serial_number: serial_number, **args)
end

def grid_supply_point(postcode:)
Cephalopod::Repos::V1::GSP::Resource.new.get(query: {postcode: postcode})
end

end
12 changes: 12 additions & 0 deletions lib/cephalopod/models/charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module Cephalopod
module Models
class Charge < Base

attribute :value_exc_vat, Types::Strict::Float
attribute :value_inc_vat, Types::Strict::Float
attribute :valid_from, Types::JSON::DateTime
attribute :valid_to, Types::JSON::DateTime

end
end
end
11 changes: 11 additions & 0 deletions lib/cephalopod/models/consumption.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
module Cephalopod
module Models
class Consumption < Base

attribute :consumption, Types::Strict::Float
attribute :interval_start, Types::JSON::DateTime
attribute :interval_end, Types::JSON::DateTime

end
end
end
10 changes: 10 additions & 0 deletions lib/cephalopod/models/gsp.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
module Cephalopod
module Models
class GSP < Base

attribute :group_id, Types::Strict::String

end
end
end

Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Cephalopod
module Models
class ElectricityMeterPoint < Base
class MeterPoint < Base

attribute :gsp, Types::Strict::String
attribute :mpan, Types::Strict::String
Expand Down
20 changes: 11 additions & 9 deletions lib/cephalopod/models/product.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ module Models
class Product < Base

attribute :code, Types::Strict::String
attribute :direction, Types::Strict::String
attribute :full_name, Types::Strict::String
attribute :display_name, Types::Strict::String
attribute :description, Types::Strict::String
Expand All @@ -17,16 +16,19 @@ class Product < Base
attribute :brand, Types::Strict::String
attribute :available_from, Types::Params::DateTime
attribute :available_to, Types::Params::DateTime.optional
attribute :links, Types::Strict::Array.of(Cephalopod::Models::Link)
attribute :tariffs_active_at, Types::Params::DateTime.optional
attribute :single_register_electricity_tariffs, Types::Params::Hash.map(Types::Strict::Symbol, Types::Params::Hash.map(Types::Strict::Symbol, Cephalopod::Models::Products::SingleRegisterCharge)).optional
attribute :dual_register_electricity_tariffs, Types::Params::Hash.map(Types::Strict::Symbol, Types::Params::Hash.map(Types::Strict::Symbol, Cephalopod::Models::Products::DualRegisterCharge)).optional
attribute :single_register_gas_tariffs, Types::Params::Hash.map(Types::Strict::Symbol, Types::Params::Hash.map(Types::Strict::Symbol, Cephalopod::Models::Products::SingleRegisterCharge)).optional

# attribute :tariffs_active_at, Types::Params::DateTime.optional
# attribute :single_register_electricity_tariffs, Types::Params::Hash.map(Types::Strict::String, Types::Strict::String).optional
attribute :links, Types::Strict::Array.of(Cephalopod::Models::Link)

private

def full_resource
links.detect {|x| x.self? }
end
alias_method :variable?, :is_variable
alias_method :green?, :is_green
alias_method :tracker?, :is_tracker
alias_method :prepay?, :is_prepay
alias_method :business?, :is_business
alias_method :is_restricted?, :is_restricted

end
end
Expand Down
35 changes: 35 additions & 0 deletions lib/cephalopod/models/product_summary.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
module Cephalopod
module Models
class ProductSummary < Base

attribute :code, Types::Strict::String
attribute :direction, Types::Strict::String
attribute :full_name, Types::Strict::String
attribute :display_name, Types::Strict::String
attribute :description, Types::Strict::String
attribute :is_variable, Types::Strict::Bool
attribute :is_green, Types::Strict::Bool
attribute :is_tracker, Types::Strict::Bool
attribute :is_prepay, Types::Strict::Bool
attribute :is_business, Types::Strict::Bool
attribute :is_restricted, Types::Strict::Bool
attribute :term, Types::Integer.optional
attribute :brand, Types::Strict::String
attribute :available_from, Types::Params::DateTime
attribute :available_to, Types::Params::DateTime.optional
attribute :links, Types::Strict::Array.of(Cephalopod::Models::Link)

def details
@details ||= Cephalopod::Repos::V1::Products::Resource.new.get(code: code)
end

alias_method :variable?, :is_variable
alias_method :green?, :is_green
alias_method :tracker?, :is_tracker
alias_method :prepay?, :is_prepay
alias_method :business?, :is_business
alias_method :is_restricted?, :is_restricted

end
end
end
26 changes: 26 additions & 0 deletions lib/cephalopod/models/products/dual_register_charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module Cephalopod
module Models
module Products
class DualRegisterCharge < Base

attribute :code, Types::Strict::String
attribute :standing_charge_exc_vat, Types::Coercible::Float
attribute :standing_charge_inc_vat, Types::Coercible::Float
attribute :online_discount_exc_vat, Types::Coercible::Float
attribute :online_discount_inc_vat, Types::Coercible::Float
attribute :dual_fuel_discount_exc_vat, Types::Coercible::Float
attribute :dual_fuel_discount_inc_vat, Types::Coercible::Float
attribute :exit_fees_exc_vat, Types::Coercible::Float
attribute :exit_fees_inc_vat, Types::Coercible::Float
attribute :exit_fees_type, Types::Strict::String
attribute :day_unit_rate_exc_vat, Types::Coercible::Float
attribute :day_unit_rate_inc_vat, Types::Coercible::Float
attribute :night_unit_rate_exc_vat, Types::Coercible::Float
attribute :night_unit_rate_inc_vat, Types::Coercible::Float

attribute :links, Types::Strict::Array.of(Cephalopod::Models::Link)

end
end
end
end
24 changes: 24 additions & 0 deletions lib/cephalopod/models/products/single_register_charge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
module Cephalopod
module Models
module Products
class SingleRegisterCharge < Base

attribute :code, Types::Strict::String
attribute :standing_charge_exc_vat, Types::Coercible::Float
attribute :standing_charge_inc_vat, Types::Coercible::Float
attribute :online_discount_exc_vat, Types::Coercible::Float
attribute :online_discount_inc_vat, Types::Coercible::Float
attribute :dual_fuel_discount_exc_vat, Types::Coercible::Float
attribute :dual_fuel_discount_inc_vat, Types::Coercible::Float
attribute :exit_fees_exc_vat, Types::Coercible::Float
attribute :exit_fees_inc_vat, Types::Coercible::Float
attribute :exit_fees_type, Types::Strict::String
attribute :standard_unit_rate_exc_vat, Types::Coercible::Float
attribute :standard_unit_rate_inc_vat, Types::Coercible::Float

attribute :links, Types::Strict::Array.of(Cephalopod::Models::Link)

end
end
end
end
Binary file added lib/cephalopod/repos/.DS_Store
Binary file not shown.
2 changes: 1 addition & 1 deletion lib/cephalopod/repos/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def initialize(client: Cephalopod.client,
end

def get(method: :get, headers: {}, query: {}, body: nil, **params, &block)
parse_response(@client.run_request(method, build_path(query: query, **params), body, headers, &block))
@get ||= parse_response(@client.run_request(method, build_path(query: query, **params), body, headers, &block))
end

private
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Cephalopod
module Repos
module V1
module ElectricityMeterPoints
module Consumption
class Collection < Base

model Cephalopod::Models::Consumption
resource_route '/v1/electricity-meter-points/:mpan/meters/:serial_number/consumption/'

end
end
end
end
end
end
14 changes: 14 additions & 0 deletions lib/cephalopod/repos/v1/electricity_meter_points/resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
module Cephalopod
module Repos
module V1
module ElectricityMeterPoints
class Resource < Base

model Cephalopod::Models::MeterPoint
resource_route '/v1/electricity-meter-points/:mpan/'

end
end
end
end
end
16 changes: 16 additions & 0 deletions lib/cephalopod/repos/v1/gas_meter_points/consumption/collection.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
module Cephalopod
module Repos
module V1
module GasMeterPoints
module Consumption
class Collection < Base

model Cephalopod::Models::Consumption
resource_route '/v1/gas-meter-points/:mpan/meters/:serial_number/consumption/'

end
end
end
end
end
end
Loading

0 comments on commit 7118197

Please sign in to comment.