tf2r 0.1.1 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/tf2r.rb +1 -0
- data/lib/tf2r/api.rb +7 -4
- data/lib/tf2r/raffle.rb +64 -0
- data/lib/tf2r/scraper.rb +56 -61
- data/lib/tf2r/version.rb +1 -1
- data/spec/raffle_spec.rb +34 -0
- data/spec/scraper_spec.rb +43 -60
- data/spec/spec_helper.rb +5 -0
- data/spec/vcr/cassettes/{scrape_ranks.yml → info_page.yml} +4 -4
- data/spec/vcr/cassettes/{scrape_raffle_for_creator.yml → kstzcbd.yml} +4 -4
- data/spec/vcr/cassettes/{scrape_raffle_for_raffle.yml → raffle_kstzcbd.yml} +4 -4
- data/spec/vcr/cassettes/{scrape_raffle_for_participants.yml → raffle_kstzcbd_full.yml} +4 -4
- data/spec/vcr/cassettes/user_76561198061719848.yml +275 -0
- data/spec/vcr/cassettes/{scrape_user_not_found.yml → user_fake.yml} +4 -4
- metadata +17 -16
- data/spec/vcr/cassettes/raffles.yml +0 -243
- data/spec/vcr/cassettes/scrape_user_real.yml +0 -275
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4c5bd71115ed3cebd26737dc98ab7dcc23355253
|
4
|
+
data.tar.gz: 1eef157620f392d7f5247cc262eef47a02451fcd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dab7f6734488422f90ca4defcd8af4f071eb6778a36f90551721b5a778bee9bb9e96f3b41d03f7126b42574b09274251efbcc3a7510277ceed2f95c339282d7b
|
7
|
+
data.tar.gz: e3755809104357d54bdfec5bc792565914d1e9c6ab230d0f349dcfb3cb54f51da60e4101b6c0820ad8abbebeeabe420bc6c5e2d68e2742660283046df7ad8c95
|
data/Gemfile.lock
CHANGED
data/lib/tf2r.rb
CHANGED
data/lib/tf2r/api.rb
CHANGED
@@ -1,13 +1,15 @@
|
|
1
1
|
module TF2R
|
2
|
+
# This class handles interaction with TF2R's "API", which is really just an
|
3
|
+
# open endpoint located at http://tf2r.com/job.php, in use all over the site
|
4
|
+
# to have dynamically updating pages.
|
2
5
|
class API
|
3
6
|
HOST = 'http://tf2r.com'
|
4
7
|
ENDPOINT = '/job.php'
|
5
8
|
|
6
|
-
def self.
|
9
|
+
def self.raffle_info(link_snippet)
|
7
10
|
params = {checkraffle: true, rid: link_snippet[1..-1],
|
8
11
|
lastentrys: 0, lastchat: 0}
|
9
|
-
|
10
|
-
JSON.parse(response)['message']
|
12
|
+
request(params)['message']
|
11
13
|
end
|
12
14
|
|
13
15
|
private
|
@@ -20,7 +22,8 @@ module TF2R
|
|
20
22
|
request = Net::HTTP::Post.new(ENDPOINT)
|
21
23
|
request.body = URI.encode_www_form(params)
|
22
24
|
|
23
|
-
response = http.request(request)
|
25
|
+
response = http.request(request).body
|
26
|
+
JSON.parse(response)
|
24
27
|
end
|
25
28
|
end
|
26
29
|
end
|
data/lib/tf2r/raffle.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module TF2R
|
2
|
+
# This class provides a simple wrapper around grabbing information for a
|
3
|
+
# raffle from the Scraper and API.
|
4
|
+
# TODO: make this an actuall wrapper instead of accepting pre-made info.
|
5
|
+
class Raffle
|
6
|
+
attr_reader :link_snippet, :api_info, :scraper_info
|
7
|
+
|
8
|
+
def initialize(link_snippet, api_info, scraper_info)
|
9
|
+
@link_snippet = link_snippet
|
10
|
+
@api_info = api_info
|
11
|
+
@scraper_info = scraper_info
|
12
|
+
end
|
13
|
+
|
14
|
+
def info
|
15
|
+
@info ||= {link_snippet: @link_snippet, title: title,
|
16
|
+
description: description, start_time: start_time,
|
17
|
+
end_time: end_time, win_chance: win_chance,
|
18
|
+
current_entries: current_entries, max_entries: max_entries,
|
19
|
+
is_done: is_done}
|
20
|
+
end
|
21
|
+
|
22
|
+
def creator
|
23
|
+
@scraper_info[1]
|
24
|
+
end
|
25
|
+
|
26
|
+
def participants
|
27
|
+
@api_info['newentry']
|
28
|
+
end
|
29
|
+
|
30
|
+
private
|
31
|
+
|
32
|
+
def title
|
33
|
+
@scraper_info[0][:title]
|
34
|
+
end
|
35
|
+
|
36
|
+
def description
|
37
|
+
@scraper_info[0][:description]
|
38
|
+
end
|
39
|
+
|
40
|
+
def start_time
|
41
|
+
@scraper_info[0][:start_time]
|
42
|
+
end
|
43
|
+
|
44
|
+
def end_time
|
45
|
+
@scraper_info[0][:end_time]
|
46
|
+
end
|
47
|
+
|
48
|
+
def current_entries
|
49
|
+
@api_info['cur_entry']
|
50
|
+
end
|
51
|
+
|
52
|
+
def max_entries
|
53
|
+
@api_info['max_entry']
|
54
|
+
end
|
55
|
+
|
56
|
+
def win_chance
|
57
|
+
(@api_info['wc'] / 100.0).round(5)
|
58
|
+
end
|
59
|
+
|
60
|
+
def is_done
|
61
|
+
@api_info['ended']
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
data/lib/tf2r/scraper.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
module TF2R
|
2
2
|
# @author Justin Kim
|
3
3
|
class Scraper
|
4
|
+
class InvalidUserPage < StandardError; end
|
4
5
|
# Creates a Scraper. Pass values using the options hash.
|
5
6
|
#
|
6
7
|
# :user_agent a String used for the User-Agent header
|
@@ -78,8 +79,7 @@ module TF2R
|
|
78
79
|
# s.scrape_raffle_for_creator(p) #=>
|
79
80
|
# {:steam_id=>76561198061719848,
|
80
81
|
# :username=>"Yulli",
|
81
|
-
# :avatar_link=>
|
82
|
-
# "http://media.steampowered.com/steamcommunity/public/images/avatars/bc/bc9dc4302d23f2e2f37f59c59f29c27dbc8cade6_full.jpg",
|
82
|
+
# :avatar_link=>"http://media.steampowered.com/steamcommunity/public/images/avatars/bc/bc9dc4302d23f2e2f37f59c59f29c27dbc8cade6_full.jpg",
|
83
83
|
# :posrep=>11458,
|
84
84
|
# :negrep=>0,
|
85
85
|
# :colour=>"70b01b"}
|
@@ -106,13 +106,16 @@ module TF2R
|
|
106
106
|
negrep = /(\d+)/.match(infos.css('.downvb').text)[1].to_i
|
107
107
|
|
108
108
|
# The creator's username colour. Corresponds to rank.
|
109
|
-
colour =
|
109
|
+
colour = extract_colour(user_anchor.attribute('style').to_s)
|
110
110
|
|
111
111
|
{steam_id: steam_id, username: username, avatar_link: avatar_link,
|
112
112
|
posrep: posrep, negrep: negrep, colour: colour}
|
113
113
|
end
|
114
114
|
|
115
|
-
# Scrapes a raffle page for information about the raffle.
|
115
|
+
# Scrapes a raffle page for some information about the raffle.
|
116
|
+
#
|
117
|
+
# The information is incomplete. This should be used in conjunction with
|
118
|
+
# the API as part of TF2R::Raffle.
|
116
119
|
#
|
117
120
|
# @example
|
118
121
|
# p = s.fetch('http://tf2r.com/kstzcbd.html')
|
@@ -121,23 +124,15 @@ module TF2R
|
|
121
124
|
# :title=>"Just one refined [1 hour]",
|
122
125
|
# :description=>"Plain and simple.",
|
123
126
|
# :start_time=>2012-10-29 09:51:45 -0400,
|
124
|
-
# :end_time=>2012-10-29 09:53:01 -0400
|
125
|
-
# :win_chance=>0.1,
|
126
|
-
# :current_entries=>10,
|
127
|
-
# :max_entries=>10,
|
128
|
-
# :is_done=>true}
|
127
|
+
# :end_time=>2012-10-29 09:53:01 -0400}
|
129
128
|
#
|
130
129
|
# @param page [Mechanize::Page] the raffle page.
|
131
|
-
# @return [Hash] a representation of the raffle.
|
130
|
+
# @return [Hash] a partial representation of the raffle.
|
132
131
|
# * :link_snippet (+String+) — the "raffle id" in the URL.
|
133
132
|
# * :title (+String+) — the raffle's title.
|
134
133
|
# * :description (+String+) — the raffle's "message".
|
135
134
|
# * :start_time (+Time+) — the creation time of the raffle.
|
136
135
|
# * :end_time (+Time+) — the projects/observed end time for the raffle.
|
137
|
-
# * :win_chance (+Float+) — a participant's chance to win the raffle.
|
138
|
-
# * :current_entries (+Fixnum+) — the current number of participants.
|
139
|
-
# * :max_entries (+Fixnum+) — the maximum number of particpants allowed.
|
140
|
-
# * :is_done (+Boolean+) — whether new users can enter the raffle.
|
141
136
|
def scrape_raffle_for_raffle(page)
|
142
137
|
# Reag classed some things "raffle_infomation". That's spelled right.
|
143
138
|
infos = page.parser.css('.raffle_infomation')
|
@@ -146,37 +141,19 @@ module TF2R
|
|
146
141
|
raffle_tds = infos[3].css('td')
|
147
142
|
|
148
143
|
# 'kabc123' for http://tf2r.com/kabc123.html'
|
149
|
-
link_snippet =
|
150
|
-
title = infos[0].text
|
144
|
+
link_snippet = extract_link_snippet(page.uri.path)
|
145
|
+
title = extract_title(infos[0].text)
|
151
146
|
description = raffle_tds[1].text
|
152
147
|
|
153
148
|
# This doesn't work right now, because Miz just displays "10%" in the
|
154
149
|
# page HTML and updates it with JS after a call to the API.
|
155
150
|
# win_chance = /(.+)%/.match(infos.css('#winc').text)[1].to_f / 100
|
156
151
|
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
start_time = raffle_tds[9].attribute('data-rstart-unix').to_s
|
161
|
-
start_time = DateTime.strptime(start_time, '%s').to_time
|
162
|
-
end_time = raffle_tds[11].attribute('data-rsend-unix').to_s
|
163
|
-
end_time= DateTime.strptime(end_time, '%s').to_time
|
164
|
-
|
165
|
-
entries = /(\d+)\/(\d+)/.match(infos.css('#entry').text)
|
166
|
-
current_entries = entries[1].to_i
|
167
|
-
max_entries = entries[2].to_i
|
168
|
-
|
169
|
-
text = page.parser.css('.welcome_font').css('div')[3..-1].text
|
170
|
-
is_done = end_time <= Time.now ||
|
171
|
-
current_entries == max_entries ||
|
172
|
-
(page.parser.css('.welcome_font').size > 5 &&
|
173
|
-
page.parser.css('.welcome_font')[5..-1].text.downcase.include?('winner')
|
174
|
-
)
|
152
|
+
start_time = extract_start_time(raffle_tds[9])
|
153
|
+
end_time = extract_end_time(raffle_tds[11])
|
175
154
|
|
176
155
|
{link_snippet: link_snippet, title: title, description: description,
|
177
|
-
start_time: start_time, end_time: end_time
|
178
|
-
current_entries: current_entries, max_entries: max_entries,
|
179
|
-
is_done: is_done}
|
156
|
+
start_time: start_time, end_time: end_time}
|
180
157
|
end
|
181
158
|
|
182
159
|
# Scrapes a raffle page for all the participants.
|
@@ -196,7 +173,7 @@ module TF2R
|
|
196
173
|
user_anchor = participant.children[1]
|
197
174
|
steam_id = extract_steam_id(user_anchor.to_s)
|
198
175
|
username = participant.text
|
199
|
-
colour =
|
176
|
+
colour = extract_colour(user_anchor.children[0].attribute('style'))
|
200
177
|
|
201
178
|
participants << {steam_id: steam_id, username: username, colour: colour}
|
202
179
|
end
|
@@ -211,8 +188,7 @@ module TF2R
|
|
211
188
|
# s.scrape_user(p) #=>
|
212
189
|
# {:steam_id=>76561198061719848,
|
213
190
|
# :username=>"Yulli",
|
214
|
-
# :avatar_link=>
|
215
|
-
# "http://media.steampowered.com/steamcommunity/public/images/avatars/bc/bc9dc4302d23f2e2f37f59c59f29c27dbc8cade6_full.jpg",
|
191
|
+
# :avatar_link=>"http://media.steampowered.com/steamcommunity/public/images/avatars/bc/bc9dc4302d23f2e2f37f59c59f29c27dbc8cade6_full.jpg",
|
216
192
|
# :posrep=>11459,
|
217
193
|
# :negrep=>0,
|
218
194
|
# :colour=>"70b01b"}
|
@@ -227,21 +203,20 @@ module TF2R
|
|
227
203
|
# * :colour (+String+) — hex colour code of the user's username.
|
228
204
|
def scrape_user(user_page)
|
229
205
|
if user_page.parser.css('.profile_info').empty?
|
230
|
-
|
231
|
-
steam_id = extract_steam_id(user_page.uri.to_s)
|
232
|
-
username, avatar_link, posrep, negrep, colour = nil, nil, nil, nil, nil
|
206
|
+
raise InvalidUserPage, 'The given page does not correspond to any user.'
|
233
207
|
else
|
234
208
|
infos = user_page.parser.css('.raffle_infomation') #sic
|
235
|
-
|
209
|
+
avatar_anchor = infos[0].css('img')[0]
|
210
|
+
user_anchor = infos[1].css('a')[0]
|
236
211
|
|
237
212
|
steam_id = extract_steam_id(user_page.uri.to_s)
|
238
213
|
username = /TF2R Item Raffles - (.+)/.match(user_page.title)[1]
|
239
|
-
avatar_link =
|
214
|
+
avatar_link = avatar_anchor.attribute('src').to_s
|
240
215
|
|
241
216
|
posrep = infos.css('.upvb').text.to_i
|
242
217
|
negrep = infos.css('.downvb').text.to_i
|
243
218
|
|
244
|
-
colour =
|
219
|
+
colour = extract_colour(user_anchor.attribute('style').to_s)
|
245
220
|
end
|
246
221
|
|
247
222
|
{steam_id: steam_id, username: username, avatar_link: avatar_link,
|
@@ -255,9 +230,8 @@ module TF2R
|
|
255
230
|
# @example
|
256
231
|
# p = s.fetch('http://tf2r.com/info.html')
|
257
232
|
# s.scrape_user(p) #=>
|
258
|
-
# [{:colour=>"ebe2ca", :name=>"User",
|
259
|
-
#
|
260
|
-
# ...]
|
233
|
+
# [{:colour=>"ebe2ca", :name=>"User",
|
234
|
+
# :description=>"Every new or existing user has this rank."}, ...]
|
261
235
|
#
|
262
236
|
# @param info_page [Mechanize::Page] the info page.
|
263
237
|
# @return [Array] contains Hashes representing each of the ranks.
|
@@ -280,7 +254,7 @@ module TF2R
|
|
280
254
|
def extract_rank(div)
|
281
255
|
name = div.children[0].text
|
282
256
|
description = div.children[2].text
|
283
|
-
colour =
|
257
|
+
colour = extract_colour(div.children[0].attribute('style').to_s)
|
284
258
|
|
285
259
|
{name: name, description: description, colour: colour}
|
286
260
|
end
|
@@ -297,26 +271,47 @@ module TF2R
|
|
297
271
|
/http:\/\/tf2r.com\/user\/(\d+)\.html/.match(href)[1].to_i
|
298
272
|
end
|
299
273
|
|
300
|
-
# Extracts a
|
274
|
+
# Extracts a hex colour code.
|
301
275
|
#
|
302
276
|
# @example
|
303
|
-
#
|
277
|
+
# extract_colour('color:#70B01B;') #=> '70b01b'
|
304
278
|
#
|
305
279
|
# @param href [String] Any string containing a hex colour code.
|
306
|
-
# @return [String] The
|
307
|
-
def
|
280
|
+
# @return [String] The hex colour code, downcased.
|
281
|
+
def extract_colour(str)
|
308
282
|
/#(\w+)\s*;/.match(str)[1].downcase
|
309
283
|
end
|
310
284
|
|
311
|
-
#
|
285
|
+
# Extracts the Time from any String.
|
312
286
|
#
|
313
|
-
# @
|
314
|
-
#
|
287
|
+
# @param [String] a String containing a unix timestamp value.
|
288
|
+
# @return [Time] the time represented by the unix timestamp.
|
289
|
+
def extract_time(str)
|
290
|
+
DateTime.strptime(str, '%s').to_time
|
291
|
+
end
|
292
|
+
|
293
|
+
def extract_start_time(td)
|
294
|
+
extract_time(td.attribute('data-rstart-unix').to_s)
|
295
|
+
end
|
296
|
+
|
297
|
+
def extract_end_time(td)
|
298
|
+
extract_time(td.attribute('data-rsend-unix').to_s)
|
299
|
+
end
|
300
|
+
|
301
|
+
# Extract the link_snippet from a path or link.
|
302
|
+
#
|
303
|
+
# @param [String] any raffle link or path.
|
304
|
+
# @return [String] the raffle's link snippet.
|
305
|
+
def extract_link_snippet(text)
|
306
|
+
/\/(k.+)\.html/.match(text)[1]
|
307
|
+
end
|
308
|
+
|
309
|
+
# Extract the title from a raffle title string.
|
315
310
|
#
|
316
|
-
# @param
|
317
|
-
# @return [
|
318
|
-
def
|
319
|
-
|
311
|
+
# @param [String] the raffle title string.
|
312
|
+
# @return [String] the actual raffle title.
|
313
|
+
def extract_title(text)
|
314
|
+
text.split('Title: ')[-1]
|
320
315
|
end
|
321
316
|
end
|
322
317
|
end
|
data/lib/tf2r/version.rb
CHANGED
data/spec/raffle_spec.rb
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe TF2R::Raffle do
|
4
|
+
let(:scraper) { TF2R::Scraper.new({}) }
|
5
|
+
let(:scraper_info) {
|
6
|
+
VCR.use_cassette('kstzcbd') do
|
7
|
+
scraper.scrape_raffle(scraper.fetch('http://tf2r.com/kstzcbd.html'))
|
8
|
+
end
|
9
|
+
}
|
10
|
+
let(:raffle) {
|
11
|
+
TF2R::Raffle.new('kstzcbd', TF2R::API.raffle_info('kstzcbd'), scraper_info)
|
12
|
+
}
|
13
|
+
|
14
|
+
describe '#info' do
|
15
|
+
let(:result) { raffle.info }
|
16
|
+
|
17
|
+
it 'returns correct values' do
|
18
|
+
expect(result[:link_snippet]).to eql('kstzcbd')
|
19
|
+
expect(result[:title]).to eql('Just one refined [1 hour]')
|
20
|
+
expect(result[:description]).to eql('Plain and simple.')
|
21
|
+
expect(result[:start_time]).to eql(
|
22
|
+
Time.new(2012, 10, 29, 14, 51, 45, '+01:00')
|
23
|
+
)
|
24
|
+
expect(result[:end_time]).to eql(
|
25
|
+
Time.new(2012, 10, 29, 14, 53, 01, '+01:00')
|
26
|
+
)
|
27
|
+
expect(result[:win_chance]).to eql(0.1)
|
28
|
+
# TODO: use a raffle where current_entries != max_entries
|
29
|
+
expect(result[:current_entries]).to eql(10)
|
30
|
+
expect(result[:max_entries]).to eql(10)
|
31
|
+
expect(result[:is_done]).to eql(true)
|
32
|
+
end
|
33
|
+
end
|
34
|
+
end
|
data/spec/scraper_spec.rb
CHANGED
@@ -2,6 +2,11 @@ require 'spec_helper'
|
|
2
2
|
|
3
3
|
describe TF2R::Scraper do
|
4
4
|
let(:scraper) { TF2R::Scraper.new({}) }
|
5
|
+
let(:raffle_page) {
|
6
|
+
VCR.use_cassette('raffle_kstzcbd') do
|
7
|
+
scraper.fetch('http://tf2r.com/kstzcbd.html')
|
8
|
+
end
|
9
|
+
}
|
5
10
|
|
6
11
|
it 'is instantiable' do
|
7
12
|
expect{
|
@@ -60,26 +65,25 @@ describe TF2R::Scraper do
|
|
60
65
|
end
|
61
66
|
|
62
67
|
describe '#scrape_raffle' do
|
63
|
-
let(:
|
64
|
-
VCR.use_cassette('scrape_raffle_for_raffle') do
|
65
|
-
scraper.fetch('http://tf2r.com/kstzcbd.html')
|
66
|
-
end
|
67
|
-
}
|
68
|
-
let(:result) { scraper.scrape_raffle(page) }
|
68
|
+
let(:result) { scraper.scrape_raffle(raffle_page) }
|
69
69
|
|
70
70
|
it 'returns an Array' do
|
71
71
|
expect(result).to be_an(Array)
|
72
72
|
end
|
73
73
|
|
74
74
|
it 'returns the same as its child methods' do
|
75
|
-
expect(result[0]).to eql(scraper.scrape_raffle_for_raffle(
|
76
|
-
expect(result[1]).to eql(scraper.scrape_raffle_for_creator(
|
77
|
-
expect(result[2]).to eql(scraper.scrape_raffle_for_participants(
|
75
|
+
expect(result[0]).to eql(scraper.scrape_raffle_for_raffle(raffle_page))
|
76
|
+
expect(result[1]).to eql(scraper.scrape_raffle_for_creator(raffle_page))
|
77
|
+
expect(result[2]).to eql(scraper.scrape_raffle_for_participants(raffle_page))
|
78
78
|
end
|
79
79
|
end
|
80
80
|
|
81
81
|
describe '#scrape_main_page' do
|
82
|
-
let(:result) {
|
82
|
+
let(:result) {
|
83
|
+
VCR.use_cassette('raffle_kstzcbd') do
|
84
|
+
scraper.scrape_main_page
|
85
|
+
end
|
86
|
+
}
|
83
87
|
|
84
88
|
before(:each) do
|
85
89
|
raffles_file = File.new(File.join(File.dirname(__FILE__), 'raffles.html'))
|
@@ -90,11 +94,9 @@ describe TF2R::Scraper do
|
|
90
94
|
end
|
91
95
|
|
92
96
|
it 'returns an Array of Strings' do
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
expect(result[-1]).to be_a(String)
|
97
|
-
end
|
97
|
+
expect(result).to be_an(Array)
|
98
|
+
expect(result[0]).to be_a(String)
|
99
|
+
expect(result[-1]).to be_a(String)
|
98
100
|
end
|
99
101
|
|
100
102
|
it 'returns raffle links in reverse chronological order' do
|
@@ -105,12 +107,7 @@ describe TF2R::Scraper do
|
|
105
107
|
end
|
106
108
|
|
107
109
|
describe '#scrape_raffle_for_creator' do
|
108
|
-
let(:
|
109
|
-
VCR.use_cassette('scrape_raffle_for_creator') do
|
110
|
-
scraper.fetch('http://tf2r.com/kstzcbd.html')
|
111
|
-
end
|
112
|
-
}
|
113
|
-
let(:result) { scraper.scrape_raffle_for_creator(page) }
|
110
|
+
let(:result) { scraper.scrape_raffle_for_creator(raffle_page) }
|
114
111
|
|
115
112
|
it 'returns a Hash representation of a user' do
|
116
113
|
expect(result).to be_a(Hash)
|
@@ -138,12 +135,7 @@ describe TF2R::Scraper do
|
|
138
135
|
|
139
136
|
|
140
137
|
describe '#scrape_raffle_for_raffle' do
|
141
|
-
let(:
|
142
|
-
VCR.use_cassette('scrape_raffle_for_raffle') do
|
143
|
-
scraper.fetch('http://tf2r.com/kstzcbd.html')
|
144
|
-
end
|
145
|
-
}
|
146
|
-
let(:result) { scraper.scrape_raffle_for_raffle(page) }
|
138
|
+
let(:result) { scraper.scrape_raffle_for_raffle(raffle_page) }
|
147
139
|
|
148
140
|
it 'returns a Hash representation of a raffle' do
|
149
141
|
expect(result).to be_a(Hash)
|
@@ -153,19 +145,18 @@ describe TF2R::Scraper do
|
|
153
145
|
expect(result[:link_snippet]).to eql('kstzcbd')
|
154
146
|
expect(result[:title]).to eql('Just one refined [1 hour]')
|
155
147
|
expect(result[:description]).to eql('Plain and simple.')
|
156
|
-
expect(result[:start_time]).to eql(
|
157
|
-
|
158
|
-
|
159
|
-
|
160
|
-
|
161
|
-
|
162
|
-
expect(result[:is_done]).to eql(true)
|
148
|
+
expect(result[:start_time]).to eql(
|
149
|
+
Time.new(2012, 10, 29, 14, 51, 45, '+01:00')
|
150
|
+
)
|
151
|
+
expect(result[:end_time]).to eql(
|
152
|
+
Time.new(2012, 10, 29, 14, 53, 01, '+01:00')
|
153
|
+
)
|
163
154
|
end
|
164
155
|
end
|
165
156
|
|
166
157
|
describe '#scrape_raffle_for_participants' do
|
167
158
|
let(:page) {
|
168
|
-
VCR.use_cassette('
|
159
|
+
VCR.use_cassette('raffle_kstzcbd_full') do
|
169
160
|
scraper.fetch('http://tf2r.com/kstzcbd.html?full')
|
170
161
|
end
|
171
162
|
}
|
@@ -190,19 +181,19 @@ describe TF2R::Scraper do
|
|
190
181
|
|
191
182
|
describe '#scrape_user' do
|
192
183
|
context 'a real user page is given' do
|
193
|
-
let(:
|
194
|
-
VCR.use_cassette('
|
184
|
+
let(:user_page) {
|
185
|
+
VCR.use_cassette('user_76561198061719848') do
|
195
186
|
scraper.fetch('http://tf2r.com/user/76561198061719848.html')
|
196
187
|
end
|
197
188
|
}
|
198
|
-
let(:result) { scraper.scrape_user(
|
189
|
+
let(:result) { scraper.scrape_user(user_page) }
|
199
190
|
|
200
191
|
it 'returns a Hash' do
|
201
192
|
expect(result).to be_a(Hash)
|
202
193
|
end
|
203
194
|
|
204
|
-
# This spec is also brittle. It relies on my user account, which is
|
205
|
-
# constantly.
|
195
|
+
# This spec is also brittle. It relies on my TF2R user account, which is
|
196
|
+
# updated constantly.
|
206
197
|
# TODO: use a stagnant alt account
|
207
198
|
it 'returns correct values' do
|
208
199
|
expect(result[:steam_id]).to eql(76561198061719848)
|
@@ -215,38 +206,28 @@ describe TF2R::Scraper do
|
|
215
206
|
end
|
216
207
|
|
217
208
|
context 'a user page for a non-existent user is given' do
|
218
|
-
let(:
|
219
|
-
VCR.use_cassette('
|
209
|
+
let(:user_page) {
|
210
|
+
VCR.use_cassette('user_fake') do
|
220
211
|
scraper.fetch('http://tf2r.com/user/123456.html')
|
221
212
|
end
|
222
213
|
}
|
223
|
-
let(:result) { scraper.scrape_user(page) }
|
224
|
-
it 'returns a Hash' do
|
225
|
-
expect(result).to be_a(Hash)
|
226
|
-
end
|
227
214
|
|
228
|
-
it '
|
229
|
-
expect
|
230
|
-
|
231
|
-
|
232
|
-
it 'returns mostly nil values' do
|
233
|
-
expect(result[:username]).to be_nil
|
234
|
-
expect(result[:avatar_link]).to be_nil
|
235
|
-
expect(result[:posrep]).to be_nil
|
236
|
-
expect(result[:negrep]).to be_nil
|
237
|
-
expect(result[:colour]).to be_nil
|
215
|
+
it 'raises InvalidUserPage' do
|
216
|
+
expect {
|
217
|
+
scraper.scrape_user(user_page)
|
218
|
+
}.to raise_error(TF2R::Scraper::InvalidUserPage)
|
238
219
|
end
|
239
220
|
end
|
240
221
|
end
|
241
222
|
|
242
223
|
|
243
224
|
describe '#scrape_ranks' do
|
244
|
-
let(:
|
245
|
-
VCR.use_cassette('
|
225
|
+
let(:info_page) {
|
226
|
+
VCR.use_cassette('info_page') do
|
246
227
|
scraper.fetch('http://tf2r.com/info.html')
|
247
228
|
end
|
248
229
|
}
|
249
|
-
let(:result) { scraper.scrape_ranks(
|
230
|
+
let(:result) { scraper.scrape_ranks(info_page) }
|
250
231
|
|
251
232
|
it 'returns an Array of Hashes' do
|
252
233
|
expect(result).to be_an(Array)
|
@@ -256,7 +237,9 @@ describe TF2R::Scraper do
|
|
256
237
|
|
257
238
|
it 'returns correct values' do
|
258
239
|
expect(result[0][:name]).to eql('User')
|
259
|
-
expect(result[0][:description]).to eql(
|
240
|
+
expect(result[0][:description]).to eql(
|
241
|
+
'Every new or existing user has this rank.'
|
242
|
+
)
|
260
243
|
expect(result[0][:colour]).to eql('ebe2ca')
|
261
244
|
|
262
245
|
# This is brittle. Every time a new rank is added, this fails.
|