trmnl-api 0.0.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: 0ba1eee66530c227b2e43b24de28e6913753d4f627af3d979c3add5cbba3846c
4
- data.tar.gz: da5463562b79f6b883f90925cb61ffc44b357c3e64eec615f5cbb0a433f3a36a
3
+ metadata.gz: 569d9d6bbfd5690691f558a31079e9837b9c39141c02e5f43e44bd58cfbf2663
4
+ data.tar.gz: e77c972a0cebebc856605a9fb3c4b7fc99bb6c475f786900e7eba0e3b508875e
5
5
  SHA512:
6
- metadata.gz: 03430a130bfa5a302b67c674ece3951b88e0629f256c7937351d562a7f52117b7b0a0704817fbda6a4d771ee0ddd59542f80e9cccd5970c6ab5a8dfe1e9be0ba
7
- data.tar.gz: 71d6d93f75797dad4ef0ae37e6b454d65adcff265149de1f6e32175ae7d5970b4aa590283eed5d95ae93ccfad89672c1a835ce4529af8e1ea5d53a4844a5f532
6
+ metadata.gz: 26a8717e1cdd331584bc6f390bfc07ef30b072b2f9a5a406c562fd5eee369576b17b149f762ca4f9e48b0c763891e92618e0823eae384d549392910e917b1e8d
7
+ data.tar.gz: 5b9cfef970999778d870a606961a6dd54de4233eb2343a72b07ff11d26729d91652a324a99fda045998d6f706b91cf078e77ba077d047798ce77e058a162cdef
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -48,14 +48,28 @@ require "trmnl/api"
48
48
  This client provides access to multiple endpoints. Each endpoint will answer either a `Success` or `Failure` (as provided by {dry_monads_link}) based on result of the API call. This allows you pattern match in your own code when using each endpoint. Example:
49
49
 
50
50
  ``` ruby
51
- case endpoint.call token: "secret"
51
+ client = TRMNL::API::Client.new
52
+
53
+ case client.display token: "secret"
52
54
  in Success(payload) then puts payload
53
55
  in Failure(response) then puts response
54
- else puts "Unknown HTTP response."
56
+ else puts "Unknown response."
55
57
  end
56
58
  ```
57
59
 
58
- See each endpoint for further details.
60
+ See xref:_endpoints[Endpoints] for further details.
61
+
62
+ === Configuration
63
+
64
+ By default, you shouldn't need to change the default configuration but you can always use a block to adjust settings as desired. If you don't configure the client, then the following defaults will be used:
65
+
66
+ [source,ruby]
67
+ ----
68
+ client = TRMNL::API::Client.new do |settings|
69
+ settings.content_type = "application/json",
70
+ settings.uri = "https://trmnl.app/api"
71
+ end
72
+ ----
59
73
 
60
74
  === Environment
61
75
 
@@ -64,16 +78,18 @@ You can configure the client via the following environment variables:
64
78
  * `TRMNL_API_CONTENT_TYPE`: Defines the HTTP `Content-Type` header. You shouldn't need to change this but is here if you need it. Default: "application/json".
65
79
  * `TRMNL_API_URI`: Defines the API URI. Default: "https://trmnl.app/api".
66
80
 
67
- Any/all environment changes will be applied to all endpoint objects.
81
+ Any/all environment changes will be applied unless you override these settings via the client configuration block shown above.
82
+
83
+ === Endpoints
68
84
 
69
- === Current Screen
85
+ ==== Current Screen
70
86
 
71
87
  Allows you to obtain current screen being displayed for your device. You must supply your device's API token as the `token`. Example:
72
88
 
73
89
  [source,ruby]
74
90
  ----
75
- endpoint = TRMNL::API::Endpoints::CurrentScreen.new
76
- endpoint.call token: "secret"
91
+ client = TRMNL::API::Client.new
92
+ client.current_screen token: "secret"
77
93
 
78
94
  # Success(
79
95
  # #<data TRMNL::API::Models::CurrentScreen
@@ -84,14 +100,14 @@ endpoint.call token: "secret"
84
100
  # )
85
101
  ----
86
102
 
87
- === Display
103
+ ==== Display
88
104
 
89
- Allows you to obtain current screen being displayed for your device. You must supply your device's API token as the `token`. Example:
105
+ Allows you to obtain current screen being displayed for your device with additional information not provided by the xref:_current_screen[Current Screen] endpoint. You must supply your device's API token as the `token`. Example:
90
106
 
