cinebase 5.6.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: 25f1d3c24d08ce94759a22227a9d47764c194d9c9ace00d91b7daee90eedbc2d
4
- data.tar.gz: 18f1ee13b3ffdd0274f6eed018cf1fdb834f1e598b99d680255b81d6c425ff94
3
+ metadata.gz: d3391ceb2ce2a381b5309a15b3a0fa381473715b64ea5beace6ec2b3ad78b539
4
+ data.tar.gz: c42a345d442e3ac08927f4bf62a2a9643fbfb7968bc8ce1dff4f67f044477b15
5
5
  SHA512:
6
- metadata.gz: 49f12f235682b35ce9455b0a81468c8c99518b30271e8355c73560bf9e5af5b580a3476b1aff6bc995b78b70301012f078044c677454aefa65be5a2805589f0d
7
- data.tar.gz: e5161f9a03d258e22a24dae4653280365633143bc473f61aec603ccbd79b176090da97cff6f9346a16e5b7180f3de44933a93fb232c28e0ddb6201b246abcbc0
6
+ metadata.gz: 9239e384416cd3f879076fa276f70dc0156daf58ca3c603e5d114d5fa0c2c9bb2a67143648591811987a072a17034dac2e3cc3989ee5b1a7b82b0a4e7a05ed89
7
+ data.tar.gz: 0d608521b1be86249fc9beb78e2ddfe236f5be565ace247579f2bed487b066891ae7686e34d77e5aa56e8812f9fc4403e1547f8d61cba4ce22fb88198404b879
data/CHANGELOG.md CHANGED
@@ -2,6 +2,21 @@
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
+
13
+ ## [5.7.0] - 2026-11-01
14
+ ### Added
15
+ - `cinebase/a1web`, loaded only on request, for chains whose websites run on Admit One's A1Web, such as Arc, Reel and Movie House
16
+ - `A1web::Client` reads venues from the website's cinema menu and site codes from `GlobalSiteData`, or from a venue's own page when the home page lists none. It reads a venue's `/whatson` page, which holds every advertised date, as UTF-8 although A1Web sends Windows-1252. It retries dropped connections.
17
+ - `A1web::Listings` reads each showtime's date, start, absolute booking link and tags (icon names, `a1-event-*` icon classes and group headings). It handles date panels keyed by timestamp, tabs carrying a timestamp, and tabs with only a label such as `Tue 03 Nov`, reads repeated mobile copies once, and adds the venue path that Movie House booking links leave out.
18
+ - `A1web::Cinema` and `A1web::Performance` base classes. A chain defines `Cinema.website` and `#brand`, optionally `Cinema.locations` with addresses and coordinates, and `Performance.cinema_class`, `.title_sanitizer` and optionally `.variants`. 3D and IMAX also come from titles.
19
+
5
20
  ## [5.6.0] - 2026-10-31
6
21
  ### Added
7
22
  - `Cinema#latitude` and `Cinema#longitude`, `nil` by default. Chain gems return Floats in decimal degrees when they know where a cinema is.
data/README.md CHANGED
@@ -220,6 +220,65 @@ CinemaChainName::Performance.at('X0712', from: Date.today, to: Date.today + 365)
220
220
  request per 50 films. `Cinebase::Boxoffice::Client.new(base_url:)` is the
221
221
  underlying client, with `theaters`, `schedule` and `movies`.
222
222
 
