Class: Douban::Subject

Inherits:
Object
  • Object
show all
Includes:
Equal
Defined in:
lib/douban/subject.rb

Direct Known Subclasses

Book, Movie, Music

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Equal

#==

Constructor Details

#initialize(atom = '') ⇒ Subject

Returns a new instance of Subject.



27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
# File 'lib/douban/subject.rb', line 27

def initialize(atom='')
  doc = case atom
    when REXML::Document then atom.root
    when REXML::Element then atom
    when nil then nil
    else REXML::Document.new(atom).root
  end
  @doc = doc

  id=REXML::XPath.first(doc,"./id")
  @id=id.text if id
  title=REXML::XPath.first(doc,"./title")
  @title=title.text if title
  @category={}
  category=REXML::XPath.first(doc,"./category")
  @category['term']=category.attributes['term'] if category
  @category['scheme']=category.attributes['scheme'] if category
  REXML::XPath.each(doc,"./db:tag") do |tag|
    @tag||=[]
    t=Tag.new
    t.title=tag.attributes['name']
    t.count=tag.attributes['count']
    @tag<< t
  end
  @author||=Author.new
  name=REXML::XPath.first(doc,"./author/name")
  @author.name=name.text if name
  uri=REXML::XPath.first(doc,"./author/uri")
  @author.uri=uri.text if uri
  REXML::XPath.each(doc,"./author/link") do |link|
    @author.link||={}
    @author.link[link.attributes['rel']]=link.attributes['href']
  end
  summary=REXML::XPath.first(doc,"./summary")
  @summary=summary.text if summary
  REXML::XPath.each(doc,"./link") do |link|
    @link||={}
    @link[link.attributes['rel']]=link.attributes['href']
  end
  REXML::XPath.each(doc,"./db:attribute") do |attribute|
    @attribute ||= {}
    @attribute[attribute.attributes['name']] ||= []
    @attribute[attribute.attributes['name']] << attribute.text
  end
  @rating={}
  rating=REXML::XPath.first(doc,"./gd:rating")
  if rating
    @rating['min']=rating.attributes['min']
    @rating['numRaters']=rating.attributes['numRaters']
    @rating['average']=rating.attributes['average']
    @rating['max']=rating.attributes['max']
  end
end

Class Method Details

.attr_namesObject



10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/douban/subject.rb', line 10

def attr_names
  [
    :id,
    :title,
    :category,
    :author,
    :link,
    :summary,
    :attribute,
    :tag,
    :rating
  ]
end

Instance Method Details

#aka(lang) ⇒ Object



99
100
101
# File 'lib/douban/subject.rb', line 99

def aka(lang)
  akas(lang).first
end

#akas(lang = nil) ⇒ Object



85
86
87
88
89
90
91
92
93
94
95
96
97
# File 'lib/douban/subject.rb', line 85

def akas(lang = nil)
  if lang.nil?
    @attribute["aka"]
  else
    res = []
    REXML::XPath.each(@doc,"./db:attribute") do |attribute|
      if attribute.attributes["name"] == 'aka' and attribute.attributes["lang"] == lang
        res << attribute.text
      end
    end
    res
  end
end

#subject_idObject

initialize



81
82
83
# File 'lib/douban/subject.rb', line 81

def subject_id
  /\/(\d+)$/.match(@id)[1].to_i rescue nil
end