drive 0.26.0 → 0.27.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: 0bd7bffb05fd92820dff9636b8e45a6d534ba93d4f3e494f27d510110d27c484
4
- data.tar.gz: 6b5e577baef1bb0ccb36b5b870d0ad89cacd229dcbec1fe9265bfa9ed84a514a
3
+ metadata.gz: 7828b1081161322331a35102e95e1cb6f9285a69dd0ffffdd2694dd52eaf8e70
4
+ data.tar.gz: ef2efd33b75c5cded08f19eb6bf2f010659645af095199f10ebdd9cf68bee4e8
5
5
  SHA512:
6
- metadata.gz: 5964e392a7e3ea8d8c3eb20a17675101133e83ad0f57f667411cecd9c8984a23a0bf1089d7640e7a804a9a4c60f75592a2826c3eb575c34d8c5496c7ae2ba055
7
- data.tar.gz: 4815745a2c6193d595268c4f412fd6046e588c353558c55468056b496767db37126592fa237be636c6d20e9f7c44a62d2612f233150a113e65bdc774b2cd2617
6
+ metadata.gz: 9d25f9859ee31c5bf425de1b15f9ed438e7d22bb54c3cc130328ba8226c8d3431352343687f5634f1e1b572117e369a6125c30e5ae6d4df650a807eb5e2be01e
7
+ data.tar.gz: fabfa2d25ae5aadccd3285fdba0d5a34d607b6a80171d2f796c0fc6e0ebc5cbc97e3d2420e92c0adcdf7c1228ae8a8fa5e0d460699699fef0ed78c55810d75ab
data/CHANGELOG.md CHANGED
@@ -6,6 +6,12 @@ For more information about changelogs, check [Keep a Changelog](http://keepachan
6
6
 
7
7
  ## [Unreleased]
8
8
 
9
+ ## 0.27.0 - 2026-11-17
10
+
11
+ * [FEATURE] `rate_limited agent: 60`: pages one user agent may ask for a minute, from every address
12
+ together, for a scraper spread over thousands of addresses. A crawler is not counted, nor a
13
+ controller that says `def agent_limited? = false`; one refused is told nothing of what was counted.
14
+
9
15
  ## 0.26.0 - 2026-11-10
10
16
 
11
17
  * [BREAKING] The easy design system and the error pages are `wordmark`'s
@@ -120,7 +126,8 @@ For more information about changelogs, check [Keep a Changelog](http://keepachan
120
126
  flown by `easy/flyer.js` (ES5, included once by the page) and drawn by `sky` and `flyer` in `easy/components`.
121
127
  On a holiday, by the visitor's own calendar, the plane flies in the day's colors with one line of smoke per color:
122
128
  New Year's Day and Eve, Valentine's Day, St. Patrick's Day, Easter, Memorial Day, Independence Day, National
123
- Aviation Day, Halloween, Veterans Day, Thanksgiving, Wright Brothers Day and Christmas. `date:` flies it on another day, to see one.
129
+ Aviation Day, Halloween, Veterans Day, Thanksgiving, Wright Brothers Day and Christmas. `date:` flies it on another
130
+ day, to see one.
124
131
 
125
132
  * [FEATURE] `i-small-plane`, easyAnnual's single engine, in the sprite beside easyFAA's `i-plane`.
126
133
 
data/README.md CHANGED
@@ -1,12 +1,13 @@
1
1
  # Drive
2
2
 
3
3
  A Rails engine for the easyFAA apps, each behind Cloudflare. What their pages look like, the error
4
- pages included, is [`wordmark`](https://github.com/easyfaa/design)'s.
4
+ pages included, is [`easyfaa`](https://github.com/easyfaa/easyfaa/tree/main/design)'s.
5
5
  A copy of the tables a source publishes is the [`mirroring`](https://github.com/easyfaa/mirroring)
6
6
  gem's.
7
7
 
8
8
  - **`RateLimited`**: `rate_limited` holds an address to 20 pages a minute and 200 a day, or the
9
- app's own limits, and tells it so in words. A search engine's own crawler has no limit.
9
+ app's own limits, and tells it so in words; with `agent:`, a user agent too, from every address
10
+ together. A search engine's own crawler has no limit.
10
11
  - **`Crawler`**: whether a request comes from a search engine's crawler: Cloudflare vouches for
11
12
  it (where `EDGE_KEY` is set), or its address is in the ranges Google, Bing, Apple, Claude,
12
13
  ChatGPT or Perplexity publish, fetched once a day.
@@ -2,8 +2,12 @@
2
2
  # crawler (Crawler) is never held to, so it can list every page. Refused, a client is told why in
3
3
  # words, with status 429, rather than with an error the app would report.
4
4
  #
5
+ # A limit by user agent, from every address together, refuses a scraper spread over thousands of
6
+ # addresses, which still sends a few browsers' names. It is told nothing of what was counted. A
7
+ # controller whose callers all share one user agent turns it off with `def agent_limited? = false`.
8
+ #
5
9
  # rate_limited # 20 a minute and 200 a day
6
- # rate_limited minute: 120, day: nil, scope: 'public'
10
+ # rate_limited minute: 120, day: nil, agent: 60, scope: 'public'
7
11
  module RateLimited
8
12
  extend ActiveSupport::Concern
9
13
 
@@ -14,27 +18,47 @@ module RateLimited
14
18
  class_methods do
15
19
  # @param minute [Integer, nil] pages an address may ask for a minute.
16
20
  # @param day [Integer, nil] pages an address may ask for a day.
21
+ # @param agent [Integer, nil] pages one user agent may ask for a minute, from every address.
17
22
  # @param scope [String, nil] the controllers counted together; each its own if nil.
18
- def rate_limited(minute: 20, day: 200, scope: nil)
23
+ def rate_limited(minute: 20, day: 200, agent: nil, scope: nil)
19
24
  { minute: [minute, 1.minute], day: [day, 1.day] }.each do |name, (to, within)|
20
25
  next unless to
21
26
 
22
27
  rate_limit(to:, within:, name: name.to_s, scope:, unless: -> { crawler? },
23
28
  with: -> { refuse_flood minute:, day: })
24
29
  end
30
+ limit_agents(agent, scope:) if agent
31
+ end
32
+
33
+ private
34
+
35
+ def limit_agents(to, scope:)
36
+ rate_limit(to:, within: 1.minute, by: -> { request.user_agent.to_s }, name: 'agent', scope:,
37
+ if: -> { agent_limited? }, with: -> { refuse_agent_flood })
25
38
  end
26
39
  end
27
40
 
28
41
  private
29
42
 
30
- # Whether a search engine vouches for this client, asked once a request.
31
- def crawler? = defined?(@crawler) ? @crawler : @crawler = Crawler.verified?(request)
43
+ def crawler?
44
+ # A search engine is asked once a request whether it vouches for this client.
45
+ defined?(@crawler) ? @crawler : @crawler = Crawler.verified?(request)
46
+ end
32
47
 
33
- # What a client asking too often is told. A host may say it its own way.
34
48
  def refuse_flood(minute:, day:)
49
+ # A host may say it its own way.
35
50
  limits = { minute:, day: }.compact.map { |per, count| "#{count} a #{per}" }
36
51
  render plain: 'Too many requests. Each address may ask for so many pages: ' \
37
52
  "#{limits.join ' and '}.\n",
38
53
  status: :too_many_requests
39
54
  end
55
+
56
+ def agent_limited? = !crawler?
57
+
58
+ def refuse_agent_flood
59
+ logger.warn "Agent limit: #{request.user_agent} on #{request.path}"
60
+ response.cache_control.merge! private: true, no_store: true
61
+ render plain: "Too many requests right now. Wait a minute, then reload this page.\n",
62
+ status: :too_many_requests
63
+ end
40
64
  end
@@ -47,9 +47,8 @@ module Drive
47
47
  uri = URI "https://api.cloudflare.com/client/v4/zones/#{zone}/purge_cache"
48
48
  response = Net::HTTP.post uri, body.to_json, 'Authorization' => "Bearer #{token}",
49
49
  'Content-Type' => 'application/json'
50
- return if response.is_a? Net::HTTPSuccess
51
-
52
- raise "Cloudflare purge failed: #{response.code} #{response.body}"
50
+ failure = "Cloudflare purge failed: #{response.code} #{response.body}"
51
+ raise failure unless response.is_a? Net::HTTPSuccess
53
52
  end
54
53
 
55
54
  # The zone, from the environment or the credentials.
data/lib/drive/version.rb CHANGED
@@ -1,4 +1,4 @@
1
1
  module Drive
2
2
  # Version of the gem, read by the gemspec.
3
- VERSION = '0.26.0'.freeze
3
+ VERSION = '0.27.0'.freeze
4
4
  end
data/lib/drive.rb CHANGED
@@ -4,6 +4,6 @@ require 'drive/cloudflare'
4
4
  require 'drive/engine'
5
5
 
6
6
  # What every easyFAA app in front of Cloudflare needs alike: Drive::Cloudflare, Crawler,
7
- # RateLimited, NNumber, Designation and Serial. What its pages look like is wordmark's.
7
+ # RateLimited, NNumber, Designation and Serial. What its pages look like is easyfaa's.
8
8
  module Drive
9
9
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: drive
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.26.0
4
+ version: 0.27.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - claudiob