class Concerns::API

Public Class Methods

get_categories() click to toggle source
# File lib/api.rb, line 4
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_data() click to toggle source
# File lib/api.rb, line 49
def self.reset_data #resets event_fight.all for back funtionality
  Concerns::EventFight.clear
end
scrape_fights(array_of_events) click to toggle source
# File lib/api.rb, line 12
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
  self.reset_data
  array_of_events.each do |event|
    event.clear_fights #resets events.event_fights array
    begin #creates error if api is down
    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
  rescue OpenURI::HTTPError => e_message
     puts e_message
     puts "UFC API is down"
     return
   end
  end
end