trmnl-api 0.14.0 → 0.15.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: dedac6b2c46579d646be122b295be973bb3c39bbc35847da48d77f9ce2f506ae
4
- data.tar.gz: dd7ec7571e0c2cc0f74c67bfc78c4e157b8590a8736913102e582959a85f920c
3
+ metadata.gz: 31b74fda2dfe3464acf9af9f49d56fd0836170d300229e32f46bbe3b468042ef
4
+ data.tar.gz: ce20f646705190666a4b1e59f04afafc94349e5099bad69b5392f015b26dccff
5
5
  SHA512:
6
- metadata.gz: 4b18624c8fb2ec5c395a58824691d5da9a1495f8bc26fe41f1119de67cde3aa6d1d2ef4657a72a490e74db51343c7aceea5930887624cdb5c9ae3a7e1fb46f65
7
- data.tar.gz: 91c83467a642383c7edf9ed5decd35b9514fbf45382a9574e39e4510dc48ac748bc70ae0cec6a218fa312652268968826cd2c65c216015d589564a6cd64c07dc
6
+ metadata.gz: d51252433f8303f6c47d26dec1db210304086a90a3feb2de6470cee096cb223e08817c937de85ee3ccd202dd84cec6c5de1093841be805b58b9d4c3f9e53d250
7
+ data.tar.gz: 8633a2468b9397c236a15f3a531a256c414bfae4f1187b76916f9de150e394c2b10363ed6a7f3779fe5c112f23846bacfaec1e789367a17a5f36f43f64010a08
checksums.yaml.gz.sig CHANGED
Binary file
data/README.adoc CHANGED
@@ -62,13 +62,17 @@ See xref:_endpoints[Endpoints] for further details.
62
62
 
63
63
  === Configuration
64
64
 
65
- 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
+ By default, you shouldn't need to change the default configuration but you can use a block to adjust settings as desired. If you don't configure the client, then the following defaults will be used:
66
66
 
67
67
  [source,ruby]
68
68
  ----
69
69
  client = TRMNL::API.new do |settings|
70
70
  settings.content_type = "application/json",
71
+ settings.timeout_connect = 2
72
+ settings.timeout_read = 10
73
+ settings.timeout_write = 10
71
74
  settings.uri = "https://trmnl.app/api"
75
+ settings.user_agent = "TRMNL API"
72
76
  end
73
77
  ----
74
78
 
@@ -77,7 +81,11 @@ end
77
81
  You can configure the client via the following environment variables:
78
82
 
79
83
  * `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"`.
84
+ * `TRMNL_API_TIMEOUT_CONNECT`: Defines the time, in seconds, for establishing a connection. Default: `2`.
85
+ * `TRMNL_API_TIMEOUT_READ`: Defines the time, in seconds, for reading content. Default: `10`.
86
+ * `TRMNL_API_TIMEOUT_WRITE`: Defines the time, in seconds, for writing content. Default: `10`.
80
87
  * `TRMNL_API_URI`: Defines the API URI. Default: `"https://trmnl.app/api"`.
88
+ * `TRMNL_API_USER_AGENT`: Defines the HTTP `User-Agent` header. You shouldn't need to change this but is here if you need need to customize this for upstream application identification. Default: `"TRMNL API"`. The fully computed user agent ends up being: `"http.rb/6.0.0 (TRMNL API)"` (only the value in parenthesis is configurable).
81
89
 
82
90
  Any/all environment changes will be applied unless you override these settings via the client configuration block shown above.
83
91
 
@@ -143,18 +151,6 @@ client.display token: "secret"
143
151
  # )
144
152
  ----
145
153
 
146
- ==== Firmware
147
-
148
- Allows you to obtain the current stable firmware version. Example:
149
-
150
- [source,ruby]
151
- ----
152
- client = TRMNL::API.new
153
- client.firmware
154
-
155
- # Success(#<data TRMNL::API::Models::Firmware url="https://trmnl-fw.s3.us-east-2.amazonaws.com/FW1.4.8.bin", version="1.4.8">)
156
- ----
157
-
158
154
  ==== IP Addresses
