cinebase 5.7.0 → 5.8.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: 633f0032bb5a5125614e1cbd9c8cc2bc3af6b85f14d044a91b7f6a8852267434
4
- data.tar.gz: cd99f85f54e48a7ec255d576de45fc3a41a7dab453de192982e42c3db42a37ea
3
+ metadata.gz: d3391ceb2ce2a381b5309a15b3a0fa381473715b64ea5beace6ec2b3ad78b539
4
+ data.tar.gz: c42a345d442e3ac08927f4bf62a2a9643fbfb7968bc8ce1dff4f67f044477b15
5
5
  SHA512:
6
- metadata.gz: ec6071a79582a6b476fa02cf1a96a3e5888f47c546ffd1250903099cf2469f2a8fd0ef8bd9dec69a8938b8ccaff86669b7a273b854166daac36ac265b0670a53
7
- data.tar.gz: 5d4971c4c9aed5182123eb52f554fb9edc16019ce18fcd24ead64dc1a22508cf52384abf1f3d98e93853603b366512bd8417f7c602d8d6ab3b7046ec2f578785
6
+ metadata.gz: 9239e384416cd3f879076fa276f70dc0156daf58ca3c603e5d114d5fa0c2c9bb2a67143648591811987a072a17034dac2e3cc3989ee5b1a7b82b0a4e7a05ed89
7
+ data.tar.gz: 0d608521b1be86249fc9beb78e2ddfe236f5be565ace247579f2bed487b066891ae7686e34d77e5aa56e8812f9fc4403e1547f8d61cba4ce22fb88198404b879
data/CHANGELOG.md CHANGED
@@ -2,6 +2,14 @@
2
2
  All notable changes to this project will be documented in this file.
3
3
  This project adheres to [Semantic Versioning](http://semver.org/).
4
4
 
5
+ ## [5.8.0] - 2026-11-02
6
+ ### Added
7
+ - `Cinebase::Http` for chain gems' requests: `get`, and `post` with a form or JSON body, over HTTPS. It retries dropped connections twice, never follows redirects, raises errors that name the status and host but leave out the body, and returns bodies as UTF-8. `keep_alive: true` reuses one connection per host until `close`. Tests stub `Cinebase::Http.pause` to skip the waits between attempts.
8
+ - `Cinebase::London` for London dates and times: `today`, `utc` for a listed wall-clock time (the hour repeated when clocks go back is read as BST) and `date` for a time's London date.
9
+
10
+ ### Changed
11
+ - The A1Web and Boxoffice clients, and `Performance#starting_at`, use them. `Performance#starting_at` no longer raises for a local time in the repeated hour when clocks go back.
12
+
5
13
  ## [5.7.0] - 2026-11-01
6
14
  ### Added
7
15
  - `cinebase/a1web`, loaded only on request, for chains whose websites run on Admit One's A1Web, such as Arc, Reel and Movie House
data/cinebase.gemspec CHANGED
@@ -20,6 +20,7 @@ Gem::Specification.new do |spec|
20
20
 
21
21
  spec.add_development_dependency 'base64'
22
22
  spec.add_development_dependency 'bundler', '>= 2.0'
23
+ spec.add_development_dependency 'minitest-mock'
23
24
  spec.add_development_dependency 'minitest-reporters'
24
25
  spec.add_development_dependency 'rake', '>= 13.0'
25
26
  spec.add_development_dependency 'simplecov', '~> 0.22'
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'cgi/escape'
4
- require 'net/http'
5
4
 
6
5
  module Cinebase
7
6
  module A1web
@@ -14,16 +13,13 @@ module Cinebase
14
13
  SITE_CODE = /var sitecode\s*=\s*'([^']+)'/
15
14
  # Pages claim ISO-8859-1 but send Windows-1252 dashes and quotes
16
15
  LEGACY_ENCODING = Encoding::Windows_1252
17
- RETRIES = 2
18
- TRANSIENT_ERRORS = [
19
- EOFError, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::OpenTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError
20
- ].freeze
21
16
 
22
17
  attr_reader :base_url
23
18
 
24
19
  # @param [String] base_url the chain website, e.g. 'https://www.arccinema.co.uk'
25
20
  def initialize(base_url:)
