Module: GameFaqs::List

Extended by:
Caching
Defined in:
lib/gamefaqs/list.rb

Constant Summary collapse

PLATFORMS_PATH =
"//ul.systems/li/a"
PLATFORMS_ID_PATH =
"//form#search/select[@name=platform]/option"
GAMES_PATH =
"//div.body/table/tr/td/a"

Class Method Summary collapse

Class Method Details

.faqs(game, type = nil, refresh = false) ⇒ Object



106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
# File 'lib/gamefaqs/list.rb', line 106

def faqs(game, type=nil, refresh=false)
  faqs = cached_value("faqs-#{game.to_s.downcase}", [], refresh) do |faqs|
    url = game.homepage.sub(/\/home\//, "/game/")
    doc = Hpricot(open(url))
    GameFaqs.find_table(doc, /./, "table/tr") do |tr, header|
      faq = {}
      faq[:type] = header
      tr.search("td:eq(0)") do |td|
        td.search("a") do |a|
          review[:id] = GameFaqs.extract_id(a['href'])
          review[:title] = a.inner_html.strip
        end
      end
      tr.search("td:eq(1)") do |td|
        td.search("a") do |a|
          review[:author] = a.inner_html.strip
        end
      end
      tr.search("td:eq(2)") do |td|
        review[:score] = td.inner_html.strip
      end
      review[:game] = game   
      reviews << Review.new(review)
    end
  end
  if type
    types = FAQ::FAQ_TYPES
    raise ArgumentError.new("Type must be one of #{types.join(', ')}") unless types.include?(type.to_sym)
    faqs.reject { |faq| faq.type != type.to_sym}
  else
    faqs
  end
end

.games(platform, refresh = false) ⇒ Object

find all games (very expensive, takes nearly a minute, because of 27 html requests and resulting parsing)



21
22
23
24
25
26
27
28
29
30
# File 'lib/gamefaqs/list.rb', line 21

def games(platform, refresh=false)
  platform = Platform.find(platform) unless platform.is_a?(Platform)
  cached_value("games-#{platform.to_s.downcase}", []) do |games|
    letters = ('a'..'z').to_a << '0'
    letters.each do |letter|
      doc = Hpricot(open("#{platform.homepage}list_#{letter}.html"))
      insert_games_to_array(games, doc, platform, /Games by/)
    end
  end
end

.games_by_genre(platform, genre, refresh = false) ⇒ Object



32
33
34
35
36
37
38
# File 'lib/gamefaqs/list.rb', line 32

def games_by_genre(platform, genre, refresh=false)
  platform = Platform.find(platform) unless platform.is_a?(Platform)
  cached_value("games_by_#{genre.downcase}-#{platform.to_s.downcase}", []) do |games|
    doc = Hpricot(open("#{platform.homepage}cat_#{Game.GENRES[genre]}.html"))
    insert_games_to_array(games, doc, platform, /Games by Category/)
  end
end

.platform_ids(refresh = false) ⇒ Object

find all IDs for every platform (cached, do only once)



141
142
143
144
145
146
147
148
# File 'lib/gamefaqs/list.rb', line 141

def platform_ids(refresh=false)
  cached_value("game_ids", {}, refresh) do |ids|
    search_doc = Hpricot(open(SEARCH_URL))
    search_doc.search(PLATFORMS_ID_PATH).each do |option|
      ids[option['label']] = option['value']
    end
  end
end

.platforms(refresh = false) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/gamefaqs/list.rb', line 10

def platforms(refresh=false)
  cached_value("platforms", [], refresh) do |platforms|
    systems_doc = Hpricot(open("#{GameFaqs::BASE_URL}/systems.html"))
    systems_doc.search(PLATFORMS_PATH).each do |link|
      name = link.inner_html
      platforms << Platform.new({:name => name, :homepage => link['href'], :id => platform_ids[name]})
    end
  end
end

.questions(game, refresh = false) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/gamefaqs/list.rb', line 82

def questions(game, refresh=false)
  questions = cached_value("questions-#{game.to_s.downcase}", [], refresh) do |questions|
    url = game.homepage.sub(/\/home\//, "/qna/") << "?type=-1"
    doc = Hpricot(open(url))
    GameFaqs.find_table(doc, /Help$/, "table/tr") do |tr, header|
      question = {}
      question[:type] = header
      tr.search("td:eq(0)") do |td|
        td.search("a") do |a|
          question[:id] = GameFaqs.extract_question_id(a['href'])
          question[:title] = a.inner_html.strip
        end
      end
      tr.search("td:eq(1)") do |td|
        question[:status] = td.inner_html
      end
      tr.search("td:eq(2)") do |td|
        question[:replies] = td.inner_html
      end
      questions << Question.new(game, question[:id], question)
    end
  end
end

.reviews(game, type = nil, refresh = false) ⇒ Object



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
80
# File 'lib/gamefaqs/list.rb', line 48

def reviews(game, type=nil, refresh=false)
  reviews = cached_value("reviews-#{game.to_s.downcase}", [], refresh) do |reviews|
    url = game.homepage.sub(/\/home\//, "/review/")
    doc = Hpricot(open(url))
    GameFaqs.find_table(doc, /Reviews/, "table/tr") do |tr, header|
      review = {}
      review[:type] = Review.review_type(header)
      tr.search("td:eq(0)") do |td|
        td.search("a") do |a|
          review[:id] = GameFaqs.extract_id(a['href'])
          review[:title] = a.inner_html.strip
        end
      end
      tr.search("td:eq(1)") do |td|
        td.search("a") do |a|
          review[:author] = a.inner_html.strip
        end
      end
      tr.search("td:eq(2)") do |td|
        review[:score] = td.inner_html.strip
      end
      review[:game] = game   
      reviews << Review.new(review)
    end
  end
  if type
    types = Review::REVIEW_TYPES
    raise ArgumentError.new("Type must be one of #{types.join(', ')}") unless types.include?(type.to_sym)
    reviews.reject { |review| review.type != type.to_sym}
  else
    reviews
  end
end

.top_games(platform, refresh = false) ⇒ Object



40
41
42
43
44
45
46
# File 'lib/gamefaqs/list.rb', line 40

def top_games(platform, refresh=false)
  platform = Platform.find(platform) unless platform.is_a?(Platform)
  cached_value("top_games-#{platform.to_s.downcase}", []) do |games|
    doc = Hpricot(open(platform.homepage))
    insert_games_to_array(games, doc, platform, /Top 10 Games/, "ul/li/a")
  end
end