159
155
 
160
156
  Allows you obtain a list of public IP addresses for all TRMNL core servers because plugin poll requests will only originate from these IPs. Example:
@@ -178,6 +174,22 @@ client.ip_addresses
178
174
  # )
179
175
  ----
180
176
 
177
+ ==== Latest Firmware
178
+
179
+ Allows you to obtain the latest stable firmware version. Example:
180
+
181
+ [source,ruby]
182
+ ----
183
+ client = TRMNL::API.new
184
+ client.latest_firmware
185
+
186
+ # Success(
187
+ # #<data TRMNL::API::Models::LatestFirmware
188
+ # url="https://trmnl-fw.s3.us-east-2.amazonaws.com/FW1.4.8.bin",
189
+ # version="1.4.8">
190
+ # )
191
+ ----
192
+
181
193
  ==== Log
182
194
 
183
195
  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:
@@ -13,8 +13,8 @@ module TRMNL
13
13
  categories: Endpoints::Category,
14
14
  current_screen: Endpoints::CurrentScreen,
15
15
  display: Endpoints::Display,
16
- firmware: Endpoints::Firmware,
17
16
  ip_addresses: Endpoints::IPAddress,
17
+ latest_firmware: Endpoints::LatestFirmware,
18
18
  log: Endpoints::Log,
19
19
  models: Endpoints::Model,
20
20
  palettes: Endpoints::Palette,
@@ -40,10 +40,10 @@ module TRMNL
40
40
 
41
41
  def display(**) = endpoints.fetch(__method__).call(**)
42
42
 
43
- def firmware = endpoints.fetch(__method__).call
44
-
45
43
  def ip_addresses = endpoints.fetch(__method__).call
46
44
 
45
+ def latest_firmware = endpoints.fetch(__method__).call
46
+
47
47
  def log(**) = endpoints.fetch(__method__).call(**)
48
48
 
49
49
  def models(**) = endpoints.fetch(__method__).call(**)
@@ -1,11 +1,25 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "http"
4
+
3
5
  module TRMNL
4
6
  module API
5
7
  module Configuration
6
8
  # Provides API client configuration content.
7
- Content = Struct.new :content_type, :uri do
8
- def headers = {"Content-Type" => content_type}.compact
9
+ Content = Struct.new(
10
+ :content_type,
11
+ :timeout_connect,
12
+ :timeout_read,
13
+ :timeout_write,
14
+ :uri,
15
+ :user_agent
16
+ ) do
17
+ def headers
18
+ agent = " (#{user_agent})" if user_agent
19
+ {content_type: content_type, user_agent: "http.rb/#{HTTP::VERSION}#{agent}"}.compact
20
+ end
21
+
22
+ def timeout = {connect: timeout_connect, read: timeout_read, write: timeout_write}
9
23
  end
10
24
  end
11
25
  end
@@ -13,13 +13,21 @@ module TRMNL
13
13
  def call
14
14
  model[
15
15
  content_type: environment.fetch("TRMNL_API_CONTENT_TYPE", "application/json"),
16
- uri: environment.fetch("TRMNL_API_URI", "https://trmnl.com/api")
16
+ timeout_connect: environment.fetch("TRMNL_API_TIMEOUT_CONNECT", 2).to_i,
17
+ timeout_read: environment.fetch("TRMNL_API_TIMEOUT_READ", 10).to_i,
18
+ timeout_write: environment.fetch("TRMNL_API_TIMEOUT_WRITE", 10).to_i,
19
+ uri: environment.fetch("TRMNL_API_URI", "https://trmnl.com/api"),
20
+ user_agent: "http.rb/#{HTTP::VERSION} (#{default_user_agent})"
17
21
  ]
18
22
  end
19
23
 
20
24
  private
21
25
 
22
26
  attr_reader :model, :environment
27
+
28
+ def default_user_agent
29
+ environment.fetch "TRMNL_API_USER_AGENT", TRMNL::API.name.gsub("::", " ")
30
+ end
23
31
  end
24
32
  end
