nohead 0.23.0 → 0.25.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: a99211f471e2f677b87b48aee6ccf2343e95574763b69461ce33991f7f442491
4
- data.tar.gz: 68d39ad922b0f0368699117e1b71d1a1391202b6e6896794e59f4db67c2b7353
3
+ metadata.gz: 674200ce5cc87013392845f0cde4d8c6fef0d4ad24792546a02b720078202b1a
4
+ data.tar.gz: 50d85ee9bd83c38c2cc6a6ec1708c2fd62d9c16fca281656db0d514e2e505c58
5
5
  SHA512:
6
- metadata.gz: 2feda70365877cc8f3c199fb4021efdb80c026f3aff8871abc19ba95589087fe16e9fd0709cbb072590f47ed53161be2c662f80360edd8591f7f9fab36ad9896
7
- data.tar.gz: 17783fc5240e96c4eb2ba6d1c13b271204af61da9403621b9af1fd88efa05f5df4e2b30a43973426acc69bad76b0c3dfc04268a55cc66bf1c4028662cc9b595d
6
+ metadata.gz: 20266a9a1f0877783c66c806ad33f91f776e5ddf78415d156317de82d04078312363c41702301fbd3ea5c04404b483f44aa9ca4bec31e5d1e010388a9fc8211e
7
+ data.tar.gz: e4faa5a115ec9d06fc26b5cff609006285982367254d9480874c170ca2c784a4de044c4263f731b3acfd9f77dda6b1b24a636c9b7324c3d111cd8e7991b675ac
data/CHANGELOG.md CHANGED
@@ -5,6 +5,36 @@ Changes to the `nohead` gem that you can notice. Versions follow
5
5
  releases; a change that could break your code is a major one. Each release's
6
6
  section is its GitHub release's notes.
7
7
 
8
+ ## 0.25.0
9
+
10
+ - A field's search options are its configuration's `search` hash, as the
11
+ API now has them: `searchable`, `filterable`, `sortable` and `returned`,
12
+ where a relation's `searchable` and an asset's or relation's `returned`
13
+ take a list (related field IDs, or an asset's parts) in place of `true`.
14
+ The gem passes configurations through as they are, so code that sets
15
+ the old `searchable`, `filterable`, `sortable`, `included`, `expanded`
16
+ or `searched` options needs the new shape. Search-only related fields
17
+ filter and facet under `expanded.<key>.unreturned.<field>`.
18
+
19
+ ## 0.24.0
20
+
21
+ - A `base_url` with an IPv6 address (`http://[::1]:3000`) connects; it
22
+ failed to resolve the bracketed address.
23
+ - `assets.upload` sends at most `byte_size` bytes of a file, and raises
24
+ `UploadError` when the file has fewer; it sent every byte the file had.
25
+ A path or an open file that isn't a regular file, such as a named pipe
26
+ or `/dev/stdin`, needs `byte_size`, as a stream does: its size on disk
27
+ isn't what it holds.
28
+ - The path of a directory raises `Errno::EISDIR` before an upload starts,
29
+ rather than `ConnectionError` once it had.
30
+ - A `transport:` of your own gets an upload's body as an object that reads
31
+ as an IO does (`read`, `readpartial` and `size`) and stops at
32
+ `byte_size`, rather than the file itself.
33
+ - An empty `idempotency_key` for `assets.upload` counts as not given, as it
34
+ does for every other method.
35
+ - `Webhooks.unwrap` says a `webhook-timestamp` that isn't digits is not a
36
+ number, rather than too far from the current time, as the other SDKs do.
37
+
8
38
  ## 0.23.0
9
39
 
10
40
  - A webhook's `failing_since` and `last_succeeded_at` (its health) are
data/lib/nohead/client.rb CHANGED
@@ -151,7 +151,7 @@ module Nohead
151
151
  begin
152
152
  response = @transport.call(method, url,
153
153
  headers.merge("Content-Length" => source.byte_size.to_s),
154
- source.io, @timeout)
154
+ source, @timeout)
155
155
  return response unless RETRYABLE_STATUSES.include?(response.status) && attempt < retries