26
21
  @base_url = base_url.delete_suffix('/')
22
+ @http = Http.new
27
23
  end
28
24
 
29
25
  # Venues in the home page's cinema menu, with the site codes the home
@@ -52,29 +48,12 @@ module Cinebase
52
48
  private
53
49
 
54
50
  def get(url)
55
- uri = URI(url)
56
- attempts = 0
57
- begin
58
- response = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 10, read_timeout: 30) do |http|
59
- http.request(Net::HTTP::Get.new(uri))
60
- end
61
- rescue *TRANSIENT_ERRORS
62
- attempts += 1
63
- raise if attempts > RETRIES
64
-
65
- sleep(attempts)
66
- retry
67
- end
68
- # Omit response bodies from errors, and never follow redirects.
69
- raise "HTTP error #{response.code} from #{uri.host}" unless response.is_a?(Net::HTTPSuccess)
70
-
71
- decode(response.body)
51
+ decode(@http.get(url))
72
52
  end
73
53
 
74
54
  def decode(body)
75
- utf8 = body.dup.force_encoding(Encoding::UTF_8)
76
- if utf8.valid_encoding?
77
- utf8
55
+ if body.valid_encoding?
56
+ body
78
57
  else
79
58
  body.dup.force_encoding(LEGACY_ENCODING).encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
80
59
  end
@@ -96,12 +96,12 @@ module Cinebase
96
96
  end
97
97
 
98
98
  def local_date(timestamp)
99
- TZInfo::Timezone.get(TIME_ZONE).to_local(Time.at(timestamp).utc).to_date
99
+ London.date(Time.at(timestamp))
100
100
  end
101
101
 
102
102
  def starting_at(date, time)
103
103
  local = Time.utc(date.year, date.month, date.day, time[1].to_i, time[2].to_i)
104
- TZInfo::Timezone.get(TIME_ZONE).local_to_utc(local) { |periods| periods.first }.utc
104
+ London.utc(local)
105
105
  end
106
106
 
107
107
  # Movie House links leave out the venue's path, and only work with it
@@ -47,7 +47,7 @@ module Cinebase
47
47
  # @return [Array<Cinebase::A1web::Performance>] instances of the subclass
48
48
  def self.at(
49
49
  cinema_id,
50
- from: TZInfo::Timezone.get(TIME_ZONE).now.to_date,
50
+ from: London.today,
51
51
  to: from + 13
52
52
  )
53
53
  raise ArgumentError, 'to must not precede from' if to < from
@@ -56,7 +56,7 @@ module Cinebase
56
56
  if cinema.url.empty?
57
57
  []
58
58
  else
59
- today = TZInfo::Timezone.get(TIME_ZONE).now.to_date
59
+ today = London.today
60
60
  html = cinema_class.client.whats_on(cinema.url)
61
61
  showings = Listings.new(html, venue_url: cinema.url, today: today).showings
62
62
  performances = showings.select { |showing| (from..to).cover?(showing.date) }.map do |showing|
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require 'json'
4
- require 'net/http'
5
4
 
6
5
  module Cinebase
7
6
  module Boxoffice
@@ -16,16 +15,13 @@ module Cinebase
16
15
  MOVIES_PER_REQUEST = 50
17
16
  # Listings days run from 3am to 3am, like the website's
18
17
  DAY_STARTS_AT = 'T03:00:00'
19
- RETRIES = 2
20
- TRANSIENT_ERRORS = [
21
- EOFError, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::OpenTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError
22
- ].freeze
23
18
 
24
19
  attr_reader :base_url
25
20
 
26
21
  # @param [String] base_url the chain website, e.g. 'https://www.everymancinema.com'
27
22
  def initialize(base_url:)
28
23
  @base_url = base_url.delete_suffix('/')
24
+ @http = Http.new
29
25
  end
30
26
 
31
27
  # @return [Array<Hash>] every theatre in the website's daily build
@@ -77,25 +73,7 @@ module Cinebase
77
73
  end
78
74
 
79
75
  def get(url, accept:)