25
33
  end
@@ -8,22 +8,25 @@ module TRMNL
8
8
  module API
9
9
  # Registers application dependencies.
10
10
  module Container
11
- extend Containable
11
+ extend Containable[register: Containers::Register]
12
12
 
13
13
  register(:settings, as: :fresh) { TRMNL::API::Configuration::Loader.new.call }
14
14
  register(:requester) { API::Requester.new }
15
15
  register(:logger) { Cogger.new id: "trmnl-api", formatter: :json }
16
16
 
17
17
  register :http do
18
- HTTP.default_options = HTTP::Options.new features: {logging: {logger: self[:logger]}}
19
- HTTP
18
+ settings = self[:settings]
19
+
20
+ HTTP.timeout(settings.timeout)
21
+ .headers(settings.headers)
22
+ .use(logging: {logger: self[:logger]})
20
23
  end
21
24
 
22
25
  namespace :schemas do
23
26
  register :category, Schemas::Category
24
27
  register :current_screen, Schemas::CurrentScreen
25
28
  register :display, Schemas::Display
26
- register :firmware, Schemas::Firmware
29
+ register :latest_firmware, Schemas::LatestFirmware
27
30
  register :ip_address, Schemas::IPAddress
28
31
  register :model, Schemas::Model
29
32
  register :palette, Schemas::Palette
@@ -34,7 +37,7 @@ module TRMNL
34
37
  namespace :models do
35
38
  register :current_screen, Models::CurrentScreen
36
39
  register :display, Models::Display
37
- register :firmware, Models::Firmware
40
+ register :latest_firmware, Models::LatestFirmware
38
41
  register :ip_address, Models::IPAddress
39
42
  register :model, Models::Model
40
43
  register :palette, Models::Palette
@@ -0,0 +1,34 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "containable"
4
+ require "refinements/array"
5
+
6
+ module TRMNL
7
+ module API
8
+ module Containers
9
+ # A custom Containable register that allows specific dependencies to be overwritten.
10
+ class Register < Containable::Register
11
+ using Refinements::Array
12
+
13
+ def initialize(*, allowed_keys: %w[http logger], **)
14
+ super(*, **)
15
+ @allowed_keys = allowed_keys
16
+ end
17
+
18
+ protected
19
+
20
+ def check_duplicate key, namespaced_key
21
+ return if allowed_keys.include? namespaced_key
22
+
23
+ message = "Dependency is already registered: #{key.inspect}."
24
+
25
+ fail KeyError, message if dependencies.key? namespaced_key
26
+ end
27
+
28
+ private
29
+
30
+ attr_reader :allowed_keys
31
+ end
32
+ end
33
+ end
34
+ end
@@ -7,11 +7,11 @@ module TRMNL
7
7
  module API
8
8
  module Endpoints
9
9
  # Handles API request/response.
10
- class Firmware
10
+ class LatestFirmware
11
11
  include TRMNL::API::Dependencies[
12
12
  :requester,
13
- schema: "schemas.firmware",
14
- model: "models.firmware"
13
+ schema: "schemas.latest_firmware",
14
+ model: "models.latest_firmware"
15
15
  ]
16
16
 
17
17
  include Inspectable[schema: :type]
@@ -4,7 +4,7 @@ module TRMNL
4
4
  module API
5
5
  module Models
6
6
  # Models the data of the API response.
7
- Firmware = ::Data.define :url, :version do
7
+ LatestFirmware = ::Data.define :url, :version do
8
8
  def self.for(attributes) = new(**attributes)
9
9
  end
10
10
  end
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "core"
3
4
  require "refinements/hash"
4
5
 
5
6
  module TRMNL
@@ -36,7 +37,7 @@ module TRMNL
36
37
  private
37
38
 
38
39
  def apply_defaults
39
- self[:palette_names] ||= []
40
+ self[:palette_names] ||= Core::EMPTY_ARRAY
40
41
 
41
42
  %i[colors bit_depth scale_factor rotation width height offset_x offset_y].each do |name|