156
156
 
157
157
  failed = { status: response.status }
@@ -25,7 +25,7 @@ module Nohead
25
25
  **{ content_type: source.content_type, alt: alt }.compact,
26
26
  idempotency_key: idempotency_key)
27
27
  # A replayed start returns the first run's asset, which may be done.
28
- if idempotency_key
28
+ unless idempotency_key.to_s.empty?
29
29
  current = get(created.id)
30
30
  return current if current.status == "ready"
31
31
  # The key replays the first run's upload URL, which expires sooner.
@@ -12,7 +12,10 @@ module Nohead
12
12
  # Sends requests with Net::HTTP. A client's `transport:` can be anything
13
13
  # with the same `call`, e.g. for tests.
14
14
  class Transport
15
- # `body` is a String, an IO (streamed, with Content-Length), or nil.
15
+ # `body` is a String, nil, or, for an upload, a stream to send with its
16
+ # Content-Length: it reads as an IO does (`read(length = nil, buffer =
17
+ # nil)` and `readpartial(length, buffer = nil)`) up to that length,
18
+ # which is its `size`.
16
19
  def call(method, url, headers, body, timeout)
17
20
  uri = URI(url)
18
21
  request = Net::HTTPGenericRequest.new(method, !body.nil?, true, uri.request_uri, headers)
@@ -24,10 +27,11 @@ module Nohead
24
27
  # The proxy comes from the environment (http_proxy, no_proxy), as Net::HTTP.new.
25
28
  # No retries of its own (it retries a GET, PUT or DELETE that times out
26
29
  # or loses its connection once, and a PUT with the stream already
27
- # read): the client retries, and knows when it can.
28
- response = Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == "https", open_timeout: timeout,
29
- read_timeout: timeout, write_timeout: timeout,
30
- max_retries: 0) do |http|
30
+ # read): the client retries, and knows when it can. `hostname` is an
31
+ # IPv6 address without its brackets; Net::HTTP puts them back in Host.
32
+ response = Net::HTTP.start(uri.hostname, uri.port, use_ssl: uri.scheme == "https", open_timeout: timeout,
33
+ read_timeout: timeout, write_timeout: timeout,
34
+ max_retries: 0) do |http|
31
35
  http.request(request)
32
36
  end
33
37
  Response.new(response.code.to_i, response.each_header.to_h, response.body.to_s)
@@ -6,25 +6,75 @@ module Nohead
6
6
  # Reading files for assets.upload: a path (String or Pathname) or an IO
7
7
  # opened in binary mode (File, StringIO).
8
8
  class UploadSource
9
- attr_reader :filename, :content_type, :byte_size, :io
9
+ attr_reader :filename, :content_type, :byte_size
10
10
 
11
11
  def initialize(file, filename: nil, content_type: nil, byte_size: nil)
12
- name, size = file.is_a?(String) || file.is_a?(Pathname) ? open_path(Pathname(file)) : open_io(file)
12
+ name, size = if file.is_a?(String) || file.is_a?(Pathname)
13
+ open_path(Pathname(file), byte_size)
14
+ else
15
+ open_io(file)
16
+ end
13
17
  # An empty filename or content type is the same as none.
14
18
  @filename = present(filename) || present(name) || "upload"
15
19
  @byte_size = byte_size || size or
16
- raise UploadError, "Uploading a stream that cannot seek needs byte_size:"
20
+ raise UploadError, "Uploading a stream that isn't a file needs byte_size:"
21
+ if size && size < @byte_size
22
+ close
23
+ raise too_short(size)
24
+ end
17
25
  # Without one, the API types the file by its filename, as for every client.
18
26
  @content_type = present(content_type)
19
27
  @start = position(@io)
