diff --git a/Gemfile.lock b/Gemfile.lock index 9589226d..76d29eab 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -1,47 +1,47 @@ GEM remote: https://rubygems.org/ specs: - activemodel (6.0.3.1) - activesupport (= 6.0.3.1) - activerecord (6.0.3.1) - activemodel (= 6.0.3.1) - activesupport (= 6.0.3.1) - activesupport (6.0.3.1) + activemodel (6.0.3.4) + activesupport (= 6.0.3.4) + activerecord (6.0.3.4) + activemodel (= 6.0.3.4) + activesupport (= 6.0.3.4) + activesupport (6.0.3.4) concurrent-ruby (~> 1.0, >= 1.0.2) i18n (>= 0.7, < 2) minitest (~> 5.1) tzinfo (~> 1.1) zeitwerk (~> 2.2, >= 2.2.2) - coderay (1.1.1) - concurrent-ruby (1.1.6) - i18n (1.8.2) + coderay (1.1.3) + concurrent-ruby (1.1.7) + i18n (1.8.5) concurrent-ruby (~> 1.0) - method_source (0.8.2) - minitest (5.14.1) - mustermann (1.0.3) - pry (0.10.4) - coderay (~> 1.1.0) - method_source (~> 0.8.1) - slop (~> 3.4) + method_source (1.0.0) + minitest (5.14.2) + mustermann (1.1.1) + ruby2_keywords (~> 0.0.1) + pry (0.13.1) + coderay (~> 1.1) + method_source (~> 1.0) rack (2.2.3) - rack-protection (2.0.7) + rack-protection (2.1.0) rack - require_all (1.3.3) - sinatra (2.0.7) + require_all (3.0.0) + ruby2_keywords (0.0.2) + sinatra (2.1.0) mustermann (~> 1.0) - rack (~> 2.0) - rack-protection (= 2.0.7) + rack (~> 2.2) + rack-protection (= 2.1.0) tilt (~> 2.0) - sinatra-activerecord (2.0.12) - activerecord (>= 3.2) + sinatra-activerecord (2.0.21) + activerecord (>= 4.1) sinatra (>= 1.0) - slop (3.6.0) - sqlite3 (1.3.13) + sqlite3 (1.4.2) thread_safe (0.3.6) tilt (2.0.10) - tzinfo (1.2.7) + tzinfo (1.2.8) thread_safe (~> 0.1) - zeitwerk (2.3.0) + zeitwerk (2.4.2) PLATFORMS ruby diff --git a/Rakefile b/Rakefile index 508ef20e..f9aca5ad 100644 --- a/Rakefile +++ b/Rakefile @@ -1,5 +1,6 @@ require_relative 'config/environment' require 'sinatra/activerecord/rake' +require_relative 'app/models/make_request' desc 'starts a console' task :console do diff --git a/app/models/arena.rb b/app/models/arena.rb new file mode 100644 index 00000000..6b838ab9 --- /dev/null +++ b/app/models/arena.rb @@ -0,0 +1,4 @@ +class Arena < ActiveRecord::Base + has_many :matches + has_many :teams, through: :matches +end diff --git a/app/models/make_request.rb b/app/models/make_request.rb new file mode 100644 index 00000000..c3688312 --- /dev/null +++ b/app/models/make_request.rb @@ -0,0 +1,120 @@ +require 'uri' +require 'net/http' +require 'openssl' +require 'json' + +class MakeRequest < ActiveRecord::Base + + +@@urls = [ + # URI("https://api-football-v1.p.rapidapi.com/v2/topscorers/2"), #scorers in Premier League + URI("https://api-football-v1.p.rapidapi.com/v2/teams/league/2"), #teams in Premier League + # URI("https://api-football-v1.p.rapidapi.com/v2/leagueTable/2"), #team standings in Premier League + URI("https://api-football-v1.p.rapidapi.com/v2/fixtures/league/2?timezone=Europe%2FLondon") #fixtures in Premier League +] +@@http = [] +@@urls.each do |url| + h = Net::HTTP.new(url.host, url.port) + h.use_ssl = true + h.verify_mode = OpenSSL::SSL::VERIFY_NONE + @@http.push(h) +end + +@@requests = [] +@@urls.each do |url| + r = Net::HTTP::Get.new(url) + r["x-rapidapi-key"] = '680adbf113mshff91628eec25474p194702jsnd1c76f97e364' + r["x-rapidapi-host"] = 'api-football-v1.p.rapidapi.com' + @@requests.push(r) +end + +@@responses = [] +count = 0 +@@http.each do |h| + @@responses.push(h.request(@@requests[count])) + count += 1 +end + +def self.find_team_id_by_name(name) + Team.find_by(name: name) +end + +def self.find_arena_id_by_name(name) + Arena.find_by(name: name) +end + + +def self.populate_match + match = JSON.parse(@@responses[1].read_body) + match_filtered = match['api']['fixtures'].map{|m| [m['event_date'], m['homeTeam']['team_name'], m['awayTeam']['team_name'], m['goalsHomeTeam'], m['goalsAwayTeam'], m['venue']]} + + match_filtered.each do |m| + Match.create(date: m[0], home_team_id: self.find_team_id_by_name(m[1]), away_team_id: self.find_team_id_by_name(m[2]), home_team_goals: m[3], away_team_goals: m[4], arena_id: self.find_arena_id_by_name(m[5])) + end +end + +def self.populate_team + team = JSON.parse(@@responses[0].read_body) + team_filtered = team['api']['teams'].map{|m| m['name']} + team_filtered.each do |t| + Team.create(name: t) + end +end + + + +def self.populate_arena + team = JSON.parse(@@responses[0].read_body) + arena = team['api']['teams'].map{|m| m['venue_name']} + arena.each do |a| + Arena.create(name: a) + end +end + +end + + + + + +# def populateCharity(url) +# parsed = populateHelper(url) +# parsed.each do |charity| +# Charity.create(ein: charity["ein"], name: charity["charityName"], category: charity["category"], +# state: charity["state"], accepting_donations: charity["acceptingDonations"]) +# end +# end + +# end + + + + + + + + + + + + + + + + + + + +# repeats = standings['api']['leagues'].map {|league| league['name']} +# #pp repeats.uniq +# players = JSON.parse(responses[1].read_body) +# player_stats = players['api']['topscorers'].map {|player| [player['player_name'], player['goals']['total'], player['team_name'], player['nationality']]} +# #pp player_stats +# # pp players +# # teams = JSON.parse(responses[2].read_body) +# # pp teams +# champs = JSON.parse(responses[3].read_body) +# #pp champs['api']['standings'][0].map {|team| [team['teamName'], team['description'] ]} +# national_teams = JSON.parse(responses[4].read_body) + + diff --git a/app/models/match.rb b/app/models/match.rb new file mode 100644 index 00000000..6648b8d5 --- /dev/null +++ b/app/models/match.rb @@ -0,0 +1,15 @@ +# require 'make_request.rb' + +class Match < ActiveRecord::Base + belongs_to :arena + belongs_to :teams + + def home_team_goals + pp match['api']['fixtures'].map{|m|m['goalsHomeTeam']} + end + + def test + p "it works!" + end + +end diff --git a/app/models/team.rb b/app/models/team.rb new file mode 100644 index 00000000..83975ffb --- /dev/null +++ b/app/models/team.rb @@ -0,0 +1,11 @@ +class Team < ActiveRecord::Base + has_many :matches + has_many :arenas, through: :matches + + + + +end + + + diff --git a/config/environment.rb b/config/environment.rb index 4dbe13e5..4e38a3b5 100644 --- a/config/environment.rb +++ b/config/environment.rb @@ -1,5 +1,7 @@ require 'bundler' Bundler.require + ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: 'db/development.db') require_all 'lib' +require_all 'app/models' diff --git a/db/migrate/20201206005755_create_teams.rb b/db/migrate/20201206005755_create_teams.rb new file mode 100644 index 00000000..3eef64e9 --- /dev/null +++ b/db/migrate/20201206005755_create_teams.rb @@ -0,0 +1,7 @@ +class CreateTeams < ActiveRecord::Migration[6.0] + def change + create_table :teams do |t| + t.string :name + end + end +end diff --git a/db/migrate/20201206010054_create_matches.rb b/db/migrate/20201206010054_create_matches.rb new file mode 100644 index 00000000..70d94744 --- /dev/null +++ b/db/migrate/20201206010054_create_matches.rb @@ -0,0 +1,12 @@ +class CreateMatches < ActiveRecord::Migration[6.0] + def change + create_table :matches do |t| + t.string :date + t.integer :home_team_id + t.integer :away_team_id + t.integer :home_team_goals + t.integer :away_team_goals + t.integer :arena_id + end + end +end diff --git a/db/migrate/20201206213753_create_arenas.rb b/db/migrate/20201206213753_create_arenas.rb new file mode 100644 index 00000000..6be62abc --- /dev/null +++ b/db/migrate/20201206213753_create_arenas.rb @@ -0,0 +1,7 @@ +class CreateArenas < ActiveRecord::Migration[6.0] + def change + create_table :arenas do |t| + t.string :name + end + end +end diff --git a/db/schema.rb b/db/schema.rb new file mode 100644 index 00000000..9d018b08 --- /dev/null +++ b/db/schema.rb @@ -0,0 +1,32 @@ +# This file is auto-generated from the current state of the database. Instead +# of editing this file, please use the migrations feature of Active Record to +# incrementally modify your database, and then regenerate this schema definition. +# +# This file is the source Rails uses to define your schema when running `rails +# db:schema:load`. When creating a new database, `rails db:schema:load` tends to +# be faster and is potentially less error prone than running all of your +# migrations from scratch. Old migrations may fail to apply correctly if those +# migrations use external dependencies or application code. +# +# It's strongly recommended that you check this file into your version control system. + +ActiveRecord::Schema.define(version: 2020_12_06_213753) do + + create_table "arenas", force: :cascade do |t| + t.string "name" + end + + create_table "matches", force: :cascade do |t| + t.datetime "date" + t.integer "home_team_id" + t.integer "away_team_id" + t.integer "home_team_goals" + t.integer "away_team_goals" + t.integer "arena_id" + end + + create_table "teams", force: :cascade do |t| + t.string "name" + end + +end diff --git a/db/seeds.rb b/db/seeds.rb new file mode 100644 index 00000000..225133b9 --- /dev/null +++ b/db/seeds.rb @@ -0,0 +1,14 @@ +Match.destroy_all +Arena.destroy_all +Team.destroy_all + +t1 = Team.create(name: "Liverpool") +t2 = Team.create(name: "Manchester City") + +a1 = Arena.create(name: "First Arena") +a2 = Arena.create(name: "Second Arena") + +# m1 = Match.create(date: "#{Time.now}", home_team_id: t1.id, away_team_id: t2.id, home_team_goals: 3, away_team_goals: 2, arena_id: a1.id) + + +puts "Bingo!" \ No newline at end of file