91
107
  [source,ruby]
92
108
  ----
93
- endpoint = TRMNL::API::Endpoints::Display.new
94
- endpoint.call token: "secret"
109
+ client = TRMNL::API::Client.new
110
+ client.display token: "secret"
95
111
 
96
112
  # Success(
97
113
  # #<struct TRMNL::API::Models::Display
@@ -107,73 +123,72 @@ endpoint.call token: "secret"
107
123
  # )
108
124
  ----
109
125
 
110
- === Firmware
126
+ ==== Firmware
111
127
 
112
128
  Allows you to obtain the current stable firmware version. Example:
113
129
 
114
130
  [source,ruby]
115
131
  ----
116
- endpoint = TRMNL::API::Endpoints::Firmware.new
117
- endpoint.call
132
+ client = TRMNL::API::Client.new
133
+ client.firmware
118
134
 
119
135
  # Success(#<data TRMNL::API::Models::Firmware url="https://trmnl-fw.s3.us-east-2.amazonaws.com/FW1.4.8.bin", version="1.4.8">)
120
136
  ----
121
137
 
122
- === Log
138
+ ==== Log
123
139
 
124
140
  Allows you to create a log entry (which is what the device reports when it captures an error). You must supply your device's API token as the `token`. Example:
125
141
 
126
142
  [source,ruby]
127
143
  ----
128
- endpoint = TRMNL::API::Endpoints::Log.new
129
- endpoint.call token: "secret",
130
- log: {
131
- logs_array: [
132
- {
133
- log_id: 1,
134
- creation_timestamp: 1742022124,
135
- log_message: "returned code is not OK: 404",
136
- log_codeline: 597,
137
- device_status_stamp: {
138
- wifi_status: "connected",
139
- wakeup_reason: "timer",
140
- current_fw_version: "1.4.7",
141
- free_heap_size: 160656,
142
- special_function: "none",
143
- refresh_rate: 30,
144
- battery_voltage: 4.772,
145
- time_since_last_sleep_start: 31,
146
- wifi_rssi_level: -54
147
- },
148
- additional_info: {
149
- retry_attempt: 1
150
- },
151
- log_sourcefile: "src/bl.cpp"
152
- }
153
- ]
154
- }
144
+ client = TRMNL::API::Client.new
145
+ client.log token: "secret",
146
+ log: {
147
+ logs_array: [
148
+ {
149
+ log_id: 1,
150
+ creation_timestamp: 1742022124,
151
+ log_message: "returned code is not OK: 404",
152
+ log_codeline: 597,
153
+ device_status_stamp: {
154
+ wifi_status: "connected",
155
+ wakeup_reason: "timer",
156
+ current_fw_version: "1.4.7",
157
+ free_heap_size: 160656,
158
+ special_function: "none",
159
+ refresh_rate: 30,
160
+ battery_voltage: 4.772,
161
+ time_since_last_sleep_start: 31,
162
+ wifi_rssi_level: -54
163
+ },
164
+ additional_info: {
165
+ retry_attempt: 1
166
+ },
167
+ log_sourcefile: "src/bl.cpp"
168
+ }
169
+ ]
170
+ }
155
171
 
156
172
  # Success(#<HTTP::Response/1.1 204 No Content...)
157
173
  ----
158
174
 
159
175
  You'll either get a 204 No Content or 200 OK response depending on if the device exists or not.
160
176
 
161
- === Setup
177
+ ==== Setup
162
178
 
163
179
  Allows you to obtain the setup response for when a new device is setup. You must supply your device's MAC Address as the `id`. Example:
164
180
 
165
-
166
181
  [source,ruby]
167
182
  ----
168
- endpoint = TRMNL::API::Endpoints::Setup.new
169
- endpoint.call id: "B0:81:84:22:BD:E8"
183
+ client = TRMNL::API::Client.new
184
+ client.setup id: "A1:B2:C3:D4:E5:F6"
170
185
 
171
186
  # Success(
172
187
  # #<data TRMNL::API::Models::Setup
173
188
  # api_key="secret",
174
- # friendly_id="40DEF8",
189
+ # friendly_id="F51FDE",
175
190
  # image_url="https://usetrmnl.com/images/setup/setup-logo.bmp",
