Class: Gamercard::CardParser

Inherits:
Object
  • Object
show all
Defined in:
lib/gamercard/card_parser.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(http_response) ⇒ CardParser



7
8
9
# File 'lib/gamercard/card_parser.rb', line 7

def initialize http_response
  self.raw_response = http_response
end

Instance Attribute Details

#raw_responseObject

Returns the value of attribute raw_response.



5
6
7
# File 'lib/gamercard/card_parser.rb', line 5

def raw_response
  @raw_response
end

Class Method Details

.parse(http_response) ⇒ Object



11
12
13
# File 'lib/gamercard/card_parser.rb', line 11

def self.parse http_response
  new(http_response).parse
end

Instance Method Details

#count_reputation(doc) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/gamercard/card_parser.rb', line 32

def count_reputation doc
  rep = doc.css('.RepContainer')
  counts = {
    1.0  => rep.css('.Full').size,
    0.75 => rep.css('.ThreeQuarter').size,
    0.5  => rep.css('.Half').size,
    0.25 => rep.css('.Quarter').size
  }
  counts.inject(0) do |rep, (value, count)|
    rep += count * value
    rep
  end
end

#parseObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/gamercard/card_parser.rb', line 15

def parse
  doc = Nokogiri::HTML(raw_response.body)
  gamertag = doc.css("#Gamertag").first
  {
    'gamertag'    => gamertag.content,
    'link'        => gamertag.attribute("href").value,
    'gamerpic'    => doc.css("#Gamerpic").first.attribute("src").value,
    'gamerscore'  => doc.css("#Gamerscore").first.content.to_i,
    'location'    => doc.css("#Location").first.content,
    'motto'       => doc.css("#Motto").first.content,
    'name'        => doc.css("#Name").first.content,
    'bio'         => doc.css("#Bio").first.content,
    'reputation'  => count_reputation(doc),
    'recent_games' => summarize_games(doc)
  }
end

#summarize(game) ⇒ Object



53
54
55
56
57
58
59
60
61
62
63
64
65
66
# File 'lib/gamercard/card_parser.rb', line 53

def summarize game
  link = game.css('a').first
  {
    'comparison_url'         => link.attribute('href').value,
    'image'                  => link.css('img').first.attribute('src').value,
    'title'                  => link.css('.Title').first.content,
    'last_played'            => link.css('.LastPlayed').first.content,
    'earned_gamerscore'      => link.css('.EarnedGamerscore').first.content.to_i,
    'available_gamerscore'   => link.css('.AvailableGamerscore').first.content.to_i,
    'earned_achievements'    => link.css('.EarnedAchievements').first.content.to_i,
    'available_achievements' => link.css('.AvailableAchievements').first.content.to_i,
    'percentage_complete'    => link.css('.PercentageComplete').first.content
  }
end

#summarize_games(doc) ⇒ Object



46
47
48
49
50
51
# File 'lib/gamercard/card_parser.rb', line 46

def summarize_games doc
  doc.css('#PlayedGames li').inject([]) do |array, game_node|
    array.push summarize(game_node)
    array
  end
end