80
- uri = URI(url)
81
- request = Net::HTTP::Get.new(uri)
82
- request['Accept'] = accept
83
- attempts = 0
84
- begin
85
- response = Net::HTTP.start(uri.host, uri.port, use_ssl: true, open_timeout: 10, read_timeout: 30) do |http|
86
- http.request(request)
87
- end
88
- rescue *TRANSIENT_ERRORS
89
- attempts += 1
90
- raise if attempts > RETRIES
91
-
92
- sleep(attempts)
93
- retry
94
- end
95
- # Omit response bodies from errors, and never follow redirects.
96
- raise "HTTP error #{response.code} from #{uri.host}" unless response.is_a?(Net::HTTPSuccess)
97
-
98
- response.body
76
+ @http.get(url, headers: { 'Accept' => accept })
99
77
  end
100
78
  end
101
79
  end
@@ -28,7 +28,7 @@ module Cinebase
28
28
  # @return [Array<Cinebase::Boxoffice::Performance>] instances of the subclass
29
29
  def self.at(
30
30
  cinema_id,
31
- from: TZInfo::Timezone.get('Europe/London').now.to_date,
31
+ from: London.today,
32
32
  to: from + 13
33
33
  )
34
34
  raise ArgumentError, 'to must not precede from' if to < from
@@ -0,0 +1,114 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'json'
4
+ require 'net/http'
5
+
6
+ module Cinebase
7
+ # HTTPS requests for chain gems. Retries dropped connections, never
8
+ # follows redirects and leaves response bodies out of errors, which can
9
+ # be whole HTML pages.
10
+ class Http
11
+ OPEN_TIMEOUT = 10
12
+ READ_TIMEOUT = 30
13
+ RETRIES = 2
14
+ TRANSIENT_ERRORS = [
15
+ EOFError, Errno::ECONNRESET, Errno::ECONNREFUSED, Net::OpenTimeout, Net::ReadTimeout, OpenSSL::SSL::SSLError
16
+ ].freeze
17
+
18
+ # Waits between attempts. Tests stub it.
19
+ # @param [Integer] seconds
20
+ def self.pause(seconds)
21
+ sleep(seconds)
22
+ end
23
+
24
+ # @param [Integer] read_timeout seconds to wait for a response
25
+ # @param [Hash] headers sent with every request, e.g. a User-Agent
26
+ # @param [Boolean] keep_alive reuse one connection per host until
27
+ # {#close}, for chains that need many requests to one slow site
28
+ def initialize(read_timeout: READ_TIMEOUT, headers: {}, keep_alive: false)
29
+ @read_timeout = read_timeout
30
+ @headers = headers
31
+ @keep_alive = keep_alive
32
+ @connections = {}
33
+ end
34
+
35
+ # @param [String] url
36
+ # @param [Hash] headers
37
+ # @return [String] the response body as UTF-8
38
+ def get(url, headers: {})
39
+ perform(Net::HTTP::Get.new(URI(url), @headers.merge(headers)))
40
+ end
41
+
42
+ # @param [String] url
43
+ # @param [Hash] form fields sent form-encoded
44
+ # @param [Hash, Array] json a body sent as JSON
45
+ # @param [Hash] headers
46
+ # @return [String] the response body as UTF-8
47
+ def post(url, form: nil, json: nil, headers: {})
48
+ request = Net::HTTP::Post.new(URI(url), @headers.merge(headers))
49
+ if json
50
+ request['Content-Type'] = 'application/json'
51
+ request.body = JSON.generate(json)
52
+ else
53
+ request.set_form_data(form || {})
54
+ end
55
+ perform(request)
56
+ end
57
+
58
+ # Close kept-alive connections
59
+ # @return [nil]
60
+ def close
61
+ @connections.each_value { |connection| connection.finish if connection.started? }
62
+ @connections.clear
63
+ nil
64
+ end
65
+
66
+ private
67
+
68
+ def perform(request)
69
+ uri = request.uri
70
+ attempts = 0
71
+ begin
72
+ response = send_request(uri, request)
73
+ rescue *TRANSIENT_ERRORS
74
+ drop(uri)
75
+ attempts += 1
76
+ raise if attempts > RETRIES
77
+
78
+ self.class.pause(attempts)
79
+ retry
80
+ end
81
+
82
+ if response.is_a?(Net::HTTPSuccess)
83
+ String.new(response.body.to_s, encoding: Encoding::UTF_8)
84
+ else
85
+ raise "HTTP error #{response.code} from #{uri.host}"
86
+ end
87
+ end
88
+
89
+ def send_request(uri, request)
90
+ if @keep_alive
91
+ connection(uri).request(request)
92
+ else
93
+ start(uri) { |http| http.request(request) }
94
+ end
95
+ end
96
+
97
+ # Net::HTTP's own retries would repeat requests behind ours
98
+ def start(uri, &block)
99
+ Net::HTTP.start(uri.host, uri.port, use_ssl: uri.scheme == 'https', open_timeout: OPEN_TIMEOUT,
100
+ read_timeout: @read_timeout, max_retries: 0, &block)
101
+ end
102
+
103
+ def connection(uri)
104
+ @connections[[uri.host, uri.port]] ||= start(uri)
105
+ end
106
+
107
+ def drop(uri)
108
+ connection = @connections.delete([uri.host, uri.port])
109
+ connection.finish if connection&.started?
110
+ rescue IOError
111
+ nil
112
+ end
113
+ end
114
+ end
@@ -0,0 +1,32 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cinebase
4
+ # Dates and times in London, where chain websites list showtimes
5
+ module London
6
+ ZONE = 'Europe/London'
7
+
8
+ # @return [Date] today's date in London
9
+ def self.today
10
+ zone.now.to_date
11
+ end
12
+
13
+ # @param [Time] local a London wall-clock time whose fields are read as
14
+ # is, whatever its offset
15
+ # @return [Time] the UTC time. A time in the hour repeated when clocks go
16
+ # back is read as British Summer Time.
17
+ def self.utc(local)
18
+ zone.local_to_utc(local) { |periods| periods.first }.utc
19
+ end
20
+
21
+ # @param [Time] time
22
+ # @return [Date] its date in London
23
+ def self.date(time)
24
+ zone.to_local(time).to_date
25
+ end
26
+
27
+ # @return [TZInfo::Timezone]
28
+ def self.zone
29
+ TZInfo::Timezone.get(ZONE)
30
+ end
31
+ end
32
+ end
@@ -35,7 +35,7 @@ module Cinebase
35
35
  if @time.utc?
