Class: TalkTalkTV::FilmPage

Inherits:
Object
  • Object
show all
Defined in:
lib/talktalk_tv/film_page.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(body:, url: nil) ⇒ FilmPage

Returns a new instance of FilmPage.



7
8
9
10
# File 'lib/talktalk_tv/film_page.rb', line 7

def initialize(body:, url: nil)
  @body = body
  @url = url
end

Instance Attribute Details

#bodyObject (readonly)

Returns the value of attribute body.



12
13
14
# File 'lib/talktalk_tv/film_page.rb', line 12

def body
  @body
end

#urlObject (readonly)

Returns the value of attribute url.



12
13
14
# File 'lib/talktalk_tv/film_page.rb', line 12

def url
  @url
end

Class Method Details

.from_url(url) ⇒ Object



14
15
16
17
# File 'lib/talktalk_tv/film_page.rb', line 14

def self.from_url(url)
  response = HTTPClient.new.get(url)
  FilmPage.new(body: Nokogiri::HTML(response.body), url: url)
end

Instance Method Details

#buy_priceObject



59
60
61
# File 'lib/talktalk_tv/film_page.rb', line 59

def buy_price
  select_price(body, '.c-retailButton--purchase .c-retailButton__price  > text()')
end

#certificateObject



45
46
47
48
# File 'lib/talktalk_tv/film_page.rb', line 45

def certificate
  match = body.css('.g-assetInfo li').map { |n| %r{CERT (.*)}.match(n.content) }.compact.first
  match && match[1]
end

#filmObject



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/talktalk_tv/film_page.rb', line 19

def film
  Film.new(
    title: title,
    url: url,
    image_url: image_url,
    release_year: release_year,
    certificate: certificate,
    running_time_in_minutes: running_time_in_minutes,
    rental_price: rental_price,
    buy_price: buy_price
  )
end

#image_urlObject



36
37
38
# File 'lib/talktalk_tv/film_page.rb', line 36

def image_url
  body.css('.c-packShot noscript img').first.attributes['src'].value
end

#release_yearObject



40
41
42
43
# File 'lib/talktalk_tv/film_page.rb', line 40

def release_year
  match = body.css('.g-assetInfo li').map { |n| %r{(\d{4})}.match(n.content) }.compact.first
  match && match[1].to_i
end

#rental_priceObject



55
56
57
# File 'lib/talktalk_tv/film_page.rb', line 55

def rental_price
  select_price(body, '.c-retailButton--rental .c-retailButton__price  > text()')
end

#running_time_in_minutesObject



50
51
52
53
# File 'lib/talktalk_tv/film_page.rb', line 50

def running_time_in_minutes
  match = body.css('.g-assetInfo li').map { |n| %r{(\d+) HRS (\d+) MINS}.match(n.content) }.compact.first
  match && (match[1].to_i * 60 + match[2].to_i)
end

#titleObject



32
33
34
# File 'lib/talktalk_tv/film_page.rb', line 32

def title
  body.css('.g-assetTitle').first.content.strip
end