Class: Sherdog

Inherits:
Object
  • Object
show all
Defined in:
lib/ruby-sherdog-api.rb

Overview

Example usage: Sherdog.fighter_query(‘royce gracie’)

Constant Summary collapse

GOOGLE_URL =
"http://www.google.com/search?q="
SHERDOG_URL =
"http://www.sherdog.com/fighter/"

Class Method Summary collapse

Class Method Details

.fighter_query(fighter) ⇒ Object



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
80
81
82
# File 'lib/ruby-sherdog-api.rb', line 32

def self.fighter_query(fighter)
  sherdog_page = sherdog_page(fighter)

  if sherdog_page == "Fighter not found"
    return "Fighter not found"
  end

  fighter_hash = fighter_template

  fighter_hash[:link] = sherdog_link(fighter)
  fighter_hash[:img_url] = sherdog_page.css('.bio_fighter [itemprop="image"]').attr('src').to_s
  fighter_hash[:name] = sherdog_page.css('h1[itemprop="name"] .fn').text
  fighter_hash[:nickname] = sherdog_page.css('h1[itemprop="name"] .nickname').text.gsub(/"/, '')
  fighter_hash[:age] = sherdog_page.css('.item.birthday strong').text.gsub(/[^\d]/, '')
  fighter_hash[:birthday] = sherdog_page.css('span[itemprop="birthDate"]').text
  fighter_hash[:locality] = sherdog_page.css('span[itemprop="addressLocality"]').text
  fighter_hash[:nationality] = sherdog_page.css('strong[itemprop="nationality"]').text
  fighter_hash[:flag_url] = sherdog_page.css('.birthplace img').attr('src').to_s
  fighter_hash[:association] = sherdog_page.css('.item.association span[itemprop="name"]').text
  fighter_hash[:height] = sherdog_page.css('.item.height strong').text
  fighter_hash[:weight] = sherdog_page.css('.item.weight strong').text
  fighter_hash[:weight_class] = sherdog_page.css('.item.wclass strong').text

  record = sherdog_page.css('.record .count_history')

  wins = record.css('.left_side .bio_graph')[0]
  fighter_hash[:wins][:total] = wins.css('.counter').text
  fighter_hash[:wins][:knockouts] = wins.css('.graph_tag:nth-child(3)').text.to_i.to_s
  fighter_hash[:wins][:submissions] = wins.css('.graph_tag:nth-child(5)').text.to_i.to_s
  fighter_hash[:wins][:decisions] = wins.css('.graph_tag:nth-child(7)').text.to_i.to_s
  fighter_hash[:wins][:others] = wins.css('.graph_tag:nth-child(9)').text.to_i.to_s

  losses = record.css('.left_side .bio_graph')[1]
  fighter_hash[:losses][:total] = losses.css('.counter').text
  fighter_hash[:losses][:knockouts] = losses.css('.graph_tag:nth-child(3)').text.to_i.to_s
  fighter_hash[:losses][:submissions] = losses.css('.graph_tag:nth-child(5)').text.to_i.to_s
  fighter_hash[:losses][:decisions] = losses.css('.graph_tag:nth-child(7)').text.to_i.to_s
  fighter_hash[:losses][:others] = losses.css('.graph_tag:nth-child(9)').text.to_i.to_s

  if record.at('.right_side .bio_graph .card .result:contains("Draws") + span')
    fighter_hash[:draws] = record.at('.right_side .bio_graph .card .result:contains("Draws") + span').text
  end

  if record.at('.right_side .bio_graph .card .result:contains("N/C") + span')
    fighter_hash[:no_contests] = record.at('.right_side .bio_graph .card .result:contains("N/C") + span').text
  end

  fighter_hash[:fights] = build_fights(sherdog_page)

  return fighter_hash
end

Public methods: Sherdog.fighter_query(‘name of fighter’) Sherdog.sherdog_link(‘name of fighter’)



19
20
21
22
23
24
25
26
27
28
29
30
# File 'lib/ruby-sherdog-api.rb', line 19

def self.sherdog_link(fighter)
  sherdog_links = google_search(fighter).css('.g .r a').select do |link|
    link.to_s.include? SHERDOG_URL
  end
  if sherdog_links != []
    first_link = sherdog_links.first.attribute('href').to_s
    first_link_endtrim = first_link.partition('&').first
    fir_link_trimmed = first_link_endtrim[first_link_endtrim.index(SHERDOG_URL)..-1]
  else
    "Fighter not found"
  end
end