36
36
  @time
37
37
  else
38
- TZInfo::Timezone.get('Europe/London').local_to_utc(@time)
38
+ London.utc(@time)
39
39
  end
40
40
  end
41
41
 
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Cinebase version
4
4
  module Cinebase
5
- VERSION = '5.7.0'
5
+ VERSION = '5.8.0'
6
6
  end
data/lib/cinebase.rb CHANGED
@@ -7,5 +7,7 @@ require 'cinebase/version'
7
7
 
8
8
  require 'cinebase/cinema'
9
9
  require 'cinebase/festival'
10
+ require 'cinebase/http'
11
+ require 'cinebase/london'
10
12
  require 'cinebase/performance'
11
13
  require 'cinebase/title_sanitizer'
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cinebase
3
3
  version: !ruby/object:Gem::Version
4
- version: 5.7.0
4
+ version: 5.8.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Andy Croll
@@ -37,6 +37,20 @@ dependencies:
37
37
  - - ">="
38
38
  - !ruby/object:Gem::Version
39
39
  version: '2.0'
40
+ - !ruby/object:Gem::Dependency
41
+ name: minitest-mock
42
+ requirement: !ruby/object:Gem::Requirement
43
+ requirements:
44
+ - - ">="
45
+ - !ruby/object:Gem::Version
46
+ version: '0'
47
+ type: :development
48
+ prerelease: false
49
+ version_requirements: !ruby/object:Gem::Requirement
50
+ requirements:
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: '0'
40
54
  - !ruby/object:Gem::Dependency
41
55
  name: minitest-reporters
42
56
  requirement: !ruby/object:Gem::Requirement
@@ -156,6 +170,8 @@ files:
156
170
  - lib/cinebase/boxoffice/showtime.rb
157
171
  - lib/cinebase/cinema.rb
158
172
  - lib/cinebase/festival.rb
173
+ - lib/cinebase/http.rb
174
+ - lib/cinebase/london.rb
159
175
  - lib/cinebase/performance.rb
160
176
  - lib/cinebase/title_sanitizer.rb
161
177
  - lib/cinebase/version.rb