28
+ @left = @byte_size
29
+ end
30
+
31
+ # The bytes for the transport, as IO#read reads them: the first
32
+ # byte_size, the length the upload declared. Raises UploadError when the
33
+ # file ends sooner.
34
+ def read(length = nil, buffer = nil)
35
+ if @left.zero?
36
+ buffer&.clear
37
+ return length ? nil : buffer || +""
38
+ end
39
+
40
+ chunk = @io.read(length ? [length, @left].min : @left, buffer)
41
+ size = chunk.to_s.bytesize
42
+ raise too_short(byte_size - @left + size) if chunk.nil? || (length.nil? && size < @left)
43
+
44
+ @left -= size
45
+ chunk
46
+ end
47
+
48
+ # As IO#readpartial: what the file has ready, so a pipe streams as it
49
+ # fills (IO.copy_stream, and so Net::HTTP, reads with it), up to
50
+ # byte_size in all.
51
+ def readpartial(length, buffer = nil)
52
+ return read(length, buffer) || raise(EOFError) unless @io.respond_to?(:readpartial)
53
+ raise EOFError if @left.zero?
54
+
55
+ chunk = @io.readpartial([length, @left].min, buffer)
56
+ @left -= chunk.bytesize
57
+ chunk
58
+ rescue EOFError
59
+ raise too_short(byte_size - @left) unless @left.zero?
60
+
61
+ raise
20
62
  end
21
63
 
22
64
  # Whether the bytes can be sent again (for retries): a pipe or socket
23
65
  # can't seek back to where they start.
24
66
  def replayable? = !@start.nil? && @io.respond_to?(:pos=)
25
67
 
68
+ # The length a transport sends, as IO#size.
69
+ alias size byte_size
70
+
71
+ # Raises Errno::ESPIPE, as a pipe's rewind does, for bytes that can't be
72
+ # sent again.
26
73
  def rewind
27
- @io.pos = @start if replayable?
74
+ raise Errno::ESPIPE, filename unless replayable?
75
+
76
+ @io.pos = @start
77
+ @left = @byte_size
28
78
  end
29
79
 
30
80
  def close
@@ -33,14 +83,25 @@ module Nohead
33
83
 
34
84
  private
35
85
 
86
+ def too_short(size)
87
+ UploadError.new("Could not upload #{filename}: it has #{size} bytes, fewer than its byte_size (#{byte_size})")
88
+ end
89
+
36
90
  def present(value)
37
91
  value unless value.to_s.empty?
38
92
  end
39
93
 
40
- def open_path(path)
94
+ def open_path(path, byte_size)
95
+ stat = path.stat # a missing path raises Errno::ENOENT, as opening it would
96
+ raise Errno::EISDIR, path.to_s if stat.directory?
97
+
98
+ # A pipe's or a device's size (/dev/stdin) is not what it holds.
99
+ size = stat.size if stat.file?
100
+ raise UploadError, "Uploading #{path.basename}, which is not a file, needs byte_size:" unless size || byte_size
101
+
41
102
  @io = path.open("rb")
42
103
  @owned = true
43
- [path.basename.to_s, path.size]
104
+ [path.basename.to_s, size]
44
105
  end
45
106
 
46
107
  def open_io(io)
@@ -58,6 +119,8 @@ module Nohead
58
119
  end
59
120
 
60
121
  def remaining(io)
122
+ # A device's size (File.open("/dev/stdin")) is not what it holds.
123
+ return nil if io.respond_to?(:stat) && !io.stat.file?
61
124
  return io.size - io.pos if io.respond_to?(:size) && io.respond_to?(:pos)
62
125
 
63
126
  nil
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Nohead
4
- VERSION = "0.23.0"
4
+ VERSION = "0.25.0"
5
5
  end
@@ -27,7 +27,8 @@ module Nohead
27
27
  unless id && timestamp && signatures
28
28
  raise WebhookVerificationError, "Missing webhook-id, webhook-timestamp or webhook-signature header"
29
29
  end
30
- unless timestamp.match?(/\A\d+\z/) && (Time.now.to_i - timestamp.to_i).abs <= tolerance
30
+ raise WebhookVerificationError, "The webhook timestamp is not a number" unless timestamp.match?(/\A\d+\z/)
31
+ if (Time.now.to_i - timestamp.to_i).abs > tolerance
31
32
  raise WebhookVerificationError, "The webhook timestamp is too far from the current time"
32
33
  end
33
34
 
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: nohead
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.23.0
4
+ version: 0.25.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nohead