176
- # message="Register at usetrmnl.com/signup with Device ID '40DEF8'"
191
+ # message="Register at usetrmnl.com/signup with Device ID 'F51FDE'"
177
192
  # >
178
193
  # )
179
194
  ----
@@ -1,39 +1,43 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- require "dry/monads"
4
- require "http"
3
+ require "inspectable"
5
4
 
6
5
  module TRMNL
7
6
  module API
8
- # Provides a low level configurable and monadic API client.
9
- # :reek:DataClump
7
+ # Provides the primary client for making API requests.
10
8
  class Client
11
- include Dry::Monads[:result]
12
-
13
- HEADERS = {}.freeze
14
-
15
- def initialize settings: TRMNL::API::Configuration::Loader.new.call, http: HTTP
16
- @settings = settings
17
- @http = http
18
-
9
+ include Dependencies[:settings]
10
+
11
+ include Endpoints::Dependencies[
12
+ endpoint_current_screen: :current_screen,
13
+ endpoint_display: :display,
14
+ endpoint_firmware: :firmware,
15
+ endpoint_log: :log,
16
+ endpoint_setup: :setup
17
+ ]
18
+
19
+ include Inspectable[
20
+ endpoint_current_screen: :class,
21
+ endpoint_display: :class,
22
+ endpoint_firmware: :class,
23
+ endpoint_log: :class,
24
+ endpoint_setup: :class
25
+ ]
26
+
27
+ def initialize(**)
28
+ super
19
29
  yield settings if block_given?
20
30
  end
21
31
 
22
- def get(path, headers: HEADERS, **params) = call(__method__, path, headers, params:)
32
+ def current_screen(**) = endpoint_current_screen.call(**)
23
33
 
24
- def post(path, headers: HEADERS, **json) = call(__method__, path, headers, json:)
34
+ def display(**) = endpoint_display.call(**)
25
35
 
26
- private
36
+ def firmware = endpoint_firmware.call
27
37
 
28
- attr_reader :settings, :http
38
+ def log(**) = endpoint_log.call(**)
29
39
 
30
- # rubocop:todo Metrics/ParameterLists
31
- def call method, path, headers, **options
32
- http.headers(settings.headers.merge(headers))
33
- .public_send(method, "#{settings.uri}/#{path}", options)
34
- .then { |response| response.status.success? ? Success(response) : Failure(response) }
35
- end
36
- # rubocop:enable Metrics/ParameterLists
40
+ def setup(**) = endpoint_setup.call(**)
37
41
  end
38
42
  end
39
43
  end
@@ -10,11 +10,12 @@ module TRMNL
10
10
  module Container
11
11
  extend Containable
12
12
 
13
- register(:client) { API::Client.new }
13
+ register(:settings) { TRMNL::API::Configuration::Loader.new.call }
14
+ register(:requester) { API::Requester.new }
14
15
  register(:logger) { Cogger.new id: "trmnl-api", formatter: :json }
15
16
 
16
17
  register :http do
17
- HTTP.default_options = ::HTTP::Options.new features: {logging: {logger: self[:logger]}}
18
+ HTTP.default_options = HTTP::Options.new features: {logging: {logger: self[:logger]}}
18
19
  HTTP
19
20
  end
20
21
 
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "containable"
4
+
5
+ module TRMNL
6
+ module API
7
+ module Endpoints
8
+ # Registers all endpoints.
9
+ module Container
10
+ extend Containable
11
+
12
+ register(:current_screen) { CurrentScreen.new }
13
+ register(:display) { Display.new }
14
+ register(:firmware) { Firmware.new }
15
+ register(:log) { Log.new }
16
+ register(:setup) { Setup.new }
17
+ end
18
+ end
19
+ end
20
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "inspectable"
3
4
  require "pipeable"
4
5
 
5
6
  module TRMNL
@@ -7,16 +8,17 @@ module TRMNL
7
8
  module Endpoints
8
9
  # Handles API request/response.
9
10
  class CurrentScreen
