tibber 0.1.0 → 0.1.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 +4 -4
- data/CHANGELOG.md +4 -1
- data/Gemfile +1 -1
- data/README.md +16 -8
- data/lib/tibber/api.rb +3 -5
- data/lib/tibber/authorization.rb +6 -6
- data/lib/tibber/client.rb +3 -4
- data/lib/tibber/const.rb +81 -5
- data/lib/tibber/error.rb +2 -2
- data/lib/tibber/request.rb +19 -12
- data/lib/tibber/version.rb +1 -1
- data/lib/tibber.rb +36 -4
- metadata +3 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: ad415b5607dc399b32cbeca2414445c8aa5c13945d7e831168b61be8ba4c5680
|
4
|
+
data.tar.gz: c07a1c65eecbb6afb61d1c08bfa8c0b3980b1690a30a246a6033422327b3ad30
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: afa6b65748b4cf01c1d524cbce1c1dea093460f9b9de63dde564a723ec754ce4d66071661317fa1d96d5d889b61b967dd600be252e77f9a57ac0b958e05ec2bb
|
7
|
+
data.tar.gz: 682d3a9280953ec497e506a769447864a5053c8188843020f335f37166754736e5a3891b16a092e790fff61ee62e32cbd920576df38cb9f84a680c8030fb4d99
|
data/CHANGELOG.md
CHANGED
data/Gemfile
CHANGED
data/README.md
CHANGED
@@ -1,9 +1,9 @@
|
|
1
1
|
# Tibber API
|
2
|
+
|
2
3
|
[](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,11 +14,15 @@ gem 'tibber'
|
|
14
14
|
|
15
15
|
And then execute:
|
16
16
|
|
17
|
-
|
17
|
+
```console
|
18
|
+
> bundle install
|
19
|
+
```
|
18
20
|
|
19
21
|
Or install it yourself as:
|
20
22
|
|
21
|
-
|
23
|
+
```console
|
24
|
+
> gem install tibber
|
25
|
+
```
|
22
26
|
|
23
27
|
## Usage
|
24
28
|
|
@@ -41,7 +45,9 @@ client.login
|
|
41
45
|
```
|
42
46
|
|
43
47
|
## Resources
|
48
|
+
|
44
49
|
### Authentication
|
50
|
+
|
45
51
|
```ruby
|
46
52
|
# setup
|
47
53
|
#
|
@@ -54,9 +60,8 @@ rescue Tibber::AuthenticationError => e
|
|
54
60
|
end
|
55
61
|
```
|
56
62
|
|
57
|
-
|
58
|
-
|
59
63
|
### Graph QL Data resources
|
64
|
+
|
60
65
|
Endpoint for data related requests
|
61
66
|
|
62
67
|
```ruby
|
@@ -85,13 +90,16 @@ end
|
|
85
90
|
2. Add release to [CHANGELOG.md](CHANGELOG.md)
|
86
91
|
3. Commit.
|
87
92
|
4. Test build.
|
88
|
-
```
|
89
|
-
> rake build
|
90
93
|
|
94
|
+
```console
|
95
|
+
> rake test
|
96
|
+
> rake build
|
91
97
|
```
|
92
98
|
5. Release
|
93
|
-
|
99
|
+
|
100
|
+
```console
|
94
101
|
> rake release
|
102
|
+
```
|
95
103
|
|
96
104
|
## Contributing
|
97
105
|
|
data/lib/tibber/api.rb
CHANGED
@@ -1,12 +1,11 @@
|
|
1
|
-
|
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
|
data/lib/tibber/authorization.rb
CHANGED
@@ -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(
|
10
|
-
raise ConfigurationError,
|
11
|
-
|
12
|
-
# will do sanity check if token
|
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
|
-
|
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
|
data/lib/tibber/const.rb
CHANGED
@@ -1,22 +1,98 @@
|
|
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
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
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
|
-
|
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
|
59
|
+
# Defines time resolution constants:
|
60
|
+
# - HOURLY
|
61
|
+
# - DAILY
|
62
|
+
# - WEEKLY
|
63
|
+
# - MONTHLY
|
64
|
+
# - ANNUAL
|
14
65
|
enum %w[HOURLY DAILY WEEKLY MONTHLY ANNUAL]
|
15
66
|
end
|
67
|
+
|
68
|
+
# The `HomeType` class defines different types of homes as constants.
|
69
|
+
#
|
70
|
+
# @example Usage:
|
71
|
+
# HomeType::APARTMENT # => "APARTMENT"
|
72
|
+
# HomeType::HOUSE # => "HOUSE"
|
16
73
|
class HomeType < Enum
|
74
|
+
# Defines home type constants:
|
75
|
+
# - APARTMENT
|
76
|
+
# - ROWHOUSE
|
77
|
+
# - HOUSE
|
78
|
+
# - COTTAGE
|
17
79
|
enum %w[APARTMENT ROWHOUSE HOUSE COTTAGE]
|
18
80
|
end
|
81
|
+
|
82
|
+
# The `Avatar` class defines different avatar types as constants.
|
83
|
+
#
|
84
|
+
# @example Usage:
|
85
|
+
# Avatar::COTTAGE # => "COTTAGE"
|
86
|
+
# Avatar::CASTLE # => "CASTLE"
|
19
87
|
class Avatar < Enum
|
88
|
+
# Defines avatar type constants:
|
89
|
+
# - APARTMENT
|
90
|
+
# - ROWHOUSE
|
91
|
+
# - FLOORHOUSE1
|
92
|
+
# - FLOORHOUSE2
|
93
|
+
# - FLOORHOUSE3
|
94
|
+
# - COTTAGE
|
95
|
+
# - CASTLE
|
20
96
|
enum %w[APARTMENT ROWHOUSE FLOORHOUSE1 FLOORHOUSE2 FLOORHOUSE3 COTTAGE CASTLE]
|
21
97
|
end
|
22
98
|
end
|
data/lib/tibber/error.rb
CHANGED
@@ -1,5 +1,6 @@
|
|
1
|
-
|
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
|
data/lib/tibber/request.rb
CHANGED
@@ -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
|
28
|
+
query = (query % params) if params&.any?
|
17
29
|
options = {
|
18
30
|
"query": query
|
19
31
|
}
|
20
|
-
result = post(
|
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
|
-
|
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
|
data/lib/tibber/version.rb
CHANGED
data/lib/tibber.rb
CHANGED
@@ -1,19 +1,51 @@
|
|
1
|
-
|
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
|
-
|
10
|
-
|
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 [
|
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.
|
4
|
+
version: 0.1.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Janco Tanis
|
8
|
-
autorequire:
|
9
8
|
bindir: exe
|
10
9
|
cert_chain: []
|
11
|
-
date:
|
10
|
+
date: 2025-03-20 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
|
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: []
|