42
43
  self[name] ||= 0
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "core"
3
4
  require "refinements/hash"
4
5
 
5
6
  module TRMNL
@@ -14,7 +15,7 @@ module TRMNL
14
15
  def initialize(**)
15
16
  super
16
17
  self[:grays] ||= 0
17
- self[:colors] ||= []
18
+ self[:colors] ||= Core::EMPTY_ARRAY
18
19
  freeze
19
20
  end
20
21
  end
@@ -1,11 +1,13 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "core"
4
+
3
5
  module TRMNL
4
6
  module API
5
7
  module Models
6
8
  # Models the payload of the API response.
7
9
  Recipe = ::Data.define :data, :meta do
8
- def self.empty(meta: Recipes::Meta.new) = new(data: [], meta:)
10
+ def self.empty(meta: Recipes::Meta.new) = new(data: Core::EMPTY_ARRAY, meta:)
9
11
 
10
12
  def self.for(**attributes)
11
13
  meta = attributes.slice :from,
@@ -1,5 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require "core"
3
4
  require "dry/monads"
4
5
  require "http"
5
6
 
@@ -10,22 +11,20 @@ module TRMNL
10
11
  include Dependencies[:settings, :http, :logger]
11
12
  include Dry::Monads[:result]
12
13
 
13
- HEADERS = {}.freeze
14
-
15
14
  def initialize(**)
16
15
  super
17
16
  yield settings if block_given?
18
17
  end
19
18
 
20
- def get(path, headers: HEADERS, **params) = call(__method__, path, headers, params:)
19
+ def get(path, headers: Core::EMPTY_HASH, **params) = call(__method__, path, headers, params:)
21
20
 
22
- def post(path, headers: HEADERS, **json) = call(__method__, path, headers, json:)
21
+ def post(path, headers: Core::EMPTY_HASH, **json) = call(__method__, path, headers, json:)
23
22
 
24
23
  private
25
24
 
26
25
  def call method, path, headers, **options
27
- http.headers(settings.headers.merge(headers))
28
- .public_send(method, uri(path), options)
26
+ http.headers(headers)
27
+ .public_send(method, uri(path), **options)
29
28
  .then { |response| response.status.success? ? Success(response) : Failure(response) }
30
29
  rescue HTTP::RequestError => error then handle_bad_request path, error
31
30
  rescue HTTP::ConnectionError => error then handle_bad_connection path, error
@@ -6,7 +6,7 @@ module TRMNL
6
6
  module API
7
7
  module Schemas
8
8
  # Validates API response.
9
- Firmware = Dry::Schema.JSON do
9
+ LatestFirmware = Dry::Schema.JSON do
10
10
  required(:url).filled :string
11
11
  required(:version).filled :string
12
12
  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.14.0"
5
+ spec.version = "0.15.0"
6
6
  spec.authors = ["TRMNL"]
7
7
  spec.email = ["engineering@trmnl.com"]
8
8
  spec.homepage = "https://github.com/usetrmnl/trmnl-api"
@@ -25,10 +25,11 @@ Gem::Specification.new do |spec|
25
25
 
26
26
  spec.add_dependency "cogger", "~> 2.0"
27
27
  spec.add_dependency "containable", "~> 2.0"
28
+ spec.add_dependency "core", "~> 3.2"
28
29
  spec.add_dependency "dry-monads", "~> 1.9"
29
30
  spec.add_dependency "dry-schema", "~> 1.14"
30
31
  spec.add_dependency "dry-types", "~> 1.8"
31
- spec.add_dependency "http", "~> 5.3"
32
+ spec.add_dependency "http", "~> 6.0"
32
33
  spec.add_dependency "infusible", "~> 5.0"
33
34
  spec.add_dependency "inspectable", "~> 1.0"
34
35
  spec.add_dependency "pipeable", "~> 2.0"
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.14.0
4
+ version: 0.15.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - TRMNL
@@ -63,6 +63,20 @@ dependencies:
63
63
  - - "~>"
64
64
  - !ruby/object:Gem::Version
65
65
  version: '2.0'
