tibber 0.1.0 → 0.2.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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 64eba9e3ecaabcac3c6fa77b2d93bdd0a4808b9399a2094987aebfef332586c2
4
- data.tar.gz: eca76b85e9d6d79961c27048046ebe4c6d3d42a5d2432367188b50a9ec1bc10c
3
+ metadata.gz: 9988f6aad8ec756b4be98dd516806a85dbbe03594a7a5062365796ecb6ae1e79
4
+ data.tar.gz: 72354be54db643b5a93c843672272a4d427d6ade18ff29dd01ec83b8d2a06c20
5
5
  SHA512:
6
- metadata.gz: fe85a0c01428961c7eb6f8ef27192ac4b49877b67d6031f37c9d850886fa5e6919bc9313d994dd9aa37fe733f77200a5be42070f015ec12245d2e4c9847e4839
7
- data.tar.gz: 9a35eb84167e0f37606ccdce5f8340ed8e4332d856c190643c5a2c5018b6c0ef0a338b272efd1881e8c8923ffd9587ce13aac8990bd6c058e911e853dbed25d6
6
+ metadata.gz: 8a519bf958e5ed6d00522ec2cc98bdeb9d21e212df9e63752db48bf9758d0b32098d2cb0d736fbfbdde465912f5d13b4de1ae821526633b7087f1cbb4be162c9
7
+ data.tar.gz: a756bb3d459b41a97f2fb1279f5e66059cc2085acc4081ae1b5937230f0aeda927b12f60d7434965926138698accd6a328916523ad2c112a6cc25605270d3f0c
data/CHANGELOG.md CHANGED
@@ -1,4 +1,13 @@
1
- ## [Unreleased]
1
+ # Changelog
2
2
 
3
3
  ## [0.1.0] - 2024-02-25
4
+
4
5
  - Initial release
6
+
7
+ ## [0.1.1] - 2025-03-20
8
+
9
+ - update tests, simplify some code, add docs
10
+
11
+ ## [0.2.0] - 2025-09-26
12
+
13
+ - support quarterly resolution for price info
data/Gemfile CHANGED
@@ -2,5 +2,5 @@
2
2
 
3
3
  source 'https://rubygems.org'
4
4
 
5
- # Specify your gem's dependencies in hudu.gemspec
5
+ # Specify your gem's dependencies in tibber.gemspec
6
6
  gemspec
data/README.md CHANGED
@@ -1,9 +1,9 @@
1
1
  # Tibber API