10
- include Dependencies[
11
- :client,
11
+ include TRMNL::API::Dependencies[
12
+ :requester,
12
13
  contract: "contracts.current_screen",
13
14
  model: "models.current_screen"
14
15
  ]
15
16
 
17
+ include Inspectable[contract: :class]
16
18
  include Pipeable
17
19
 
18
20
  def call token:
19
- pipe client.get("current_screen", headers: {"Access-Token" => token}),
21
+ pipe requester.get("current_screen", headers: {"Access-Token" => token}),
20
22
  try(:parse, catch: JSON::ParserError),
21
23
  validate(contract, as: :to_h),
22
24
  to(model, :for)
@@ -0,0 +1,11 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "infusible"
4
+
5
+ module TRMNL
6
+ module API
7
+ module Endpoints
8
+ Dependencies = Infusible[Container]
9
+ end
10
+ end
11
+ end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "inspectable"
3
4
  require "pipeable"
4
5
 
5
6
  module TRMNL
@@ -7,11 +8,17 @@ module TRMNL
7
8
  module Endpoints
8
9
  # Handles API request/response.
9
10
  class Display
10
- include Dependencies[:client, contract: "contracts.display", model: "models.display"]
11
+ include TRMNL::API::Dependencies[
12
+ :requester,
13
+ contract: "contracts.display",
14
+ model: "models.display"
15
+ ]
16
+
17
+ include Inspectable[contract: :class]
11
18
  include Pipeable
12
19
 
13
20
  def call token:
14
- pipe client.get("display", headers: {"Access-Token" => token}),
21
+ pipe requester.get("display", headers: {"Access-Token" => token}),
15
22
  try(:parse, catch: JSON::ParserError),
16
23
  validate(contract, as: :to_h),
17
24
  to(model, :for)
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "inspectable"
3
4
  require "pipeable"
4
5
 
5
6
  module TRMNL
@@ -7,11 +8,17 @@ module TRMNL
7
8
  module Endpoints
8
9
  # Handles API request/response.
9
10
  class Firmware
10
- include Dependencies[:client, contract: "contracts.firmware", model: "models.firmware"]
11
+ include TRMNL::API::Dependencies[
12
+ :requester,
13
+ contract: "contracts.firmware",
14
+ model: "models.firmware"
15
+ ]
16
+
17
+ include Inspectable[contract: :class]
11
18
  include Pipeable
12
19
 
13
20
  def call
14
- pipe client.get("firmware/latest"),
21
+ pipe requester.get("firmware/latest"),
15
22
  try(:parse, catch: JSON::ParserError),
16
23
  validate(contract, as: :to_h),
17
24
  to(model, :for)
@@ -5,9 +5,9 @@ module TRMNL
5
5
  module Endpoints
6
6
  # Handles API request/response.
7
7
  class Log
8
- include Dependencies[:client]
8
+ include TRMNL::API::Dependencies[:requester]
9
9
 
10
- def call(token:, **) = client.post("log", headers: {"Access-Token" => token}, **)
10
+ def call(token:, **) = requester.post("log", headers: {"Access-Token" => token}, **)
11
11
  end
12
12
  end
13
13
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "inspectable"
3
4
  require "pipeable"
4
5
 
5
6
  module TRMNL
@@ -7,11 +8,17 @@ module TRMNL
7
8
  module Endpoints
8
9
  # Handles API request/response.
9
10
  class Setup
10
- include Dependencies[:client, contract: "contracts.setup", model: "models.setup"]
11
+ include TRMNL::API::Dependencies[
12
+ :requester,
13
+ contract: "contracts.setup",
14
+ model: "models.setup"
15
+ ]
16
+
17
+ include Inspectable[contract: :class]
11
18
  include Pipeable
12
19
 
13
20
  def call id:
14
- pipe client.get("setup", headers: {"ID" => id}),
21
+ pipe requester.get("setup", headers: {"ID" => id}),
15
22
  try(:parse, catch: JSON::ParserError),
16
23
  validate(contract, as: :to_h),
17
24
  to(model, :for)
@@ -0,0 +1,38 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "dry/monads"
4
+ require "http"
5
+
6
+ module TRMNL
7
+ module API
8
+ # Provides a low level configurable and monadic API client.
9
+ # :reek:DataClump
10
+ class Requester
11
+ include Dependencies[:settings, :http]
12
+ include Dry::Monads[:result]
13
+
14
+ HEADERS = {}.freeze
15
+
16
+ def initialize(**)
17
+ super
18
+ yield settings if block_given?
19
+ end
20
+
21
+ def get(path, headers: HEADERS, **params) = call(__method__, path, headers, params:)
22
+
23
+ def post(path, headers: HEADERS, **json) = call(__method__, path, headers, json:)
24
+
25
+ private
26
+
27
+ attr_reader :settings, :http
28
+
29
+ # rubocop:todo Metrics/ParameterLists
30
+ def call method, path, headers, **options
31
+ http.headers(settings.headers.merge(headers))
32
+ .public_send(method, "#{settings.uri}/#{path}", options)
33
+ .then { |response| response.status.success? ? Success(response) : Failure(response) }
34
+ end
35
+ # rubocop:enable Metrics/ParameterLists
36
+ end
37
+ end
38
+ end
data/lib/trmnl/api.rb CHANGED
@@ -18,5 +18,7 @@ module TRMNL
18
18
  def self.loader registry = Zeitwerk::Registry
19
19
  @loader ||= registry.loaders.find { |loader| loader.tag == "trmnl-api" }
20
20
  end
21
+
22
+ def self.new(&) = Client.new(&)
21
23
  end
22
24
  end
data/trmnl-api.gemspec CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  Gem::Specification.new do |spec|
4
4
  spec.name = "trmnl-api"
5
- spec.version = "0.0.0"
5
+ spec.version = "0.2.0"
6
6
  spec.authors = ["TRMNL"]
7
7
  spec.email = ["support@usetrmnl.com"]
8
8
  spec.homepage = "https://github.com/usetrmnl/trmnl-api"
@@ -29,6 +29,7 @@ Gem::Specification.new do |spec|
29
29
  spec.add_dependency "dry-schema", "~> 1.14"
30
30
  spec.add_dependency "http", "~> 5.2"
31
31
  spec.add_dependency "infusible", "~> 4.3"
32
+ spec.add_dependency "inspectable", "~> 0.3"
32
33
  spec.add_dependency "pipeable", "~> 1.3"
33
34
  spec.add_dependency "zeitwerk", "~> 2.7"
34
35
 
data.tar.gz.sig CHANGED
Binary file
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: trmnl-api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TRMNL
@@ -119,6 +119,20 @@ dependencies:
119
119
  - - "~>"
120
120
  - !ruby/object:Gem::Version
121
121
  version: '4.3'
122
+ - !ruby/object:Gem::Dependency
123
+ name: inspectable
124
+ requirement: !ruby/object:Gem::Requirement
125
+ requirements:
126
+ - - "~>"
127
+ - !ruby/object:Gem::Version
128
+ version: '0.3'
129
+ type: :runtime
130
+ prerelease: false
131
+ version_requirements: !ruby/object:Gem::Requirement
132
+ requirements:
133
+ - - "~>"
134
+ - !ruby/object:Gem::Version
135
+ version: '0.3'
122
136
  - !ruby/object:Gem::Dependency
123
137
  name: pipeable
124
138
  requirement: !ruby/object:Gem::Requirement
@@ -167,7 +181,9 @@ files:
167
181
  - lib/trmnl/api/contracts/firmware.rb
168
182
  - lib/trmnl/api/contracts/setup.rb
169
183
  - lib/trmnl/api/dependencies.rb
184
+ - lib/trmnl/api/endpoints/container.rb
170
185
  - lib/trmnl/api/endpoints/current_screen.rb
186
+ - lib/trmnl/api/endpoints/dependencies.rb
171
187
  - lib/trmnl/api/endpoints/display.rb
172
188
  - lib/trmnl/api/endpoints/firmware.rb
173
189
  - lib/trmnl/api/endpoints/log.rb
@@ -176,6 +192,7 @@ files:
176
192
  - lib/trmnl/api/models/display.rb
177
193
  - lib/trmnl/api/models/firmware.rb
178
194
  - lib/trmnl/api/models/setup.rb
195
+ - lib/trmnl/api/requester.rb
179
196
  - trmnl-api.gemspec
180
197
  homepage: https://github.com/usetrmnl/trmnl-api
181
198
  licenses:
@@ -201,7 +218,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
201
218
  - !ruby/object:Gem::Version
202
219
  version: '0'
203
220
  requirements: []
204
- rubygems_version: 3.6.8
221
+ rubygems_version: 3.6.9
205
222
  specification_version: 4
206
223
  summary: A monadic TRMNL API client.
207
224
  test_files: []
metadata.gz.sig CHANGED
Binary file