223
+ ### A1Web chains
224
+
225
+ Chains whose websites run on Admit One's A1Web (Arc, Reel, Movie House)
226
+ share a client, a what's on page parser and base classes. These are not
227
+ loaded by `require 'cinebase'`:
228
+
229
+ ```ruby
230
+ require 'cinebase/a1web'
231
+
232
+ module CinemaChainName
233
+ class Cinema < Cinebase::A1web::Cinema
234
+ def self.website
235
+ 'https://www.example.co.uk'
236
+ end
237
+
238
+ # Optional: A1Web sites publish no structured addresses
239
+ def self.locations
240
+ {
241
+ 'EX_HIGH' => {
242
+ street_address: '1 High Street', locality: 'Exampleton', postal_code: 'EX1 1AA',
243
+ latitude: 52.5, longitude: -1.25
244
+ }
245
+ }
246
+ end
247
+
248
+ def brand
249
+ 'Example'
250
+ end
251
+ end
252
+
253
+ class Performance < Cinebase::A1web::Performance
254
+ def self.cinema_class
255
+ Cinema
256
+ end
257
+
258
+ def self.title_sanitizer
259
+ Internal::TitleSanitizer
260
+ end
261
+
262
+ # Optional: the chain's own showtime icons and headings
263
+ def self.variants
264
+ super.merge('Hypersense' => 'hypersense')
265
+ end
266
+ end
267
+ end
268
+
269
+ CinemaChainName::Cinema.all # venues in the cinema menu, cached for an hour
270
+ CinemaChainName::Performance.at('EX_HIGH', from: Date.today, to: Date.today + 365)
271
+ ```
272
+
273
+ Cinema ids are A1Web site codes such as `ARC_BEE`. The home page's
274
+ `GlobalSiteData` lists them, and where it does not, each venue's own page
275
+ sets one. `Performance.at` makes one request: the venue's `/whatson` page
276
+ holds every advertised date. `Cinebase::A1web::Listings` reads all three
277
+ known templates, whose date panels are keyed by timestamp (Arc), have a
278
+ timestamp on their tab (Movie House) or have only a label such as
279
+ `Tue 03 Nov` (Reel). Showtime tags are icon names, `a1-event-*` icon
280
+ classes and group headings; `Performance::VARIANTS` maps the shared ones.
281
+
223
282
  ## Contributing
224
283
 