66
+ - !ruby/object:Gem::Dependency
67
+ name: core
68
+ requirement: !ruby/object:Gem::Requirement
69
+ requirements:
70
+ - - "~>"
71
+ - !ruby/object:Gem::Version
72
+ version: '3.2'
73
+ type: :runtime
74
+ prerelease: false
75
+ version_requirements: !ruby/object:Gem::Requirement
76
+ requirements:
77
+ - - "~>"
78
+ - !ruby/object:Gem::Version
79
+ version: '3.2'
66
80
  - !ruby/object:Gem::Dependency
67
81
  name: dry-monads
68
82
  requirement: !ruby/object:Gem::Requirement
@@ -111,14 +125,14 @@ dependencies:
111
125
  requirements:
112
126
  - - "~>"
113
127
  - !ruby/object:Gem::Version
114
- version: '5.3'
128
+ version: '6.0'
115
129
  type: :runtime
116
130
  prerelease: false
117
131
  version_requirements: !ruby/object:Gem::Requirement
118
132
  requirements:
119
133
  - - "~>"
120
134
  - !ruby/object:Gem::Version
121
- version: '5.3'
135
+ version: '6.0'
122
136
  - !ruby/object:Gem::Dependency
123
137
  name: infusible
124
138
  requirement: !ruby/object:Gem::Requirement
@@ -190,14 +204,15 @@ files:
190
204
  - lib/trmnl/api/configuration/content.rb
191
205
  - lib/trmnl/api/configuration/loader.rb
192
206
  - lib/trmnl/api/container.rb
207
+ - lib/trmnl/api/containers/register.rb
193
208
  - lib/trmnl/api/dependencies.rb
194
209
  - lib/trmnl/api/endpoints/category.rb
195
210
  - lib/trmnl/api/endpoints/container.rb
196
211
  - lib/trmnl/api/endpoints/current_screen.rb
197
212
  - lib/trmnl/api/endpoints/dependencies.rb
198
213
  - lib/trmnl/api/endpoints/display.rb
199
- - lib/trmnl/api/endpoints/firmware.rb
200
214
  - lib/trmnl/api/endpoints/ip_address.rb
215
+ - lib/trmnl/api/endpoints/latest_firmware.rb
201
216
  - lib/trmnl/api/endpoints/log.rb
202
217
  - lib/trmnl/api/endpoints/model.rb
203
218
  - lib/trmnl/api/endpoints/palette.rb
@@ -206,8 +221,8 @@ files:
206
221
  - lib/trmnl/api/locale_reducer.rb
207
222
  - lib/trmnl/api/models/current_screen.rb
208
223
  - lib/trmnl/api/models/display.rb
209
- - lib/trmnl/api/models/firmware.rb
210
224
  - lib/trmnl/api/models/ip_address.rb
225
+ - lib/trmnl/api/models/latest_firmware.rb
211
226
  - lib/trmnl/api/models/model.rb
212
227
  - lib/trmnl/api/models/palette.rb
213
228
  - lib/trmnl/api/models/recipe.rb
@@ -220,8 +235,8 @@ files:
220
235
  - lib/trmnl/api/schemas/category.rb
221
236
  - lib/trmnl/api/schemas/current_screen.rb
222
237
  - lib/trmnl/api/schemas/display.rb
223
- - lib/trmnl/api/schemas/firmware.rb
224
238
  - lib/trmnl/api/schemas/ip_address.rb
239
+ - lib/trmnl/api/schemas/latest_firmware.rb
225
240
  - lib/trmnl/api/schemas/model.rb
226
241
  - lib/trmnl/api/schemas/palette.rb
227
242
  - lib/trmnl/api/schemas/recipe.rb
@@ -253,7 +268,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
253
268
  - !ruby/object:Gem::Version
254
269
  version: '0'
255
270
  requirements: []
256
- rubygems_version: 4.0.9
271
+ rubygems_version: 4.0.11
257
272
  specification_version: 4
258
273
  summary: A monadic TRMNL API client.
259
274
  test_files: []
metadata.gz.sig CHANGED
Binary file