Class: ExUA::Category
- Inherits:
-
Object
- Object
- ExUA::Category
- Defined in:
- lib/ex_ua/category.rb
Overview
Represents a category
Defined Under Namespace
Classes: NotFound
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#parent ⇒ Object
readonly
Returns the value of attribute parent.
-
#uri ⇒ Object
readonly
Returns the value of attribute uri.
Instance Method Summary collapse
-
#canonical_url ⇒ String
Canonical url.
-
#categories ⇒ Array<ExUA::Category>
List of subcategories.
-
#description ⇒ String
Category description.
- #initialize(options = {}) ⇒ Category constructor
-
#items ⇒ Array<ExUA::Item>
Download items.
-
#name ⇒ String
Category name.
-
#next ⇒ ExUA::Category
Next category.
-
#next? ⇒ Boolean
Is there a next category?.
-
#page ⇒ Fixnum
Current page number.
- #path ⇒ Object
-
#picture ⇒ String
Category picture.
-
#prev ⇒ ExUA::Category
Previous category.
-
#prev? ⇒ Boolean
Is there a previous category?.
- #to_s ⇒ Object
Constructor Details
Instance Attribute Details
#id ⇒ Object (readonly)
Returns the value of attribute id.
12 13 14 |
# File 'lib/ex_ua/category.rb', line 12 def id @id end |
#parent ⇒ Object (readonly)
Returns the value of attribute parent.
12 13 14 |
# File 'lib/ex_ua/category.rb', line 12 def parent @parent end |
#uri ⇒ Object (readonly)
Returns the value of attribute uri.
12 13 14 |
# File 'lib/ex_ua/category.rb', line 12 def uri @uri end |
Instance Method Details
#canonical_url ⇒ String
Canonical url
31 32 33 |
# File 'lib/ex_ua/category.rb', line 31 def canonical_url @canonical_url ||= page_content.root.xpath("//link[@rel='canonical']/@href").first.value end |
#categories ⇒ Array<ExUA::Category>
List of subcategories
58 59 60 61 62 63 64 |
# File 'lib/ex_ua/category.rb', line 58 def categories page_content.search('table.include_0 a b').map do |link| if match = link.parent.attributes["href"].value.match(%r{(?<url>[^?]+)\?r=(?<r>\d+)}) Category.new(parent: self, url: match['url'], name: link.text) end end.compact end |
#description ⇒ String
Category description
43 44 45 |
# File 'lib/ex_ua/category.rb', line 43 def description @description ||= page_content.root.xpath("//meta[@name='description']/@content").first.value end |
#items ⇒ Array<ExUA::Item>
Download items
102 103 104 105 106 107 108 109 |
# File 'lib/ex_ua/category.rb', line 102 def items table_rows = page_content.search('table.list tr') table_rows.map do |tr| tr.search("a[title!='']") end.reject(&:empty?).map do |links| Item.parse_links(links) end end |
#name ⇒ String
Category name
37 38 39 |
# File 'lib/ex_ua/category.rb', line 37 def name @name ||= page_content.root.xpath("//meta[@name='title']/@content").first.value end |
#next ⇒ ExUA::Category
Next category
78 79 80 81 |
# File 'lib/ex_ua/category.rb', line 78 def next raise NotFound, "No link to a next category found" unless next? Category.new(id: self.id, url: next_url) end |
#next? ⇒ Boolean
Is there a next category?
67 68 69 |
# File 'lib/ex_ua/category.rb', line 67 def next? !!next_url end |
#page ⇒ Fixnum
Current page number
92 93 94 |
# File 'lib/ex_ua/category.rb', line 92 def page uri.query_values["p"].to_i end |
#path ⇒ Object
96 97 98 |
# File 'lib/ex_ua/category.rb', line 96 def path uri.path end |
#picture ⇒ String
Category picture
49 50 51 52 53 54 |
# File 'lib/ex_ua/category.rb', line 49 def picture @picture ||= ( pic = page_content.root.xpath("//link[@rel='image_src']/@href").first pic && pic.value.split("?").first ) end |
#prev ⇒ ExUA::Category
Previous category
85 86 87 88 |
# File 'lib/ex_ua/category.rb', line 85 def prev raise NotFound, "No link to a previous category found" unless prev? Category.new(id: self.id, url: prev_url) end |
#prev? ⇒ Boolean
Is there a previous category?
72 73 74 |
# File 'lib/ex_ua/category.rb', line 72 def prev? !!prev_url end |
#to_s ⇒ Object
25 26 27 |
# File 'lib/ex_ua/category.rb', line 25 def to_s "id:#{id} name:'#{name}' page: #{page}" end |