225
284
  1. [Fork it](https://github.com/andycroll/cinebase/fork)
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'
@@ -0,0 +1,94 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cinebase
4
+ module A1web
5
+ # Cinema base class for an A1Web chain. Subclasses define `.website` and
6
+ # `#brand`, and may define `.locations` with addresses and coordinates,
7
+ # which A1Web sites do not publish in a structured form.
8
+ class Cinema < Cinebase::Cinema
9
+ # Worker processes outlive a day's listings, so refetch the venue list
10
+ CACHE_SECONDS = 3600
11
+ ADDRESS_PARTS = %i(street_address extended_address locality region postal_code).freeze
12
+
13
+ # @return [String] the chain website, e.g. 'https://www.arccinema.co.uk'
14
+ def self.website
15
+ raise NotImplementedError, "#{self} must define .website"
16
+ end
17
+
18
+ # @return [Cinebase::A1web::Client]
19
+ def self.client
20
+ Client.new(base_url: website)
21
+ end
22
+
23
+ # @return [Hash{String => Hash}] by site code: :street_address,
24
+ # :extended_address, :locality, :region, :postal_code, :latitude and
25
+ # :longitude, each optional
26
+ def self.locations
27
+ {}
28
+ end
29
+
30
+ # Every venue in the website's cinema menu
31
+ # @return [Array<Cinebase::A1web::Cinema>] instances of the subclass
32
+ def self.all
33
+ venues_hash.keys.map { |id| new(id) }
34
+ end
35
+
36
+ # @param [String] id the A1Web site code, e.g. 'ARC_BEE' or 'arc_bee'
37
+ def initialize(id)
38
+ @id = id.to_s.upcase
39
+ end
40
+
41
+ # @return [Hash] contains :street_address, :extended_address,
42
+ # :locality, :region, :postal_code, :country_name
43
+ def adr
44
+ ADDRESS_PARTS.to_h { |part| [part, location[part]] }.merge(country_name: 'United Kingdom')
45
+ end
46
+
47
+ # @return [Float, nil]
48
+ def latitude
49
+ location[:latitude]
50
+ end
51
+
52
+ # @return [Float, nil]
53
+ def longitude
54
+ location[:longitude]
55
+ end
56
+
57
+ # @return [String] e.g. 'Beeston'
58
+ def name
59
+ venue[:name].to_s
60
+ end
61
+
62
+ # @return [String] the venue's own site, e.g. 'https://beeston.arccinema.co.uk'
63
+ def url
64
+ venue[:url].to_s
65
+ end
66
+
67
+ # Chains that list no site codes on their home page set one on each
68
+ # venue's own page
69
+ def self.venues_hash
70
+ now = Process.clock_gettime(Process::CLOCK_MONOTONIC)
71
+ if @venues_hash.nil? || now - @fetched_at > CACHE_SECONDS
72
+ @fetched_at = now
73
+ api = client
74
+ @venues_hash = api.venues.filter_map do |venue|
75
+ code = venue[:code] || api.site_code(venue[:url])
76
+ [code.upcase, venue] if code
77
+ end.to_h
78
+ end
79
+ @venues_hash
80
+ end
81
+ private_class_method :venues_hash
82
+
83
+ private
84
+
85
+ def location
86
+ self.class.locations.fetch(id, {})
87
+ end
88
+
89
+ def venue
90
+ self.class.send(:venues_hash).fetch(id, {})
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,63 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi/escape'
4
+
5
+ module Cinebase
6
+ module A1web
7
+ # Reads venues and listings pages from an A1Web chain website. No token
8
+ # is needed.
9
+ class Client
10
+ # The cinema menu on every page links each venue's own site
11
+ VENUE = %r{<option[^>]*value="(https?://[^"]+)"[^>]*>([^<]*)</option>}
12
+ SITE_CODES = /GlobalSiteData\['([^']+)'\]\s*=\s*'([^']+)'/
13
+ SITE_CODE = /var sitecode\s*=\s*'([^']+)'/
14
+ # Pages claim ISO-8859-1 but send Windows-1252 dashes and quotes
15
+ LEGACY_ENCODING = Encoding::Windows_1252
16
+
17
+ attr_reader :base_url
18
+
19
+ # @param [String] base_url the chain website, e.g. 'https://www.arccinema.co.uk'
20
+ def initialize(base_url:)
21
+ @base_url = base_url.delete_suffix('/')
22
+ @http = Http.new
23
+ end
24
+
25
+ # Venues in the home page's cinema menu, with the site codes the home
26
+ # page lists. Some chains list none.
27
+ # @return [Array<Hash>] with :url, :name and :code (nil when unlisted)
28
+ def venues
29
+ html = get(@base_url + '/')
30
+ codes = html.scan(SITE_CODES).to_h { |url, code| [url.delete_suffix('/'), code] }
31
+ html.scan(VENUE).map { |url, name| [url.delete_suffix('/'), name] }.uniq(&:first).map do |url, name|
32
+ { url: url, name: CGI.unescapeHTML(name).strip, code: codes[url] }
33
+ end
34
+ end
35
+
36
+ # @param [String] venue_url e.g. 'https://moviehouse.co.uk/coleraine'
37
+ # @return [String, nil] the site code the venue's home page sets, e.g. 'MH_COLE'
38
+ def site_code(venue_url)
39
+ get(venue_url)[SITE_CODE, 1]
40
+ end
41
+
42
+ # @param [String] venue_url e.g. 'https://beeston.arccinema.co.uk'
43
+ # @return [String] the venue's what's on page, with every advertised date
44
+ def whats_on(venue_url)
45
+ get("#{venue_url.delete_suffix('/')}/whatson")
46
+ end
47
+
48
+ private
49
+
50
+ def get(url)
51
+ decode(@http.get(url))
52
+ end
53
+
54
+ def decode(body)
55
+ if body.valid_encoding?
56
+ body
57
+ else
58
+ body.dup.force_encoding(LEGACY_ENCODING).encode(Encoding::UTF_8, invalid: :replace, undef: :replace)
59
+ end
60
+ end
61
+ end
62
+ end
63
+ end
@@ -0,0 +1,127 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cgi/escape'
4
+ require 'date'
5
+
6
+ module Cinebase
7
+ module A1web
8
+ # Reads every showtime from a venue's what's on page. Templates differ in
9
+ # how they date their panels: Arc keys each panel by its local midnight,
10
+ # Movie House gives that timestamp on the panel's tab, and Reel's tabs
11
+ # give only a label such as 'Today' or 'Tue 03 Nov'.
12
+ class Listings
13
+ PANEL = /<div id="panel_?(\d+)"[^>]*tab-content/
14
+ TAB = %r{<button data-target="(\d+)"([^>]*)>(.*?)</button>}m
15
+ TAB_TIMESTAMP = /value="(\d+)"/
16
+ FILM = %r{<h2[^>]*>\s*<a[^>]*href="[^"]*/event/(\d+)[^"]*"[^>]*>(.*?)</a>}m
17
+ # Group headings, such as 'Subtitled', come before the showtimes they label
18
+ HEADING_OR_SHOWTIME = %r{
19
+ <p\sclass="[^"]*font-semibold[^"]*">([^<]*)</p>
20
+ |
21
+ <a\sclass="[^"]*perfButton[^"]*"\shref="([^"]*)"[^>]*>(.*?)</a>
22
+ }mx
23
+ ICON = /alt="([^"]*)"/
24
+ ICON_SUFFIX = /(?:\A|\s+)icon\z/i
25
+ EVENT_ICON = /class="a1-event-([\w-]+)/
26
+ TIME = /(\d{1,2}):(\d{2})/
27
+ DAY_MONTH = /(\d{1,2}) (#{Date::ABBR_MONTHNAMES.compact.join('|')})/
28
+ # Panel keys this long are timestamps rather than tab numbers
29
+ TIMESTAMP_DIGITS = 9
30
+ TIME_ZONE = 'Europe/London'
31
+
32
+ # @param [String] html a what's on page
33
+ # @param [String] venue_url the venue's own site, e.g. 'https://beeston.arccinema.co.uk'
34
+ # @param [Date] today the date in London when the page was fetched
35
+ def initialize(html, venue_url:, today:)
36
+ @html = html
37
+ @venue_url = venue_url
38
+ @today = today
39
+ end
40
+
41
+ # Each showtime once, as Reel and Movie House repeat them for small screens
42
+ # @return [Array<Cinebase::A1web::Showing>]
43
+ def showings
44
+ all = @html.split(PANEL).drop(1).each_slice(2).flat_map do |key, panel|
45
+ date = panel_date(key)
46
+ date ? panel_showings(panel, date) : []
47
+ end
48
+ all.uniq(&:booking_url)
49
+ end
50
+
51
+ private
52
+
53
+ def panel_showings(panel, date)
54
+ panel.split(FILM).drop(1).each_slice(3).flat_map do |film_id, title, markup|
55
+ heading = nil
56
+ markup.to_s.scan(HEADING_OR_SHOWTIME).filter_map do |label, href, button|
57
+ if label
58
+ heading = text(label)
59
+ nil
60
+ elsif (time = text(button).match(TIME))
61
+ Showing.new(film_id: film_id, title: text(title), date: date, starting_at: starting_at(date, time),
62
+ booking_url: booking_url(href), tags: tags(button, heading))
63
+ end
64
+ end
65
+ end
66
+ end
67
+
68
+ def panel_date(key)
69
+ if key.length >= TIMESTAMP_DIGITS
70
+ local_date(key.to_i)
71
+ else
72
+ tab_dates[key]
73
+ end
74
+ end
75
+
76
+ def tab_dates
77
+ @tab_dates ||= @html.scan(TAB).to_h do |key, attributes, label|
78
+ timestamp = attributes[TAB_TIMESTAMP, 1]
79
+ [key, timestamp ? local_date(timestamp.to_i) : label_date(text(label))]
80
+ end
81
+ end
82
+
83
+ def label_date(label)
84
+ case label
85
+ when /\AToday\z/i then @today
86
+ when /\ATomorrow\z/i then @today + 1
87
+ when DAY_MONTH then next_date(Regexp.last_match(1).to_i, Date::ABBR_MONTHNAMES.index(Regexp.last_match(2)))
88
+ end
89
+ end
90
+
91
+ # Labels have no year, so the first such date from today
92
+ def next_date(day, month)
93
+ years = [@today.year, @today.year + 1]
94
+ dates = years.filter_map { |year| Date.new(year, month, day) if Date.valid_date?(year, month, day) }
95
+ dates.find { |date| date >= @today }
96
+ end
97
+
98
+ def local_date(timestamp)
99
+ London.date(Time.at(timestamp))
100
+ end
101
+
102
+ def starting_at(date, time)
103
+ local = Time.utc(date.year, date.month, date.day, time[1].to_i, time[2].to_i)
104
+ London.utc(local)
105
+ end
106
+
107
+ # Movie House links leave out the venue's path, and only work with it
108
+ def booking_url(href)
109
+ href = CGI.unescapeHTML(href)
110
+ venue_path = URI(@venue_url).path.delete_suffix('/')
111
+ if href.start_with?('/') && !href.start_with?("#{venue_path}/")
112
+ href = venue_path + href
113
+ end
114
+ URI.join(@venue_url, href).to_s
115
+ end
116
+
117
+ def tags(button, heading)
118
+ icons = button.scan(ICON).map { |(alt)| text(alt).sub(ICON_SUFFIX, '') }
119
+ (icons + button.scan(EVENT_ICON).flatten + [heading]).compact.reject(&:empty?)
120
+ end
121
+
122
+ def text(html)
123
+ CGI.unescapeHTML(html.gsub(/<[^>]*>/, ' ')).gsub(/\s+/, ' ').strip
124
+ end
125
+ end
126
+ end
127
+ end
@@ -0,0 +1,92 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cinebase
4
+ module A1web
5
+ # Performance base class for an A1Web chain. Subclasses define
6
+ # `.cinema_class` and `.title_sanitizer`, and may override `.variants`.
7
+ class Performance < Cinebase::Performance
8
+ # Showtime icons and group headings shared by A1Web chains, and the
9
+ # variants they mean. Chains merge in their own, such as screen brands.
10
+ VARIANTS = {
11
+ 'Event Cinema' => 'arts',
12
+ 'IMAX' => 'imax',
13
+ 'Kids Club' => 'kids',
14
+ 'Parent & Baby' => 'baby',
15
+ 'Sensory Screening' => 'relaxed',
16
+ 'Silver Screen' => 'senior',
17
+ 'Subtitled' => 'subtitled',
18
+ 'subtitles' => 'subtitled'
19
+ }.freeze
20
+
21
+ # A tag, or the title, as some chains list 3D and IMAX copies as films
22
+ THREE_D = /\b3D\b/i
23
+ IMAX = /\bIMAX\b/i
24
+ LANGUAGE = /\((#{TitleSanitizer::LANGUAGES.join('|')})(?: language)?\)/i
25
+ TIME_ZONE = 'Europe/London'
26
+
27
+ # @return [Class] the chain's Cinebase::A1web::Cinema subclass
28
+ def self.cinema_class
29
+ raise NotImplementedError, "#{self} must define .cinema_class"
30
+ end
31
+
32
+ # @return [Class] the chain's Cinebase::TitleSanitizer subclass
33
+ def self.title_sanitizer
34
+ raise NotImplementedError, "#{self} must define .title_sanitizer"
35
+ end
36
+
37
+ # @return [Hash{String => String}] tags and the variants they mean
38
+ def self.variants
39
+ VARIANTS
40
+ end
41
+
42
+ # Performances within an inclusive range of listings days, from the
43
+ # venue's what's on page, which holds every advertised date.
44
+ # @param [String] cinema_id e.g. 'ARC_BEE'
45
+ # @param [Date] from first date (defaults to today in London)
46
+ # @param [Date] to last date (defaults to 13 days after from)
47
+ # @return [Array<Cinebase::A1web::Performance>] instances of the subclass
48
+ def self.at(
49
+ cinema_id,
50
+ from: London.today,
51
+ to: from + 13
52
+ )
53
+ raise ArgumentError, 'to must not precede from' if to < from
54
+
55
+ cinema = cinema_class.new(cinema_id)
56
+ if cinema.url.empty?
57
+ []
58
+ else
59
+ today = London.today
60
+ html = cinema_class.client.whats_on(cinema.url)
61
+ showings = Listings.new(html, venue_url: cinema.url, today: today).showings
62
+ performances = showings.select { |showing| (from..to).cover?(showing.date) }.map do |showing|
63
+ new(attributes(showing).merge(cinema_id: cinema.id, cinema_name: cinema.name))
64
+ end
65
+ performances.sort_by(&:starting_at)
66
+ end
67
+ end
68
+
69
+ def self.attributes(showing)
70
+ title = showing.title
71
+ {
72
+ booking_url: showing.booking_url,
73
+ dimension: [title, *showing.tags].any? { |text| text.match?(THREE_D) } ? '3d' : '2d',
74
+ festival: Festival.from_title(title, year: showing.date.year),
75
+ film_name: title_sanitizer.new(title).sanitized,
76
+ film_year: TitleSanitizer.film_year(title),
77
+ language: title[LANGUAGE, 1],
78
+ starting_at: showing.starting_at,
79
+ variant: variant(showing)
80
+ }
81
+ end
82
+ private_class_method :attributes
83
+
84
+ def self.variant(showing)
85
+ variant = showing.tags.filter_map { |tag| variants[tag] }
86
+ variant << 'imax' if showing.title.match?(IMAX)
87
+ variant.uniq
88
+ end
89
+ private_class_method :variant
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,20 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Cinebase
4
+ module A1web
5
+ # One showtime on a what's on page.
6
+ # @!attribute [r] film_id
7
+ # @return [String] the website's event id, e.g. '110406'
8
+ # @!attribute [r] title
9
+ # @return [String] the film title as listed
10
+ # @!attribute [r] date
11
+ # @return [Date] the listings date
12
+ # @!attribute [r] starting_at
13
+ # @return [Time] UTC
14
+ # @!attribute [r] booking_url
15
+ # @return [String]
16
+ # @!attribute [r] tags
17
+ # @return [Array<String>] icon names and group headings, e.g. 'IMAX', 'Subtitled'
18
+ Showing = Data.define(:film_id, :title, :date, :starting_at, :booking_url, :tags)
19
+ end
20
+ end
@@ -0,0 +1,17 @@
1
+ # frozen_string_literal: true
2
+
3
+ require 'cinebase'
4
+
5
+ module Cinebase
6
+ # Shared support for chains whose websites run on Admit One's A1Web, such
7
+ # as Arc, Reel and Movie House. Not loaded by `require 'cinebase'`; these
8
+ # gems `require 'cinebase/a1web'`.
9
+ module A1web
10
+ end
11
+ end
12
+
13
+ require 'cinebase/a1web/client'
14
+ require 'cinebase/a1web/showing'
15
+ require 'cinebase/a1web/listings'
16
+ require 'cinebase/a1web/cinema'
17
+ require 'cinebase/a1web/performance'
@@ -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.6.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.6.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
@@ -143,6 +157,12 @@ files:
143
157
  - bin/setup
144
158
  - cinebase.gemspec
145
159
  - lib/cinebase.rb
160
+ - lib/cinebase/a1web.rb
161
+ - lib/cinebase/a1web/cinema.rb
162
+ - lib/cinebase/a1web/client.rb
163
+ - lib/cinebase/a1web/listings.rb
164
+ - lib/cinebase/a1web/performance.rb
165
+ - lib/cinebase/a1web/showing.rb
146
166
  - lib/cinebase/boxoffice.rb
147
167
  - lib/cinebase/boxoffice/cinema.rb
148
168
  - lib/cinebase/boxoffice/client.rb
@@ -150,6 +170,8 @@ files:
150
170
  - lib/cinebase/boxoffice/showtime.rb
151
171
  - lib/cinebase/cinema.rb
152
172
  - lib/cinebase/festival.rb
173
+ - lib/cinebase/http.rb
174
+ - lib/cinebase/london.rb
153
175
  - lib/cinebase/performance.rb
154
176
  - lib/cinebase/title_sanitizer.rb
155
177
  - lib/cinebase/version.rb