Class: Letteropend::List
- Inherits:
-
Object
- Object
- Letteropend::List
- Defined in:
- lib/letteropend/list.rb
Overview
The List class
Constant Summary collapse
- @@valid_events =
[:new_page, :new_film]
Instance Attribute Summary collapse
-
#films ⇒ Object
readonly
Returns the value of attribute films.
-
#list ⇒ Object
readonly
Returns the value of attribute list.
-
#pages ⇒ Object
readonly
Returns the value of attribute pages.
-
#username ⇒ Object
readonly
Returns the value of attribute username.
Class Method Summary collapse
-
.config(&events) ⇒ Object
Method to declare class events.
-
.on(event, &block) ⇒ Object
Assign events to class.
Instance Method Summary collapse
-
#initialize(username, list = "films", &events) ⇒ List
constructor
Created a new list instance.
- #method_missing(sym, *args) ⇒ Object
-
#on(event, &block) ⇒ Object
Assign events to instance.
Constructor Details
#initialize(username, list = "films", &events) ⇒ List
Created a new list instance
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 |
# File 'lib/letteropend/list.rb', line 16 def initialize(username, list="films", &events) @username = username @list = list @films = [] @pages = ["/#{username}/#{list}/page/1/"] # assign events to list object if block_given? instance_eval(&events) end begin # get page using Nokogiri new_page page = Nokogiri::HTML(open("http://www.letterboxd.com#{@pages.last}")) # append visible films to @films page.css(".poster").each do |film| title = film.css(".frame-title").text url_full = film.css("a")[0].attr("href") url = /\/film\/(\S+)\//.match(url_full).captures[0] @films.push( Film.new(url, title: title) ) new_film end # see if there is a next page next_page = page.css('a.paginate-next')[0] if ( next_page ) @pages.push( next_page.attr("href") ) end end while ( next_page ) end |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(sym, *args) ⇒ Object
88 89 90 91 92 93 94 |
# File 'lib/letteropend/list.rb', line 88 def method_missing(sym, *args) if (@@valid_events.include? sym) # no method was defined for this event else super end end |
Instance Attribute Details
#films ⇒ Object (readonly)
Returns the value of attribute films.
8 9 10 |
# File 'lib/letteropend/list.rb', line 8 def films @films end |
#list ⇒ Object (readonly)
Returns the value of attribute list.
8 9 10 |
# File 'lib/letteropend/list.rb', line 8 def list @list end |
#pages ⇒ Object (readonly)
Returns the value of attribute pages.
8 9 10 |
# File 'lib/letteropend/list.rb', line 8 def pages @pages end |
#username ⇒ Object (readonly)
Returns the value of attribute username.
8 9 10 |
# File 'lib/letteropend/list.rb', line 8 def username @username end |
Class Method Details
.config(&events) ⇒ Object
Method to declare class events
68 69 70 71 72 |
# File 'lib/letteropend/list.rb', line 68 def self.config(&events) if block_given? class_eval(&events) end end |
.on(event, &block) ⇒ Object
Assign events to class
78 79 80 81 82 83 84 85 86 |
# File 'lib/letteropend/list.rb', line 78 def self.on(event, &block) if block_given? if (@@valid_events.include? event) define_method(event, block) else puts "Error: trying to assign invalid class event | Letteropend::List, event: #{event}" end end end |
Instance Method Details
#on(event, &block) ⇒ Object
Assign events to instance
55 56 57 58 59 60 61 62 63 |
# File 'lib/letteropend/list.rb', line 55 def on(event, &block) if block_given? if (@@valid_events.include? event) define_singleton_method(event, block) else puts "Error: trying to assign invalid event | Letteropend::List, event: #{event}" end end end |