Class: ExUA::Category

Inherits:
Object
  • Object
show all
Defined in:
lib/ex_ua/category.rb

Overview

Represents a category

Examples:

Usage

#You usually get categories thru ExUA::Client object
categories = ExUA::Client.base_categories('ru')
sub_categories = categories.first.categories
items = sub_categories.first.categories.first.items

Defined Under Namespace

Classes: NotFound

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Category

@param ex_ua client @param id Category id @param options



17
18
19
20
21
22
23
# File 'lib/ex_ua/category.rb', line 17

def initialize(options={})
  @id = options[:id]
  @uri = Addressable::URI.parse(options[:url] || url_from_id(id))
  @uri.site = ExUA::BASE_URL
  @name = options.delete(:name)
  @parent = options.delete(:parent)
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



12
13
14
# File 'lib/ex_ua/category.rb', line 12

def id
  @id
end

#parentObject (readonly)

Returns the value of attribute parent.



12
13
14
# File 'lib/ex_ua/category.rb', line 12

def parent
  @parent
end

#uriObject (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_urlString

Canonical url

Returns:

  • (String)


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

#categoriesArray<ExUA::Category>

List of subcategories

Returns:



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

#descriptionString

Category description

Returns:

  • (String)


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

#itemsArray<ExUA::Item>

Download items

Returns:



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

#nameString

Category name

Returns:

  • (String)


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

#nextExUA::Category

Next category

Returns:

Raises:



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?

Returns:

  • (Boolean)


67
68
69
# File 'lib/ex_ua/category.rb', line 67

def next?
  !!next_url
end

#pageFixnum

Current page number

Returns:

  • (Fixnum)


92
93
94
# File 'lib/ex_ua/category.rb', line 92

def page
  uri.query_values["p"].to_i
end

#pathObject



96
97
98
# File 'lib/ex_ua/category.rb', line 96

def path
  uri.path
end

#pictureString

Category picture

Returns:

  • (String)

    url for a 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

#prevExUA::Category

Previous category

Returns:

Raises:



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?

Returns:

  • (Boolean)


72
73
74
# File 'lib/ex_ua/category.rb', line 72

def prev?
  !!prev_url
end

#to_sObject



25
26
27
# File 'lib/ex_ua/category.rb', line 25

def to_s
  "id:#{id} name:'#{name}' page: #{page}"
end