2
+
2
3
  [![Version](https://img.shields.io/gem/v/tibber.svg)](https://rubygems.org/gems/tibber)
3
4
 
4
5
  This is a wrapper for the Tibber rest API.
5
6
 
6
-
7
7
  ## Installation
8
8
 
9
9
  Add this line to your application's Gemfile:
@@ -14,15 +14,20 @@ gem 'tibber'
14
14
 
15
15
  And then execute:
16
16
 
17
- $ bundle install
17
+ ```console
18
+ > bundle install
19
+ ```
18
20
 
19
21
  Or install it yourself as:
20
22
 
21
- $ gem install tibber
23
+ ```console
24
+ > gem install tibber
25
+ ```
22
26
 
23
27
  ## Usage
24
28
 
25
- Before you start making the requests to API provide the endpoint and api key using the configuration wrapping.
29
+ Before you start making the requests to API provide the endpoint and api key
30
+ using the configuration wrapping.
26
31
 
27
32
  ```ruby
28
33
  require 'tibber'
@@ -35,13 +40,15 @@ Tibber.configure do |config|
35
40
  end
36
41
 
37
42
  # or configure with options hash
38
- client = Tibber.client
43
+ client = Tibber.client(access_token: ENV['TIBBER_ACCESS_TOKEN'], logger: Logger.new(TEST_LOGGER))
39
44
  client.login
40
45
 
41
46
  ```
42
47
 
43
48
  ## Resources
49
+
44
50
  ### Authentication
51
+
45
52
  ```ruby
46
53
  # setup
47
54
  #
@@ -54,13 +61,12 @@ rescue Tibber::AuthenticationError => e
54
61
  end
55
62
  ```
56
63
 
57
-
58
-
59
64
  ### Graph QL Data resources
65
+
60
66
  Endpoint for data related requests
61
67
 
62
68
  ```ruby
63
- # show todays prices
69
+ # show todays prices; by default prices are per hour
64
70
  prices = client.price_info
65
71
 
66
72
  prices.homes.each do |home|
@@ -70,6 +76,8 @@ prices.homes.each do |home|
70
76
  end
71
77
  end
72
78
 
79
+ # but also possible per quarter
80
+ prices = client.price_info(Tibber::Resolution::QUARTER_HOURLY)
73
81
  ```
74
82
 
75
83
  |Resource|API endpoint|
@@ -85,17 +93,21 @@ end
85
93
  2. Add release to [CHANGELOG.md](CHANGELOG.md)
86
94
  3. Commit.
87
95
  4. Test build.
88
- ```
89
- > rake build
90
96
 
97
+ ```console
98
+ > rake test
99
+ > rake build
91
100
  ```
101
+
92
102
  5. Release
93
- ```
103
+
104
+ ```console
94
105
  > rake release
106
+ ```
95
107
 
96
108
  ## Contributing
97
109
 
98
- Bug reports and pull requests are welcome on GitHub at https://github.com/jancotanis/tibber.
110
+ Bug reports and pull requests are welcome on [GitHub](https://github.com/jancotanis/tibber).
99
111
 
100
112
  ## License
101
113
 
data/lib/tibber/api.rb CHANGED
@@ -1,12 +1,11 @@
1
- require "wrapi"
1
+ # frozen_string_literal: true
2
+
3
+ require 'wrapi'
2
4
  require File.expand_path('request', __dir__)
3
5
  require File.expand_path('authorization', __dir__)
4
6
 
5
7
  module Tibber
6
- # @private
7
8
  class API
8
-
9
- # @private
10
9
  attr_accessor *WrAPI::Configuration::VALID_OPTIONS_KEYS
11
10
 
12
11
  # Creates a new API and copies settings from singleton
@@ -30,6 +29,5 @@ module Tibber
30
29
  include Request::GraphQL
31
30
  include WrAPI::Authentication
32
31
  include Authentication
33
-
34
32
  end
35
33
  end
@@ -1,19 +1,19 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('error', __dir__)
2
4
 
3
5
  module Tibber
4
6
  # Deals with authentication flow and stores it within global configuration
5
7
  module Authentication
6
-
7
8
  # Authorize to the Tibber portal using the access_token
8
9
  # @see https://developer.tibber.com/docs/guides/calling-api
9
- def login(options = {})
10
- raise ConfigurationError, "Accesstoken/api-key not set" unless access_token
11
- # only bearer token needed
12
- # will do sanity check if token if valid
10
+ def login(_options = {})
11
+ raise ConfigurationError, 'Accesstoken/api-key not set' unless access_token
12
+
13
+ # only bearer token needed will do sanity check if token is valid by callig api
13
14
  graphql_call('{viewer{name}}')
14
15
  rescue GraphQLError => e
15
16
  raise AuthenticationError.new e
16
17
  end
17
-
18
18
  end
19
19
  end
data/lib/tibber/client.rb CHANGED
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require File.expand_path('api', __dir__)
2
4
  require File.expand_path('const', __dir__)
3
5
  require File.expand_path('error', __dir__)
@@ -17,16 +19,13 @@ module Tibber
17
19
  raise AuthenticationError.new e
18
20
  end
19
21
 
20
- private
21
22
  def self.api_endpoint(method, query)
22
-
23
23
  # all records
24
24
  self.send(:define_method, method) do |params = {}|
25
- r = graphql_call(query, params)
25
+ graphql_call(query, params)
26
26
  end
27
27
  end
28
28
 
29
- public
30
29
  # return device information
31
30
  # product_type
32
31
  # product_name
@@ -105,13 +104,13 @@ module Tibber
105
104
  }
106
105
  }
107
106
  }'
