Class: Concerns::API

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

Class Method Summary collapse

Class Method Details

.get_categoriesObject



2
3
4
5
6
7
8
9
# File 'lib/api.rb', line 2

def self.get_categories
  cats = RestClient.get('http://ufc-data-api.ufc.com/api/v1/us/events')
  @data = JSON.parse(cats)

  @data.each do |events|
    Concerns::Events.new_from_api(events)
  end
end

.reset_dataObject



43
44
45
# File 'lib/api.rb', line 43

def self.reset_data
  Concerns::EventFight.clear
end

.scrape_fights(array_of_events) ⇒ Object



11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/api.rb', line 11

def self.scrape_fights(array_of_events)
  #for each event id this will scrape event data iterate through the fights and create EventFight objects for each fight on the event and associate those fights to an event
  #binding.pry
  self.reset_data #resets fight.all for back funtionality
  array_of_events.each do |event|
    event.clear_fights #resets events.event_fights array

    doc = Nokogiri::HTML(open("http://ufc-data-api.ufc.com/api/v1/us/events/#{event.id}"))

    doc.css('.flipcard-front-pre').each do |card|
      red_name = card.css('.fighter-name-red').text.strip

      blue_name = card.css('.fighter-name-blue').text.strip

      #takes valid data and makes a hash out of it
      fight_stats = card.css('.fight-card-match-up td').map {|stat| stat.text}.select {|stat| stat != stat.empty?}

      fight_hash = {}
      fight_hash[:red_record] = fight_stats.first
      fight_hash[:blue_record] = fight_stats[2]
      fight_hash[:red_height] = fight_stats[3]
      fight_hash[:blue_height] = fight_stats[5]
      fight_hash[:red_weight] = fight_stats[6]
      fight_hash[:blue_weight] = fight_stats[8]
      #creates new fight and passes names and stat values to it
      new_fight = Concerns::EventFight.new(red_name, blue_name, fight_hash)
      #adds the fight to the Events.event_fights array
      event.event_fights << new_fight
    end
  end
end