Class: Concerns::API
- Inherits:
-
Object
- Object
- Concerns::API
- Defined in:
- lib/api.rb
Class Method Summary collapse
- .get_categories ⇒ Object
-
.reset_data ⇒ Object
resets event_fight.all for back funtionality.
- .scrape_fights(array_of_events) ⇒ Object
Class Method Details
.get_categories ⇒ Object
4 5 6 7 8 9 10 |
# 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 ⇒ Object
resets event_fight.all for back funtionality
49 50 51 |
# 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) ⇒ Object
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 42 43 44 45 46 |
# 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 => puts puts "UFC API is down" return end end end |