108
- api_endpoint :price_info, '
107
+ api_endpoint :_price_info, '
109
108
  {
110
109
  viewer {
111
110
  homes {
112
111
  id
113
112
  currentSubscription{
114
- priceInfo{
113
+ priceInfo(resolution: %{resolution}){
115
114
  current{
116
115
  startsAt
117
116
  total
@@ -138,7 +137,12 @@ module Tibber
138
137
  }
139
138
  }
140
139
  }'
141
- api_endpoint:_send_push_notification, '
140
+
141
+ def price_info(resolution = Resolution::HOURLY)
142
+ _price_info({ resolution: resolution })
143
+ end
144
+
145
+ api_endpoint :_send_push_notification, '
142
146
  mutation {
143
147
  sendPushNotification(input: {
144
148
  title: "%{title}",
data/lib/tibber/const.rb CHANGED
@@ -1,22 +1,99 @@
1
+ # frozen_string_literal: true
1
2
 
2
3
  module Tibber
4
+ # The `Enum` class provides a way to define constant-based enumerations dynamically.
3
5
  class Enum
4
- def self.enum(array)
5
- array.each do |c|
6
- const_set c,c
7
- end
6
+ # Defines constants from an array of symbols or strings.
7
+ #
8
+ # @param array [Array<Symbol, String>] List of values to be set as constants.
9
+ # @example
10
+ # class Colors < Enum
11
+ # enum %w[RED GREEN BLUE]
12
+ # end
13
+ #
14
+ # Colors::RED # => "RED"
15
+ # Colors::GREEN # => "GREEN"
16
+ #
17
+ # The above example dynamically defines constants `RED`, `GREEN`, and `BLUE` inside the `Colors` class.
18
+ def self.enum(array)
19
+ array.each do |cnst|
20
+ const_set cnst, cnst
8
21
  end
22
+ end
9
23
  end
24
+
25
+ # The `Screens` class defines a set of screen names as constants.
26
+ # It inherits from `Enum` and uses the `enum` method to define constants dynamically.
10
27
  class Screens < Enum
11
- enum %w[HOME REPORTS CONSUMPTION COMPARISON DISAGGREGATION HOME_PROFILE CUSTOMER_PROFILE METER_READING NOTIFICATIONS INVOICES]
28
+ # Defines screen names as constants.
29
+ #
30
+ # @example Usage:
31
+ # Screens::HOME # => "HOME"
32
+ # Screens::REPORTS # => "REPORTS"
33
+ # Screens::NOTIFICATIONS # => "NOTIFICATIONS"
34
+ #
35
+ # This dynamically defines the following constants:
36
+ # - HOME
37
+ # - REPORTS
38
+ # - CONSUMPTION
39
+ # - COMPARISON
40
+ # - DISAGGREGATION
41
+ # - HOME_PROFILE
42
+ # - CUSTOMER_PROFILE
43
+ # - METER_READING
44
+ # - NOTIFICATIONS
45
+ # - INVOICES
46
+ enum %w[
47
+ HOME REPORTS CONSUMPTION COMPARISON DISAGGREGATION
48
+ HOME_PROFILE CUSTOMER_PROFILE METER_READING NOTIFICATIONS INVOICES
49
+ ]
12
50
  end
51
+
52
+ # The `Resolution` class defines different time resolutions as constants.
53
+ # It inherits from `Enum` and dynamically sets constants using `enum`.
54
+ #
55
+ # @example Usage:
56
+ # Resolution::HOURLY # => "HOURLY"
57
+ # Resolution::DAILY # => "DAILY"
13
58
  class Resolution < Enum
14
- enum %w[HOURLY DAILY WEEKLY MONTHLY ANNUAL]
59
+ # Defines time resolution constants:
60
+ # - QUARTER_HOURLY
61
+ # - HOURLY
62
+ # - DAILY
63
+ # - WEEKLY
64
+ # - MONTHLY
65
+ # - ANNUAL
66
+ enum %w[QUARTER_HOURLY HOURLY DAILY WEEKLY MONTHLY ANNUAL]
15
67
  end
68
+
69
+ # The `HomeType` class defines different types of homes as constants.
70
+ #
71
+ # @example Usage:
72
+ # HomeType::APARTMENT # => "APARTMENT"
73
+ # HomeType::HOUSE # => "HOUSE"
16
74
  class HomeType < Enum
75
+ # Defines home type constants:
76
+ # - APARTMENT
77
+ # - ROWHOUSE
78
+ # - HOUSE
79
+ # - COTTAGE
17
80
  enum %w[APARTMENT ROWHOUSE HOUSE COTTAGE]
18
81
  end
82
+
83
+ # The `Avatar` class defines different avatar types as constants.
84
+ #
85
+ # @example Usage:
86
+ # Avatar::COTTAGE # => "COTTAGE"
87
+ # Avatar::CASTLE # => "CASTLE"
19
88
  class Avatar < Enum
89
+ # Defines avatar type constants:
90
+ # - APARTMENT
91
+ # - ROWHOUSE
92
+ # - FLOORHOUSE1
93
+ # - FLOORHOUSE2
94
+ # - FLOORHOUSE3
95
+ # - COTTAGE
96
+ # - CASTLE
20
97
  enum %w[APARTMENT ROWHOUSE FLOORHOUSE1 FLOORHOUSE2 FLOORHOUSE3 COTTAGE CASTLE]
21
98
  end
22
99
  end
data/lib/tibber/error.rb CHANGED
@@ -1,5 +1,6 @@
1
- module Tibber
1
+ # frozen_string_literal: true
2
2
 
3
+ module Tibber
3
4
  # Generic error to be able to rescue all Hudu errors
4
5
  class TibberError < StandardError; end
5
6
 
@@ -11,5 +12,4 @@ module Tibber
11
12
 
12
13
  # Issue authenticting
13
14
  class AuthenticationError < TibberError; end
14
-
15
15
  end
@@ -1,9 +1,10 @@
1
+ # frozen_string_literal: true
2
+
1
3
  require 'faraday'
2
4
 
3
5
  module Tibber
4
6
  # Deals with requests
5
7
  module Request
6
-
7
8
  # JSON-RPC is a stateless, light-weight remote procedure call (RPC) protocol. Primarily this
8
9
  # specification defines several data structures and the rules around their processing. It is
9
10
  # transport agnostic in that the concepts can be used within the same process, over sockets, over
@@ -11,27 +12,33 @@ module Tibber
11
12
  #
12
13
  # https://www.jsonrpc.org/specification
13
14
  module GraphQL
14
-
15
+ ##
16
+ # Executes a GraphQL query with optional parameters.
17
+ #
18
+ # @param query [String] The GraphQL query string.
19
+ # @param params [Hash, nil] (optional) Parameters to be interpolated into the query.
20
+ # @return [WrAPI::Request::Entity] The parsed response data.
21
+ # @raise [GraphQLError] If the response contains GraphQL errors.
22
+ #
23
+ # @example Basic GraphQL call
24
+ # query = "{ user(id: %d) { name email } }"
25
+ # graphql_call(query, { id: 123 })
26
+ #
15
27
  def graphql_call(query, params = nil)
16
- query = (query % params) if params && params.size > 0
28
+ query = (query % params) if params&.any?
17
29
  options = {
18
30
  "query": query
19
31
  }
20
- result = post( '', options )
32
+ result = post('', options)
21
33
  raise GraphQLError.new(result.body['errors']) if result.body['errors']
22
- data = result.body['data']
23
- WrAPI::Request::Entity.create(data['viewer'] ? data['viewer'] : data)
24
34
 
35
+ data = result.body['data']
36
+ WrAPI::Request::Entity.create(data['viewer'] || data)
25
37
  rescue Faraday::BadRequestError => e
26
38
  body = e.response[:body]
27
- if body && body['errors']
28
- error = body['errors']
29
- else
30
- error = e.to_s
31
- end
39
+ error = body&.dig('errors') || e.to_s
32
40
  raise GraphQLError.new(error)
33
41
  end
34
-
35
42
  end
36
43
  end
37
44
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Tibber
4
- VERSION = '0.1.0'
4
+ VERSION = '0.2.0'
5
5
  end
data/lib/tibber.rb CHANGED
@@ -1,19 +1,51 @@
1
- require "wrapi"
1
+ # frozen_string_literal: true
2
+
3
+ require 'wrapi'
2
4
  require File.expand_path('tibber/client', __dir__)
3
5
  require File.expand_path('tibber/version', __dir__)
4
6
 
7
+ # The `Tibber` module provides an API client for interacting with the Tibber GraphQL API.
8
+ # It extends `WrAPI::Configuration` and `WrAPI::RespondTo`, enabling configuration handling.
9
+ #
10
+ # @example Creating a client instance:
11
+ # client = Tibber.client(api_key: "your-api-key")
12
+ #
13
+ # @example Resetting the client configuration:
14
+ # Tibber.reset
15
+ #
5
16
  module Tibber
6
17
  extend WrAPI::Configuration
7
18
  extend WrAPI::RespondTo
8
19
 
9
- DEFAULT_UA = "Ruby Tibber API client #{Tibber::VERSION}".freeze
10
- DEFAULT_ENDPOINT = 'https://api.tibber.com/v1-beta/gql'.freeze
20
+ # Default User-Agent string for API requests.
21
+ DEFAULT_UA = "Ruby Tibber API client #{Tibber::VERSION}"
22
+
23
+ # Default API endpoint for Tibber.
24
+ DEFAULT_ENDPOINT = 'https://api.tibber.com/v1-beta/gql'
25
+
26
+ # Creates a new instance of the Tibber API client.
27
+ #
28
+ # @param options [Hash] Optional configuration overrides.
29
+ # @option options [String] :user_agent Custom user agent string.
30
+ # @option options [String] :endpoint Custom API endpoint.
11
31
  #
12
- # @return [Hudu::Client]
32
+ # @return [Tibber::Client] The initialized API client.
33
+ #
34
+ # @example Creating a client with default settings:
35
+ # client = Tibber.client
36
+ #
37
+ # @example Creating a client with a custom user agent:
38
+ # client = Tibber.client(user_agent: "My Custom UA")
13
39
  def self.client(options = {})
14
40
  Tibber::Client.new({ user_agent: DEFAULT_UA, endpoint: DEFAULT_ENDPOINT }.merge(options))
15
41
  end
16
42
 
43
+ # Resets the Tibber module's configuration to default values.
44
+ #
45
+ # @example Resetting the configuration:
46
+ # Tibber.reset
47
+ #
48
+ # @return [void]
17
49
  def self.reset
18
50
  super
19
51
  self.endpoint = nil
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: tibber
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Janco Tanis
8
- autorequire:
9
8
  bindir: exe
10
9
  cert_chain: []
11
- date: 2024-02-26 00:00:00.000000000 Z
10
+ date: 2025-09-26 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: faraday
@@ -94,7 +93,6 @@ dependencies:
94
93
  - - ">="
95
94
  - !ruby/object:Gem::Version
96
95
  version: '0'
97
- description:
98
96
  email: gems@jancology.com
99
97
  executables: []
100
98
  extensions: []
@@ -121,7 +119,6 @@ licenses:
121
119
  metadata:
122
120
  homepage_uri: https://rubygems.org/gems/tibber
123
121
  source_code_uri: https://github.com/jancotanis/tibber
124
- post_install_message:
125
122
  rdoc_options: []
126
123
  require_paths:
127
124
  - lib
@@ -136,8 +133,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
136
133
  - !ruby/object:Gem::Version
137
134
  version: '0'
138
135
  requirements: []
139
- rubygems_version: 3.2.12
140
- signing_key:
136
+ rubygems_version: 3.6.2
141
137
  specification_version: 4
142
138
  summary: A Ruby wrapper for the Tibber APIs (readonly)
143
139
  test_files: []